Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/aienux/kernel/arm64/gicv3.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / aienux / kernel / arm64 / gicv3.om` 450 lines ยท 11582 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note aienux/kernel/arm64/gicv3.om: Pure-Omega GICv3 Distributor + per-core Redistributor (with LPI tables) 3 note + ICC_*_EL1 CPU interface + GICv3 ITS (GITS_CBASER/GITS_BASERn + MAPD/MAPC/MAPTI/INVALL/SYNC) + IRQ dispatch. 4 5 const GICD_CTLR ulong 0x0000 6 const GICD_PIDR2 ulong 0xffe8 7 const GICD_IGROUPR ulong 0x0080 8 const GICD_ISENABLER ulong 0x0100 9 const GICD_IPRIORITYR ulong 0x0400 10 const GICD_IROUTER ulong 0x6000 11 12 const GICR_CTLR ulong 0x0000 13 const GICR_TYPER ulong 0x0008 14 const GICR_WAKER ulong 0x0014 15 const GICR_PROPBASER ulong 0x0070 16 const GICR_PENDBASER ulong 0x0078 17 const GICR_SGI ulong 0x10000 18 const GICR_IGROUPR0 ulong 0x10080 19 const GICR_ISENABLER0 ulong 0x10100 20 const GICR_IPRIORITYR ulong 0x10400 21 22 const GITS_CTLR ulong 0x0000 23 const GITS_TYPER ulong 0x0008 24 const GITS_CBASER ulong 0x0080 25 const GITS_CWRITER ulong 0x0088 26 const GITS_CREADR ulong 0x0090 27 const GITS_BASER0 ulong 0x0100 28 const GITS_TRANSLATER ulong 0x10040 29 30 note ITS attribute bits (IHI 0069H): Valid (bit 63), InnerCache=Normal-NC (0b001 << 59), Shareability=OSH (0b10 << 10) 31 const GITS_ATTR_NC_OSH ulong 0x8800000000000800 32 note GICR_PROPBASER/PENDBASER attrs: InnerCache=Normal-NC (0b001 << 7), Shareability=OSH (0b10 << 10) 33 const GICR_ATTR_NC_OSH ulong 0x0000000000000880 34 const GICR_PEND_PTZ ulong 0x4000000000000000 35 36 const GIC_MAX_HANDLERS ulong 64 37 const GIC_BOOT_LPI_SLOT ulong 64 38 39 note gic_state: 40 note 0=gicd_pa, 1=gicr_base, 2=gicr_len, 3=gits_pa, 4=arch_rev, 5=its_cmdq_pa, 41 note 6=its_cwrite, 7=next_lpi, 8=lpi_prop_pa, 9=boot_rd_pa, 10=its_ready, 11=its_cmd_count, 42 note 12=gicd_rwp_timeout (1 when GICD_CTLR.RWP never cleared; gicv3_init then fails closed) 43 global gic_state ulong 16 44 note Slots 0..63 = MADT secondary core indices, slot 64 = dedicated boot core slot (even if BSP MADT idx != 0) 45 global gic_lpi_pend ulong 65 46 global gic_irq_id ulong 64 47 global gic_irq_tag ulong 64 48 global gic_irq_hits ulong 64 49 50 fn gic_my_affinity to ulong 51 m = arm64_sysrd SYSREG_MPIDR_EL1 52 lo = and m 0xffffff 53 hi = shl and shr m 32 0xff 24 54 ret or lo hi 55 end 56 57 note Wait for GICD_CTLR.RWP (bit 31) to clear. Returns false, and sets gic_state 12, if it is still set after 58 note `1000000` polls: the caller must not continue as if the Distributor write had taken effect. 59 fn gicd_rwp_wait gicd ulong to bool 60 i = ulong 0 61 while lt i `1000000` bound `1000000` 62 if eq and fwld add gicd GICD_CTLR 4 0x80000000 0 63 ret true 64 end 65 i = add i 1 66 end 67 put gic_state 12 1 68 ret false 69 end 70 71 note Locate this CPU's Redistributor frame by matching GICR_TYPER[63:32] against MPIDR affinity. 72 fn gicr_find_mine base ulong length ulong to ulong 73 aff = gic_my_affinity 74 off = ulong 0 75 while lt off length bound 256 76 rd = add base off 77 typer = fwld add rd GICR_TYPER 8 78 raff = shr typer 32 79 if eq raff aff 80 ret rd 81 end 82 if ne and typer 16 0 83 ret 0 84 end 85 stride = ulong 0x20000 86 if ne and typer 2 0 87 stride = ulong 0x40000 88 end 89 off = add off stride 90 end 91 ret 0 92 end 93 94 fn gicv3_enable_ppi30 rd ulong to void 95 ig = fwld add rd GICR_IGROUPR0 4 96 x = fwst add rd GICR_IGROUPR0 4 or ig shl ulong 1 30 97 poff = add GICR_IPRIORITYR 28 98 pv = fwld add rd poff 4 99 pv = or and pv ulong 0xff00ffff ulong 0x00800000 100 x = fwst add rd poff 4 pv 101 x = fwst add rd GICR_ISENABLER0 4 shl ulong 1 30 102 end 103 104 note Pre-allocate shared LPI property table and per-core 64 KiB pending table ON THE BOOT CORE ONLY 105 note (zero concurrent allocator access from secondary cores during 20-core SMP bring-up). 106 fn gic_prealloc_lpi_tables core_idx ulong to bool 107 if ge core_idx 65 108 ret false 109 end 110 prop = get gic_state 8 111 if eq prop 0 112 prop = dma_alloc_nc 16 65536 113 if eq prop 0 114 ret false 115 end 116 i = ulong 0 117 while lt i 65536 bound 8192 118 x = fwst add prop i 8 0x8181818181818181 119 i = add i 8 120 end 121 arm64_dsb_sy 122 put gic_state 8 prop 123 end 124 if eq get gic_lpi_pend core_idx 0 125 pend = dma_alloc_nc 16 65536 126 if eq pend 0 127 ret false 128 end 129 put gic_lpi_pend core_idx pend 130 end 131 ret true 132 end 133 134 note Configure per-core GICR LPI Property + Pending tables (pre-allocated on boot core) and enable LPIs. 135 fn gicr_init_lpis rd ulong core_idx ulong to bool 136 if ge core_idx 65 137 ret false 138 end 139 prop = get gic_state 8 140 pend = get gic_lpi_pend core_idx 141 if or eq prop 0 eq pend 0 142 ret false 143 end 144 note Disable EnableLPIs while programming PROPBASER/PENDBASER. 145 ctlr = fwld add rd GICR_CTLR 4 146 x = fwst add rd GICR_CTLR 4 and ctlr ulong 0xfffffffe 147 arm64_dsb_sy 148 x = fwst add rd GICR_PROPBASER 8 or or GICR_ATTR_NC_OSH prop 15 149 x = fwst add rd GICR_PENDBASER 8 or or GICR_PEND_PTZ GICR_ATTR_NC_OSH pend 150 arm64_dsb_sy 151 x = fwst add rd GICR_CTLR 4 or ctlr 1 152 arm64_dsb_sy 153 ret true 154 end 155 156 note Initialize per-core GICR + LPIs + ICC_*_EL1 CPU interface (boot core and every secondary core). 157 fn gicv3_init_cpu core_idx ulong to bool 158 base = get gic_state 1 159 length = get gic_state 2 160 if or eq base 0 eq length 0 161 ret false 162 end 163 rd = gicr_find_mine base length 164 if eq rd 0 165 ret false 166 end 167 if eq get gic_state 9 0 168 put gic_state 9 rd 169 end 170 waker = fwld add rd GICR_WAKER 4 171 x = fwst add rd GICR_WAKER 4 and waker ulong 0xfffffffd 172 i = ulong 0 173 while lt i `1000000` bound `1000000` 174 if eq and fwld add rd GICR_WAKER 4 4 0 175 break 176 end 177 i = add i 1 178 end 179 gicv3_enable_ppi30 rd 180 if ne get gic_state 3 0 181 if not gicr_init_lpis rd core_idx 182 ret false 183 end 184 end 185 sre = arm64_sysrd SYSREG_ICC_SRE_EL1 186 arm64_syswr SYSREG_ICC_SRE_EL1 or sre 1 187 arm64_syswr SYSREG_ICC_PMR_EL1 0xf0 188 arm64_syswr SYSREG_ICC_BPR1_EL1 0 189 arm64_syswr SYSREG_ICC_CTLR_EL1 0 190 arm64_syswr SYSREG_ICC_IGRPEN1_EL1 1 191 arm64_isb 192 ret true 193 end 194 195 note Initialize GICv3 Distributor (once on boot core) + boot core GICR + ICC_*_EL1. 196 fn gicv3_init gicd ulong gicr ulong gicr_len ulong gits ulong to bool 197 if or eq gicd 0 eq gicr 0 198 ret false 199 end 200 put gic_state 0 gicd 201 put gic_state 1 gicr 202 put gic_state 2 gicr_len 203 put gic_state 3 gits 204 put gic_state 7 8192 205 put gic_state 8 0 206 put gic_state 9 0 207 put gic_state 10 0 208 put gic_state 11 0 209 for ci ulong 65 210 put gic_lpi_pend ci 0 211 end 212 pidr2 = fwld add gicd GICD_PIDR2 4 213 put gic_state 4 and shr pidr2 4 15 214 put gic_state 12 0 215 x = fwst add gicd GICD_CTLR 4 0 216 if not gicd_rwp_wait gicd 217 ret false 218 end 219 x = fwst add gicd GICD_CTLR 4 0x12 220 if not gicd_rwp_wait gicd 221 ret false 222 end 223 if ne gits 0 224 if not gic_prealloc_lpi_tables GIC_BOOT_LPI_SLOT 225 ret false 226 end 227 end 228 ret gicv3_init_cpu GIC_BOOT_LPI_SLOT 229 end 230 231 fn gicv3_set_spi_prio gicd ulong intid ulong prio ulong to void 232 poff = add GICD_IPRIORITYR mul div intid 4 4 233 psh = mul rem intid 4 8 234 pv = fwld add gicd poff 4 235 clr = xor ulong 0xffffffff shl ulong 0xff psh 236 ins = shl and prio 0xff psh 237 x = fwst add gicd poff 4 or and pv clr ins 238 end 239 240 note Enable SPI intid (32..1019) routed to the current CPU's affinity. 241 fn gicv3_enable_spi intid ulong prio ulong to bool 242 if or lt intid 32 ge intid 1020 243 ret false 244 end 245 gicd = get gic_state 0 246 if eq gicd 0 247 ret false 248 end 249 reg = mul div intid 32 4 250 bit = shl ulong 1 rem intid 32 251 g1 = fwld add add gicd GICD_IGROUPR reg 4 252 x = fwst add add gicd GICD_IGROUPR reg 4 or g1 bit 253 gicv3_set_spi_prio gicd intid prio 254 aff = and arm64_sysrd SYSREG_MPIDR_EL1 0x000000ff00ffffff 255 x = fwst add add gicd GICD_IROUTER mul intid 8 8 aff 256 x = fwst add add gicd GICD_ISENABLER reg 4 bit 257 arm64_dsb_sy 258 ret true 259 end 260 261 fn gic_its_quiescent_wait gits ulong to bool 262 i = ulong 0 263 while lt i `1000000` bound `1000000` 264 if ne and fwld add gits GITS_CTLR 4 0x80000000 0 265 ret true 266 end 267 i = add i 1 268 end 269 ret false 270 end 271 272 fn gic_its_write_baser gits ulong off ulong tbl ulong to void 273 bv = or or GITS_ATTR_NC_OSH tbl 15 274 x = fwst add gits off 8 bv 275 rb = fwld add gits off 8 276 ps = and shr rb 8 3 277 if eq ps 1 278 bv2 = or GITS_ATTR_NC_OSH shl ulong 1 8 279 bv2 = or or bv2 tbl 3 280 x = fwst add gits off 8 bv2 281 else 282 if eq ps 2 283 bv2 = or GITS_ATTR_NC_OSH shl ulong 2 8 284 bv2 = or bv2 tbl 285 x = fwst add gits off 8 bv2 286 end 287 end 288 end 289 290 note Configure GITS_BASER0..7 Device (type 1) and Collection (type 4) tables in Normal-NC OSH memory. 291 fn gic_its_setup_basers gits ulong to bool 292 i = ulong 0 293 while lt i 8 bound 8 294 off = add GITS_BASER0 mul i 8 295 val = fwld add gits off 8 296 btype = and shr val 56 7 297 if or eq btype 1 eq btype 4 298 tbl = dma_alloc_nc 16 65536 299 if eq tbl 0 300 ret false 301 end 302 gic_its_write_baser gits off tbl 303 end 304 i = add i 1 305 end 306 ret true 307 end 308 309 note GICv3 ITS bring-up: poll Quiescent, program GITS_CBASER (Normal-NC, OSH), GITS_BASERn, enable ITS. 310 fn gic_its_init to bool 311 gits = get gic_state 3 312 if eq gits 0 313 ret false 314 end 315 x = fwst add gits GITS_CTLR 4 0 316 arm64_dsb_sy 317 if not gic_its_quiescent_wait gits 318 ret false 319 end 320 cq = dma_alloc_nc 16 65536 321 if eq cq 0 322 ret false 323 end 324 put gic_state 5 cq 325 put gic_state 6 0 326 if not gic_its_setup_basers gits 327 ret false 328 end 329 note GITS_CBASER: Valid(63) | InnerCache=Normal-NC(1<<59) | Shareability=OSH(2<<10) | cq | (16-1) 330 cbaser = or or GITS_ATTR_NC_OSH cq 15 331 x = fwst add gits GITS_CBASER 8 cbaser 332 x = fwst add gits GITS_CWRITER 8 0 333 arm64_dsb_sy 334 x = fwst add gits GITS_CTLR 4 1 335 arm64_dsb_sy 336 put gic_state 10 1 337 ret true 338 end 339 340 note Enqueue one 32-byte ITS command, advance GITS_CWRITER, and wait for GITS_CREADR to catch up. 341 fn gic_its_cmd w0 ulong w1 ulong w2 ulong w3 ulong to bool 342 gits = get gic_state 3 343 cq = get gic_state 5 344 if or eq gits 0 or eq cq 0 eq get gic_state 10 0 345 ret false 346 end 347 cw = get gic_state 6 348 slot = add cq cw 349 x = fwst add slot 0 8 w0 350 x = fwst add slot 8 8 w1 351 x = fwst add slot 16 8 w2 352 x = fwst add slot 24 8 w3 353 arm64_dsb_sy 354 ncw = and add cw 32 0xffff 355 put gic_state 6 ncw 356 x = fwst add gits GITS_CWRITER 8 ncw 357 arm64_dsb_sy 358 put gic_state 11 add get gic_state 11 1 359 i = ulong 0 360 while lt i `1000000` bound `1000000` 361 cr = and fwld add gits GITS_CREADR 8 0xfffe0 362 if eq cr ncw 363 ret true 364 end 365 i = add i 1 366 end 367 ret false 368 end 369 370 note Compute target RDbase field for MAPC/SYNC (either physical RD address or Processor_Number per GITS_TYPER.PTA). 371 fn gic_its_rd_target to ulong 372 gits = get gic_state 3 373 rd = get gic_state 9 374 typer = fwld add gits GITS_TYPER 8 375 note GITS_TYPER.PTA (bit 19): 1 = physical address >> 16, 0 = GICR_TYPER.Processor_Number 376 if ne and typer 0x80000 0 377 ret shr rd 16 378 end 379 rtyper = fwld add rd GICR_TYPER 8 380 ret and shr rtyper 8 0xffff 381 end 382 383 note Allocate LPI range for PCIe MSI-X DeviceID, submit MAPD/MAPC/MAPTI/INVALL/SYNC, 384 note and return doorbell PA (gits_pa + GITS_TRANSLATER). Stores lpi_base in out_lpi[0]; 0 on failure. 385 fn gic_its_map_msix devid ulong nvecs ulong out_lpi edit ulong 1 to ulong 386 gits = get gic_state 3 387 if or eq gits 0 or eq get gic_state 10 0 or eq nvecs 0 gt nvecs 64 388 ret 0 389 end 390 itt = dma_alloc_nc 1 4096 391 if eq itt 0 392 ret 0 393 end 394 lpi = get gic_state 7 395 put gic_state 7 add lpi nvecs 396 rdt = gic_its_rd_target 397 note MAPD (0x08): w0 = 0x08 | (devid << 32), w1 = size(6 = 7-bit EventID), w2 = itt | Valid(bit 63) 398 if not gic_its_cmd or 0x08 shl devid 32 6 or itt shl ulong 1 63 0 399 ret 0 400 end 401 note MAPC (0x09): icid=0 -> target RDbase, Valid(bit 63) 402 if not gic_its_cmd 0x09 0 or shl ulong 1 63 shl rdt 16 0 403 ret 0 404 end 405 note MAPTI (0x0a) for each event 0..nvecs-1 -> lpi + ev on collection 0 406 ev = ulong 0 407 while lt ev nvecs bound 64 408 w0 = or 0x0a shl devid 32 409 w1 = or ev shl add lpi ev 32 410 if not gic_its_cmd w0 w1 0 0 411 ret 0 412 end 413 ev = add ev 1 414 end 415 note INVALL (0x0d) + SYNC (0x05) 416 if not gic_its_cmd 0x0d 0 0 0 417 ret 0 418 end 419 if not gic_its_cmd 0x05 0 shl rdt 16 0 420 ret 0 421 end 422 put out_lpi 0 lpi 423 ret add gits GITS_TRANSLATER 424 end 425 426 fn irq_register intid ulong tag ulong to bool 427 i = ulong 0 428 while lt i GIC_MAX_HANDLERS bound 64 429 if or eq get gic_irq_id i 0 eq get gic_irq_id i intid 430 put gic_irq_id i intid 431 put gic_irq_tag i tag 432 put gic_irq_hits i 0 433 ret true 434 end 435 i = add i 1 436 end 437 ret false 438 end 439 440 note irqhook: called by vbar_el1 EL1h IRQ vector with intid from ICC_IAR1_EL1 (SPIs 32..1019 and LPIs 8192+). 441 fn irqhook intid ulong to void 442 i = ulong 0 443 while lt i GIC_MAX_HANDLERS bound 64 444 if eq get gic_irq_id i intid 445 put gic_irq_hits i add get gic_irq_hits i 1 446 ret 447 end 448 i = add i 1 449 end 450 end