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