RTEMS 6.1-rc5
Loading...
Searching...
No Matches
arm-gicv3.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-2-Clause */
2
12/*
13 * Copyright (C) 2022 embedded brains GmbH & Co. KG
14 * Copyright (C) 2019 On-Line Applications Research Corporation (OAR)
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#ifndef _RTEMS_DEV_IRQ_ARM_GICV3_H
39#define _RTEMS_DEV_IRQ_ARM_GICV3_H
40
41#include <dev/irq/arm-gic.h>
42#include <dev/irq/arm-gic-arch.h>
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
54#define PRIORITY_DEFAULT 127
55
56#define MPIDR_AFFINITY2(val) BSP_FLD64(val, 16, 23)
57#define MPIDR_AFFINITY2_GET(reg) BSP_FLD64GET(reg, 16, 23)
58#define MPIDR_AFFINITY2_SET(reg, val) BSP_FLD64SET(reg, val, 16, 23)
59#define MPIDR_AFFINITY1(val) BSP_FLD64(val, 8, 15)
60#define MPIDR_AFFINITY1_GET(reg) BSP_FLD64GET(reg, 8, 15)
61#define MPIDR_AFFINITY1_SET(reg, val) BSP_FLD64SET(reg, val, 8, 15)
62#define MPIDR_AFFINITY0(val) BSP_FLD64(val, 0, 7)
63#define MPIDR_AFFINITY0_GET(reg) BSP_FLD64GET(reg, 0, 7)
64#define MPIDR_AFFINITY0_SET(reg, val) BSP_FLD64SET(reg, val, 0, 7)
65
66#define ICC_SGIR_AFFINITY3(val) BSP_FLD64(val, 48, 55)
67#define ICC_SGIR_AFFINITY3_GET(reg) BSP_FLD64GET(reg, 48, 55)
68#define ICC_SGIR_AFFINITY3_SET(reg, val) BSP_FLD64SET(reg, val, 48, 55)
69#define ICC_SGIR_IRM BSP_BIT32(40)
70#define ICC_SGIR_AFFINITY2(val) BSP_FLD64(val, 32, 39)
71#define ICC_SGIR_AFFINITY2_GET(reg) BSP_FLD64GET(reg, 32, 39)
72#define ICC_SGIR_AFFINITY2_SET(reg, val) BSP_FLD64SET(reg, val, 32, 39)
73#define ICC_SGIR_INTID(val) BSP_FLD64(val, 24, 27)
74#define ICC_SGIR_INTID_GET(reg) BSP_FLD64GET(reg, 24, 27)
75#define ICC_SGIR_INTID_SET(reg, val) BSP_FLD64SET(reg, val, 24, 27)
76#define ICC_SGIR_AFFINITY1(val) BSP_FLD64(val, 16, 23)
77#define ICC_SGIR_AFFINITY1_GET(reg) BSP_FLD64GET(reg, 16, 23)
78#define ICC_SGIR_AFFINITY1_SET(reg, val) BSP_FLD64SET(reg, val, 16, 23)
79#define ICC_SGIR_CPU_TARGET_LIST(val) BSP_FLD64(val, 0, 15)
80#define ICC_SGIR_CPU_TARGET_LIST_GET(reg) BSP_FLD64GET(reg, 0, 15)
81#define ICC_SGIR_CPU_TARGET_LIST_SET(reg, val) BSP_FLD64SET(reg, val, 0, 15)
82
83#ifdef ARM_MULTILIB_ARCH_V4
84/* cpuif->iccicr */
85#define ICC_CTLR "p15, 0, %0, c12, c12, 4"
86
87/* cpuif->iccpmr */
88#define ICC_PMR "p15, 0, %0, c4, c6, 0"
89
90/* cpuif->iccbpr */
91#define ICC_BPR0 "p15, 0, %0, c12, c8, 3"
92#define ICC_BPR1 "p15, 0, %0, c12, c12, 3"
93
94/* cpuif->icciar */
95#define ICC_IAR0 "p15, 0, %0, c12, c8, 0"
96#define ICC_IAR1 "p15, 0, %0, c12, c12, 0"
97
98/* cpuif->icceoir */
99#define ICC_EOIR0 "p15, 0, %0, c12, c8, 1"
100#define ICC_EOIR1 "p15, 0, %0, c12, c12, 1"
101
102#define ICC_SRE "p15, 0, %0, c12, c12, 5"
103
104#define ICC_IGRPEN0 "p15, 0, %0, c12, c12, 6"
105#define ICC_IGRPEN1 "p15, 0, %0, c12, c12, 7"
106
107#define MPIDR "p15, 0, %0, c0, c0, 5"
108
109#define READ_SR(SR_NAME) \
110({ \
111 uint32_t value; \
112 __asm__ volatile("mrc " SR_NAME : "=r" (value) ); \
113 value; \
114})
115
116#define WRITE_SR(SR_NAME, VALUE) \
117 __asm__ volatile("mcr " SR_NAME " \n" : : "r" (VALUE) );
118
119#define ICC_SGI1 "p15, 0, %Q0, %R0, c12"
120#define WRITE64_SR(SR_NAME, VALUE) \
121 __asm__ volatile("mcrr " SR_NAME " \n" : : "r" (VALUE) );
122
123#else /* ARM_MULTILIB_ARCH_V4 */
124
125/* AArch64 GICv3 registers are not named in GCC */
126#define ICC_IGRPEN0_EL1 "S3_0_C12_C12_6, %0"
127#define ICC_IGRPEN1_EL1 "S3_0_C12_C12_7, %0"
128#define ICC_IGRPEN1_EL3 "S3_6_C12_C12_7, %0"
129#define ICC_IGRPEN0 ICC_IGRPEN0_EL1
130#define ICC_IGRPEN1 ICC_IGRPEN1_EL1
131#define ICC_PMR "S3_0_C4_C6_0, %0"
132#define ICC_EOIR1 "S3_0_C12_C12_1, %0"
133#define ICC_SRE "S3_0_C12_C12_5, %0"
134#define ICC_BPR0 "S3_0_C12_C8_3, %0"
135#define ICC_BPR1 "S3_0_C12_C12_3, %0"
136#define ICC_CTLR "S3_0_C12_C12_4, %0"
137#define ICC_IAR1 "%0, S3_0_C12_C12_0"
138#define MPIDR "%0, mpidr_el1"
139#define MPIDR_AFFINITY3(val) BSP_FLD64(val, 32, 39)
140#define MPIDR_AFFINITY3_GET(reg) BSP_FLD64GET(reg, 32, 39)
141#define MPIDR_AFFINITY3_SET(reg, val) BSP_FLD64SET(reg, val, 32, 39)
142
143#define ICC_SGI1 "S3_0_C12_C11_5, %0"
144#define WRITE64_SR(SR_NAME, VALUE) \
145 __asm__ volatile("msr " SR_NAME " \n" : : "r" (VALUE) );
146#define WRITE_SR(SR_NAME, VALUE) WRITE64_SR(SR_NAME, VALUE)
147
148#define READ_SR(SR_NAME) \
149({ \
150 uint64_t value; \
151 __asm__ volatile("mrs " SR_NAME : "=&r" (value) ); \
152 value; \
153})
154
155#endif /* ARM_MULTILIB_ARCH_V4 */
156
157static inline volatile gic_redist *gicv3_get_redist(uint32_t cpu_index)
158{
159 return (volatile gic_redist *)
160 ((uintptr_t)BSP_ARM_GIC_REDIST_BASE + cpu_index * 0x20000);
161}
162
163static inline volatile gic_sgi_ppi *gicv3_get_sgi_ppi(uint32_t cpu_index)
164{
165 return (volatile gic_sgi_ppi *)
166 ((uintptr_t)BSP_ARM_GIC_REDIST_BASE + cpu_index * 0x20000 + 0x10000);
167}
168
169static inline void gicv3_sgi_ppi_enable(
170 rtems_vector_number vector,
171 uint32_t cpu_index
172)
173{
174 volatile gic_sgi_ppi *sgi_ppi = gicv3_get_sgi_ppi(cpu_index);
175
176 /* Set G1NS */
177 sgi_ppi->icspigrpr[0] |= 1U << vector;
178 sgi_ppi->icspigrpmodr[0] &= ~(1U << vector);
179
180 /* Set enable */
181 sgi_ppi->icspiser[0] = 1U << vector;
182}
183
184static inline void gicv3_sgi_ppi_disable(
185 rtems_vector_number vector,
186 uint32_t cpu_index
187)
188{
189 volatile gic_sgi_ppi *sgi_ppi = gicv3_get_sgi_ppi(cpu_index);
190
191 sgi_ppi->icspicer[0] = 1U << vector;
192}
193
194static inline bool gicv3_sgi_ppi_is_enabled(
195 rtems_vector_number vector,
196 uint32_t cpu_index
197)
198{
199 volatile gic_sgi_ppi *sgi_ppi = gicv3_get_sgi_ppi(cpu_index);
200
201 return (sgi_ppi->icspiser[0] & (1U << vector)) != 0;
202}
203
204static inline void gicv3_sgi_ppi_set_priority(
205 rtems_vector_number vector,
206 uint8_t priority,
207 uint32_t cpu_index
208)
209{
210 volatile gic_sgi_ppi *sgi_ppi = gicv3_get_sgi_ppi(cpu_index);
211
212 sgi_ppi->icspiprior[vector] = priority;
213}
214
215static inline uint8_t gicv3_sgi_ppi_get_priority(
216 rtems_vector_number vector,
217 uint32_t cpu_index
218)
219{
220 volatile gic_sgi_ppi *sgi_ppi = gicv3_get_sgi_ppi(cpu_index);
221
222 return sgi_ppi->icspiprior[vector];
223}
224
225static inline bool gicv3_sgi_ppi_is_pending(
226 rtems_vector_number vector,
227 uint32_t cpu_index
228)
229{
230 volatile gic_sgi_ppi *sgi_ppi = gicv3_get_sgi_ppi(cpu_index);
231
232 return (sgi_ppi->icspispendr[0] & (1U << vector)) != 0;
233}
234
235static inline void gicv3_ppi_set_pending(
236 rtems_vector_number vector,
237 uint32_t cpu_index
238)
239{
240 volatile gic_sgi_ppi *sgi_ppi = gicv3_get_sgi_ppi(cpu_index);
241
242 sgi_ppi->icspispendr[0] = 1U << vector;
243}
244
245static inline void gicv3_ppi_clear_pending(
246 rtems_vector_number vector,
247 uint32_t cpu_index
248)
249{
250 volatile gic_sgi_ppi *sgi_ppi = gicv3_get_sgi_ppi(cpu_index);
251
252 sgi_ppi->icspicpendr[0] = 1U << vector;
253}
254
255static inline void gicv3_trigger_sgi(
256 rtems_vector_number vector,
257 uint32_t targets
258)
259{
260#ifndef ARM_MULTILIB_ARCH_V4
261 uint64_t mpidr;
262#else
263 uint32_t mpidr;
264#endif
265 mpidr = READ_SR(MPIDR);
266 uint64_t value = ICC_SGIR_AFFINITY2(MPIDR_AFFINITY2_GET(mpidr))
267 | ICC_SGIR_INTID(vector)
268 | ICC_SGIR_AFFINITY1(MPIDR_AFFINITY1_GET(mpidr))
269 | ICC_SGIR_CPU_TARGET_LIST(targets);
270#ifndef ARM_MULTILIB_ARCH_V4
271 value |= ICC_SGIR_AFFINITY3(MPIDR_AFFINITY3_GET(mpidr));
272#endif
273 WRITE64_SR(ICC_SGI1, value);
274}
275
276static inline uint32_t gicv3_get_id_count(volatile gic_dist *dist)
277{
278 uint32_t id_count = GIC_DIST_ICDICTR_IT_LINES_NUMBER_GET(dist->icdictr);
279
280 id_count = 32 * (id_count + 1);
281 id_count = id_count <= 1020 ? id_count : 1020;
282
283 return id_count;
284}
285
286static void gicv3_init_dist(volatile gic_dist *dist)
287{
288 uint32_t id_count = gicv3_get_id_count(dist);
289 uint32_t id;
290
291 dist->icddcr = GIC_DIST_ICDDCR_ARE_NS | GIC_DIST_ICDDCR_ARE_S
292 | GIC_DIST_ICDDCR_ENABLE_GRP1S | GIC_DIST_ICDDCR_ENABLE_GRP1NS
293 | GIC_DIST_ICDDCR_ENABLE_GRP0;
294
295 for (id = 0; id < id_count; id += 32) {
296 /* Disable all interrupts */
297 dist->icdicer[id / 32] = 0xffffffff;
298
299 /* Set G1NS */
300 dist->icdigr[id / 32] = 0xffffffff;
301 dist->icdigmr[id / 32] = 0;
302 }
303
304 for (id = 0; id < id_count; ++id) {
305 gic_id_set_priority(dist, id, PRIORITY_DEFAULT);
306 }
307
308 for (id = 32; id < id_count; ++id) {
309 gic_id_set_targets(dist, id, 0x01);
310 }
311}
312
313static void gicv3_init_cpu_interface(uint32_t cpu_index)
314{
315 /* Initialize Interrupt Controller System Register Enable Register */
316#ifdef BSP_ARM_GIC_ICC_SRE
317 WRITE_SR(ICC_SRE, BSP_ARM_GIC_ICC_SRE);
318#endif
319
320 /* Initialize Interrupt Controller Interrupt Priority Mask Register */
321#ifdef BSP_ARM_GIC_ICC_PMR
322 WRITE_SR(ICC_PMR, BSP_ARM_GIC_ICC_PMR);
323#endif
324
325 /* Initialize Interrupt Controller Binary Point Register 0 */
326#ifdef BSP_ARM_GIC_ICC_BPR0
327 WRITE_SR(ICC_BPR0, BSP_ARM_GIC_ICC_BPR0);
328#endif
329
330 /* Initialize Interrupt Controller Binary Point Register 1 */
331#ifdef BSP_ARM_GIC_ICC_BPR1
332 WRITE_SR(ICC_BPR1, BSP_ARM_GIC_ICC_BPR1);
333#endif
334
335 volatile gic_redist *redist = gicv3_get_redist(cpu_index);
336 uint32_t waker = redist->icrwaker;
337 uint32_t waker_mask = GIC_REDIST_ICRWAKER_PROCESSOR_SLEEP;
338 waker &= ~waker_mask;
339 redist->icrwaker = waker;
340
341 volatile gic_sgi_ppi *sgi_ppi = gicv3_get_sgi_ppi(cpu_index);
342 /* Set G1NS */
343 sgi_ppi->icspigrpr[0] = 0xffffffff;
344 sgi_ppi->icspigrpmodr[0] = 0;
345 for (int id = 0; id < 32; id++) {
346 sgi_ppi->icspiprior[id] = PRIORITY_DEFAULT;
347 }
348
349 /* Initialize Interrupt Controller Interrupt Group Enable 0 Register */
350#ifdef BSP_ARM_GIC_ICC_IGRPEN0
351 WRITE_SR(ICC_IGRPEN0, BSP_ARM_GIC_ICC_IGRPEN0);
352#endif
353
354 /* Initialize Interrupt Controller Interrupt Group Enable 1 Register */
355#ifdef BSP_ARM_GIC_ICC_IGRPEN1
356 WRITE_SR(ICC_IGRPEN1, BSP_ARM_GIC_ICC_IGRPEN1);
357#endif
358
359 /* Initialize Interrupt Controller Control Register */
360#ifdef BSP_ARM_GIC_ICC_CTRL
361 WRITE_SR(ICC_CTLR, BSP_ARM_GIC_ICC_CTRL);
362#endif
363}
364
365static inline void gicv3_get_attributes(
366 rtems_vector_number vector,
368)
369{
370 attributes->is_maskable = true;
371 attributes->maybe_enable = true;
372 attributes->maybe_disable = true;
373 attributes->can_raise = true;
374 attributes->can_get_priority = true;
375 attributes->can_set_priority = true;
376 attributes->maximum_priority = 255;
377
378 if ( vector <= ARM_GIC_IRQ_SGI_LAST ) {
379 /*
380 * It is implementation-defined whether implemented SGIs are permanently
381 * enabled, or can be enabled and disabled by writes to GICD_ISENABLER0 and
382 * GICD_ICENABLER0.
383 */
384 attributes->can_raise_on = true;
385 attributes->cleared_by_acknowledge = true;
387 } else {
388 attributes->can_disable = true;
389 attributes->can_clear = true;
391
392 if ( vector > ARM_GIC_IRQ_PPI_LAST ) {
393 /* SPI */
394 attributes->can_get_affinity = true;
395 attributes->can_set_affinity = true;
396 }
397 }
398}
399
402#ifdef __cplusplus
403}
404#endif
405
406#endif /* _RTEMS_DEV_IRQ_ARM_GICV3_H */
This header file provides interfaces of the ARM Generic Interrupt Controller (GIC) support.
ISR_Vector_number rtems_vector_number
This integer type represents interrupt vector numbers.
Definition: intr.h:102
@ RTEMS_INTERRUPT_NO_SIGNAL
This interrupt signal variant indicates that the interrupt cannot be triggered by a signal.
Definition: intr.h:2039
@ RTEMS_INTERRUPT_UNSPECIFIED_SIGNAL
This interrupt signal variant indicates that the interrupt trigger signal is unspecified.
Definition: intr.h:2033
Definition: arm-gic-regs.h:108
Definition: arm-gic-regs.h:217
Definition: arm-gic-regs.h:249
This structure provides the attributes of an interrupt vector.
Definition: intr.h:2076
bool can_raise_on
This member is true, if the interrupt vector can be raised on a processor by rtems_interrupt_raise_on...
Definition: intr.h:2140
bool can_disable
This member is true, if the interrupt vector can be disabled by rtems_interrupt_vector_disable(),...
Definition: intr.h:2117
bool can_set_priority
This member is true, if the priority of the interrupt vector can be set by rtems_interrupt_set_priori...
Definition: intr.h:2198
bool maybe_enable
This member is true, if the interrupt vector may be enabled by rtems_interrupt_vector_enable(),...
Definition: intr.h:2107
rtems_interrupt_signal_variant trigger_signal
This member describes the trigger signal of the interrupt associated with the interrupt vector.
Definition: intr.h:2186
bool can_raise
This member is true, if the interrupt vector can be raised by rtems_interrupt_raise(),...
Definition: intr.h:2134
bool can_clear
This member is true, if the interrupt vector can be cleared by rtems_interrupt_clear(),...
Definition: intr.h:2146
bool can_set_affinity
This member is true, if the affinity set of the interrupt vector can be set by rtems_interrupt_set_af...
Definition: intr.h:2165
bool cleared_by_acknowledge
This member is true, if the pending status of the interrupt associated with the interrupt vector is c...
Definition: intr.h:2153
uint32_t maximum_priority
This member represents the maximum priority value of the interrupt vector. By convention,...
Definition: intr.h:2209
bool maybe_disable
This member is true, if the interrupt vector may be disabled by rtems_interrupt_vector_disable(),...
Definition: intr.h:2128
bool can_get_priority
This member is true, if the priority of the interrupt vector can be obtained by rtems_interrupt_get_p...
Definition: intr.h:2192
bool is_maskable
This member is true, if the interrupt vector is maskable by rtems_interrupt_local_disable(),...
Definition: intr.h:2085
bool can_get_affinity
This member is true, if the affinity set of the interrupt vector can be obtained by rtems_interrupt_g...
Definition: intr.h:2159