blob: 5e78d42c2db02cea5c8db7f8170557968557e505 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2015 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <trace.h>
24#include <assert.h>
25#include <err.h>
26#include <bits.h>
27#include <kernel/spinlock.h>
28#include <kernel/thread.h>
29#include <kernel/mp.h>
30#include <platform/interrupts.h>
31#include <platform/bcm28xx.h>
32
33#if defined (BCM2836)
34#include <arch/arm.h>
35#elif defined (BCM2837)
36#include <arch/arm64.h>
37#else
38#error Unknown BCM28XX Variant
39#endif
40
41
42#define LOCAL_TRACE 0
43
44/* global interrupt controller */
45#define INTC_PEND0 (ARMCTRL_INTC_BASE + 0x0)
46#define INTC_PEND1 (ARMCTRL_INTC_BASE + 0x4)
47#define INTC_PEND2 (ARMCTRL_INTC_BASE + 0x8)
48#define INTC_FAST (ARMCTRL_INTC_BASE + 0xc)
49#define INTC_ENABLE1 (ARMCTRL_INTC_BASE + 0x10)
50#define INTC_ENABLE2 (ARMCTRL_INTC_BASE + 0x14)
51#define INTC_ENABLE3 (ARMCTRL_INTC_BASE + 0x18)
52#define INTC_DISABLE1 (ARMCTRL_INTC_BASE + 0x1c)
53#define INTC_DISABLE2 (ARMCTRL_INTC_BASE + 0x20)
54#define INTC_DISABLE3 (ARMCTRL_INTC_BASE + 0x24)
55
56/* per-cpu local interrupt controller bits.
57 * each is repeated 4 times, one per cpu.
58 */
59#define INTC_LOCAL_TIMER_INT_CONTROL0 (ARM_LOCAL_BASE + 0x40)
60#define INTC_LOCAL_TIMER_INT_CONTROL1 (ARM_LOCAL_BASE + 0x44)
61#define INTC_LOCAL_TIMER_INT_CONTROL2 (ARM_LOCAL_BASE + 0x48)
62#define INTC_LOCAL_TIMER_INT_CONTROL3 (ARM_LOCAL_BASE + 0x4c)
63
64#define INTC_LOCAL_MAILBOX_INT_CONTROL0 (ARM_LOCAL_BASE + 0x50)
65#define INTC_LOCAL_MAILBOX_INT_CONTROL1 (ARM_LOCAL_BASE + 0x54)
66#define INTC_LOCAL_MAILBOX_INT_CONTROL2 (ARM_LOCAL_BASE + 0x58)
67#define INTC_LOCAL_MAILBOX_INT_CONTROL3 (ARM_LOCAL_BASE + 0x5c)
68
69#define INTC_LOCAL_IRQ_PEND0 (ARM_LOCAL_BASE + 0x60)
70#define INTC_LOCAL_IRQ_PEND1 (ARM_LOCAL_BASE + 0x64)
71#define INTC_LOCAL_IRQ_PEND2 (ARM_LOCAL_BASE + 0x68)
72#define INTC_LOCAL_IRQ_PEND3 (ARM_LOCAL_BASE + 0x6c)
73
74#define INTC_LOCAL_FIQ_PEND0 (ARM_LOCAL_BASE + 0x70)
75#define INTC_LOCAL_FIQ_PEND1 (ARM_LOCAL_BASE + 0x74)
76#define INTC_LOCAL_FIQ_PEND2 (ARM_LOCAL_BASE + 0x78)
77#define INTC_LOCAL_FIQ_PEND3 (ARM_LOCAL_BASE + 0x7c)
78
79#define INTC_LOCAL_MAILBOX0_SET0 (ARM_LOCAL_BASE + 0x80)
80#define INTC_LOCAL_MAILBOX0_SET1 (ARM_LOCAL_BASE + 0x90)
81#define INTC_LOCAL_MAILBOX0_SET2 (ARM_LOCAL_BASE + 0xa0)
82#define INTC_LOCAL_MAILBOX0_SET3 (ARM_LOCAL_BASE + 0xb0)
83
84#define INTC_LOCAL_MAILBOX0_CLR0 (ARM_LOCAL_BASE + 0xc0)
85#define INTC_LOCAL_MAILBOX0_CLR1 (ARM_LOCAL_BASE + 0xd0)
86#define INTC_LOCAL_MAILBOX0_CLR2 (ARM_LOCAL_BASE + 0xe0)
87#define INTC_LOCAL_MAILBOX0_CLR3 (ARM_LOCAL_BASE + 0xf0)
88
89struct int_handler_struct {
90 int_handler handler;
91 void *arg;
92};
93
94static struct int_handler_struct int_handler_table[MAX_INT];
95
96static spin_lock_t lock = SPIN_LOCK_INITIAL_VALUE;
97
98status_t mask_interrupt(unsigned int vector)
99{
100 LTRACEF("vector %u\n", vector);
101
102 spin_lock_saved_state_t state;
103 spin_lock_irqsave(&lock, state);
104
105 if (vector >= INTERRUPT_ARM_LOCAL_CNTPSIRQ && vector <= INTERRUPT_ARM_LOCAL_CNTVIRQ) {
106 // local timer interrupts, mask on all cpus
107 for (uint cpu = 0; cpu < 4; cpu++) {
108 uintptr_t reg = INTC_LOCAL_TIMER_INT_CONTROL0 + cpu * 4;
109
110 *REG32(reg) &= (1 << (vector - INTERRUPT_ARM_LOCAL_CNTPSIRQ));
111 }
112 } else if (/* vector >= ARM_IRQ1_BASE && */ vector < (ARM_IRQ0_BASE + 32)) {
113 uintptr_t reg;
114 if (vector >= ARM_IRQ0_BASE)
115 reg = INTC_DISABLE3;
116 else if (vector >= ARM_IRQ2_BASE)
117 reg = INTC_DISABLE2;
118 else
119 reg = INTC_DISABLE1;
120
121 *REG32(reg) = 1 << (vector % 32);
122 } else {
123 PANIC_UNIMPLEMENTED;
124 }
125
126 spin_unlock_irqrestore(&lock, state);
127
128 return NO_ERROR;
129}
130
131status_t unmask_interrupt(unsigned int vector)
132{
133 LTRACEF("vector %u\n", vector);
134
135 spin_lock_saved_state_t state;
136 spin_lock_irqsave(&lock, state);
137
138 if (vector >= INTERRUPT_ARM_LOCAL_CNTPSIRQ && vector <= INTERRUPT_ARM_LOCAL_CNTVIRQ) {
139 // local timer interrupts, unmask for all cpus
140 for (uint cpu = 0; cpu < 4; cpu++) {
141 uintptr_t reg = INTC_LOCAL_TIMER_INT_CONTROL0 + cpu * 4;
142
143 *REG32(reg) |= (1 << (vector - INTERRUPT_ARM_LOCAL_CNTPSIRQ));
144 }
145 } else if (/* vector >= ARM_IRQ1_BASE && */ vector < (ARM_IRQ0_BASE + 32)) {
146 uintptr_t reg;
147 if (vector >= ARM_IRQ0_BASE)
148 reg = INTC_ENABLE3;
149 else if (vector >= ARM_IRQ2_BASE)
150 reg = INTC_ENABLE2;
151 else
152 reg = INTC_ENABLE1;
153
154 *REG32(reg) = 1 << (vector % 32);
155 } else {
156 PANIC_UNIMPLEMENTED;
157 }
158
159 spin_unlock_irqrestore(&lock, state);
160
161 return NO_ERROR;
162}
163
164void register_int_handler(unsigned int vector, int_handler handler, void *arg)
165{
166 if (vector >= MAX_INT)
167 panic("register_int_handler: vector out of range %d\n", vector);
168
169 spin_lock_saved_state_t state;
170 spin_lock_irqsave(&lock, state);
171
172 int_handler_table[vector].handler = handler;
173 int_handler_table[vector].arg = arg;
174
175 spin_unlock_irqrestore(&lock, state);
176}
177
178enum handler_return platform_irq(struct arm_iframe *frame)
179{
180 uint vector;
181 uint cpu = arch_curr_cpu_num();
182
183 THREAD_STATS_INC(interrupts);
184
185 // see what kind of irq it is
186 uint32_t pend = *REG32(INTC_LOCAL_IRQ_PEND0 + cpu * 4);
187
188 pend &= ~(1 << (INTERRUPT_ARM_LOCAL_GPU_FAST % 32)); // mask out gpu interrupts
189
190 if (pend != 0) {
191 // it's a local interrupt
192 LTRACEF("local pend 0x%x\n", pend);
193 vector = ARM_IRQ_LOCAL_BASE + ctz(pend);
194 goto decoded;
195 }
196
197 // XXX disable for now, since all of the interesting irqs are mirrored into the other banks
198#if 0
199 // look in bank 0 (ARM interrupts)
200 pend = *REG32(INTC_PEND0);
201 LTRACEF("pend0 0x%x\n", pend);
202 pend &= ~((1<<8)|(1<<9)); // mask out bit 8 and 9
203 if (pend != 0) {
204 // it's a bank 0 interrupt
205 vector = ARM_IRQ0_BASE + ctz(pend);
206 goto decoded;
207 }
208#endif
209
210 // look for VC interrupt bank 1
211 pend = *REG32(INTC_PEND1);
212 LTRACEF("pend1 0x%x\n", pend);
213 if (pend != 0) {
214 // it's a bank 1 interrupt
215 vector = ARM_IRQ1_BASE + ctz(pend);
216 goto decoded;
217 }
218
219 // look for VC interrupt bank 2
220 pend = *REG32(INTC_PEND2);
221 LTRACEF("pend2 0x%x\n", pend);
222 if (pend != 0) {
223 // it's a bank 2 interrupt
224 vector = ARM_IRQ2_BASE + ctz(pend);
225 goto decoded;
226 }
227
228 vector = 0xffffffff;
229
230decoded:
231 LTRACEF("cpu %u vector %u\n", cpu, vector);
232
233 // dispatch the irq
234 enum handler_return ret = INT_NO_RESCHEDULE;
235
236#if WITH_SMP
237 if (vector == INTERRUPT_ARM_LOCAL_MAILBOX0) {
238 pend = *REG32(INTC_LOCAL_MAILBOX0_CLR0 + 0x10 * cpu);
239 LTRACEF("mailbox0 clr 0x%x\n", pend);
240
241 // ack it
242 *REG32(INTC_LOCAL_MAILBOX0_CLR0 + 0x10 * cpu) = pend;
243
244 if (pend & (1 << MP_IPI_GENERIC)) {
245 PANIC_UNIMPLEMENTED;
246 }
247 if (pend & (1 << MP_IPI_RESCHEDULE)) {
248 ret = mp_mbx_reschedule_irq();
249 }
250 } else
251#endif // WITH_SMP
252 if (vector == 0xffffffff) {
253 ret = INT_NO_RESCHEDULE;
254 } else if (int_handler_table[vector].handler) {
255 ret = int_handler_table[vector].handler(int_handler_table[vector].arg);
256 } else {
257 panic("irq %u fired on cpu %u but no handler set!\n", vector, cpu);
258 }
259
260 return ret;
261}
262
263enum handler_return platform_fiq(struct arm_iframe *frame)
264{
265 PANIC_UNIMPLEMENTED;
266}
267
268void bcm2835_send_ipi(uint irq, uint cpu_mask)
269{
270 LTRACEF("irq %u, cpu_mask 0x%x\n", irq, cpu_mask);
271
272 for (uint i = 0; i < 4; i++) {
273 if (cpu_mask & (1<<i)) {
274 LTRACEF("sending to cpu %u\n", i);
275 *REG32(INTC_LOCAL_MAILBOX0_SET0 + 0x10 * i) = (1 << irq);
276 }
277 }
278}
279
280void intc_init(void)
281{
282 // mask everything
283 *REG32(INTC_DISABLE1) = 0xffffffff;
284 *REG32(INTC_DISABLE2) = 0xffffffff;
285 *REG32(INTC_DISABLE3) = 0xffffffff;
286
287#if WITH_SMP
288 // unable mailbox irqs on all cores
289 for (uint i = 0; i < 4; i++) {
290 *REG32(INTC_LOCAL_MAILBOX_INT_CONTROL0 + 0x4 * i) = 0x1;
291 }
292#endif
293}
294