interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / aienux / kernel / arm64 / gicv3.om

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