interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / arch / gic.c

206 lines · 7335 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* gic.c -- the interrupt controller from the ACPI MADT, and the IRQ enable
18 * service for device code. Two kinds:
19 * - GICv3 (Spark, QEMU gic-version=3): distributor, this CPU's redistributor
20 * (found by GICR_TYPER affinity), system-register CPU interface. Register
21 * offsets: GICv3 spec 12.8-12.11.
22 * - GICv2 (Raspberry Pi 5 GIC-400, QEMU gic-version=2): distributor and the
23 * memory-mapped CPU interface from the MADT GICC Physical Base Address.
24 * Offsets as Linux include/linux/irqchip/arm-gic.h (GIC_DIST_*, GIC_CPU_*).
25 * Group registers are left as firmware set them: with the Security
26 * Extensions (the Pi) this non-secure view enables Group 1 through bit 0 of
27 * GICD_CTLR/GICC_CTLR and IGROUPR is not ours; without them (QEMU) bit 0
28 * enables Group 0, the reset group, which GICC_IAR acknowledges. Same as
29 * Linux irq-gic.c (GICD_ENABLE, GICC_ENABLE, priority mask 0xf0). */
30#include "arch.h"
31#include "ck_internal.h"
32
33#define GICD_CTLR 0x0000
34#define GICD_IGROUPR 0x0080
35#define GICD_ISENABLER 0x0100
36#define GICD_IPRIORITYR 0x0400
37#define GICD_ITARGETSR 0x0800
38#define GICD_IROUTER 0x6000
39#define GICD_PIDR2 0xffe8
40#define GICD_V2_PIDR2 0x0fe8
41#define GICR_TYPER 0x0008
42#define GICR_WAKER 0x0014
43#define GICR_SGI 0x10000
44#define GICR_IGROUPR0 (GICR_SGI + 0x0080)
45#define GICR_ISENABLER0 (GICR_SGI + 0x0100)
46#define GICR_IPRIORITYR (GICR_SGI + 0x0400)
47#define GICC_CTLR 0x00
48#define GICC_PMR 0x04
49#define GICC_BPR 0x08
50#define GICC_IAR 0x0c
51#define GICC_EOIR 0x10
52
53static volatile uint8_t *gicd, *rd, *gicc;
54static unsigned gic_kind; /* 0 none, 2 GICv2 (MMIO CPU interface), 3 GICv3 */
55
56static uint32_t r32(volatile uint8_t *b, uint32_t off) { return *(volatile uint32_t *)(b + off); }
57static void w32(volatile uint8_t *b, uint32_t off, uint32_t v) { *(volatile uint32_t *)(b + off) = v; }
58static uint64_t r64(volatile uint8_t *b, uint32_t off) { return *(volatile uint64_t *)(b + off); }
59static void w64(volatile uint8_t *b, uint32_t off, uint64_t v) { *(volatile uint64_t *)(b + off) = v; }
60
61static void set_prio(volatile uint8_t *b, uint32_t base, uint32_t intid, uint8_t prio)
62{
63 uint32_t off = base + (intid / 4) * 4, sh = (intid % 4) * 8;
64 w32(b, off, (r32(b, off) & ~(0xffu << sh)) | ((uint32_t)prio << sh));
65}
66
67static void rwp_wait(void)
68{
69 for (unsigned i = 0; i < 1000000 && (r32(gicd, GICD_CTLR) & (1u << 31)); i++)
70 ;
71}
72
73static uint64_t my_affinity(void)
74{
75 uint64_t m = ck_rd(mpidr_el1);
76 return (m & 0xffffff) | ((m >> 32) & 0xff) << 24;
77}
78
79static void ppi_enable(uint32_t intid, uint8_t prio)
80{
81 if (gic_kind == 2) { /* GICv2: SGIs/PPIs are banked in the distributor */
82 set_prio(gicd, GICD_IPRIORITYR, intid, prio);
83 w32(gicd, GICD_ISENABLER, 1u << intid);
84 return;
85 }
86 w32(rd, GICR_IGROUPR0, r32(rd, GICR_IGROUPR0) | (1u << intid));
87 set_prio(rd, GICR_IPRIORITYR, intid, prio);
88 w32(rd, GICR_ISENABLER0, 1u << intid);
89}
90
91static int gicv2_init(const struct ck_madt_gic *m, struct ck_gic_report *out)
92{
93 gicd = ck_mmio_map(m->gicd, 0x1000);
94 gicc = ck_mmio_map(m->gicc_base, 0x2000);
95 gic_kind = 2;
96 w32(gicd, GICD_CTLR, 1);
97 w32(gicc, GICC_PMR, 0xf0);
98 w32(gicc, GICC_BPR, 0);
99 w32(gicc, GICC_CTLR, 1);
100 ppi_enable(30, 0x80); /* EL1 physical timer */
101 out->gicd = m->gicd;
102 out->gicr = 0;
103 out->gicc = m->gicc_base;
104 out->kind = 2;
105 out->arch_rev = (r32(gicd, GICD_V2_PIDR2) >> 4) & 0xf;
106 out->icc_sre = 0;
107 out->rd_frame = 0;
108 return 0;
109}
110
111int ck_gic_init(const struct ck_madt_gic *m, struct ck_gic_report *out)
112{
113 int kind = ck_madt_gic_kind(m);
114 if (!m->gicd || !kind)
115 return -1;
116 if (kind == 2)
117 return gicv2_init(m, out);
118 uint64_t region = m->gicr ? m->gicr : m->gicc_gicr;
119 uint64_t len = m->gicr ? m->gicr_len : 0x20000;
120 if (!region || !len)
121 return -1;
122 gicd = ck_mmio_map(m->gicd, 0x10000);
123 volatile uint8_t *r = ck_mmio_map(region, (len + 0xfff) & ~0xfffull);
124 uint64_t aff = my_affinity();
125 rd = 0;
126 for (uint64_t off = 0; off + 0x20000 <= len;) {
127 uint64_t typer = r64(r, (uint32_t)off + GICR_TYPER);
128 if ((typer >> 32) == aff) {
129 rd = r + off;
130 break;
131 }
132 if (typer & (1u << 4)) /* Last */
133 break;
134 off += (typer & 2) ? 0x40000 : 0x20000; /* VLPIS adds two frames */
135 }
136 if (!rd)
137 return -2;
138 /* Wake this redistributor: clear ProcessorSleep, wait ChildrenAsleep. */
139 w32(rd, GICR_WAKER, r32(rd, GICR_WAKER) & ~(1u << 1));
140 unsigned i;
141 for (i = 0; i < 1000000 && (r32(rd, GICR_WAKER) & (1u << 2)); i++)
142 ;
143 if (i == 1000000)
144 return -3;
145 gic_kind = 3;
146 /* Distributor: affinity routing, group 1 enabled (as the Rust kernel). */
147 w32(gicd, GICD_CTLR, (1u << 4) | (1u << 1));
148 rwp_wait();
149 /* CPU interface. */
150 ck_wr_s(ICC_SRE_EL1, ck_rd_s(ICC_SRE_EL1) | 1);
151 ck_isb();
152 ck_wr_s(ICC_PMR_EL1, 0xff);
153 ck_wr_s(ICC_BPR1_EL1, 0);
154 ck_wr_s(ICC_IGRPEN1_EL1, 1);
155 ck_isb();
156 ppi_enable(30, 0x80); /* EL1 physical timer */
157 out->gicd = m->gicd;
158 out->gicr = region;
159 out->gicc = 0;
160 out->kind = 3;
161 out->arch_rev = (r32(gicd, GICD_PIDR2) >> 4) & 0xf;
162 out->icc_sre = ck_rd_s(ICC_SRE_EL1) & 1;
163 out->rd_frame = (uint64_t)(uintptr_t)rd;
164 return 0;
165}
166
167/* Acknowledge the highest-priority pending interrupt; 1020-1023 = none. */
168uint32_t ck_gic_ack(void)
169{
170 if (gic_kind == 2)
171 return r32(gicc, GICC_IAR) & 0x3ff; /* GICC_IAR_INT_ID_MASK; CPUID bits only for SGIs */
172 return (uint32_t)ck_rd_s(ICC_IAR1_EL1) & 0xffffff;
173}
174
175void ck_gic_eoi(uint32_t intid)
176{
177 if (gic_kind == 2)
178 w32(gicc, GICC_EOIR, intid);
179 else
180 ck_wr_s(ICC_EOIR1_EL1, intid);
181}
182
183int ck_irq_enable(uint32_t intid)
184{
185 if (!gicd || !gic_kind || intid >= 1020)
186 return -1;
187 if (intid < 32) {
188 ppi_enable(intid, 0xa0);
189 return 0;
190 }
191 uint32_t word = intid / 32, bit = 1u << (intid % 32);
192 if (gic_kind == 2) {
193 /* ITARGETSR0-7 read back this CPU's own target bit (GICv2 4.3.12). */
194 uint8_t self = (uint8_t)(r32(gicd, GICD_ITARGETSR) & 0xff);
195 set_prio(gicd, GICD_IPRIORITYR, intid, 0xa0);
196 set_prio(gicd, GICD_ITARGETSR, intid, self);
197 w32(gicd, GICD_ISENABLER + word * 4, bit);
198 return 0;
199 }
200 w32(gicd, GICD_IGROUPR + word * 4, r32(gicd, GICD_IGROUPR + word * 4) | bit);
201 set_prio(gicd, GICD_IPRIORITYR, intid, 0xa0);
202 uint64_t m = ck_rd(mpidr_el1);
203 w64(gicd, GICD_IROUTER + intid * 8, (m & 0xffffff) | (m & 0xff00000000ull));
204 w32(gicd, GICD_ISENABLER + word * 4, bit);
205 return 0;
206}