xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2836.c (revision 91cc58af37efebb3ef1d959e07d8953473962d96)
14e46a66eSAndrew Turner /*
24e46a66eSAndrew Turner  * Copyright 2015 Andrew Turner.
3120b6fc9SSvatopluk Kraus  * Copyright 2016 Svatopluk Kraus
44e46a66eSAndrew Turner  * All rights reserved.
54e46a66eSAndrew Turner  *
64e46a66eSAndrew Turner  * Redistribution and use in source and binary forms, with or without
74e46a66eSAndrew Turner  * modification, are permitted provided that the following conditions are
84e46a66eSAndrew Turner  * met:
94e46a66eSAndrew Turner  *
104e46a66eSAndrew Turner  *  1. Redistributions of source code must retain the above copyright
114e46a66eSAndrew Turner  *     notice, this list of conditions and the following disclaimer.
124e46a66eSAndrew Turner  *  2. Redistributions in binary form must reproduce the above copyright
134e46a66eSAndrew Turner  *     notice, this list of conditions and the following disclaimer in the
144e46a66eSAndrew Turner  *     documentation and/or other materials provided with the distribution.
154e46a66eSAndrew Turner  *
164e46a66eSAndrew Turner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
174e46a66eSAndrew Turner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
184e46a66eSAndrew Turner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
194e46a66eSAndrew Turner  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
204e46a66eSAndrew Turner  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
214e46a66eSAndrew Turner  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
224e46a66eSAndrew Turner  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
234e46a66eSAndrew Turner  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
244e46a66eSAndrew Turner  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
254e46a66eSAndrew Turner  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
264e46a66eSAndrew Turner  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274e46a66eSAndrew Turner  */
284e46a66eSAndrew Turner 
294e46a66eSAndrew Turner #include <sys/cdefs.h>
304e46a66eSAndrew Turner __FBSDID("$FreeBSD$");
314e46a66eSAndrew Turner 
32120b6fc9SSvatopluk Kraus #include "opt_platform.h"
33120b6fc9SSvatopluk Kraus 
344e46a66eSAndrew Turner #include <sys/param.h>
354e46a66eSAndrew Turner #include <sys/systm.h>
364e46a66eSAndrew Turner #include <sys/bus.h>
37120b6fc9SSvatopluk Kraus #include <sys/cpuset.h>
384e46a66eSAndrew Turner #include <sys/kernel.h>
394e46a66eSAndrew Turner #include <sys/module.h>
40120b6fc9SSvatopluk Kraus #include <sys/proc.h>
414e46a66eSAndrew Turner #include <sys/rman.h>
42120b6fc9SSvatopluk Kraus #ifdef SMP
43120b6fc9SSvatopluk Kraus #include <sys/smp.h>
44120b6fc9SSvatopluk Kraus #endif
454e46a66eSAndrew Turner 
464e46a66eSAndrew Turner #include <machine/bus.h>
47120b6fc9SSvatopluk Kraus #include <machine/intr.h>
484e46a66eSAndrew Turner #include <machine/resource.h>
49120b6fc9SSvatopluk Kraus #ifdef SMP
50120b6fc9SSvatopluk Kraus #include <machine/smp.h>
51120b6fc9SSvatopluk Kraus #endif
524e46a66eSAndrew Turner 
534e46a66eSAndrew Turner #include <dev/ofw/ofw_bus_subr.h>
544e46a66eSAndrew Turner #include <dev/ofw/ofw_bus.h>
554e46a66eSAndrew Turner 
56120b6fc9SSvatopluk Kraus #include "pic_if.h"
574e46a66eSAndrew Turner 
58120b6fc9SSvatopluk Kraus #define	BCM_LINTC_CONTROL_REG		0x00
59120b6fc9SSvatopluk Kraus #define	BCM_LINTC_PRESCALER_REG		0x08
60120b6fc9SSvatopluk Kraus #define	BCM_LINTC_GPU_ROUTING_REG	0x0c
61120b6fc9SSvatopluk Kraus #define	BCM_LINTC_PMU_ROUTING_SET_REG	0x10
62120b6fc9SSvatopluk Kraus #define	BCM_LINTC_PMU_ROUTING_CLR_REG	0x14
63120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TIMER_CFG_REG(n)	(0x40 + (n) * 4)
64120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX_CFG_REG(n)	(0x50 + (n) * 4)
65120b6fc9SSvatopluk Kraus #define	BCM_LINTC_PENDING_REG(n)	(0x60 + (n) * 4)
66120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX0_SET_REG(n)	(0x80 + (n) * 16)
67120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX1_SET_REG(n)	(0x84 + (n) * 16)
68120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX2_SET_REG(n)	(0x88 + (n) * 16)
69120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX3_SET_REG(n)	(0x8C + (n) * 16)
70120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX0_CLR_REG(n)	(0xC0 + (n) * 16)
71120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX1_CLR_REG(n)	(0xC4 + (n) * 16)
72120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX2_CLR_REG(n)	(0xC8 + (n) * 16)
73120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX3_CLR_REG(n)	(0xCC + (n) * 16)
74120b6fc9SSvatopluk Kraus 
75120b6fc9SSvatopluk Kraus /* Prescaler Register */
76120b6fc9SSvatopluk Kraus #define	BCM_LINTC_PSR_19_2		0x80000000	/* 19.2 MHz */
77120b6fc9SSvatopluk Kraus 
78120b6fc9SSvatopluk Kraus /* GPU Interrupt Routing Register */
79120b6fc9SSvatopluk Kraus #define	BCM_LINTC_GIRR_IRQ_CORE(n)	(n)
80120b6fc9SSvatopluk Kraus #define	BCM_LINTC_GIRR_FIQ_CORE(n)	((n) << 2)
81120b6fc9SSvatopluk Kraus 
82120b6fc9SSvatopluk Kraus /* PMU Interrupt Routing Register */
83120b6fc9SSvatopluk Kraus #define	BCM_LINTC_PIRR_IRQ_EN_CORE(n)	(1 << (n))
84120b6fc9SSvatopluk Kraus #define	BCM_LINTC_PIRR_FIQ_EN_CORE(n)	(1 << ((n) + 4))
85120b6fc9SSvatopluk Kraus 
86120b6fc9SSvatopluk Kraus /* Timer Config Register */
87120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TCR_IRQ_EN_TIMER(n)	(1 << (n))
88120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TCR_FIQ_EN_TIMER(n)	(1 << ((n) + 4))
89120b6fc9SSvatopluk Kraus 
90120b6fc9SSvatopluk Kraus /* MBOX Config Register */
91120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MCR_IRQ_EN_MBOX(n)	(1 << (n))
92120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MCR_FIQ_EN_MBOX(n)	(1 << ((n) + 4))
93120b6fc9SSvatopluk Kraus 
94120b6fc9SSvatopluk Kraus #define	BCM_LINTC_CNTPSIRQ_IRQ		0
95120b6fc9SSvatopluk Kraus #define	BCM_LINTC_CNTPNSIRQ_IRQ		1
96120b6fc9SSvatopluk Kraus #define	BCM_LINTC_CNTHPIRQ_IRQ		2
97120b6fc9SSvatopluk Kraus #define	BCM_LINTC_CNTVIRQ_IRQ		3
98120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX0_IRQ		4
99120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX1_IRQ		5
100120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX2_IRQ		6
101120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX3_IRQ		7
102120b6fc9SSvatopluk Kraus #define	BCM_LINTC_GPU_IRQ		8
103120b6fc9SSvatopluk Kraus #define	BCM_LINTC_PMU_IRQ		9
104120b6fc9SSvatopluk Kraus #define	BCM_LINTC_AXI_IRQ		10
105120b6fc9SSvatopluk Kraus #define	BCM_LINTC_LTIMER_IRQ		11
106120b6fc9SSvatopluk Kraus 
107120b6fc9SSvatopluk Kraus #define	BCM_LINTC_NIRQS			12
108120b6fc9SSvatopluk Kraus 
109120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TIMER0_IRQ		BCM_LINTC_CNTPSIRQ_IRQ
110120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TIMER1_IRQ		BCM_LINTC_CNTPNSIRQ_IRQ
111120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TIMER2_IRQ		BCM_LINTC_CNTHPIRQ_IRQ
112120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TIMER3_IRQ		BCM_LINTC_CNTVIRQ_IRQ
113120b6fc9SSvatopluk Kraus 
114120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TIMER0_IRQ_MASK	(1 << BCM_LINTC_TIMER0_IRQ)
115120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TIMER1_IRQ_MASK	(1 << BCM_LINTC_TIMER1_IRQ)
116120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TIMER2_IRQ_MASK	(1 << BCM_LINTC_TIMER2_IRQ)
117120b6fc9SSvatopluk Kraus #define	BCM_LINTC_TIMER3_IRQ_MASK	(1 << BCM_LINTC_TIMER3_IRQ)
118120b6fc9SSvatopluk Kraus #define	BCM_LINTC_MBOX0_IRQ_MASK	(1 << BCM_LINTC_MBOX0_IRQ)
119120b6fc9SSvatopluk Kraus #define	BCM_LINTC_GPU_IRQ_MASK		(1 << BCM_LINTC_GPU_IRQ)
120120b6fc9SSvatopluk Kraus #define	BCM_LINTC_PMU_IRQ_MASK		(1 << BCM_LINTC_PMU_IRQ)
121120b6fc9SSvatopluk Kraus 
122120b6fc9SSvatopluk Kraus #define	BCM_LINTC_UP_PENDING_MASK	\
123120b6fc9SSvatopluk Kraus     (BCM_LINTC_TIMER0_IRQ_MASK |	\
124120b6fc9SSvatopluk Kraus      BCM_LINTC_TIMER1_IRQ_MASK |	\
125120b6fc9SSvatopluk Kraus      BCM_LINTC_TIMER2_IRQ_MASK |	\
126120b6fc9SSvatopluk Kraus      BCM_LINTC_TIMER3_IRQ_MASK |	\
127120b6fc9SSvatopluk Kraus      BCM_LINTC_GPU_IRQ_MASK |		\
128120b6fc9SSvatopluk Kraus      BCM_LINTC_PMU_IRQ_MASK)
129120b6fc9SSvatopluk Kraus 
130120b6fc9SSvatopluk Kraus #define	BCM_LINTC_SMP_PENDING_MASK	\
131120b6fc9SSvatopluk Kraus     (BCM_LINTC_UP_PENDING_MASK |	\
132120b6fc9SSvatopluk Kraus      BCM_LINTC_MBOX0_IRQ_MASK)
133120b6fc9SSvatopluk Kraus 
134120b6fc9SSvatopluk Kraus #ifdef SMP
135120b6fc9SSvatopluk Kraus #define BCM_LINTC_PENDING_MASK		BCM_LINTC_SMP_PENDING_MASK
136120b6fc9SSvatopluk Kraus #else
137120b6fc9SSvatopluk Kraus #define BCM_LINTC_PENDING_MASK		BCM_LINTC_UP_PENDING_MASK
138120b6fc9SSvatopluk Kraus #endif
139120b6fc9SSvatopluk Kraus 
140120b6fc9SSvatopluk Kraus struct bcm_lintc_irqsrc {
141120b6fc9SSvatopluk Kraus 	struct intr_irqsrc	bli_isrc;
142120b6fc9SSvatopluk Kraus 	u_int			bli_irq;
143120b6fc9SSvatopluk Kraus 	union {
144120b6fc9SSvatopluk Kraus 		u_int		bli_mask;	/* for timers */
145120b6fc9SSvatopluk Kraus 		u_int		bli_value;	/* for GPU */
146120b6fc9SSvatopluk Kraus 	};
147120b6fc9SSvatopluk Kraus };
148120b6fc9SSvatopluk Kraus 
149120b6fc9SSvatopluk Kraus struct bcm_lintc_softc {
150120b6fc9SSvatopluk Kraus 	device_t		bls_dev;
151120b6fc9SSvatopluk Kraus 	struct mtx		bls_mtx;
152120b6fc9SSvatopluk Kraus 	struct resource *	bls_mem;
153120b6fc9SSvatopluk Kraus 	bus_space_tag_t		bls_bst;
154120b6fc9SSvatopluk Kraus 	bus_space_handle_t	bls_bsh;
155120b6fc9SSvatopluk Kraus 	struct bcm_lintc_irqsrc	bls_isrcs[BCM_LINTC_NIRQS];
156120b6fc9SSvatopluk Kraus };
157120b6fc9SSvatopluk Kraus 
158120b6fc9SSvatopluk Kraus static struct bcm_lintc_softc *bcm_lintc_sc;
159120b6fc9SSvatopluk Kraus 
160120b6fc9SSvatopluk Kraus #ifdef SMP
161120b6fc9SSvatopluk Kraus #define BCM_LINTC_NIPIS		32	/* only mailbox 0 is used for IPI */
162120b6fc9SSvatopluk Kraus CTASSERT(INTR_IPI_COUNT <= BCM_LINTC_NIPIS);
163120b6fc9SSvatopluk Kraus #endif
164120b6fc9SSvatopluk Kraus 
165120b6fc9SSvatopluk Kraus #define	BCM_LINTC_LOCK(sc)		mtx_lock_spin(&(sc)->bls_mtx)
166120b6fc9SSvatopluk Kraus #define	BCM_LINTC_UNLOCK(sc)		mtx_unlock_spin(&(sc)->bls_mtx)
167120b6fc9SSvatopluk Kraus #define	BCM_LINTC_LOCK_INIT(sc)		mtx_init(&(sc)->bls_mtx,	\
168120b6fc9SSvatopluk Kraus     device_get_nameunit((sc)->bls_dev), "bmc_local_intc", MTX_SPIN)
169120b6fc9SSvatopluk Kraus #define	BCM_LINTC_LOCK_DESTROY(sc)	mtx_destroy(&(sc)->bls_mtx)
170120b6fc9SSvatopluk Kraus 
171120b6fc9SSvatopluk Kraus #define	bcm_lintc_read_4(sc, reg)		\
172120b6fc9SSvatopluk Kraus     bus_space_read_4((sc)->bls_bst, (sc)->bls_bsh, (reg))
173120b6fc9SSvatopluk Kraus #define	bcm_lintc_write_4(sc, reg, val)		\
174120b6fc9SSvatopluk Kraus     bus_space_write_4((sc)->bls_bst, (sc)->bls_bsh, (reg), (val))
175120b6fc9SSvatopluk Kraus 
176120b6fc9SSvatopluk Kraus static inline void
177120b6fc9SSvatopluk Kraus bcm_lintc_rwreg_clr(struct bcm_lintc_softc *sc, uint32_t reg,
178120b6fc9SSvatopluk Kraus     uint32_t mask)
179120b6fc9SSvatopluk Kraus {
180120b6fc9SSvatopluk Kraus 
181120b6fc9SSvatopluk Kraus 	bcm_lintc_write_4(sc, reg, bcm_lintc_read_4(sc, reg) & ~mask);
182120b6fc9SSvatopluk Kraus }
183120b6fc9SSvatopluk Kraus 
184120b6fc9SSvatopluk Kraus static inline void
185120b6fc9SSvatopluk Kraus bcm_lintc_rwreg_set(struct bcm_lintc_softc *sc, uint32_t reg,
186120b6fc9SSvatopluk Kraus     uint32_t mask)
187120b6fc9SSvatopluk Kraus {
188120b6fc9SSvatopluk Kraus 
189120b6fc9SSvatopluk Kraus 	bcm_lintc_write_4(sc, reg, bcm_lintc_read_4(sc, reg) | mask);
190120b6fc9SSvatopluk Kraus }
191120b6fc9SSvatopluk Kraus 
192120b6fc9SSvatopluk Kraus static void
193120b6fc9SSvatopluk Kraus bcm_lintc_timer_mask(struct bcm_lintc_softc *sc, struct bcm_lintc_irqsrc *bli)
194120b6fc9SSvatopluk Kraus {
195120b6fc9SSvatopluk Kraus 	cpuset_t *cpus;
196120b6fc9SSvatopluk Kraus 	uint32_t cpu;
197120b6fc9SSvatopluk Kraus 
198120b6fc9SSvatopluk Kraus 	cpus = &bli->bli_isrc.isrc_cpu;
199120b6fc9SSvatopluk Kraus 
200120b6fc9SSvatopluk Kraus 	BCM_LINTC_LOCK(sc);
201120b6fc9SSvatopluk Kraus 	for (cpu = 0; cpu < 4; cpu++)
202120b6fc9SSvatopluk Kraus 		if (CPU_ISSET(cpu, cpus))
203120b6fc9SSvatopluk Kraus 			bcm_lintc_rwreg_clr(sc, BCM_LINTC_TIMER_CFG_REG(cpu),
204120b6fc9SSvatopluk Kraus 			    bli->bli_mask);
205120b6fc9SSvatopluk Kraus 	BCM_LINTC_UNLOCK(sc);
206120b6fc9SSvatopluk Kraus }
207120b6fc9SSvatopluk Kraus 
208120b6fc9SSvatopluk Kraus static void
209120b6fc9SSvatopluk Kraus bcm_lintc_timer_unmask(struct bcm_lintc_softc *sc, struct bcm_lintc_irqsrc *bli)
210120b6fc9SSvatopluk Kraus {
211120b6fc9SSvatopluk Kraus 	cpuset_t *cpus;
212120b6fc9SSvatopluk Kraus 	uint32_t cpu;
213120b6fc9SSvatopluk Kraus 
214120b6fc9SSvatopluk Kraus 	cpus = &bli->bli_isrc.isrc_cpu;
215120b6fc9SSvatopluk Kraus 
216120b6fc9SSvatopluk Kraus 	BCM_LINTC_LOCK(sc);
217120b6fc9SSvatopluk Kraus 	for (cpu = 0; cpu < 4; cpu++)
218120b6fc9SSvatopluk Kraus 		if (CPU_ISSET(cpu, cpus))
219120b6fc9SSvatopluk Kraus 			bcm_lintc_rwreg_set(sc, BCM_LINTC_TIMER_CFG_REG(cpu),
220120b6fc9SSvatopluk Kraus 			    bli->bli_mask);
221120b6fc9SSvatopluk Kraus 	BCM_LINTC_UNLOCK(sc);
222120b6fc9SSvatopluk Kraus }
223120b6fc9SSvatopluk Kraus 
224120b6fc9SSvatopluk Kraus static inline void
225120b6fc9SSvatopluk Kraus bcm_lintc_gpu_mask(struct bcm_lintc_softc *sc, struct bcm_lintc_irqsrc *bli)
226120b6fc9SSvatopluk Kraus {
227120b6fc9SSvatopluk Kraus 
228120b6fc9SSvatopluk Kraus 	/* It's accessed just and only by one core. */
229120b6fc9SSvatopluk Kraus 	bcm_lintc_write_4(sc, BCM_LINTC_GPU_ROUTING_REG, 0);
230120b6fc9SSvatopluk Kraus }
231120b6fc9SSvatopluk Kraus 
232120b6fc9SSvatopluk Kraus static inline void
233120b6fc9SSvatopluk Kraus bcm_lintc_gpu_unmask(struct bcm_lintc_softc *sc, struct bcm_lintc_irqsrc *bli)
234120b6fc9SSvatopluk Kraus {
235120b6fc9SSvatopluk Kraus 
236120b6fc9SSvatopluk Kraus 	/* It's accessed just and only by one core. */
237120b6fc9SSvatopluk Kraus 	bcm_lintc_write_4(sc, BCM_LINTC_GPU_ROUTING_REG, bli->bli_value);
238120b6fc9SSvatopluk Kraus }
239120b6fc9SSvatopluk Kraus 
240120b6fc9SSvatopluk Kraus static inline void
241120b6fc9SSvatopluk Kraus bcm_lintc_pmu_mask(struct bcm_lintc_softc *sc, struct bcm_lintc_irqsrc *bli)
242120b6fc9SSvatopluk Kraus {
243120b6fc9SSvatopluk Kraus 	cpuset_t *cpus;
244120b6fc9SSvatopluk Kraus 	uint32_t cpu, mask;
245120b6fc9SSvatopluk Kraus 
246120b6fc9SSvatopluk Kraus 	mask = 0;
247120b6fc9SSvatopluk Kraus 	cpus = &bli->bli_isrc.isrc_cpu;
248120b6fc9SSvatopluk Kraus 
249120b6fc9SSvatopluk Kraus 	BCM_LINTC_LOCK(sc);
250120b6fc9SSvatopluk Kraus 	for (cpu = 0; cpu < 4; cpu++)
251120b6fc9SSvatopluk Kraus 		if (CPU_ISSET(cpu, cpus))
252120b6fc9SSvatopluk Kraus 			mask |= BCM_LINTC_PIRR_IRQ_EN_CORE(cpu);
253120b6fc9SSvatopluk Kraus 	/* Write-clear register. */
254120b6fc9SSvatopluk Kraus 	bcm_lintc_write_4(sc, BCM_LINTC_PMU_ROUTING_CLR_REG, mask);
255120b6fc9SSvatopluk Kraus 	BCM_LINTC_UNLOCK(sc);
256120b6fc9SSvatopluk Kraus }
257120b6fc9SSvatopluk Kraus 
258120b6fc9SSvatopluk Kraus static inline void
259120b6fc9SSvatopluk Kraus bcm_lintc_pmu_unmask(struct bcm_lintc_softc *sc, struct bcm_lintc_irqsrc *bli)
260120b6fc9SSvatopluk Kraus {
261120b6fc9SSvatopluk Kraus 	cpuset_t *cpus;
262120b6fc9SSvatopluk Kraus 	uint32_t cpu, mask;
263120b6fc9SSvatopluk Kraus 
264120b6fc9SSvatopluk Kraus 	mask = 0;
265120b6fc9SSvatopluk Kraus 	cpus = &bli->bli_isrc.isrc_cpu;
266120b6fc9SSvatopluk Kraus 
267120b6fc9SSvatopluk Kraus 	BCM_LINTC_LOCK(sc);
268120b6fc9SSvatopluk Kraus 	for (cpu = 0; cpu < 4; cpu++)
269120b6fc9SSvatopluk Kraus 		if (CPU_ISSET(cpu, cpus))
270120b6fc9SSvatopluk Kraus 			mask |= BCM_LINTC_PIRR_IRQ_EN_CORE(cpu);
271120b6fc9SSvatopluk Kraus 	/* Write-set register. */
272120b6fc9SSvatopluk Kraus 	bcm_lintc_write_4(sc, BCM_LINTC_PMU_ROUTING_SET_REG, mask);
273120b6fc9SSvatopluk Kraus 	BCM_LINTC_UNLOCK(sc);
274120b6fc9SSvatopluk Kraus }
275120b6fc9SSvatopluk Kraus 
276120b6fc9SSvatopluk Kraus static void
277120b6fc9SSvatopluk Kraus bcm_lintc_mask(struct bcm_lintc_softc *sc, struct bcm_lintc_irqsrc *bli)
278120b6fc9SSvatopluk Kraus {
279120b6fc9SSvatopluk Kraus 
280120b6fc9SSvatopluk Kraus 	switch (bli->bli_irq) {
281120b6fc9SSvatopluk Kraus 	case BCM_LINTC_TIMER0_IRQ:
282120b6fc9SSvatopluk Kraus 	case BCM_LINTC_TIMER1_IRQ:
283120b6fc9SSvatopluk Kraus 	case BCM_LINTC_TIMER2_IRQ:
284120b6fc9SSvatopluk Kraus 	case BCM_LINTC_TIMER3_IRQ:
285120b6fc9SSvatopluk Kraus 		bcm_lintc_timer_mask(sc, bli);
286120b6fc9SSvatopluk Kraus 		return;
287120b6fc9SSvatopluk Kraus 	case BCM_LINTC_MBOX0_IRQ:
288120b6fc9SSvatopluk Kraus 	case BCM_LINTC_MBOX1_IRQ:
289120b6fc9SSvatopluk Kraus 	case BCM_LINTC_MBOX2_IRQ:
290120b6fc9SSvatopluk Kraus 	case BCM_LINTC_MBOX3_IRQ:
291120b6fc9SSvatopluk Kraus 		return;
292120b6fc9SSvatopluk Kraus 	case BCM_LINTC_GPU_IRQ:
293120b6fc9SSvatopluk Kraus 		bcm_lintc_gpu_mask(sc, bli);
294120b6fc9SSvatopluk Kraus 		return;
295120b6fc9SSvatopluk Kraus 	case BCM_LINTC_PMU_IRQ:
296120b6fc9SSvatopluk Kraus 		bcm_lintc_pmu_mask(sc, bli);
297120b6fc9SSvatopluk Kraus 		return;
298120b6fc9SSvatopluk Kraus 	default:
299120b6fc9SSvatopluk Kraus 		panic("%s: not implemented for irq %u", __func__, bli->bli_irq);
300120b6fc9SSvatopluk Kraus 	}
301120b6fc9SSvatopluk Kraus }
302120b6fc9SSvatopluk Kraus 
303120b6fc9SSvatopluk Kraus static void
304120b6fc9SSvatopluk Kraus bcm_lintc_unmask(struct bcm_lintc_softc *sc, struct bcm_lintc_irqsrc *bli)
305120b6fc9SSvatopluk Kraus {
306120b6fc9SSvatopluk Kraus 
307120b6fc9SSvatopluk Kraus 	switch (bli->bli_irq) {
308120b6fc9SSvatopluk Kraus 	case BCM_LINTC_TIMER0_IRQ:
309120b6fc9SSvatopluk Kraus 	case BCM_LINTC_TIMER1_IRQ:
310120b6fc9SSvatopluk Kraus 	case BCM_LINTC_TIMER2_IRQ:
311120b6fc9SSvatopluk Kraus 	case BCM_LINTC_TIMER3_IRQ:
312120b6fc9SSvatopluk Kraus 		bcm_lintc_timer_unmask(sc, bli);
313120b6fc9SSvatopluk Kraus 		return;
314120b6fc9SSvatopluk Kraus 	case BCM_LINTC_MBOX0_IRQ:
315120b6fc9SSvatopluk Kraus 	case BCM_LINTC_MBOX1_IRQ:
316120b6fc9SSvatopluk Kraus 	case BCM_LINTC_MBOX2_IRQ:
317120b6fc9SSvatopluk Kraus 	case BCM_LINTC_MBOX3_IRQ:
318120b6fc9SSvatopluk Kraus 		return;
319120b6fc9SSvatopluk Kraus 	case BCM_LINTC_GPU_IRQ:
320120b6fc9SSvatopluk Kraus 		bcm_lintc_gpu_unmask(sc, bli);
321120b6fc9SSvatopluk Kraus 		return;
322120b6fc9SSvatopluk Kraus 	case BCM_LINTC_PMU_IRQ:
323120b6fc9SSvatopluk Kraus 		bcm_lintc_pmu_unmask(sc, bli);
324120b6fc9SSvatopluk Kraus 		return;
325120b6fc9SSvatopluk Kraus 	default:
326120b6fc9SSvatopluk Kraus 		panic("%s: not implemented for irq %u", __func__, bli->bli_irq);
327120b6fc9SSvatopluk Kraus 	}
328120b6fc9SSvatopluk Kraus }
329120b6fc9SSvatopluk Kraus 
330120b6fc9SSvatopluk Kraus #ifdef SMP
331120b6fc9SSvatopluk Kraus static inline void
332120b6fc9SSvatopluk Kraus bcm_lintc_ipi_write(struct bcm_lintc_softc *sc, cpuset_t cpus, u_int ipi)
333120b6fc9SSvatopluk Kraus {
334120b6fc9SSvatopluk Kraus 	u_int cpu;
335120b6fc9SSvatopluk Kraus 	uint32_t mask;
336120b6fc9SSvatopluk Kraus 
337120b6fc9SSvatopluk Kraus 	mask = 1 << ipi;
338120b6fc9SSvatopluk Kraus 	for (cpu = 0; cpu < mp_ncpus; cpu++)
339120b6fc9SSvatopluk Kraus 		if (CPU_ISSET(cpu, &cpus))
340120b6fc9SSvatopluk Kraus 			bcm_lintc_write_4(sc, BCM_LINTC_MBOX0_SET_REG(cpu),
341120b6fc9SSvatopluk Kraus 			    mask);
342120b6fc9SSvatopluk Kraus }
343120b6fc9SSvatopluk Kraus 
344120b6fc9SSvatopluk Kraus static inline void
345120b6fc9SSvatopluk Kraus bcm_lintc_ipi_dispatch(struct bcm_lintc_softc *sc, u_int cpu,
346120b6fc9SSvatopluk Kraus     struct trapframe *tf)
347120b6fc9SSvatopluk Kraus {
348120b6fc9SSvatopluk Kraus 	u_int ipi;
349120b6fc9SSvatopluk Kraus 	uint32_t mask;
350120b6fc9SSvatopluk Kraus 
351120b6fc9SSvatopluk Kraus 	mask = bcm_lintc_read_4(sc, BCM_LINTC_MBOX0_CLR_REG(cpu));
352120b6fc9SSvatopluk Kraus 	if (mask == 0) {
353120b6fc9SSvatopluk Kraus 		device_printf(sc->bls_dev, "Spurious ipi detected\n");
354120b6fc9SSvatopluk Kraus 		return;
355120b6fc9SSvatopluk Kraus 	}
356120b6fc9SSvatopluk Kraus 
357120b6fc9SSvatopluk Kraus 	for (ipi = 0; mask != 0; mask >>= 1, ipi++) {
358120b6fc9SSvatopluk Kraus 		if ((mask & 0x01) == 0)
359120b6fc9SSvatopluk Kraus 			continue;
360120b6fc9SSvatopluk Kraus 		/*
361120b6fc9SSvatopluk Kraus 		 * Clear an IPI before dispatching to not miss anyone
362120b6fc9SSvatopluk Kraus 		 * and make sure that it's observed by everybody.
363120b6fc9SSvatopluk Kraus 		 */
364120b6fc9SSvatopluk Kraus 		bcm_lintc_write_4(sc, BCM_LINTC_MBOX0_CLR_REG(cpu), 1 << ipi);
3650f04f5deSOleksandr Tymoshenko #if defined(__aarch64__)
3660f04f5deSOleksandr Tymoshenko 		dsb(sy);
3670f04f5deSOleksandr Tymoshenko #else
368120b6fc9SSvatopluk Kraus 		dsb();
3690f04f5deSOleksandr Tymoshenko #endif
370120b6fc9SSvatopluk Kraus 		intr_ipi_dispatch(ipi, tf);
371120b6fc9SSvatopluk Kraus 	}
372120b6fc9SSvatopluk Kraus }
373120b6fc9SSvatopluk Kraus #endif
374120b6fc9SSvatopluk Kraus 
375120b6fc9SSvatopluk Kraus static inline void
376120b6fc9SSvatopluk Kraus bcm_lintc_irq_dispatch(struct bcm_lintc_softc *sc, u_int irq,
377120b6fc9SSvatopluk Kraus     struct trapframe *tf)
378120b6fc9SSvatopluk Kraus {
379120b6fc9SSvatopluk Kraus 	struct bcm_lintc_irqsrc *bli;
380120b6fc9SSvatopluk Kraus 
381120b6fc9SSvatopluk Kraus 	bli = &sc->bls_isrcs[irq];
382120b6fc9SSvatopluk Kraus 	if (intr_isrc_dispatch(&bli->bli_isrc, tf) != 0)
383120b6fc9SSvatopluk Kraus 		device_printf(sc->bls_dev, "Stray irq %u detected\n", irq);
384120b6fc9SSvatopluk Kraus }
385120b6fc9SSvatopluk Kraus 
386120b6fc9SSvatopluk Kraus static int
387120b6fc9SSvatopluk Kraus bcm_lintc_intr(void *arg)
388120b6fc9SSvatopluk Kraus {
389120b6fc9SSvatopluk Kraus 	struct bcm_lintc_softc *sc;
390120b6fc9SSvatopluk Kraus 	u_int cpu;
391120b6fc9SSvatopluk Kraus 	uint32_t num, reg;
392120b6fc9SSvatopluk Kraus 	struct trapframe *tf;
393120b6fc9SSvatopluk Kraus 
394120b6fc9SSvatopluk Kraus 	sc = arg;
395120b6fc9SSvatopluk Kraus 	cpu = PCPU_GET(cpuid);
396120b6fc9SSvatopluk Kraus 	tf = curthread->td_intr_frame;
397120b6fc9SSvatopluk Kraus 
398120b6fc9SSvatopluk Kraus 	for (num = 0; ; num++) {
399120b6fc9SSvatopluk Kraus 		reg = bcm_lintc_read_4(sc, BCM_LINTC_PENDING_REG(cpu));
400120b6fc9SSvatopluk Kraus 		if ((reg & BCM_LINTC_PENDING_MASK) == 0)
401120b6fc9SSvatopluk Kraus 			break;
402120b6fc9SSvatopluk Kraus #ifdef SMP
403120b6fc9SSvatopluk Kraus 		if (reg & BCM_LINTC_MBOX0_IRQ_MASK)
404120b6fc9SSvatopluk Kraus 			bcm_lintc_ipi_dispatch(sc, cpu, tf);
405120b6fc9SSvatopluk Kraus #endif
406120b6fc9SSvatopluk Kraus 		if (reg & BCM_LINTC_TIMER0_IRQ_MASK)
407120b6fc9SSvatopluk Kraus 			bcm_lintc_irq_dispatch(sc, BCM_LINTC_TIMER0_IRQ, tf);
408120b6fc9SSvatopluk Kraus 		if (reg & BCM_LINTC_TIMER1_IRQ_MASK)
409120b6fc9SSvatopluk Kraus 			bcm_lintc_irq_dispatch(sc, BCM_LINTC_TIMER1_IRQ, tf);
410120b6fc9SSvatopluk Kraus 		if (reg & BCM_LINTC_TIMER2_IRQ_MASK)
411120b6fc9SSvatopluk Kraus 			bcm_lintc_irq_dispatch(sc, BCM_LINTC_TIMER2_IRQ, tf);
412120b6fc9SSvatopluk Kraus 		if (reg & BCM_LINTC_TIMER3_IRQ_MASK)
413120b6fc9SSvatopluk Kraus 			bcm_lintc_irq_dispatch(sc, BCM_LINTC_TIMER3_IRQ, tf);
414120b6fc9SSvatopluk Kraus 		if (reg & BCM_LINTC_GPU_IRQ_MASK)
415120b6fc9SSvatopluk Kraus 			bcm_lintc_irq_dispatch(sc, BCM_LINTC_GPU_IRQ, tf);
416120b6fc9SSvatopluk Kraus 		if (reg & BCM_LINTC_PMU_IRQ_MASK)
417120b6fc9SSvatopluk Kraus 			bcm_lintc_irq_dispatch(sc, BCM_LINTC_PMU_IRQ, tf);
418120b6fc9SSvatopluk Kraus 
419120b6fc9SSvatopluk Kraus 		arm_irq_memory_barrier(0); /* XXX */
420120b6fc9SSvatopluk Kraus 	}
421120b6fc9SSvatopluk Kraus 	reg &= ~BCM_LINTC_PENDING_MASK;
422120b6fc9SSvatopluk Kraus 	if (reg != 0)
423120b6fc9SSvatopluk Kraus 		device_printf(sc->bls_dev, "Unknown interrupt(s) %x\n", reg);
424120b6fc9SSvatopluk Kraus 	else if (num == 0)
425120b6fc9SSvatopluk Kraus 		device_printf(sc->bls_dev, "Spurious interrupt detected\n");
426120b6fc9SSvatopluk Kraus 
427120b6fc9SSvatopluk Kraus 	return (FILTER_HANDLED);
428120b6fc9SSvatopluk Kraus }
429120b6fc9SSvatopluk Kraus 
430120b6fc9SSvatopluk Kraus static void
431120b6fc9SSvatopluk Kraus bcm_lintc_disable_intr(device_t dev, struct intr_irqsrc *isrc)
432120b6fc9SSvatopluk Kraus {
433120b6fc9SSvatopluk Kraus 
434120b6fc9SSvatopluk Kraus 	bcm_lintc_mask(device_get_softc(dev), (struct bcm_lintc_irqsrc *)isrc);
435120b6fc9SSvatopluk Kraus }
436120b6fc9SSvatopluk Kraus 
437120b6fc9SSvatopluk Kraus static void
438120b6fc9SSvatopluk Kraus bcm_lintc_enable_intr(device_t dev, struct intr_irqsrc *isrc)
439120b6fc9SSvatopluk Kraus {
440120b6fc9SSvatopluk Kraus 	struct bcm_lintc_irqsrc *bli = (struct bcm_lintc_irqsrc *)isrc;
441120b6fc9SSvatopluk Kraus 
442120b6fc9SSvatopluk Kraus 	arm_irq_memory_barrier(bli->bli_irq);
443120b6fc9SSvatopluk Kraus 	bcm_lintc_unmask(device_get_softc(dev), bli);
444120b6fc9SSvatopluk Kraus }
445120b6fc9SSvatopluk Kraus 
446120b6fc9SSvatopluk Kraus static int
447120b6fc9SSvatopluk Kraus bcm_lintc_map_intr(device_t dev, struct intr_map_data *data,
448120b6fc9SSvatopluk Kraus     struct intr_irqsrc **isrcp)
449120b6fc9SSvatopluk Kraus {
450cd642c88SSvatopluk Kraus 	struct intr_map_data_fdt *daf;
451120b6fc9SSvatopluk Kraus 	struct bcm_lintc_softc *sc;
452120b6fc9SSvatopluk Kraus 
453120b6fc9SSvatopluk Kraus 	if (data->type != INTR_MAP_DATA_FDT)
454120b6fc9SSvatopluk Kraus 		return (ENOTSUP);
455cd642c88SSvatopluk Kraus 
456cd642c88SSvatopluk Kraus 	daf = (struct intr_map_data_fdt *)data;
457cd642c88SSvatopluk Kraus 	if (daf->ncells != 1 || daf->cells[0] >= BCM_LINTC_NIRQS)
458120b6fc9SSvatopluk Kraus 		return (EINVAL);
459120b6fc9SSvatopluk Kraus 
460120b6fc9SSvatopluk Kraus 	sc = device_get_softc(dev);
461cd642c88SSvatopluk Kraus 	*isrcp = &sc->bls_isrcs[daf->cells[0]].bli_isrc;
462120b6fc9SSvatopluk Kraus 	return (0);
463120b6fc9SSvatopluk Kraus }
464120b6fc9SSvatopluk Kraus 
465120b6fc9SSvatopluk Kraus static void
466120b6fc9SSvatopluk Kraus bcm_lintc_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
467120b6fc9SSvatopluk Kraus {
468120b6fc9SSvatopluk Kraus 	struct bcm_lintc_irqsrc *bli = (struct bcm_lintc_irqsrc *)isrc;
469120b6fc9SSvatopluk Kraus 
470120b6fc9SSvatopluk Kraus 	if (bli->bli_irq == BCM_LINTC_GPU_IRQ)
471120b6fc9SSvatopluk Kraus 		bcm_lintc_gpu_mask(device_get_softc(dev), bli);
472120b6fc9SSvatopluk Kraus 	else {
473120b6fc9SSvatopluk Kraus 		/*
474120b6fc9SSvatopluk Kraus 		 * Handler for PPI interrupt does not make sense much unless
475120b6fc9SSvatopluk Kraus 		 * there is one bound ithread for each core for it. Thus the
476120b6fc9SSvatopluk Kraus 		 * interrupt can be masked on current core only while ithread
477120b6fc9SSvatopluk Kraus 		 * bounded to this core ensures unmasking on the same core.
478120b6fc9SSvatopluk Kraus 		 */
479120b6fc9SSvatopluk Kraus 		panic ("%s: handlers are not supported", __func__);
480120b6fc9SSvatopluk Kraus 	}
481120b6fc9SSvatopluk Kraus }
482120b6fc9SSvatopluk Kraus 
483120b6fc9SSvatopluk Kraus static void
484120b6fc9SSvatopluk Kraus bcm_lintc_post_ithread(device_t dev, struct intr_irqsrc *isrc)
485120b6fc9SSvatopluk Kraus {
486120b6fc9SSvatopluk Kraus 	struct bcm_lintc_irqsrc *bli = (struct bcm_lintc_irqsrc *)isrc;
487120b6fc9SSvatopluk Kraus 
488120b6fc9SSvatopluk Kraus 	if (bli->bli_irq == BCM_LINTC_GPU_IRQ)
489120b6fc9SSvatopluk Kraus 		bcm_lintc_gpu_unmask(device_get_softc(dev), bli);
490120b6fc9SSvatopluk Kraus 	else {
491120b6fc9SSvatopluk Kraus 		/* See comment in bcm_lintc_pre_ithread(). */
492120b6fc9SSvatopluk Kraus 		panic ("%s: handlers are not supported", __func__);
493120b6fc9SSvatopluk Kraus 	}
494120b6fc9SSvatopluk Kraus }
495120b6fc9SSvatopluk Kraus 
496120b6fc9SSvatopluk Kraus static void
497120b6fc9SSvatopluk Kraus bcm_lintc_post_filter(device_t dev, struct intr_irqsrc *isrc)
498120b6fc9SSvatopluk Kraus {
499120b6fc9SSvatopluk Kraus }
500120b6fc9SSvatopluk Kraus 
501120b6fc9SSvatopluk Kraus static int
502120b6fc9SSvatopluk Kraus bcm_lintc_setup_intr(device_t dev, struct intr_irqsrc *isrc,
503120b6fc9SSvatopluk Kraus     struct resource *res, struct intr_map_data *data)
504120b6fc9SSvatopluk Kraus {
505120b6fc9SSvatopluk Kraus 	struct bcm_lintc_softc *sc;
506120b6fc9SSvatopluk Kraus 
507120b6fc9SSvatopluk Kraus 	if (isrc->isrc_handlers == 0 && isrc->isrc_flags & INTR_ISRCF_PPI) {
508120b6fc9SSvatopluk Kraus 		sc = device_get_softc(dev);
509120b6fc9SSvatopluk Kraus 		BCM_LINTC_LOCK(sc);
510120b6fc9SSvatopluk Kraus 		CPU_SET(PCPU_GET(cpuid), &isrc->isrc_cpu);
511120b6fc9SSvatopluk Kraus 		BCM_LINTC_UNLOCK(sc);
512120b6fc9SSvatopluk Kraus 	}
513120b6fc9SSvatopluk Kraus 	return (0);
514120b6fc9SSvatopluk Kraus }
515120b6fc9SSvatopluk Kraus 
516120b6fc9SSvatopluk Kraus #ifdef SMP
517120b6fc9SSvatopluk Kraus static void
518120b6fc9SSvatopluk Kraus bcm_lintc_init_rwreg_on_ap(struct bcm_lintc_softc *sc, u_int cpu, u_int irq,
519120b6fc9SSvatopluk Kraus     uint32_t reg, uint32_t mask)
520120b6fc9SSvatopluk Kraus {
521120b6fc9SSvatopluk Kraus 
5225b613c19SSvatopluk Kraus 	if (intr_isrc_init_on_cpu(&sc->bls_isrcs[irq].bli_isrc, cpu))
523120b6fc9SSvatopluk Kraus 		bcm_lintc_rwreg_set(sc, reg, mask);
524120b6fc9SSvatopluk Kraus }
525120b6fc9SSvatopluk Kraus 
526120b6fc9SSvatopluk Kraus static void
527120b6fc9SSvatopluk Kraus bcm_lintc_init_pmu_on_ap(struct bcm_lintc_softc *sc, u_int cpu)
528120b6fc9SSvatopluk Kraus {
5295b613c19SSvatopluk Kraus 	struct intr_irqsrc *isrc = &sc->bls_isrcs[BCM_LINTC_PMU_IRQ].bli_isrc;
530120b6fc9SSvatopluk Kraus 
5315b613c19SSvatopluk Kraus 	if (intr_isrc_init_on_cpu(isrc, cpu)) {
532120b6fc9SSvatopluk Kraus 		/* Write-set register. */
533120b6fc9SSvatopluk Kraus 		bcm_lintc_write_4(sc, BCM_LINTC_PMU_ROUTING_SET_REG,
534120b6fc9SSvatopluk Kraus 		    BCM_LINTC_PIRR_IRQ_EN_CORE(cpu));
535120b6fc9SSvatopluk Kraus 	}
536120b6fc9SSvatopluk Kraus }
537120b6fc9SSvatopluk Kraus 
538120b6fc9SSvatopluk Kraus static void
539120b6fc9SSvatopluk Kraus bcm_lintc_init_secondary(device_t dev)
540120b6fc9SSvatopluk Kraus {
541120b6fc9SSvatopluk Kraus 	u_int cpu;
542120b6fc9SSvatopluk Kraus 	struct bcm_lintc_softc *sc;
543120b6fc9SSvatopluk Kraus 
544120b6fc9SSvatopluk Kraus 	cpu = PCPU_GET(cpuid);
545120b6fc9SSvatopluk Kraus 	sc = device_get_softc(dev);
546120b6fc9SSvatopluk Kraus 
547120b6fc9SSvatopluk Kraus 	BCM_LINTC_LOCK(sc);
548120b6fc9SSvatopluk Kraus 	bcm_lintc_init_rwreg_on_ap(sc, cpu, BCM_LINTC_TIMER0_IRQ,
549120b6fc9SSvatopluk Kraus 	    BCM_LINTC_TIMER_CFG_REG(cpu), BCM_LINTC_TCR_IRQ_EN_TIMER(0));
550120b6fc9SSvatopluk Kraus 	bcm_lintc_init_rwreg_on_ap(sc, cpu, BCM_LINTC_TIMER1_IRQ,
551120b6fc9SSvatopluk Kraus 	    BCM_LINTC_TIMER_CFG_REG(cpu), BCM_LINTC_TCR_IRQ_EN_TIMER(1));
552120b6fc9SSvatopluk Kraus 	bcm_lintc_init_rwreg_on_ap(sc, cpu, BCM_LINTC_TIMER2_IRQ,
553120b6fc9SSvatopluk Kraus 	    BCM_LINTC_TIMER_CFG_REG(cpu), BCM_LINTC_TCR_IRQ_EN_TIMER(2));
554120b6fc9SSvatopluk Kraus 	bcm_lintc_init_rwreg_on_ap(sc, cpu, BCM_LINTC_TIMER3_IRQ,
555120b6fc9SSvatopluk Kraus 	    BCM_LINTC_TIMER_CFG_REG(cpu), BCM_LINTC_TCR_IRQ_EN_TIMER(3));
556120b6fc9SSvatopluk Kraus 	bcm_lintc_init_pmu_on_ap(sc, cpu);
557120b6fc9SSvatopluk Kraus 	BCM_LINTC_UNLOCK(sc);
558120b6fc9SSvatopluk Kraus }
559120b6fc9SSvatopluk Kraus 
560120b6fc9SSvatopluk Kraus static void
561120b6fc9SSvatopluk Kraus bcm_lintc_ipi_send(device_t dev, struct intr_irqsrc *isrc, cpuset_t cpus,
562120b6fc9SSvatopluk Kraus     u_int ipi)
563120b6fc9SSvatopluk Kraus {
564120b6fc9SSvatopluk Kraus 	struct bcm_lintc_softc *sc = device_get_softc(dev);
565120b6fc9SSvatopluk Kraus 
566120b6fc9SSvatopluk Kraus 	KASSERT(isrc == &sc->bls_isrcs[BCM_LINTC_MBOX0_IRQ].bli_isrc,
567120b6fc9SSvatopluk Kraus 	    ("%s: bad ISRC %p argument", __func__, isrc));
568120b6fc9SSvatopluk Kraus 	bcm_lintc_ipi_write(sc, cpus, ipi);
569120b6fc9SSvatopluk Kraus }
570120b6fc9SSvatopluk Kraus 
571120b6fc9SSvatopluk Kraus static int
572120b6fc9SSvatopluk Kraus bcm_lintc_ipi_setup(device_t dev, u_int ipi, struct intr_irqsrc **isrcp)
573120b6fc9SSvatopluk Kraus {
574120b6fc9SSvatopluk Kraus 	struct bcm_lintc_softc *sc = device_get_softc(dev);
575120b6fc9SSvatopluk Kraus 
576120b6fc9SSvatopluk Kraus 	KASSERT(ipi < BCM_LINTC_NIPIS, ("%s: too high ipi %u", __func__, ipi));
577120b6fc9SSvatopluk Kraus 
578120b6fc9SSvatopluk Kraus 	*isrcp = &sc->bls_isrcs[BCM_LINTC_MBOX0_IRQ].bli_isrc;
579120b6fc9SSvatopluk Kraus 	return (0);
580120b6fc9SSvatopluk Kraus }
581120b6fc9SSvatopluk Kraus #endif
582120b6fc9SSvatopluk Kraus 
583120b6fc9SSvatopluk Kraus static int
584120b6fc9SSvatopluk Kraus bcm_lintc_pic_attach(struct bcm_lintc_softc *sc)
585120b6fc9SSvatopluk Kraus {
586120b6fc9SSvatopluk Kraus 	struct bcm_lintc_irqsrc *bisrcs;
5879346e913SAndrew Turner 	struct intr_pic *pic;
588120b6fc9SSvatopluk Kraus 	int error;
589120b6fc9SSvatopluk Kraus 	u_int flags;
590120b6fc9SSvatopluk Kraus 	uint32_t irq;
591120b6fc9SSvatopluk Kraus 	const char *name;
592120b6fc9SSvatopluk Kraus 	intptr_t xref;
593120b6fc9SSvatopluk Kraus 
594120b6fc9SSvatopluk Kraus 	bisrcs = sc->bls_isrcs;
595120b6fc9SSvatopluk Kraus 	name = device_get_nameunit(sc->bls_dev);
596120b6fc9SSvatopluk Kraus 	for (irq = 0; irq < BCM_LINTC_NIRQS; irq++) {
597120b6fc9SSvatopluk Kraus 		bisrcs[irq].bli_irq = irq;
598120b6fc9SSvatopluk Kraus 		switch (irq) {
599120b6fc9SSvatopluk Kraus 		case BCM_LINTC_TIMER0_IRQ:
600120b6fc9SSvatopluk Kraus 			bisrcs[irq].bli_mask = BCM_LINTC_TCR_IRQ_EN_TIMER(0);
601120b6fc9SSvatopluk Kraus 			flags = INTR_ISRCF_PPI;
602120b6fc9SSvatopluk Kraus 			break;
603120b6fc9SSvatopluk Kraus 		case BCM_LINTC_TIMER1_IRQ:
604120b6fc9SSvatopluk Kraus 			bisrcs[irq].bli_mask = BCM_LINTC_TCR_IRQ_EN_TIMER(1);
605120b6fc9SSvatopluk Kraus 			flags = INTR_ISRCF_PPI;
606120b6fc9SSvatopluk Kraus 			break;
607120b6fc9SSvatopluk Kraus 		case BCM_LINTC_TIMER2_IRQ:
608120b6fc9SSvatopluk Kraus 			bisrcs[irq].bli_mask = BCM_LINTC_TCR_IRQ_EN_TIMER(2);
609120b6fc9SSvatopluk Kraus 			flags = INTR_ISRCF_PPI;
610120b6fc9SSvatopluk Kraus 			break;
611120b6fc9SSvatopluk Kraus 		case BCM_LINTC_TIMER3_IRQ:
612120b6fc9SSvatopluk Kraus 			bisrcs[irq].bli_mask = BCM_LINTC_TCR_IRQ_EN_TIMER(3);
613120b6fc9SSvatopluk Kraus 			flags = INTR_ISRCF_PPI;
614120b6fc9SSvatopluk Kraus 			break;
615120b6fc9SSvatopluk Kraus 		case BCM_LINTC_MBOX0_IRQ:
616120b6fc9SSvatopluk Kraus 		case BCM_LINTC_MBOX1_IRQ:
617120b6fc9SSvatopluk Kraus 		case BCM_LINTC_MBOX2_IRQ:
618120b6fc9SSvatopluk Kraus 		case BCM_LINTC_MBOX3_IRQ:
619120b6fc9SSvatopluk Kraus 			bisrcs[irq].bli_value = 0;	/* not used */
620120b6fc9SSvatopluk Kraus 			flags = INTR_ISRCF_IPI;
621120b6fc9SSvatopluk Kraus 			break;
622120b6fc9SSvatopluk Kraus 		case BCM_LINTC_GPU_IRQ:
623120b6fc9SSvatopluk Kraus 			bisrcs[irq].bli_value = BCM_LINTC_GIRR_IRQ_CORE(0);
624120b6fc9SSvatopluk Kraus 			flags = 0;
625120b6fc9SSvatopluk Kraus 			break;
626120b6fc9SSvatopluk Kraus 		case BCM_LINTC_PMU_IRQ:
627120b6fc9SSvatopluk Kraus 			bisrcs[irq].bli_value = 0;	/* not used */
628120b6fc9SSvatopluk Kraus 			flags = INTR_ISRCF_PPI;
629120b6fc9SSvatopluk Kraus 			break;
630120b6fc9SSvatopluk Kraus 		default:
631120b6fc9SSvatopluk Kraus 			bisrcs[irq].bli_value = 0;	/* not used */
632120b6fc9SSvatopluk Kraus 			flags = 0;
633120b6fc9SSvatopluk Kraus 			break;
634120b6fc9SSvatopluk Kraus 		}
635120b6fc9SSvatopluk Kraus 
636120b6fc9SSvatopluk Kraus 		error = intr_isrc_register(&bisrcs[irq].bli_isrc, sc->bls_dev,
637120b6fc9SSvatopluk Kraus 		    flags, "%s,%u", name, irq);
638120b6fc9SSvatopluk Kraus 		if (error != 0)
639120b6fc9SSvatopluk Kraus 			return (error);
640120b6fc9SSvatopluk Kraus 	}
641120b6fc9SSvatopluk Kraus 
642120b6fc9SSvatopluk Kraus 	xref = OF_xref_from_node(ofw_bus_get_node(sc->bls_dev));
6439346e913SAndrew Turner 	pic = intr_pic_register(sc->bls_dev, xref);
6449346e913SAndrew Turner 	if (pic == NULL)
6459346e913SAndrew Turner 		return (ENXIO);
646120b6fc9SSvatopluk Kraus 
647120b6fc9SSvatopluk Kraus 	return (intr_pic_claim_root(sc->bls_dev, xref, bcm_lintc_intr, sc, 0));
648120b6fc9SSvatopluk Kraus }
649120b6fc9SSvatopluk Kraus 
650120b6fc9SSvatopluk Kraus static int
651120b6fc9SSvatopluk Kraus bcm_lintc_probe(device_t dev)
652120b6fc9SSvatopluk Kraus {
653120b6fc9SSvatopluk Kraus 
654120b6fc9SSvatopluk Kraus 	if (!ofw_bus_status_okay(dev))
655120b6fc9SSvatopluk Kraus 		return (ENXIO);
656120b6fc9SSvatopluk Kraus 
657120b6fc9SSvatopluk Kraus 	if (!ofw_bus_is_compatible(dev, "brcm,bcm2836-l1-intc"))
658120b6fc9SSvatopluk Kraus 		return (ENXIO);
659120b6fc9SSvatopluk Kraus 	device_set_desc(dev, "BCM2836 Interrupt Controller");
660120b6fc9SSvatopluk Kraus 	return (BUS_PROBE_DEFAULT);
661120b6fc9SSvatopluk Kraus }
662120b6fc9SSvatopluk Kraus 
663120b6fc9SSvatopluk Kraus static int
664120b6fc9SSvatopluk Kraus bcm_lintc_attach(device_t dev)
665120b6fc9SSvatopluk Kraus {
666120b6fc9SSvatopluk Kraus 	struct bcm_lintc_softc *sc;
667120b6fc9SSvatopluk Kraus 	int cpu, rid;
668120b6fc9SSvatopluk Kraus 
669120b6fc9SSvatopluk Kraus 	sc = device_get_softc(dev);
670120b6fc9SSvatopluk Kraus 
671120b6fc9SSvatopluk Kraus 	sc->bls_dev = dev;
672120b6fc9SSvatopluk Kraus 	if (bcm_lintc_sc != NULL)
673120b6fc9SSvatopluk Kraus 		return (ENXIO);
674120b6fc9SSvatopluk Kraus 
675120b6fc9SSvatopluk Kraus 	rid = 0;
676120b6fc9SSvatopluk Kraus 	sc->bls_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
677120b6fc9SSvatopluk Kraus 	    RF_ACTIVE);
678120b6fc9SSvatopluk Kraus 	if (sc->bls_mem == NULL) {
679120b6fc9SSvatopluk Kraus 		device_printf(dev, "could not allocate memory resource\n");
680120b6fc9SSvatopluk Kraus 		return (ENXIO);
681120b6fc9SSvatopluk Kraus 	}
682120b6fc9SSvatopluk Kraus 
683120b6fc9SSvatopluk Kraus 	sc->bls_bst = rman_get_bustag(sc->bls_mem);
684120b6fc9SSvatopluk Kraus 	sc->bls_bsh = rman_get_bushandle(sc->bls_mem);
685120b6fc9SSvatopluk Kraus 
686120b6fc9SSvatopluk Kraus 	bcm_lintc_write_4(sc, BCM_LINTC_CONTROL_REG, 0);
687120b6fc9SSvatopluk Kraus 	bcm_lintc_write_4(sc, BCM_LINTC_PRESCALER_REG, BCM_LINTC_PSR_19_2);
688120b6fc9SSvatopluk Kraus 
689120b6fc9SSvatopluk Kraus 	/* Disable all timers on all cores. */
690120b6fc9SSvatopluk Kraus 	for (cpu = 0; cpu < 4; cpu++)
691120b6fc9SSvatopluk Kraus 		bcm_lintc_write_4(sc, BCM_LINTC_TIMER_CFG_REG(cpu), 0);
692120b6fc9SSvatopluk Kraus 
693120b6fc9SSvatopluk Kraus #ifdef SMP
694120b6fc9SSvatopluk Kraus 	/* Enable mailbox 0 on all cores used for IPI. */
695120b6fc9SSvatopluk Kraus 	for (cpu = 0; cpu < 4; cpu++)
696120b6fc9SSvatopluk Kraus 		bcm_lintc_write_4(sc, BCM_LINTC_MBOX_CFG_REG(cpu),
697120b6fc9SSvatopluk Kraus 		    BCM_LINTC_MCR_IRQ_EN_MBOX(0));
698120b6fc9SSvatopluk Kraus #endif
699120b6fc9SSvatopluk Kraus 
700120b6fc9SSvatopluk Kraus 	if (bcm_lintc_pic_attach(sc) != 0) {
701120b6fc9SSvatopluk Kraus 		device_printf(dev, "could not attach PIC\n");
702120b6fc9SSvatopluk Kraus 		return (ENXIO);
703120b6fc9SSvatopluk Kraus 	}
704120b6fc9SSvatopluk Kraus 
705120b6fc9SSvatopluk Kraus 	BCM_LINTC_LOCK_INIT(sc);
706120b6fc9SSvatopluk Kraus 	bcm_lintc_sc = sc;
707120b6fc9SSvatopluk Kraus 	return (0);
708120b6fc9SSvatopluk Kraus }
709120b6fc9SSvatopluk Kraus 
710120b6fc9SSvatopluk Kraus static device_method_t bcm_lintc_methods[] = {
711120b6fc9SSvatopluk Kraus 	DEVMETHOD(device_probe,		bcm_lintc_probe),
712120b6fc9SSvatopluk Kraus 	DEVMETHOD(device_attach,	bcm_lintc_attach),
713120b6fc9SSvatopluk Kraus 
714120b6fc9SSvatopluk Kraus 	DEVMETHOD(pic_disable_intr,	bcm_lintc_disable_intr),
715120b6fc9SSvatopluk Kraus 	DEVMETHOD(pic_enable_intr,	bcm_lintc_enable_intr),
716120b6fc9SSvatopluk Kraus 	DEVMETHOD(pic_map_intr,		bcm_lintc_map_intr),
717120b6fc9SSvatopluk Kraus 	DEVMETHOD(pic_post_filter,	bcm_lintc_post_filter),
718120b6fc9SSvatopluk Kraus 	DEVMETHOD(pic_post_ithread,	bcm_lintc_post_ithread),
719120b6fc9SSvatopluk Kraus 	DEVMETHOD(pic_pre_ithread,	bcm_lintc_pre_ithread),
720120b6fc9SSvatopluk Kraus 	DEVMETHOD(pic_setup_intr,	bcm_lintc_setup_intr),
721120b6fc9SSvatopluk Kraus #ifdef SMP
722120b6fc9SSvatopluk Kraus 	DEVMETHOD(pic_init_secondary,	bcm_lintc_init_secondary),
723120b6fc9SSvatopluk Kraus 	DEVMETHOD(pic_ipi_send,		bcm_lintc_ipi_send),
724120b6fc9SSvatopluk Kraus 	DEVMETHOD(pic_ipi_setup,	bcm_lintc_ipi_setup),
725120b6fc9SSvatopluk Kraus #endif
726120b6fc9SSvatopluk Kraus 
727120b6fc9SSvatopluk Kraus 	DEVMETHOD_END
728120b6fc9SSvatopluk Kraus };
729120b6fc9SSvatopluk Kraus 
730120b6fc9SSvatopluk Kraus static driver_t bcm_lintc_driver = {
731120b6fc9SSvatopluk Kraus 	"local_intc",
732120b6fc9SSvatopluk Kraus 	bcm_lintc_methods,
733120b6fc9SSvatopluk Kraus 	sizeof(struct bcm_lintc_softc),
734120b6fc9SSvatopluk Kraus };
735120b6fc9SSvatopluk Kraus 
736120b6fc9SSvatopluk Kraus static devclass_t bcm_lintc_devclass;
737120b6fc9SSvatopluk Kraus 
738120b6fc9SSvatopluk Kraus EARLY_DRIVER_MODULE(local_intc, simplebus, bcm_lintc_driver, bcm_lintc_devclass,
739*91cc58afSOleksandr Tymoshenko     0, 0, BUS_PASS_INTERRUPT);
740