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 / crates / aienos-kernel / mm_iommu.om

3098 lines · 73579 bytes Raw Blame History
1note aienux/crates/aienos-kernel/mm_iommu.om
2note Pure OM-2 port of aienos-kernel memory allocator, map plan, SMMUv3,
3note DMA gate, PCI enumeration/BAR, device authority, EDU DMA, block RamDisk,
4note and NVMe power-fail atomicity subsystems.
5
6note ========================================================================
7note 1. Physical Frame Allocator (mem/frame_allocator.rs)
8note ========================================================================
9
10const KMI_PAGE_SIZE ulong 4096
11const KMI_MAX_BATCH_FRAMES ulong 160
12
13fn kmi_is_page_aligned addr ulong to bool
14ret eq and addr 0xfff 0
15end
16
17fn kmi_align_up addr ulong to ulong
18ret and add addr 4095 0xfffffffffffff000
19end
20
21fn kmi_align_down addr ulong to ulong
22ret and addr 0xfffffffffffff000
23end
24
25fn kmi_frame_start base ulong idx ulong to ulong
26ret add base mul idx 4096
27end
28
29note BitmapFrameAllocator layout in st edit ulong *:
30note st[0] = base_addr
31note st[1] = total_frames
32note st[2] = allocated_count
33note st[3] = cap_words
34note st[4..4+cap_words] = bitmap words (u64 each)
35
36fn kmi_fa_init st edit ulong * base ulong total_frames ulong cap_words ulong to int
37if ne and base 0xfff 0
38ret -1
39end
40if lt len st add 4 cap_words
41ret -2
42end
43need_words = div total_frames 64
44if ne rem total_frames 64 0
45need_words = add need_words 1
46end
47if gt need_words cap_words
48ret -2
49end
50max_frames = div sub 0xffffffffffffffff base 4096
51if gt total_frames max_frames
52ret -3
53end
54put st 0 base
55put st 1 total_frames
56put st 2 ulong 0
57put st 3 cap_words
58for wi ulong cap_words bound 4096
59put st add 4 wi ulong 0
60end
61ret 0
62end
63
64fn kmi_fa_total_frames st view ulong * to ulong
65ret get st 1
66end
67
68fn kmi_fa_allocated_count st view ulong * to ulong
69ret get st 2
70end
71
72fn kmi_fa_free_count st view ulong * to ulong
73tot = get st 1
74alc = get st 2
75if ge alc tot
76ret 0
77end
78ret sub tot alc
79end
80
81fn kmi_trailing_ones w ulong to ulong
82bit = ulong 0
83while lt bit 64 bound 64
84if eq and shr w bit 1 0
85ret bit
86end
87bit = add bit 1
88end
89ret 64
90end
91
92fn kmi_fa_alloc st edit ulong * addr_out edit ulong 1 to bool
93cap_w = get st 3
94tot = get st 1
95base = get st 0
96for wi ulong cap_w bound 4096
97w = get st add 4 wi
98if ne w 0xffffffffffffffff
99bi = kmi_trailing_ones w
100fidx = add mul wi 64 bi
101if lt fidx tot
102nw = or w shl ulong 1 bi
103put st add 4 wi nw
104put st 2 add get st 2 1
105put addr_out 0 add base mul fidx 4096
106ret true
107end
108end
109end
110ret false
111end
112
113fn kmi_fa_free st edit ulong * addr ulong to bool
114if ne and addr 0xfff 0
115ret false
116end
117base = get st 0
118if lt addr base
119ret false
120end
121fidx = div sub addr base 4096
122if ge fidx get st 1
123ret false
124end
125wi = div fidx 64
126bi = rem fidx 64
127w = get st add 4 wi
128mask = shl ulong 1 bi
129if eq and w mask 0
130ret false
131end
132put st add 4 wi and w xor mask 0xffffffffffffffff
133alc = get st 2
134if gt alc 0
135put st 2 sub alc 1
136end
137ret true
138end
139
140fn kmi_fa_mark_run st edit ulong * run_start ulong count ulong to void
141for k ulong count bound 65536
142f = add run_start k
143idx = add 4 div f 64
144cur = get st idx
145bit_v = shl ulong 1 rem f 64
146put st idx or cur bit_v
147end
148put st 2 add get st 2 count
149end
150
151fn kmi_fa_is_free st view ulong * i ulong to bool
152idx = add 4 div i 64
153cur = get st idx
154bit_v = shl ulong 1 rem i 64
155ret eq and cur bit_v 0
156end
157
158fn kmi_fa_alloc_contig st edit ulong * count ulong addr_out edit ulong 1 to bool
159if eq count 0
160ret false
161end
162if gt count kmi_fa_free_count st
163ret false
164end
165tot = get st 1
166run_start = ulong 0
167cur_run = ulong 0
168for i ulong tot bound 65536
169if kmi_fa_is_free st i
170if eq cur_run 0
171run_start = i
172end
173cur_run = add cur_run 1
174if eq cur_run count
175kmi_fa_mark_run st run_start count
176put addr_out 0 add get st 0 mul run_start 4096
177ret true
178end
179else
180cur_run = 0
181end
182end
183ret false
184end
185
186fn kmi_fa_release_batch st edit ulong * batch view ulong * count ulong to bool
187complete = true
188for i ulong count bound 160
189ok = kmi_fa_free st get batch i
190if not ok
191complete = false
192end
193end
194ret complete
195end
196
197fn kmi_fa_alloc_batch st edit ulong * count ulong batch edit ulong * to ulong
198tmp = new ulong 1 0
199if eq count 0
200ret 0
201end
202if gt count KMI_MAX_BATCH_FRAMES
203ret 0
204end
205if gt count len batch
206ret 0
207end
208if gt count kmi_fa_free_count st
209ret 0
210end
211got = ulong 0
212for i ulong count bound 160
213ok = kmi_fa_alloc st tmp
214if ok
215put batch i get tmp 0
216got = add got 1
217else
218ignore = kmi_fa_release_batch st batch got
219ret 0
220end
221end
222ret got
223end
224
225note ========================================================================
226note 2. Map Plan Builder and Validator (mem/map_plan.rs)
227note ========================================================================
228
229const KMI_EFI_MEMORY_RUNTIME ulong 0x8000000000000000
230const KMI_EFI_MEMORY_RO ulong 0x20000
231const KMI_EFI_MEMORY_XP ulong 0x4000
232
233const KMI_EFI_LOADER_CODE ulong 1
234const KMI_EFI_RUNTIME_CODE ulong 5
235const KMI_EFI_RUNTIME_DATA ulong 6
236const KMI_EFI_MMIO ulong 11
237
238const KMI_SCN_MEM_EXECUTE ulong 0x20000000
239const KMI_SCN_MEM_WRITE ulong 0x80000000
240
241const KMI_ATTR_NORMAL_WB ulong 0
242const KMI_ATTR_DEVICE_NGNRE ulong 1
243
244const KMI_FLAG_WRITABLE ulong 1
245const KMI_FLAG_EXECUTABLE ulong 2
246const KMI_FLAG_USER ulong 4
247const KMI_FLAG_SHAREABLE ulong 8
248const KMI_FLAG_GLOBAL ulong 16
249const KMI_FLAG_DEVICE ulong 256
250const KMI_FLAGS_KERNEL_DATA ulong 25
251
252const KMI_PLAN_OK int 0
253const KMI_PLAN_ERR_EMPTY_RANGE int 1
254const KMI_PLAN_ERR_OVERFLOW int 2
255const KMI_PLAN_ERR_RT_ATTRS_MISSING int 3
256const KMI_PLAN_ERR_RT_CANNOT_SPLIT int 4
257const KMI_PLAN_ERR_OVERLAP int 5
258const KMI_PLAN_ERR_SEC_OUTSIDE int 6
259const KMI_PLAN_ERR_CAPACITY int 7
260
261const KMI_RT_DECISION_NONE ulong 0
262const KMI_RT_DECISION_RX_UNSPLIT ulong 1
263
264fn kmi_mair_el1 to ulong
265ret 0x04ff
266end
267
268fn kmi_tcr_el1 parange uchar to ulong
269t = ulong 16
270t = or t shl ulong 1 8
271t = or t shl ulong 1 10
272t = or t shl ulong 3 12
273t = or t shl ulong 16 16
274t = or t shl ulong 1 23
275t = or t shl ulong 2 30
276ips = and ulong parange 7
277t = or t shl ips 32
278ret t
279end
280
281fn kmi_sctlr_el1 to ulong
282s = ulong 0x30d00800
283s = or s 1
284s = or s shl ulong 1 2
285s = or s shl ulong 1 12
286ret s
287end
288
289fn kmi_map_flags wr bool ex bool attr ulong to ulong
290f = or KMI_FLAG_SHAREABLE KMI_FLAG_GLOBAL
291if wr
292f = or f KMI_FLAG_WRITABLE
293end
294if ex
295f = or f KMI_FLAG_EXECUTABLE
296end
297if eq attr KMI_ATTR_DEVICE_NGNRE
298f = or f KMI_FLAG_DEVICE
299end
300ret f
301end
302
303fn kmi_range_end_ok start ulong rlen ulong to bool
304ret le rlen sub 0xffffffffffffffff start
305end
306
307fn kmi_range_overlap s1 ulong l1 ulong s2 ulong l2 ulong to bool
308e1 = add s1 l1
309e2 = add s2 l2
310if lt s1 e2
311if lt s2 e1
312ret true
313end
314end
315ret false
316end
317
318note plan_meta[4]: [count, decision, max_entries, reserved]
319note plan_out: 3 ulongs per entry [start, length, flags]
320
321fn kmi_plan_init meta edit ulong 4 max_entries ulong to void
322put meta 0 ulong 0
323put meta 1 KMI_RT_DECISION_NONE
324put meta 2 max_entries
325put meta 3 ulong 0
326end
327
328fn kmi_plan_push meta edit ulong 4 plan edit ulong * start ulong rlen ulong flg ulong to int
329cnt = get meta 0
330if ge cnt get meta 2
331ret KMI_PLAN_ERR_CAPACITY
332end
333off = mul cnt 3
334if gt add off 3 len plan
335ret KMI_PLAN_ERR_CAPACITY
336end
337put plan off start
338put plan add off 1 rlen
339put plan add off 2 flg
340put meta 0 add cnt 1
341ret KMI_PLAN_OK
342end
343
344note secs: 3 ulongs per section [va, size, characteristics]
345note img_reg[4]: [img_start, img_len, region_start, region_len]
346
347fn kmi_plan_sec_align va ulong sz ulong out_al edit ulong 2 to int
348if not kmi_range_end_ok va sz
349ret KMI_PLAN_ERR_OVERFLOW
350end
351s_end = add va sz
352if not kmi_range_end_ok s_end 4095
353ret KMI_PLAN_ERR_OVERFLOW
354end
355al_start = and va 0xfffffffffffff000
356al_end = and add s_end 4095 0xfffffffffffff000
357if lt al_end al_start
358ret KMI_PLAN_ERR_OVERFLOW
359end
360put out_al 0 al_start
361put out_al 1 sub al_end al_start
362ret KMI_PLAN_OK
363end
364
365fn kmi_plan_one_sec meta edit ulong 4 plan edit ulong * img_reg view ulong 4 sec view ulong 3 to int
366al = new ulong 2 0
367sz = get sec 1
368if eq sz 0
369ret KMI_PLAN_OK
370end
371rc = kmi_plan_sec_align get sec 0 sz al
372if ne rc KMI_PLAN_OK
373ret rc
374end
375if not kmi_range_overlap get al 0 get al 1 get img_reg 0 get img_reg 1
376ret KMI_PLAN_ERR_SEC_OUTSIDE
377end
378ch = get sec 2
379if ne and ch KMI_SCN_MEM_EXECUTE 0
380if ne and ch KMI_SCN_MEM_WRITE 0
381ret KMI_PLAN_ERR_OVERLAP
382end
383end
384flg = kmi_map_flags ne and ch KMI_SCN_MEM_WRITE 0 ne and ch KMI_SCN_MEM_EXECUTE 0 KMI_ATTR_NORMAL_WB
385ret kmi_plan_push meta plan get al 0 get al 1 flg
386end
387
388fn kmi_plan_add_loader meta edit ulong 4 plan edit ulong * img_reg view ulong 4 secs view ulong * to int
389sec = new ulong 3 0
390if not kmi_range_end_ok get img_reg 0 get img_reg 1
391ret KMI_PLAN_ERR_OVERFLOW
392end
393before = get meta 0
394nsecs = div len secs 3
395for i ulong nsecs bound 256
396put sec 0 get secs mul i 3
397put sec 1 get secs add mul i 3 1
398put sec 2 get secs add mul i 3 2
399rc = kmi_plan_one_sec meta plan img_reg sec
400if ne rc KMI_PLAN_OK
401ret rc
402end
403end
404if eq get meta 0 before
405ret kmi_plan_push meta plan get img_reg 2 get img_reg 3 KMI_FLAGS_KERNEL_DATA
406end
407ret KMI_PLAN_OK
408end
409
410note reg_info[3]: [start, length, has_rt_attrs (0 or 1)]
411note rt_attrs: 4 ulongs per attr [start, length, read_only(0/1), execute_protect(0/1)]
412
413fn kmi_plan_find_rt_attr rt_attrs view ulong * cursor ulong to long
414n = div len rt_attrs 4
415for i ulong n bound 256
416off = mul i 4
417as = get rt_attrs off
418al = get rt_attrs add off 1
419if le as cursor
420if kmi_range_end_ok as al
421ae = add as al
422if gt ae cursor
423ret long i
424end
425end
426end
427end
428ret -1
429end
430
431fn kmi_plan_rt_flags ro bool xp bool to ulong
432is_wr = false
433if not ro
434if xp
435is_wr = true
436end
437end
438ret kmi_map_flags is_wr not xp KMI_ATTR_NORMAL_WB
439end
440
441fn kmi_plan_rt_step meta edit ulong 4 plan edit ulong * cur_end edit ulong 2 attr view ulong 4 to int
442stop = add get attr 0 get attr 1
443if gt stop get cur_end 1
444stop = get cur_end 1
445end
446ro = ne get attr 2 0
447xp = ne get attr 3 0
448if not xp
449if not ro
450put meta 1 KMI_RT_DECISION_RX_UNSPLIT
451end
452end
453flg = kmi_plan_rt_flags ro xp
454rc = kmi_plan_push meta plan get cur_end 0 sub stop get cur_end 0 flg
455if ne rc KMI_PLAN_OK
456ret rc
457end
458put cur_end 0 stop
459ret KMI_PLAN_OK
460end
461
462fn kmi_plan_add_runtime meta edit ulong 4 plan edit ulong * reg_info view ulong 3 rt_attrs view ulong * to int
463cur_end = new ulong 2 0
464attr = new ulong 4 0
465if not kmi_range_end_ok get reg_info 0 get reg_info 1
466ret KMI_PLAN_ERR_OVERFLOW
467end
468if eq get reg_info 2 0
469flg = kmi_map_flags false true KMI_ATTR_NORMAL_WB
470rc = kmi_plan_push meta plan get reg_info 0 get reg_info 1 flg
471if ne rc KMI_PLAN_OK
472ret rc
473end
474put meta 1 KMI_RT_DECISION_RX_UNSPLIT
475ret KMI_PLAN_OK
476end
477put cur_end 0 get reg_info 0
478put cur_end 1 add get reg_info 0 get reg_info 1
479while lt get cur_end 0 get cur_end 1 bound 256
480idx = kmi_plan_find_rt_attr rt_attrs get cur_end 0
481if lt idx 0
482ret KMI_PLAN_ERR_RT_ATTRS_MISSING
483end
484aoff = mul ulong idx 4
485for k ulong 4
486put attr k get rt_attrs add aoff k
487end
488rc = kmi_plan_rt_step meta plan cur_end attr
489if ne rc KMI_PLAN_OK
490ret rc
491end
492end
493ret KMI_PLAN_OK
494end
495
496fn kmi_is_kernel_data_type mtype ulong to bool
497if eq mtype 2
498ret true
499end
500if eq mtype 3
501ret true
502end
503if eq mtype 4
504ret true
505end
506if eq mtype 7
507ret true
508end
509if eq mtype 9
510ret true
511end
512if eq mtype 10
513ret true
514end
515if eq mtype KMI_EFI_RUNTIME_DATA
516ret true
517end
518ret false
519end
520
521note Process one EFI memory descriptor: desc[4] = [mtype, phys_start, page_count, attribute]
522note img_ctx[3] = [img_start, img_len, has_rt_attrs]
523
524fn kmi_plan_add_desc_mmio_or_data meta edit ulong 4 plan edit ulong * mtype ulong rstart ulong rlen ulong to int
525if eq mtype KMI_EFI_MMIO
526flg = kmi_map_flags true false KMI_ATTR_DEVICE_NGNRE
527ret kmi_plan_push meta plan rstart rlen flg
528end
529if kmi_is_kernel_data_type mtype
530ret kmi_plan_push meta plan rstart rlen KMI_FLAGS_KERNEL_DATA
531end
532ret KMI_PLAN_OK
533end
534
535fn kmi_plan_step_desc meta edit ulong 4 plan edit ulong * desc view ulong 4 img_ctx view ulong 3 to int
536note Handles MMIO and normal data descriptors directly; returns 100 if LOADER_CODE, 101 if RUNTIME_CODE
537mtype = get desc 0
538pstart = get desc 1
539pages = get desc 2
540attr = get desc 3
541if gt pages div ulong 0xffffffffffffffff 4096
542ret KMI_PLAN_ERR_OVERFLOW
543end
544rlen = mul pages 4096
545if eq rlen 0
546ret KMI_PLAN_OK
547end
548if eq mtype KMI_EFI_LOADER_CODE
549ret 100
550end
551if eq mtype KMI_EFI_MMIO
552ret kmi_plan_add_desc_mmio_or_data meta plan mtype pstart rlen
553end
554is_rt = eq mtype KMI_EFI_RUNTIME_CODE
555if not is_rt
556if ne and attr KMI_EFI_MEMORY_RUNTIME 0
557if ne mtype KMI_EFI_RUNTIME_DATA
558is_rt = true
559end
560end
561end
562if is_rt
563ret 101
564end
565ret kmi_plan_add_desc_mmio_or_data meta plan mtype pstart rlen
566end
567
568fn kmi_plan_mmio_covered plan view ulong * desc_cnt ulong ms ulong me ulong to bool
569for i ulong desc_cnt bound 256
570off = mul i 3
571ps = get plan off
572pl = get plan add off 1
573pf = get plan add off 2
574if ne and pf KMI_FLAG_DEVICE 0
575if le ps ms
576if kmi_range_end_ok ps pl
577if ge add ps pl me
578ret true
579end
580end
581end
582end
583end
584ret false
585end
586
587note mmio: 2 ulongs per range [start, length]
588
589fn kmi_plan_add_mmio meta edit ulong 4 plan edit ulong * desc_cnt ulong mmio view ulong * to int
590n = div len mmio 2
591for i ulong n bound 256
592off = mul i 2
593ms = get mmio off
594ml = get mmio add off 1
595if not kmi_range_end_ok ms ml
596ret KMI_PLAN_ERR_OVERFLOW
597end
598me = add ms ml
599cov = kmi_plan_mmio_covered plan desc_cnt ms me
600if not cov
601flg = kmi_map_flags true false KMI_ATTR_DEVICE_NGNRE
602rc = kmi_plan_push meta plan ms ml flg
603if ne rc KMI_PLAN_OK
604ret rc
605end
606end
607end
608ret KMI_PLAN_OK
609end
610
611fn kmi_plan_validate_overlaps meta view ulong 4 plan view ulong * to int
612cnt = get meta 0
613for i ulong cnt bound 256
614ioff = mul i 3
615s1 = get plan ioff
616l1 = get plan add ioff 1
617if not kmi_range_end_ok s1 l1
618ret KMI_PLAN_ERR_OVERFLOW
619end
620j = add i 1
621while lt j cnt bound 256
622joff = mul j 3
623s2 = get plan joff
624l2 = get plan add joff 1
625if not kmi_range_end_ok s2 l2
626ret KMI_PLAN_ERR_OVERFLOW
627end
628if kmi_range_overlap s1 l1 s2 l2
629ret KMI_PLAN_ERR_OVERLAP
630end
631j = add j 1
632end
633end
634ret KMI_PLAN_OK
635end
636
637note ========================================================================
638note 3. SMMUv3 Descriptors, Queues, and Register Sequencing (smmu.rs)
639note ========================================================================
640
641const KMI_STE_BYTES ulong 64
642const KMI_CD_BYTES ulong 64
643const KMI_CMD_BYTES ulong 16
644const KMI_EVENT_BYTES ulong 32
645
646const KMI_SMMU_IDR0 uint 0x00
647const KMI_SMMU_IDR1 uint 0x04
648const KMI_SMMU_CR0 uint 0x20
649const KMI_SMMU_CR0ACK uint 0x24
650const KMI_SMMU_CR1 uint 0x28
651const KMI_SMMU_GBPA uint 0x44
652const KMI_SMMU_STRTAB_BASE uint 0x80
653const KMI_SMMU_STRTAB_BASE_CFG uint 0x88
654const KMI_SMMU_CMDQ_BASE uint 0x90
655const KMI_SMMU_CMDQ_PROD uint 0x98
656const KMI_SMMU_CMDQ_CONS uint 0x9c
657const KMI_SMMU_EVENTQ_BASE uint 0xa0
658const KMI_SMMU_EVENTQ_PROD uint 0x100a8
659const KMI_SMMU_EVENTQ_CONS uint 0x100ac
660
661const KMI_IDR0_S1P uint 2
662const KMI_IDR1_SIDSIZE_MASK uint 0x3f
663const KMI_IDR1_EVENTQS_SHIFT uint 16
664const KMI_IDR1_CMDQS_SHIFT uint 21
665const KMI_IDR1_QS_MASK uint 0x1f
666
667const KMI_GBPA_ABORT uint 0x00100000
668const KMI_GBPA_UPDATE uint 0x80000000
669
670const KMI_CR0_SMMUEN uint 1
671const KMI_CR0_EVENTQEN uint 4
672const KMI_CR0_CMDQEN uint 8
673const KMI_CR0_ACK_MASK uint 0x0f
674
675const KMI_CMDQ_CONS_ERR_MASK uint 0x7f000000
676const KMI_EVENTQ_OVERFLOW uint 0x80000000
677
678const KMI_CMD_CFGI_STE uchar 0x03
679const KMI_CMD_CFGI_ALL uchar 0x04
680const KMI_CMD_TLBI_NSNH_ALL uchar 0x30
681const KMI_CMD_SYNC uchar 0x46
682
683const KMI_EVENT_F_UUT uchar 0x01
684const KMI_EVENT_C_BAD_STREAMID uchar 0x02
685const KMI_EVENT_F_STE_FETCH uchar 0x03
686const KMI_EVENT_C_BAD_STE uchar 0x04
687const KMI_EVENT_F_STREAM_DISABLED uchar 0x06
688const KMI_EVENT_F_TRANSL_FORBIDDEN uchar 0x07
689const KMI_EVENT_F_CD_FETCH uchar 0x09
690const KMI_EVENT_C_BAD_CD uchar 0x0a
691const KMI_EVENT_F_WALK_EABT uchar 0x0b
692const KMI_EVENT_F_TRANSLATION uchar 0x10
693const KMI_EVENT_F_ADDR_SIZE uchar 0x11
694const KMI_EVENT_F_ACCESS uchar 0x12
695const KMI_EVENT_F_PERMISSION uchar 0x13
696
697const KMI_SMMU_OK int 0
698const KMI_SMMU_ERR_TIMEOUT int 1
699const KMI_SMMU_ERR_CMDQ int 2
700const KMI_SMMU_ERR_INVALID_WINDOW int 3
701const KMI_SMMU_ERR_UNSUPPORTED int 4
702const KMI_SMMU_ERR_ABORT_NOT_LATCHED int 5
703
704fn kmi_u32_ctz v uint to uint
705if eq v 0
706ret 32
707end
708c = uint 0
709while lt c 32 bound 32
710if ne and shr v c 1 0
711ret c
712end
713c = add c 1
714end
715ret 32
716end
717
718fn kmi_u32_is_pow2 v uint to bool
719if eq v 0
720ret false
721end
722ret eq and v sub v 1 0
723end
724
725fn kmi_ste_abort ste edit ulong 8 to void
726put ste 0 ulong 1
727for i ulong 7
728put ste add i 1 ulong 0
729end
730end
731
732fn kmi_ste_stage1 cd_addr ulong ste edit ulong 8 to void
733w0 = or ulong 1 shl ulong 5 1
734w0 = or w0 and cd_addr 0x000fffffffffffc0
735put ste 0 w0
736for i ulong 7
737put ste add i 1 ulong 0
738end
739end
740
741fn kmi_cd_stage1 ttb0 ulong asid ushort mair ulong t0sz uchar cd edit ulong 8 to int
742if gt t0sz 39
743ret KMI_SMMU_ERR_INVALID_WINDOW
744end
745if ne and ttb0 0xfff 0
746ret KMI_SMMU_ERR_INVALID_WINDOW
747end
748w0 = ulong t0sz
749w0 = or w0 shl ulong 1 8
750w0 = or w0 shl ulong 1 10
751w0 = or w0 shl ulong 3 12
752w0 = or w0 shl ulong 1 30
753w0 = or w0 shl ulong 1 31
754w0 = or w0 shl ulong 5 32
755w0 = or w0 shl ulong 1 41
756w0 = or w0 shl ulong 1 45
757w0 = or w0 shl ulong 1 46
758w0 = or w0 shl ulong asid 48
759w1 = and ttb0 0x0000fffffffffff0
760w3 = and mair 0xffff
761put cd 0 w0
762put cd 1 w1
763put cd 2 ulong 0
764put cd 3 w3
765put cd 4 ulong 0
766put cd 5 ulong 0
767put cd 6 ulong 0
768put cd 7 ulong 0
769ret KMI_SMMU_OK
770end
771
772fn kmi_smmu_cmd_w0 opcode uchar sid uint to ulong
773ret or ulong opcode shl ulong sid 32
774end
775
776fn kmi_smmu_cmd_w1 opcode uchar leaf bool to ulong
777if eq opcode KMI_CMD_CFGI_ALL
778ret 31
779end
780if leaf
781ret 1
782end
783ret 0
784end
785
786fn kmi_qcursor_encode idx uint wrap bool entries uint to uint
787low = and idx sub entries 1
788if wrap
789sh = kmi_u32_ctz entries
790ret or low shl uint 1 sh
791end
792ret low
793end
794
795fn kmi_qcursor_decode_idx val uint entries uint to uint
796ret and val sub entries 1
797end
798
799fn kmi_qcursor_decode_wrap val uint entries uint to bool
800ret ne and val entries 0
801end
802
803fn kmi_qcursor_advance cur edit uint 2 entries uint to void
804idx = add get cur 0 1
805if eq idx entries
806put cur 0 uint 0
807if eq get cur 1 0
808put cur 1 uint 1
809else
810put cur 1 uint 0
811end
812else
813put cur 0 idx
814end
815end
816
817fn kmi_smmu_aligned addr ulong nbytes ulong to bool
818if eq addr 0
819ret false
820end
821al = nbytes
822if lt al 32
823al = 32
824end
825ret eq and addr sub al 1 0
826end
827
828note geom[7]: [stream_addr, stream_entries, cmd_addr, cmd_entries, ev_addr, ev_entries, cd_addr]
829
830fn kmi_smmu_tables_valid geom view ulong 7 to bool
831se = uint and get geom 1 0xffffffff
832ce = uint and get geom 3 0xffffffff
833ee = uint and get geom 5 0xffffffff
834if lt se 1
835ret false
836end
837if not kmi_u32_is_pow2 se
838ret false
839end
840if lt ce 2
841ret false
842end
843if not kmi_u32_is_pow2 ce
844ret false
845end
846if lt ee 2
847ret false
848end
849if not kmi_u32_is_pow2 ee
850ret false
851end
852if not kmi_smmu_aligned get geom 0 mul ulong se KMI_STE_BYTES
853ret false
854end
855if not kmi_smmu_aligned get geom 2 mul ulong ce KMI_CMD_BYTES
856ret false
857end
858if not kmi_smmu_aligned get geom 4 mul ulong ee KMI_EVENT_BYTES
859ret false
860end
861if not kmi_smmu_aligned get geom 6 KMI_CD_BYTES
862ret false
863end
864ret true
865end
866
867fn kmi_smmu_event_decode raw view ulong 4 decoded edit ulong 3 to void
868w0 = get raw 0
869put decoded 0 and w0 0xff
870put decoded 1 and shr w0 32 0xffffffff
871put decoded 2 get raw 2
872end
873
874note Simulated SMMUv3 register + table state in sm edit ulong 512:
875note 0: IDR0 1: IDR1 2: GBPA 3: CR0 4: CR0ACK 5: CR1
876note 6: STRTAB_BASE 7: STRTAB_BASE_CFG 8: CMDQ_BASE 9: CMDQ_PROD
877note 10: CMDQ_CONS 11: EVENTQ_BASE 12: EVENTQ_PROD 13: EVENTQ_CONS
878note 14: gbpa_stuck 15: gbpa_no_abort 16: ack_never 17: cmdq_stuck
879note 18: cmdq_error 19: stream_entries 20: cmd_entries 21: ev_entries
880note 22: stream_addr 23: cmd_addr 24: ev_addr 25: cd_addr
881note 26: seq_counter 27: first_gbpa_seq 28: first_cr0_seq 29: first_strtab_seq
882note 30: clean_stream_seq 31: clean_cd_seq 32: clean_cmd_seq 33: inv_ev_seq
883note 34: write_count 35: all_writes_abort 36: smmuen_seen 37: last_write_reg
884note 38: last_write_val 39: consumed_count 40: last_clean_addr 41: last_clean_seq
885note 42: last_prod_seq 43: last_inv_addr 44: first_write_reg 45: non_gbpa_cr0_writes
886note 46..47: reserved
887note 48..175: stream table (16 STEs * 8 ulongs = 128)
888note 176..183: context descriptor (8 ulongs)
889note 184..215: command queue (16 entries * 2 ulongs = 32)
890note 216..279: event queue (16 entries * 4 ulongs = 64)
891note 280..407: consumed command log (64 commands * 2 ulongs = 128)
892
893const KMI_SM_IDR0 ulong 0
894const KMI_SM_IDR1 ulong 1
895const KMI_SM_GBPA ulong 2
896const KMI_SM_CR0 ulong 3
897const KMI_SM_CR0ACK ulong 4
898const KMI_SM_CR1 ulong 5
899const KMI_SM_STRTAB_BASE ulong 6
900const KMI_SM_STRTAB_CFG ulong 7
901const KMI_SM_CMDQ_BASE ulong 8
902const KMI_SM_CMDQ_PROD ulong 9
903const KMI_SM_CMDQ_CONS ulong 10
904const KMI_SM_EVENTQ_BASE ulong 11
905const KMI_SM_EVENTQ_PROD ulong 12
906const KMI_SM_EVENTQ_CONS ulong 13
907const KMI_SM_GBPA_STUCK ulong 14
908const KMI_SM_GBPA_NO_ABORT ulong 15
909const KMI_SM_ACK_NEVER ulong 16
910const KMI_SM_CMDQ_STUCK ulong 17
911const KMI_SM_CMDQ_ERROR ulong 18
912const KMI_SM_STREAM_ENT ulong 19
913const KMI_SM_CMD_ENT ulong 20
914const KMI_SM_EV_ENT ulong 21
915const KMI_SM_STREAM_ADDR ulong 22
916const KMI_SM_CMD_ADDR ulong 23
917const KMI_SM_EV_ADDR ulong 24
918const KMI_SM_CD_ADDR ulong 25
919const KMI_SM_SEQ ulong 26
920const KMI_SM_FIRST_GBPA ulong 27
921const KMI_SM_FIRST_CR0 ulong 28
922const KMI_SM_FIRST_STRTAB ulong 29
923const KMI_SM_CLEAN_STREAM ulong 30
924const KMI_SM_CLEAN_CD ulong 31
925const KMI_SM_CLEAN_CMD ulong 32
926const KMI_SM_INV_EV ulong 33
927const KMI_SM_WR_CNT ulong 34
928const KMI_SM_ALL_ABORT ulong 35
929const KMI_SM_SMMUEN_SEEN ulong 36
930const KMI_SM_LAST_REG ulong 37
931const KMI_SM_LAST_VAL ulong 38
932const KMI_SM_CONS_CNT ulong 39
933const KMI_SM_LAST_CLEAN_ADDR ulong 40
934const KMI_SM_LAST_CLEAN_SEQ ulong 41
935const KMI_SM_LAST_PROD_SEQ ulong 42
936const KMI_SM_LAST_INV_ADDR ulong 43
937const KMI_SM_FIRST_WR_REG ulong 44
938const KMI_SM_OTHER_WR ulong 45
939const KMI_SM_STE_BASE ulong 48
940const KMI_SM_CD_BASE ulong 176
941const KMI_SM_CMD_BASE ulong 184
942const KMI_SM_EV_BASE ulong 216
943const KMI_SM_CONS_LOG ulong 280
944
945fn kmi_smmu_init sm edit ulong 512 to void
946for i ulong 512
947put sm i ulong 0
948end
949put sm KMI_SM_IDR0 ulong KMI_IDR0_S1P
950idr1 = or ulong 16 or shl ulong 19 16 shl ulong 19 21
951put sm KMI_SM_IDR1 idr1
952put sm KMI_SM_GBPA ulong 0x1000
953put sm KMI_SM_STREAM_ENT ulong 16
954put sm KMI_SM_CMD_ENT ulong 16
955put sm KMI_SM_EV_ENT ulong 16
956put sm KMI_SM_STREAM_ADDR ulong 0x100000
957put sm KMI_SM_CMD_ADDR ulong 0x101000
958put sm KMI_SM_EV_ADDR ulong 0x102000
959put sm KMI_SM_CD_ADDR ulong 0x103000
960put sm KMI_SM_ALL_ABORT ulong 1
961for i ulong 128
962put sm add KMI_SM_STE_BASE i ulong 0xdead
963end
964end
965
966fn kmi_smmu_reset_log sm edit ulong 512 to void
967put sm KMI_SM_SEQ ulong 0
968put sm KMI_SM_FIRST_GBPA ulong 0
969put sm KMI_SM_FIRST_CR0 ulong 0
970put sm KMI_SM_FIRST_STRTAB ulong 0
971put sm KMI_SM_CLEAN_STREAM ulong 0
972put sm KMI_SM_CLEAN_CD ulong 0
973put sm KMI_SM_CLEAN_CMD ulong 0
974put sm KMI_SM_INV_EV ulong 0
975put sm KMI_SM_WR_CNT ulong 0
976put sm KMI_SM_ALL_ABORT ulong 1
977put sm KMI_SM_SMMUEN_SEEN ulong 0
978put sm KMI_SM_LAST_REG ulong 0
979put sm KMI_SM_LAST_VAL ulong 0
980put sm KMI_SM_CONS_CNT ulong 0
981put sm KMI_SM_LAST_CLEAN_ADDR ulong 0
982put sm KMI_SM_LAST_CLEAN_SEQ ulong 0
983put sm KMI_SM_LAST_PROD_SEQ ulong 0
984put sm KMI_SM_LAST_INV_ADDR ulong 0
985put sm KMI_SM_FIRST_WR_REG ulong 0
986put sm KMI_SM_OTHER_WR ulong 0
987end
988
989fn kmi_smmu_abort_in_effect sm view ulong 512 to bool
990g = uint and get sm KMI_SM_GBPA 0xffffffff
991if ne and g KMI_GBPA_UPDATE 0
992ret false
993end
994ret ne and g KMI_GBPA_ABORT 0
995end
996
997fn kmi_qcursor_from_raw raw uint entries uint cur edit uint 2 to void
998put cur 0 kmi_qcursor_decode_idx raw entries
999if kmi_qcursor_decode_wrap raw entries
1000put cur 1 uint 1
1001else
1002put cur 1 uint 0
1003end
1004end
1005
1006fn kmi_qcursor_eq_raw cur view uint 2 raw uint entries uint to bool
1007if ne get cur 0 kmi_qcursor_decode_idx raw entries
1008ret false
1009end
1010cw = ne get cur 1 0
1011rw = kmi_qcursor_decode_wrap raw entries
1012ret eq cw rw
1013end
1014
1015fn kmi_smmu_consume_one sm edit ulong 512 sidx ulong to void
1016qoff = add KMI_SM_CMD_BASE mul sidx 2
1017w0 = get sm qoff
1018w1 = get sm add qoff 1
1019ccnt = get sm KMI_SM_CONS_CNT
1020if lt ccnt 64
1021loff = add KMI_SM_CONS_LOG mul ccnt 2
1022put sm loff w0
1023put sm add loff 1 w1
1024end
1025put sm KMI_SM_CONS_CNT add ccnt 1
1026end
1027
1028fn kmi_smmu_consume_cmds sm edit ulong 512 prod_val uint to void
1029cur = new uint 2 0
1030entries = uint and get sm KMI_SM_CMD_ENT 0xffffffff
1031kmi_qcursor_from_raw uint and get sm KMI_SM_CMDQ_CONS 0xffffffff entries cur
1032while not kmi_qcursor_eq_raw cur prod_val entries bound 64
1033kmi_smmu_consume_one sm ulong get cur 0
1034kmi_qcursor_advance cur entries
1035end
1036enc = kmi_qcursor_encode get cur 0 ne get cur 1 0 entries
1037put sm KMI_SM_CMDQ_CONS ulong enc
1038end
1039
1040fn kmi_smmu_reg_write sm edit ulong 512 reg uint val uint to void
1041seq = add get sm KMI_SM_SEQ 1
1042put sm KMI_SM_SEQ seq
1043stored = val
1044if eq reg KMI_SMMU_GBPA
1045if ne get sm KMI_SM_GBPA_NO_ABORT 0
1046stored = and stored xor KMI_GBPA_ABORT 0xffffffff
1047end
1048if eq get sm KMI_SM_GBPA_STUCK 0
1049stored = and stored xor KMI_GBPA_UPDATE 0xffffffff
1050end
1051put sm KMI_SM_GBPA ulong stored
1052if eq get sm KMI_SM_FIRST_GBPA 0
1053put sm KMI_SM_FIRST_GBPA seq
1054end
1055else
1056if eq reg KMI_SMMU_CR0
1057put sm KMI_SM_CR0 ulong val
1058ack_n = uint and get sm KMI_SM_ACK_NEVER 0xffffffff
1059put sm KMI_SM_CR0ACK ulong and val xor ack_n 0xffffffff
1060if eq get sm KMI_SM_FIRST_CR0 0
1061put sm KMI_SM_FIRST_CR0 seq
1062end
1063if ne and val KMI_CR0_SMMUEN 0
1064put sm KMI_SM_SMMUEN_SEEN ulong 1
1065end
1066else
1067if eq reg KMI_SMMU_CR1
1068put sm KMI_SM_CR1 ulong val
1069else
1070if eq reg KMI_SMMU_STRTAB_BASE_CFG
1071put sm KMI_SM_STRTAB_CFG ulong val
1072else
1073if eq reg KMI_SMMU_CMDQ_CONS
1074put sm KMI_SM_CMDQ_CONS ulong val
1075else
1076if eq reg KMI_SMMU_CMDQ_PROD
1077put sm KMI_SM_CMDQ_PROD ulong val
1078put sm KMI_SM_LAST_PROD_SEQ seq
1079cr0ack = uint and get sm KMI_SM_CR0ACK 0xffffffff
1080if ne and cr0ack KMI_CR0_CMDQEN 0
1081if ne get sm KMI_SM_CMDQ_ERROR 0
1082put sm KMI_SM_CMDQ_CONS ulong shl uint 1 24
1083else
1084if eq get sm KMI_SM_CMDQ_STUCK 0
1085kmi_smmu_consume_cmds sm val
1086end
1087end
1088end
1089else
1090if eq reg KMI_SMMU_EVENTQ_CONS
1091put sm KMI_SM_EVENTQ_CONS ulong val
1092else
1093if eq reg KMI_SMMU_EVENTQ_PROD
1094put sm KMI_SM_EVENTQ_PROD ulong val
1095end
1096end
1097end
1098end
1099end
1100end
1101end
1102end
1103if eq get sm KMI_SM_WR_CNT 0
1104put sm KMI_SM_FIRST_WR_REG ulong reg
1105end
1106if ne reg KMI_SMMU_GBPA
1107if ne reg KMI_SMMU_CR0
1108put sm KMI_SM_OTHER_WR add get sm KMI_SM_OTHER_WR 1
1109end
1110end
1111put sm KMI_SM_WR_CNT add get sm KMI_SM_WR_CNT 1
1112put sm KMI_SM_LAST_REG ulong reg
1113put sm KMI_SM_LAST_VAL ulong val
1114if not kmi_smmu_abort_in_effect sm
1115put sm KMI_SM_ALL_ABORT ulong 0
1116end
1117end
1118
1119fn kmi_smmu_reg_write64 sm edit ulong 512 reg uint val ulong to void
1120seq = add get sm KMI_SM_SEQ 1
1121put sm KMI_SM_SEQ seq
1122if eq reg KMI_SMMU_STRTAB_BASE
1123put sm KMI_SM_STRTAB_BASE val
1124if eq get sm KMI_SM_FIRST_STRTAB 0
1125put sm KMI_SM_FIRST_STRTAB seq
1126end
1127else
1128if eq reg KMI_SMMU_CMDQ_BASE
1129put sm KMI_SM_CMDQ_BASE val
1130else
1131if eq reg KMI_SMMU_EVENTQ_BASE
1132put sm KMI_SM_EVENTQ_BASE val
1133end
1134end
1135end
1136put sm KMI_SM_WR_CNT add get sm KMI_SM_WR_CNT 1
1137put sm KMI_SM_OTHER_WR add get sm KMI_SM_OTHER_WR 1
1138put sm KMI_SM_LAST_REG ulong reg
1139put sm KMI_SM_LAST_VAL and val 0xffffffff
1140if not kmi_smmu_abort_in_effect sm
1141put sm KMI_SM_ALL_ABORT ulong 0
1142end
1143end
1144
1145fn kmi_smmu_clean sm edit ulong 512 addr ulong nbytes ulong to void
1146seq = add get sm KMI_SM_SEQ 1
1147put sm KMI_SM_SEQ seq
1148put sm KMI_SM_LAST_CLEAN_ADDR addr
1149put sm KMI_SM_LAST_CLEAN_SEQ seq
1150if eq addr get sm KMI_SM_STREAM_ADDR
1151if eq nbytes mul get sm KMI_SM_STREAM_ENT KMI_STE_BYTES
1152put sm KMI_SM_CLEAN_STREAM seq
1153end
1154end
1155if eq addr get sm KMI_SM_CD_ADDR
1156if eq nbytes KMI_CD_BYTES
1157put sm KMI_SM_CLEAN_CD seq
1158end
1159end
1160if eq addr get sm KMI_SM_CMD_ADDR
1161if eq nbytes mul get sm KMI_SM_CMD_ENT KMI_CMD_BYTES
1162put sm KMI_SM_CLEAN_CMD seq
1163end
1164end
1165end
1166
1167fn kmi_smmu_inv sm edit ulong 512 addr ulong nbytes ulong to void
1168seq = add get sm KMI_SM_SEQ 1
1169put sm KMI_SM_SEQ seq
1170put sm KMI_SM_LAST_INV_ADDR addr
1171if eq addr get sm KMI_SM_EV_ADDR
1172if eq nbytes mul get sm KMI_SM_EV_ENT KMI_EVENT_BYTES
1173put sm KMI_SM_INV_EV seq
1174end
1175end
1176end
1177
1178fn kmi_smmu_poll_gbpa sm view ulong 512 spins ulong to int
1179for i ulong spins bound 1024
1180g = uint and get sm KMI_SM_GBPA 0xffffffff
1181if eq and g KMI_GBPA_UPDATE 0
1182ret KMI_SMMU_OK
1183end
1184end
1185ret KMI_SMMU_ERR_TIMEOUT
1186end
1187
1188fn kmi_smmu_set_global_abort sm edit ulong 512 spins ulong to int
1189rc = kmi_smmu_poll_gbpa sm spins
1190if ne rc KMI_SMMU_OK
1191ret rc
1192end
1193cur = uint and get sm KMI_SM_GBPA 0xffffffff
1194nv = or and cur xor KMI_GBPA_UPDATE 0xffffffff or KMI_GBPA_ABORT KMI_GBPA_UPDATE
1195kmi_smmu_reg_write sm KMI_SMMU_GBPA nv
1196rc2 = kmi_smmu_poll_gbpa sm spins
1197if ne rc2 KMI_SMMU_OK
1198ret rc2
1199end
1200after = uint and get sm KMI_SM_GBPA 0xffffffff
1201if eq and after KMI_GBPA_ABORT 0
1202ret KMI_SMMU_ERR_ABORT_NOT_LATCHED
1203end
1204ret KMI_SMMU_OK
1205end
1206
1207fn kmi_smmu_wait_cr0 sm view ulong 512 expected uint spins ulong to int
1208exp_m = and expected KMI_CR0_ACK_MASK
1209for i ulong spins bound 1024
1210ack = uint and get sm KMI_SM_CR0ACK 0xffffffff
1211if eq and ack KMI_CR0_ACK_MASK exp_m
1212ret KMI_SMMU_OK
1213end
1214end
1215ret KMI_SMMU_ERR_TIMEOUT
1216end
1217
1218note cmds: 2 ulongs per command [w0, w1]
1219
1220fn kmi_smmu_wait_space sm view ulong 512 pcur view uint 2 entries uint spins ulong to int
1221for s ulong spins bound 1024
1222cons_raw = uint and get sm KMI_SM_CMDQ_CONS 0xffffffff
1223if ne and cons_raw KMI_CMDQ_CONS_ERR_MASK 0
1224ret KMI_SMMU_ERR_CMDQ
1225end
1226c_idx = kmi_qcursor_decode_idx cons_raw entries
1227c_wrap = kmi_qcursor_decode_wrap cons_raw entries
1228p_wrap = ne get pcur 1 0
1229if ne c_idx get pcur 0
1230ret KMI_SMMU_OK
1231end
1232if eq c_wrap p_wrap
1233ret KMI_SMMU_OK
1234end
1235end
1236ret KMI_SMMU_ERR_TIMEOUT
1237end
1238
1239fn kmi_smmu_wait_drain sm view ulong 512 pcur view uint 2 entries uint spins ulong to int
1240for s ulong spins bound 1024
1241cons_raw = uint and get sm KMI_SM_CMDQ_CONS 0xffffffff
1242if ne and cons_raw KMI_CMDQ_CONS_ERR_MASK 0
1243ret KMI_SMMU_ERR_CMDQ
1244end
1245if kmi_qcursor_eq_raw pcur cons_raw entries
1246ret KMI_SMMU_OK
1247end
1248end
1249ret KMI_SMMU_ERR_TIMEOUT
1250end
1251
1252fn kmi_smmu_push_cmd sm edit ulong 512 pcur edit uint 2 cmds view ulong * ci ulong to void
1253entries = uint and get sm KMI_SM_CMD_ENT 0xffffffff
1254sidx = ulong get pcur 0
1255qoff = add KMI_SM_CMD_BASE mul sidx 2
1256coff = mul ci 2
1257put sm qoff get cmds coff
1258put sm add qoff 1 get cmds add coff 1
1259kmi_smmu_clean sm add get sm KMI_SM_CMD_ADDR mul sidx KMI_CMD_BYTES KMI_CMD_BYTES
1260kmi_qcursor_advance pcur entries
1261enc = kmi_qcursor_encode get pcur 0 ne get pcur 1 0 entries
1262kmi_smmu_reg_write sm KMI_SMMU_CMDQ_PROD enc
1263end
1264
1265fn kmi_smmu_submit_cmds sm edit ulong 512 cmds view ulong * ncmds ulong spins ulong to int
1266pcur = new uint 2 0
1267entries = uint and get sm KMI_SM_CMD_ENT 0xffffffff
1268kmi_qcursor_from_raw uint and get sm KMI_SM_CMDQ_PROD 0xffffffff entries pcur
1269for ci ulong ncmds bound 64
1270rc = kmi_smmu_wait_space sm pcur entries spins
1271if ne rc KMI_SMMU_OK
1272ret rc
1273end
1274kmi_smmu_push_cmd sm pcur cmds ci
1275end
1276ret kmi_smmu_wait_drain sm pcur entries spins
1277end
1278
1279fn kmi_smmu_inv_ste sm edit ulong 512 sid uint spins ulong to int
1280cmds = new ulong 6 0
1281ste_addr = add get sm KMI_SM_STREAM_ADDR mul ulong sid KMI_STE_BYTES
1282kmi_smmu_clean sm ste_addr KMI_STE_BYTES
1283put cmds 0 kmi_smmu_cmd_w0 KMI_CMD_CFGI_STE sid
1284put cmds 1 kmi_smmu_cmd_w1 KMI_CMD_CFGI_STE true
1285put cmds 2 kmi_smmu_cmd_w0 KMI_CMD_TLBI_NSNH_ALL uint 0
1286put cmds 3 kmi_smmu_cmd_w1 KMI_CMD_TLBI_NSNH_ALL false
1287put cmds 4 kmi_smmu_cmd_w0 KMI_CMD_SYNC uint 0
1288put cmds 5 kmi_smmu_cmd_w1 KMI_CMD_SYNC false
1289ret kmi_smmu_submit_cmds sm cmds 3 spins
1290end
1291
1292fn kmi_smmu_abort_ste sm edit ulong 512 sid uint spins ulong to int
1293if ge ulong sid get sm KMI_SM_STREAM_ENT
1294ret KMI_SMMU_ERR_INVALID_WINDOW
1295end
1296base = add KMI_SM_STE_BASE mul ulong sid 8
1297put sm base ulong 1
1298for w ulong 7
1299put sm add base add w 1 ulong 0
1300end
1301ret kmi_smmu_inv_ste sm sid spins
1302end
1303
1304fn kmi_smmu_install_ste sm edit ulong 512 sid uint ste view ulong 8 spins ulong to int
1305if ge ulong sid get sm KMI_SM_STREAM_ENT
1306ret KMI_SMMU_ERR_INVALID_WINDOW
1307end
1308rc = kmi_smmu_abort_ste sm sid spins
1309if ne rc KMI_SMMU_OK
1310ret rc
1311end
1312base = add KMI_SM_STE_BASE mul ulong sid 8
1313for w ulong 7
1314put sm add base add w 1 get ste add w 1
1315end
1316ste_addr = add get sm KMI_SM_STREAM_ADDR mul ulong sid KMI_STE_BYTES
1317kmi_smmu_clean sm ste_addr KMI_STE_BYTES
1318put sm base get ste 0
1319ret kmi_smmu_inv_ste sm sid spins
1320end
1321
1322note drain_out[2]: [count, overflowed(0/1)]
1323note ev_out: 3 ulongs per event [event_type, stream_id, input_address]
1324
1325fn kmi_smmu_read_one_ev sm edit ulong 512 sidx ulong ev_out edit ulong * cnt ulong to void
1326raw = new ulong 4 0
1327dec = new ulong 3 0
1328kmi_smmu_inv sm add get sm KMI_SM_EV_ADDR mul sidx KMI_EVENT_BYTES KMI_EVENT_BYTES
1329eoff = add KMI_SM_EV_BASE mul sidx 4
1330for w ulong 4
1331put raw w get sm add eoff w
1332end
1333kmi_smmu_event_decode raw dec
1334ooff = mul cnt 3
1335put ev_out ooff get dec 0
1336put ev_out add ooff 1 get dec 1
1337put ev_out add ooff 2 get dec 2
1338end
1339
1340fn kmi_smmu_read_events sm edit ulong 512 ev_out edit ulong * max_ev ulong drain_out edit ulong 2 to void
1341ccur = new uint 2 0
1342entries = uint and get sm KMI_SM_EV_ENT 0xffffffff
1343prod_raw = uint and get sm KMI_SM_EVENTQ_PROD 0xffffffff
1344cons_raw = uint and get sm KMI_SM_EVENTQ_CONS 0xffffffff
1345kmi_qcursor_from_raw cons_raw entries ccur
1346cnt = ulong 0
1347while lt cnt max_ev bound 64
1348if kmi_qcursor_eq_raw ccur prod_raw entries
1349break
1350end
1351kmi_smmu_read_one_ev sm ulong get ccur 0 ev_out cnt
1352cnt = add cnt 1
1353kmi_qcursor_advance ccur entries
1354end
1355ovf = and prod_raw KMI_EVENTQ_OVERFLOW
1356ncons = or kmi_qcursor_encode get ccur 0 ne get ccur 1 0 entries ovf
1357kmi_smmu_reg_write sm KMI_SMMU_EVENTQ_CONS ncons
1358put drain_out 0 cnt
1359if ne ovf and cons_raw KMI_EVENTQ_OVERFLOW
1360put drain_out 1 ulong 1
1361else
1362put drain_out 1 ulong 0
1363end
1364end
1365
1366fn kmi_smmu_check_idr sm view ulong 512 stream_id uint to int
1367se = uint and get sm KMI_SM_STREAM_ENT 0xffffffff
1368ce = uint and get sm KMI_SM_CMD_ENT 0xffffffff
1369ee = uint and get sm KMI_SM_EV_ENT 0xffffffff
1370if ge stream_id se
1371ret KMI_SMMU_ERR_INVALID_WINDOW
1372end
1373idr0 = uint and get sm KMI_SM_IDR0 0xffffffff
1374if eq and idr0 KMI_IDR0_S1P 0
1375ret KMI_SMMU_ERR_UNSUPPORTED
1376end
1377idr1 = uint and get sm KMI_SM_IDR1 0xffffffff
1378if lt and idr1 KMI_IDR1_SIDSIZE_MASK kmi_u32_ctz se
1379ret KMI_SMMU_ERR_UNSUPPORTED
1380end
1381if lt and shr idr1 KMI_IDR1_CMDQS_SHIFT KMI_IDR1_QS_MASK kmi_u32_ctz ce
1382ret KMI_SMMU_ERR_UNSUPPORTED
1383end
1384if lt and shr idr1 KMI_IDR1_EVENTQS_SHIFT KMI_IDR1_QS_MASK kmi_u32_ctz ee
1385ret KMI_SMMU_ERR_UNSUPPORTED
1386end
1387ret KMI_SMMU_OK
1388end
1389
1390fn kmi_smmu_write_tables sm edit ulong 512 stream_id uint cd view ulong 8 to void
1391ste = new ulong 8 0
1392se = get sm KMI_SM_STREAM_ENT
1393ce = get sm KMI_SM_CMD_ENT
1394ee = get sm KMI_SM_EV_ENT
1395for s ulong se bound 64
1396sb = add KMI_SM_STE_BASE mul s 8
1397put sm sb ulong 1
1398for w ulong 7
1399put sm add sb add w 1 ulong 0
1400end
1401end
1402for w ulong 8
1403put sm add KMI_SM_CD_BASE w get cd w
1404end
1405for i ulong mul ce 2 bound 128
1406put sm add KMI_SM_CMD_BASE i ulong 0
1407end
1408for i ulong mul ee 4 bound 256
1409put sm add KMI_SM_EV_BASE i ulong 0
1410end
1411kmi_ste_stage1 get sm KMI_SM_CD_ADDR ste
1412sb_live = add KMI_SM_STE_BASE mul ulong stream_id 8
1413for w ulong 7
1414put sm add sb_live add w 1 get ste add w 1
1415end
1416put sm sb_live get ste 0
1417kmi_smmu_clean sm get sm KMI_SM_STREAM_ADDR mul se KMI_STE_BYTES
1418kmi_smmu_clean sm get sm KMI_SM_CD_ADDR KMI_CD_BYTES
1419kmi_smmu_clean sm get sm KMI_SM_CMD_ADDR mul ce KMI_CMD_BYTES
1420kmi_smmu_inv sm get sm KMI_SM_EV_ADDR mul ee KMI_EVENT_BYTES
1421end
1422
1423fn kmi_smmu_program_stream sm edit ulong 512 stream_id uint cd view ulong 8 spins ulong to int
1424cmds = new ulong 6 0
1425rc = kmi_smmu_check_idr sm stream_id
1426if ne rc KMI_SMMU_OK
1427ret rc
1428end
1429kmi_smmu_write_tables sm stream_id cd
1430se = uint and get sm KMI_SM_STREAM_ENT 0xffffffff
1431ce = uint and get sm KMI_SM_CMD_ENT 0xffffffff
1432ee = uint and get sm KMI_SM_EV_ENT 0xffffffff
1433kmi_smmu_reg_write sm KMI_SMMU_CR0 uint 0
1434rc = kmi_smmu_wait_cr0 sm uint 0 spins
1435if ne rc KMI_SMMU_OK
1436ret rc
1437end
1438kmi_smmu_reg_write sm KMI_SMMU_CR1 uint 0x0d75
1439kmi_smmu_reg_write64 sm KMI_SMMU_CMDQ_BASE or get sm KMI_SM_CMD_ADDR ulong kmi_u32_ctz ce
1440kmi_smmu_reg_write sm KMI_SMMU_CMDQ_CONS uint 0
1441kmi_smmu_reg_write sm KMI_SMMU_CMDQ_PROD uint 0
1442kmi_smmu_reg_write64 sm KMI_SMMU_EVENTQ_BASE or get sm KMI_SM_EV_ADDR ulong kmi_u32_ctz ee
1443kmi_smmu_reg_write sm KMI_SMMU_EVENTQ_CONS uint 0
1444kmi_smmu_reg_write sm KMI_SMMU_EVENTQ_PROD uint 0
1445kmi_smmu_reg_write sm KMI_SMMU_STRTAB_BASE_CFG kmi_u32_ctz se
1446kmi_smmu_reg_write64 sm KMI_SMMU_STRTAB_BASE get sm KMI_SM_STREAM_ADDR
1447qen = or KMI_CR0_CMDQEN KMI_CR0_EVENTQEN
1448kmi_smmu_reg_write sm KMI_SMMU_CR0 qen
1449rc = kmi_smmu_wait_cr0 sm qen spins
1450if ne rc KMI_SMMU_OK
1451ret rc
1452end
1453put cmds 0 kmi_smmu_cmd_w0 KMI_CMD_CFGI_ALL uint 0
1454put cmds 1 kmi_smmu_cmd_w1 KMI_CMD_CFGI_ALL false
1455put cmds 2 kmi_smmu_cmd_w0 KMI_CMD_TLBI_NSNH_ALL uint 0
1456put cmds 3 kmi_smmu_cmd_w1 KMI_CMD_TLBI_NSNH_ALL false
1457put cmds 4 kmi_smmu_cmd_w0 KMI_CMD_SYNC uint 0
1458put cmds 5 kmi_smmu_cmd_w1 KMI_CMD_SYNC false
1459rc = kmi_smmu_submit_cmds sm cmds 3 spins
1460if ne rc KMI_SMMU_OK
1461ret rc
1462end
1463en = or KMI_CR0_SMMUEN qen
1464kmi_smmu_reg_write sm KMI_SMMU_CR0 en
1465ret kmi_smmu_wait_cr0 sm en spins
1466end
1467
1468fn kmi_smmu_configure_linear sm edit ulong 512 stream_id uint cd view ulong 8 spins ulong to int
1469rc = kmi_smmu_set_global_abort sm spins
1470if ne rc KMI_SMMU_OK
1471ret rc
1472end
1473rc2 = kmi_smmu_program_stream sm stream_id cd spins
1474if ne rc2 KMI_SMMU_OK
1475kmi_smmu_reg_write sm KMI_SMMU_CR0 uint 0
1476ign = kmi_smmu_wait_cr0 sm uint 0 spins
1477ret rc2
1478end
1479ret KMI_SMMU_OK
1480end
1481
1482note Validate identity-policy DMA windows: windows is 3 ulongs per entry [iova, pa, length]
1483
1484fn kmi_smmu_validate_policy_windows windows view ulong * to int
1485n = div len windows 3
1486for i ulong n bound 64
1487off = mul i 3
1488iova = get windows off
1489pa = get windows add off 1
1490wlen = get windows add off 2
1491if ne iova pa
1492ret KMI_SMMU_ERR_INVALID_WINDOW
1493end
1494if eq wlen 0
1495ret KMI_SMMU_ERR_INVALID_WINDOW
1496end
1497if ne and iova 0xfff 0
1498ret KMI_SMMU_ERR_INVALID_WINDOW
1499end
1500if ne and wlen 0xfff 0
1501ret KMI_SMMU_ERR_INVALID_WINDOW
1502end
1503end
1504ret KMI_SMMU_OK
1505end
1506
1507note ========================================================================
1508note 4 & 5. DMA Gate (dma_gate.rs) and PCI Enumeration/BAR (pci.rs)
1509note ========================================================================
1510
1511const KMI_PCI_REG_ID ushort 0x00
1512const KMI_PCI_REG_COMMAND ushort 0x04
1513const KMI_PCI_REG_CLASS_REV ushort 0x08
1514const KMI_PCI_REG_HEADER ushort 0x0c
1515const KMI_PCI_REG_BAR0 ushort 0x10
1516
1517const KMI_PCI_CMD_IO ushort 1
1518const KMI_PCI_CMD_MEMORY ushort 2
1519const KMI_PCI_CMD_BUS_MASTER ushort 4
1520
1521const KMI_PCI_VENDOR_NONE ushort 0xffff
1522const KMI_PCI_HEADER_MF uchar 0x80
1523const KMI_PCI_TYPE0_BARS uchar 6
1524const KMI_PCI_TYPE1_BARS uchar 2
1525
1526const KMI_DMA_GRANT_CONFINED int 1
1527const KMI_DMA_GRANT_UNSAFE_BYPASS int 2
1528const KMI_DMA_DENIED_NO_SMMU int -1
1529const KMI_DMA_DENIED_SMMU_NOT_READY int -2
1530const KMI_SWEEP_FINDINGS ulong 8
1531
1532const KMI_BAR_MEM32 ulong 0
1533const KMI_BAR_MEM64 ulong 1
1534const KMI_BAR_IO ulong 2
1535
1536const KMI_BAR_OK int 0
1537const KMI_BAR_ERR_NO_SUCH int 1
1538const KMI_BAR_ERR_MISSING_HIGH int 2
1539const KMI_BAR_ERR_RESERVED int 3
1540const KMI_BAR_ERR_ABSENT int 4
1541
1542fn kmi_bme_enabled cmd ushort to bool
1543ret ne and cmd KMI_PCI_CMD_BUS_MASTER 0
1544end
1545
1546fn kmi_without_bme cmd ushort to ushort
1547ret and cmd xor KMI_PCI_CMD_BUS_MASTER 0xffff
1548end
1549
1550fn kmi_with_bme cmd ushort to ushort
1551ret or cmd or KMI_PCI_CMD_MEMORY KMI_PCI_CMD_BUS_MASTER
1552end
1553
1554fn kmi_dma_grant smmu_ready bool smmu_present bool unsafe_bypass bool to int
1555if smmu_ready
1556ret KMI_DMA_GRANT_CONFINED
1557end
1558if smmu_present
1559ret KMI_DMA_DENIED_SMMU_NOT_READY
1560end
1561if unsafe_bypass
1562ret KMI_DMA_GRANT_UNSAFE_BYPASS
1563end
1564ret KMI_DMA_DENIED_NO_SMMU
1565end
1566
1567fn kmi_bdf_valid dev uchar func uchar to bool
1568if gt dev 31
1569ret false
1570end
1571if gt func 7
1572ret false
1573end
1574ret true
1575end
1576
1577fn kmi_bdf_rid bus uchar dev uchar func uchar to uint
1578b = shl uint bus 8
1579d = shl uint dev 3
1580ret or b or d uint func
1581end
1582
1583fn kmi_pci_bar_count htype uchar to uchar
1584if eq htype 0
1585ret KMI_PCI_TYPE0_BARS
1586end
1587if eq htype 1
1588ret KMI_PCI_TYPE1_BARS
1589end
1590ret 0
1591end
1592
1593note res[4]: [kind, base, prefetchable(0/1), is_64bit(0/1)]
1594
1595fn kmi_pci_decode_bar low uint has_high bool high uint res edit ulong 4 to int
1596if ne and low 1 0
1597put res 0 KMI_BAR_IO
1598put res 1 ulong and low 0xfffffffc
1599put res 2 ulong 0
1600put res 3 ulong 0
1601ret KMI_BAR_OK
1602end
1603pref = ulong 0
1604if ne and low 8 0
1605pref = 1
1606end
1607btype = and low 6
1608if eq btype 0
1609put res 0 KMI_BAR_MEM32
1610put res 1 ulong and low 0xfffffff0
1611put res 2 pref
1612put res 3 ulong 0
1613ret KMI_BAR_OK
1614end
1615if eq btype 4
1616if not has_high
1617ret KMI_BAR_ERR_MISSING_HIGH
1618end
1619base64 = or shl ulong high 32 ulong and low 0xfffffff0
1620put res 0 KMI_BAR_MEM64
1621put res 1 base64
1622put res 2 pref
1623put res 3 ulong 1
1624ret KMI_BAR_OK
1625end
1626ret KMI_BAR_ERR_RESERVED
1627end
1628
1629note PCI FakeConfig table in cfg edit uint 516:
1630note cfg[0] = count (max 16)
1631note cfg[1] = reads
1632note cfg[2] = writes
1633note cfg[3] = reserved
1634note Each slot s in 0..15 starts at 4 + s * 32:
1635note +0: occupied (1)
1636note +1: rid (bus<<8 | dev<<3 | func)
1637note +2: stuck_cmd (1 if Command writes ignored)
1638note +3: bar_mask_valid (bit i set if bar_mask[i] active)
1639note +4..+9: bar_masks[0..5]
1640note +10..+25: regs[0..15] (dwords at offsets 0x00..0x3c)
1641
1642const KMI_PCI_CFG_STRIDE ulong 32
1643const KMI_PCI_CFG_MAX ulong 16
1644
1645fn kmi_pci_cfg_init cfg edit uint 516 to void
1646for i ulong 516
1647put cfg i uint 0
1648end
1649end
1650
1651fn kmi_pci_cfg_find cfg view uint 516 rid uint to long
1652cnt = ulong get cfg 0
1653for i ulong cnt bound 16
1654off = add 4 mul i KMI_PCI_CFG_STRIDE
1655if eq get cfg off 1
1656if eq get cfg add off 1 rid
1657ret long i
1658end
1659end
1660end
1661ret -1
1662end
1663
1664fn kmi_pci_cfg_add cfg edit uint 516 rid uint vid_did uint header uchar to long
1665idx = kmi_pci_cfg_find cfg rid
1666if lt idx 0
1667cnt = ulong get cfg 0
1668if ge cnt KMI_PCI_CFG_MAX
1669ret -1
1670end
1671idx = long cnt
1672put cfg 0 uint add cnt 1
1673end
1674off = add 4 mul ulong idx KMI_PCI_CFG_STRIDE
1675for k ulong 32
1676put cfg add off k uint 0
1677end
1678put cfg off uint 1
1679put cfg add off 1 rid
1680put cfg add off 10 vid_did
1681put cfg add off 13 shl uint header 16
1682ret idx
1683end
1684
1685fn kmi_pci_cfg_set_reg cfg edit uint 516 idx ulong dw ulong val uint to void
1686off = add 4 mul idx KMI_PCI_CFG_STRIDE
1687put cfg add off add 10 dw val
1688end
1689
1690fn kmi_pci_cfg_get_reg cfg view uint 516 idx ulong dw ulong to uint
1691off = add 4 mul idx KMI_PCI_CFG_STRIDE
1692ret get cfg add off add 10 dw
1693end
1694
1695fn kmi_pci_cfg_set_bar_mask cfg edit uint 516 idx ulong bar_i ulong mask uint to void
1696off = add 4 mul idx KMI_PCI_CFG_STRIDE
1697bmv = get cfg add off 3
1698put cfg add off 3 or bmv shl uint 1 uint and bar_i 31
1699put cfg add off add 4 bar_i mask
1700end
1701
1702fn kmi_pci_cfg_set_stuck cfg edit uint 516 idx ulong stuck bool to void
1703off = add 4 mul idx KMI_PCI_CFG_STRIDE
1704if stuck
1705put cfg add off 2 uint 1
1706else
1707put cfg add off 2 uint 0
1708end
1709end
1710
1711fn kmi_pci_cfg_read32 cfg edit uint 516 rid uint offset ushort to uint
1712put cfg 1 add get cfg 1 1
1713idx = kmi_pci_cfg_find cfg rid
1714if lt idx 0
1715ret 0xffffffff
1716end
1717dw = ulong div offset 4
1718if ge dw 16
1719ret 0
1720end
1721off = add 4 mul ulong idx KMI_PCI_CFG_STRIDE
1722ret get cfg add off add 10 dw
1723end
1724
1725fn kmi_pci_mask_bar_wr cfg view uint 516 off ulong offset ushort val uint cur uint to uint
1726if lt offset KMI_PCI_REG_BAR0
1727ret val
1728end
1729bi = ulong div sub offset KMI_PCI_REG_BAR0 4
1730if ge bi 6
1731ret val
1732end
1733bmv = get cfg add off 3
1734bit_m = shl uint 1 uint and bi 31
1735if eq and bmv bit_m 0
1736ret val
1737end
1738mask = get cfg add off add 4 bi
1739inv_m = xor mask 0xffffffff
1740ret or and val mask and cur inv_m
1741end
1742
1743fn kmi_pci_cfg_write32 cfg edit uint 516 rid uint offset ushort val uint to void
1744put cfg 2 add get cfg 2 1
1745idx = kmi_pci_cfg_find cfg rid
1746if lt idx 0
1747ret
1748end
1749dw = ulong div offset 4
1750if ge dw 16
1751ret
1752end
1753off = add 4 mul ulong idx KMI_PCI_CFG_STRIDE
1754cur = get cfg add off add 10 dw
1755nv = kmi_pci_mask_bar_wr cfg off offset val cur
1756if eq offset KMI_PCI_REG_COMMAND
1757if ne get cfg add off 2 0
1758ret
1759end
1760status = and shr cur 16 xor shr nv 16 0xffffffff
1761nv = or shl status 16 and nv 0xffff
1762end
1763put cfg add off add 10 dw nv
1764end
1765
1766fn kmi_pci_cfg_write16 cfg edit uint 516 rid uint offset ushort val ushort to void
1767put cfg 2 add get cfg 2 1
1768idx = kmi_pci_cfg_find cfg rid
1769if lt idx 0
1770ret
1771end
1772off = add 4 mul ulong idx KMI_PCI_CFG_STRIDE
1773if eq offset KMI_PCI_REG_COMMAND
1774if ne get cfg add off 2 0
1775ret
1776end
1777cur = get cfg add off 11
1778nv = or and cur 0xffff0000 uint val
1779put cfg add off 11 nv
1780end
1781end
1782
1783note fn_info[6]: [rid, vendor_id, device_id, header_type, multi_function(0/1), class_rev]
1784
1785fn kmi_pci_read_fn cfg edit uint 516 rid uint fn_out edit uint 6 to bool
1786id = kmi_pci_cfg_read32 cfg rid KMI_PCI_REG_ID
1787vid = and id 0xffff
1788if eq vid 0xffff
1789ret false
1790end
1791if eq vid 0
1792ret false
1793end
1794hdr = and shr kmi_pci_cfg_read32 cfg rid KMI_PCI_REG_HEADER 16 0xff
1795crev = kmi_pci_cfg_read32 cfg rid KMI_PCI_REG_CLASS_REV
1796put fn_out 0 rid
1797put fn_out 1 vid
1798put fn_out 2 shr id 16
1799put fn_out 3 and hdr 0x7f
1800if ne and hdr 0x80 0
1801put fn_out 4 uint 1
1802else
1803put fn_out 4 uint 0
1804end
1805put fn_out 5 crev
1806ret true
1807end
1808
1809note Walk buses start_bus..=end_bus; writes 6 uints per found function into found_out
1810
1811fn kmi_pci_append_fn found_out edit uint * fi view uint 6 cnt_io edit ulong 1 to void
1812max_f = div len found_out 6
1813cnt = get cnt_io 0
1814if lt cnt max_f
1815foff = mul cnt 6
1816for k ulong 6
1817put found_out add foff k get fi k
1818end
1819end
1820put cnt_io 0 add cnt 1
1821end
1822
1823fn kmi_pci_walk_dev cfg edit uint 516 bus uchar dev uchar found_out edit uint * cnt_io edit ulong 1 to void
1824fi = new uint 6 0
1825mf = false
1826for func ulong 8
1827rid = kmi_bdf_rid bus dev uchar and func 7
1828ok = kmi_pci_read_fn cfg rid fi
1829if eq func 0
1830if ok
1831mf = ne get fi 4 0
1832else
1833break
1834end
1835end
1836if ok
1837kmi_pci_append_fn found_out fi cnt_io
1838end
1839if eq func 0
1840if not mf
1841break
1842end
1843end
1844end
1845end
1846
1847fn kmi_pci_walk cfg edit uint 516 start_bus uchar end_bus uchar found_out edit uint * to uint
1848cnt_io = new ulong 1 0
1849if gt start_bus end_bus
1850ret 0
1851end
1852bus = ulong start_bus
1853end_b = ulong end_bus
1854while le bus end_b bound 256
1855for dev ulong 32
1856kmi_pci_walk_dev cfg uchar and bus 255 uchar and dev 31 found_out cnt_io
1857end
1858bus = add bus 1
1859end
1860ret uint and get cnt_io 0 0xffffffff
1861end
1862
1863fn kmi_pci_find_dev cfg edit uint 516 bus uchar dev uchar vid_did uint fn_out edit uint 6 to bool
1864fi = new uint 6 0
1865mf = false
1866for func ulong 8
1867rid = kmi_bdf_rid bus dev uchar and func 7
1868ok = kmi_pci_read_fn cfg rid fi
1869if eq func 0
1870if ok
1871mf = ne get fi 4 0
1872else
1873break
1874end
1875end
1876if ok
1877if eq get fi 1 and vid_did 0xffff
1878if eq get fi 2 shr vid_did 16
1879for k ulong 6
1880put fn_out k get fi k
1881end
1882ret true
1883end
1884end
1885end
1886if eq func 0
1887if not mf
1888break
1889end
1890end
1891end
1892ret false
1893end
1894
1895fn kmi_pci_find_device cfg edit uint 516 start_bus uchar end_bus uchar vid_did uint fn_out edit uint 6 to bool
1896if gt start_bus end_bus
1897ret false
1898end
1899bus = ulong start_bus
1900end_b = ulong end_bus
1901while le bus end_b bound 256
1902for dev ulong 32
1903if kmi_pci_find_dev cfg uchar and bus 255 uchar and dev 31 vid_did fn_out
1904ret true
1905end
1906end
1907bus = add bus 1
1908end
1909ret false
1910end
1911
1912fn kmi_pci_read_bar cfg edit uint 516 fn_info view uint 6 idx uchar res edit ulong 4 to int
1913htype = uchar and get fn_info 3 0xff
1914bc = kmi_pci_bar_count htype
1915if ge idx bc
1916ret KMI_BAR_ERR_NO_SUCH
1917end
1918rid = get fn_info 0
1919off = add KMI_PCI_REG_BAR0 mul ushort idx 4
1920low = kmi_pci_cfg_read32 cfg rid off
1921if eq low 0xffffffff
1922ret KMI_BAR_ERR_ABSENT
1923end
1924has_hi = lt add idx 1 bc
1925hi = uint 0
1926if has_hi
1927hi = kmi_pci_cfg_read32 cfg rid add off 4
1928end
1929ret kmi_pci_decode_bar low has_hi hi res
1930end
1931
1932fn kmi_pci_read_cmd cfg edit uint 516 rid uint to ushort
1933ret ushort and kmi_pci_cfg_read32 cfg rid KMI_PCI_REG_COMMAND 0xffff
1934end
1935
1936fn kmi_pci_update_cmd cfg edit uint 516 rid uint set_b ushort clr_b ushort to ushort
1937cur = kmi_pci_read_cmd cfg rid
1938nv = and or cur set_b xor clr_b 0xffff
1939kmi_pci_cfg_write32 cfg rid KMI_PCI_REG_COMMAND uint nv
1940ret nv
1941end
1942
1943fn kmi_u64_isolate_low1 v ulong to ulong
1944if eq v 0
1945ret 0
1946end
1947ret and v add xor v 0xffffffffffffffff 1
1948end
1949
1950note size_out[2]: [has_size(0/1), size]
1951
1952fn kmi_pci_probe_dword cfg edit uint 516 rid uint off ushort mask_bits uint to uint
1953orig = kmi_pci_cfg_read32 cfg rid off
1954kmi_pci_cfg_write32 cfg rid off 0xffffffff
1955probed = and kmi_pci_cfg_read32 cfg rid off mask_bits
1956kmi_pci_cfg_write32 cfg rid off orig
1957ret probed
1958end
1959
1960fn kmi_pci_probe_bar_size cfg edit uint 516 fn_info view uint 6 idx uchar size_out edit ulong 2 to int
1961bar = new ulong 4 0
1962rc = kmi_pci_read_bar cfg fn_info idx bar
1963if ne rc KMI_BAR_OK
1964ret rc
1965end
1966if eq get bar 0 KMI_BAR_IO
1967ret KMI_BAR_ERR_NO_SUCH
1968end
1969rid = get fn_info 0
1970cmd = kmi_pci_read_cmd cfg rid
1971dis = and cmd xor or KMI_PCI_CMD_MEMORY KMI_PCI_CMD_IO 0xffff
1972kmi_pci_cfg_write32 cfg rid KMI_PCI_REG_COMMAND uint dis
1973low_at = add KMI_PCI_REG_BAR0 mul ushort idx 4
1974low_m = kmi_pci_probe_dword cfg rid low_at 0xfffffff0
1975mask = or ulong low_m 0xffffffff00000000
1976is64 = ne get bar 3 0
1977if is64
1978hi_m = kmi_pci_probe_dword cfg rid add low_at 4 0xffffffff
1979mask = or shl ulong hi_m 32 ulong low_m
1980end
1981kmi_pci_cfg_write32 cfg rid KMI_PCI_REG_COMMAND uint cmd
1982if eq mask 0
1983put size_out 0 ulong 0
1984put size_out 1 ulong 0
1985ret KMI_BAR_OK
1986end
1987if not is64
1988if eq low_m 0
1989put size_out 0 ulong 0
1990put size_out 1 ulong 0
1991ret KMI_BAR_OK
1992end
1993end
1994put size_out 0 ulong 1
1995put size_out 1 kmi_u64_isolate_low1 mask
1996ret KMI_BAR_OK
1997end
1998
1999note sweep_out[40]:
2000note 0: functions 1: bridges 2: bridges_bme 3: endpoints_bme 4: still_enabled
2001note 8 + i*4: [rid, cmd_before, cmd_after, cleared(0/1)] for i in 0..7
2002
2003fn kmi_sweep_fn cfg edit uint 516 rid uint sweep edit ulong 40 to uchar
2004id = kmi_pci_cfg_read32 cfg rid KMI_PCI_REG_ID
2005if eq and id 0xffff 0xffff
2006ret 0
2007end
2008put sweep 0 add get sweep 0 1
2009hdr = and shr kmi_pci_cfg_read32 cfg rid KMI_PCI_REG_HEADER 16 0xff
2010cmd = ushort and kmi_pci_cfg_read32 cfg rid KMI_PCI_REG_COMMAND 0xffff
2011if eq and hdr 0x7f 1
2012put sweep 1 add get sweep 1 1
2013if kmi_bme_enabled cmd
2014put sweep 2 add get sweep 2 1
2015end
2016else
2017if kmi_bme_enabled cmd
2018kmi_pci_cfg_write16 cfg rid KMI_PCI_REG_COMMAND kmi_without_bme cmd
2019after = ushort and kmi_pci_cfg_read32 cfg rid KMI_PCI_REG_COMMAND 0xffff
2020ep_idx = get sweep 3
2021cleared = not kmi_bme_enabled after
2022if lt ep_idx KMI_SWEEP_FINDINGS
2023foff = add 8 mul ep_idx 4
2024put sweep foff ulong rid
2025put sweep add foff 1 ulong cmd
2026put sweep add foff 2 ulong after
2027if cleared
2028put sweep add foff 3 ulong 1
2029else
2030put sweep add foff 3 ulong 0
2031end
2032end
2033put sweep 3 add ep_idx 1
2034if not cleared
2035put sweep 4 add get sweep 4 1
2036end
2037end
2038end
2039if ne and hdr 0x80 0
2040ret 2
2041end
2042ret 1
2043end
2044
2045fn kmi_sweep_dev cfg edit uint 516 bus uchar dev uchar sweep edit ulong 40 to void
2046for func ulong 8
2047rid = kmi_bdf_rid bus dev uchar and func 7
2048st = kmi_sweep_fn cfg rid sweep
2049if eq func 0
2050if eq st 0
2051break
2052end
2053if eq st 1
2054break
2055end
2056end
2057end
2058end
2059
2060fn kmi_sweep_bus_master cfg edit uint 516 start_bus uchar end_bus uchar sweep edit ulong 40 to void
2061for i ulong 40
2062put sweep i ulong 0
2063end
2064if gt start_bus end_bus
2065ret
2066end
2067bus = ulong start_bus
2068end_b = ulong end_bus
2069while le bus end_b bound 256
2070for dev ulong 32
2071kmi_sweep_dev cfg uchar and bus 255 uchar and dev 31 sweep
2072end
2073bus = add bus 1
2074end
2075end
2076
2077note ========================================================================
2078note 6. Device Registry and Capability-Gated DMA Authority (device.rs)
2079note ========================================================================
2080
2081const KMI_DMA_QUIESCED ulong 0
2082const KMI_DMA_TRANSLATION_INSTALLED ulong 1
2083const KMI_DMA_ACTIVE ulong 2
2084const KMI_DMA_REVOKING ulong 3
2085
2086const KMI_HW_NONE ulong 0
2087const KMI_HW_INSTALL ulong 1
2088const KMI_HW_ENABLE_BME ulong 2
2089const KMI_HW_DISABLE_BME ulong 3
2090const KMI_HW_QUIESCE ulong 4
2091const KMI_HW_ABORT_STREAM ulong 5
2092const KMI_HW_INVALIDATE ulong 6
2093
2094const KMI_EFF_INSTALL ulong 1
2095const KMI_EFF_BME_ON ulong 2
2096const KMI_EFF_BME_OFF ulong 3
2097const KMI_EFF_STOP_WORK ulong 4
2098const KMI_EFF_QUIESCE ulong 5
2099const KMI_EFF_ABORT ulong 6
2100const KMI_EFF_INVALIDATE ulong 7
2101
2102const KMI_DEV_OK int 0
2103const KMI_DEV_ERR_CAP_INVALID int 1
2104const KMI_DEV_ERR_CAP_RIGHTS int 2
2105const KMI_DEV_ERR_CAP_DUP_TABLE int 3
2106const KMI_DEV_ERR_NOT_DEVICE int 4
2107const KMI_DEV_ERR_WRONG_STATE int 5
2108const KMI_DEV_ERR_NO_WINDOW int 6
2109const KMI_DEV_ERR_BAD_WINDOW int 7
2110const KMI_DEV_ERR_FULL int 8
2111const KMI_DEV_ERR_DUPLICATE int 9
2112const KMI_DEV_ERR_HARDWARE int 10
2113
2114const KMI_RIGHT_NONE ulong 0
2115const KMI_RIGHT_READ ulong 1
2116const KMI_RIGHT_WRITE ulong 2
2117const KMI_RIGHT_MAP ulong 4
2118const KMI_RIGHT_REVOKE ulong 8
2119const KMI_RIGHT_ALL ulong 0x3f
2120
2121note Minimal CapTable in ct edit ulong 64:
2122note ct[0] = table_id
2123note ct[1] = cap_slots (max 4)
2124note ct[2..3] = reserved
2125note slot i in 0..3 at 4 + i*8:
2126note +0: occupied(0/1) +1: gen +2: resource +3: rights
2127note +4: has_parent +5: parent_table +6: parent_idx +7: parent_gen
2128
2129fn kmi_cap_init ct edit ulong 64 table_id ulong cap_slots ulong to void
2130for i ulong 64
2131put ct i ulong 0
2132end
2133put ct 0 table_id
2134put ct 1 cap_slots
2135end
2136
2137fn kmi_cap_pack_handle idx ulong gen ulong to ulong
2138ret or shl gen 32 and idx 0xffffffff
2139end
2140
2141fn kmi_cap_handle_idx h ulong to ulong
2142ret and h 0xffffffff
2143end
2144
2145fn kmi_cap_handle_gen h ulong to ulong
2146ret shr h 32
2147end
2148
2149fn kmi_cap_insert ct edit ulong 64 res ulong rights ulong h_out edit ulong 1 to bool
2150cap_s = get ct 1
2151for i ulong cap_s bound 4
2152off = add 4 mul i 8
2153if eq get ct off 0
2154ng = add get ct add off 1 1
2155put ct off ulong 1
2156put ct add off 1 ng
2157put ct add off 2 res
2158put ct add off 3 rights
2159put ct add off 4 ulong 0
2160put ct add off 5 ulong 0
2161put ct add off 6 ulong 0
2162put ct add off 7 ulong 0
2163put h_out 0 kmi_cap_pack_handle i ng
2164ret true
2165end
2166end
2167ret false
2168end
2169
2170fn kmi_cap_lookup ct view ulong 64 handle ulong req_rights ulong res_out edit ulong 1 to int
2171idx = kmi_cap_handle_idx handle
2172gen = kmi_cap_handle_gen handle
2173if ge idx get ct 1
2174ret KMI_DEV_ERR_CAP_INVALID
2175end
2176off = add 4 mul idx 8
2177if eq get ct off 0
2178ret KMI_DEV_ERR_CAP_INVALID
2179end
2180if ne get ct add off 1 gen
2181ret KMI_DEV_ERR_CAP_INVALID
2182end
2183r = get ct add off 3
2184if ne and r req_rights req_rights
2185ret KMI_DEV_ERR_CAP_RIGHTS
2186end
2187put res_out 0 get ct add off 2
2188ret KMI_DEV_OK
2189end
2190
2191fn kmi_cap_remove ct edit ulong 64 handle ulong to bool
2192idx = kmi_cap_handle_idx handle
2193gen = kmi_cap_handle_gen handle
2194if ge idx get ct 1
2195ret false
2196end
2197off = add 4 mul idx 8
2198if eq get ct off 0
2199ret false
2200end
2201if ne get ct add off 1 gen
2202ret false
2203end
2204put ct off ulong 0
2205ret true
2206end
2207
2208fn kmi_cap_derive_into src view ulong 64 handle ulong dst edit ulong 64 new_rights ulong h_out edit ulong 1 to int
2209res_b = new ulong 1 0
2210rc = kmi_cap_lookup src handle new_rights res_b
2211if ne rc KMI_DEV_OK
2212ret rc
2213end
2214cap_s = get dst 1
2215for i ulong cap_s bound 4
2216off = add 4 mul i 8
2217if eq get dst off 0
2218ng = add get dst add off 1 1
2219put dst off ulong 1
2220put dst add off 1 ng
2221put dst add off 2 get res_b 0
2222put dst add off 3 new_rights
2223put dst add off 4 ulong 1
2224put dst add off 5 get src 0
2225put dst add off 6 kmi_cap_handle_idx handle
2226put dst add off 7 kmi_cap_handle_gen handle
2227put h_out 0 kmi_cap_pack_handle i ng
2228ret KMI_DEV_OK
2229end
2230end
2231ret KMI_DEV_ERR_FULL
2232end
2233
2234note Revoke handle in c0 and any descendants in c0 or c1 (two-table set, covers all device.rs tests)
2235
2236fn kmi_cap_clear_children ct edit ulong 64 ptab ulong pidx ulong pgen ulong to void
2237cap_s = get ct 1
2238for i ulong cap_s bound 4
2239off = add 4 mul i 8
2240if eq get ct off 1
2241if eq get ct add off 4 1
2242if eq get ct add off 5 ptab
2243if eq get ct add off 6 pidx
2244if eq get ct add off 7 pgen
2245put ct off ulong 0
2246end
2247end
2248end
2249end
2250end
2251end
2252end
2253
2254fn kmi_cap_revoke2 target_tid ulong handle ulong c0 edit ulong 64 c1 edit ulong 64 has_c1 bool to int
2255found_c0 = eq get c0 0 target_tid
2256found_c1 = false
2257if has_c1
2258found_c1 = eq get c1 0 target_tid
2259end
2260if found_c0
2261if found_c1
2262ret KMI_DEV_ERR_CAP_DUP_TABLE
2263end
2264end
2265if not found_c0
2266if not found_c1
2267ret KMI_DEV_ERR_CAP_DUP_TABLE
2268end
2269end
2270idx = kmi_cap_handle_idx handle
2271gen = kmi_cap_handle_gen handle
2272if found_c0
2273if not kmi_cap_remove c0 handle
2274ret KMI_DEV_ERR_CAP_INVALID
2275end
2276kmi_cap_clear_children c0 target_tid idx gen
2277if has_c1
2278kmi_cap_clear_children c1 target_tid idx gen
2279end
2280else
2281if not kmi_cap_remove c1 handle
2282ret KMI_DEV_ERR_CAP_INVALID
2283end
2284kmi_cap_clear_children c0 target_tid idx gen
2285kmi_cap_clear_children c1 target_tid idx gen
2286end
2287ret KMI_DEV_OK
2288end
2289
2290note DmaWindow validation
2291
2292fn kmi_dma_window_valid iova ulong pages uint to bool
2293if eq pages 0
2294ret false
2295end
2296if ne and iova 0xfff 0
2297ret false
2298end
2299nbytes = mul ulong pages 4096
2300ret kmi_range_end_ok iova nbytes
2301end
2302
2303note Hardware recorder hw edit ulong 128:
2304note 0: effect_count 1: fail_step 2: bme(0/1) 3: aborted(0/1)
2305note 4: last_fail_step 5: last_fault
2306note 8 + i*3: [effect_kind, arg1, arg2] (up to 40 effects)
2307
2308fn kmi_hw_init hw edit ulong 128 fail_step ulong to void
2309for i ulong 128
2310put hw i ulong 0
2311end
2312put hw 1 fail_step
2313end
2314
2315fn kmi_hw_clear_effects hw edit ulong 128 to void
2316put hw 0 ulong 0
2317end
2318
2319fn kmi_hw_record hw edit ulong 128 kind ulong a1 ulong a2 ulong to void
2320cnt = get hw 0
2321if lt cnt 40
2322off = add 8 mul cnt 3
2323put hw off kind
2324put hw add off 1 a1
2325put hw add off 2 a2
2326end
2327put hw 0 add cnt 1
2328end
2329
2330fn kmi_hw_step_check hw view ulong 128 step ulong to bool
2331ret ne get hw 1 step
2332end
2333
2334note DeviceTable in dtab edit ulong 128:
2335note dtab[0] = cap_slots (max 8)
2336note slot i in 0..7 at 4 + i*12:
2337note +0: occupied(0/1) +1: resource +2: dev_id +3: rid
2338note +4: sid +5: state +6: has_win +7: win_iova
2339note +8: win_pages +9: has_auth +10: auth_tab +11: auth_handle
2340
2341fn kmi_dtab_init dtab edit ulong 128 cap_slots ulong to void
2342for i ulong 128
2343put dtab i ulong 0
2344end
2345put dtab 0 cap_slots
2346end
2347
2348fn kmi_dtab_find dtab view ulong 128 res ulong to long
2349cap_s = get dtab 0
2350for i ulong cap_s bound 8
2351off = add 4 mul i 12
2352if eq get dtab off 1
2353if eq get dtab add off 1 res
2354ret long i
2355end
2356end
2357end
2358ret -1
2359end
2360
2361note dev_spec[8]: [resource, dev_id, rid, sid, state, has_win, win_iova, win_pages]
2362
2363fn kmi_dtab_register dtab edit ulong 128 spec view ulong 8 to int
2364if ne get spec 4 KMI_DMA_QUIESCED
2365ret KMI_DEV_ERR_WRONG_STATE
2366end
2367if ne get spec 5 0
2368if not kmi_dma_window_valid get spec 6 uint and get spec 7 0xffffffff
2369ret KMI_DEV_ERR_BAD_WINDOW
2370end
2371end
2372cap_s = get dtab 0
2373free_i = long -1
2374for i ulong cap_s bound 8
2375off = add 4 mul i 12
2376if eq get dtab off 1
2377if eq get dtab add off 1 get spec 0
2378ret KMI_DEV_ERR_DUPLICATE
2379end
2380if eq get dtab add off 2 get spec 1
2381ret KMI_DEV_ERR_DUPLICATE
2382end
2383if eq get dtab add off 3 get spec 2
2384ret KMI_DEV_ERR_DUPLICATE
2385end
2386if eq get dtab add off 4 get spec 3
2387ret KMI_DEV_ERR_DUPLICATE
2388end
2389else
2390if lt free_i 0
2391free_i = long i
2392end
2393end
2394end
2395if lt free_i 0
2396ret KMI_DEV_ERR_FULL
2397end
2398foff = add 4 mul ulong free_i 12
2399put dtab foff ulong 1
2400put dtab add foff 1 get spec 0
2401put dtab add foff 2 get spec 1
2402put dtab add foff 3 get spec 2
2403put dtab add foff 4 get spec 3
2404put dtab add foff 5 KMI_DMA_QUIESCED
2405put dtab add foff 6 get spec 5
2406put dtab add foff 7 get spec 6
2407put dtab add foff 8 get spec 7
2408put dtab add foff 9 ulong 0
2409put dtab add foff 10 ulong 0
2410put dtab add foff 11 ulong 0
2411ret KMI_DEV_OK
2412end
2413
2414fn kmi_dtab_set_window dtab edit ulong 128 res ulong iova ulong pages uint to int
2415idx = kmi_dtab_find dtab res
2416if lt idx 0
2417ret KMI_DEV_ERR_NOT_DEVICE
2418end
2419off = add 4 mul ulong idx 12
2420if ne get dtab add off 5 KMI_DMA_QUIESCED
2421ret KMI_DEV_ERR_WRONG_STATE
2422end
2423if not kmi_dma_window_valid iova pages
2424ret KMI_DEV_ERR_BAD_WINDOW
2425end
2426put dtab add off 6 ulong 1
2427put dtab add off 7 iova
2428put dtab add off 8 ulong pages
2429ret KMI_DEV_OK
2430end
2431
2432fn kmi_dev_teardown dtab edit ulong 128 idx ulong hw edit ulong 128 to int
2433off = add 4 mul idx 12
2434dev_id = get dtab add off 2
2435rid = get dtab add off 3
2436sid = get dtab add off 4
2437put dtab add off 5 KMI_DMA_REVOKING
2438put dtab add off 6 ulong 0
2439put dtab add off 7 ulong 0
2440put dtab add off 8 ulong 0
2441put dtab add off 9 ulong 0
2442put dtab add off 10 ulong 0
2443put dtab add off 11 ulong 0
2444first_step = ulong 0
2445kmi_hw_record hw KMI_EFF_STOP_WORK dev_id ulong 0
2446kmi_hw_record hw KMI_EFF_BME_OFF rid ulong 0
2447if kmi_hw_step_check hw KMI_HW_DISABLE_BME
2448put hw 2 ulong 0
2449else
2450if eq first_step 0
2451first_step = KMI_HW_DISABLE_BME
2452end
2453end
2454kmi_hw_record hw KMI_EFF_QUIESCE dev_id ulong 0
2455if not kmi_hw_step_check hw KMI_HW_QUIESCE
2456if eq first_step 0
2457first_step = KMI_HW_QUIESCE
2458end
2459end
2460kmi_hw_record hw KMI_EFF_ABORT sid ulong 0
2461if kmi_hw_step_check hw KMI_HW_ABORT_STREAM
2462put hw 3 ulong 1
2463else
2464if eq first_step 0
2465first_step = KMI_HW_ABORT_STREAM
2466end
2467end
2468kmi_hw_record hw KMI_EFF_INVALIDATE sid ulong 0
2469if not kmi_hw_step_check hw KMI_HW_INVALIDATE
2470if eq first_step 0
2471first_step = KMI_HW_INVALIDATE
2472end
2473end
2474if eq first_step 0
2475put dtab add off 5 KMI_DMA_QUIESCED
2476ret KMI_DEV_OK
2477end
2478put hw 4 first_step
2479put hw 5 ulong 0xdead
2480ret KMI_DEV_ERR_HARDWARE
2481end
2482
2483fn kmi_dev_activate ct view ulong 64 handle ulong dtab edit ulong 128 hw edit ulong 128 to int
2484res_b = new ulong 1 0
2485rc = kmi_cap_lookup ct handle KMI_RIGHT_MAP res_b
2486if ne rc KMI_DEV_OK
2487ret rc
2488end
2489idx = kmi_dtab_find dtab get res_b 0
2490if lt idx 0
2491ret KMI_DEV_ERR_NOT_DEVICE
2492end
2493off = add 4 mul ulong idx 12
2494if ne get dtab add off 5 KMI_DMA_QUIESCED
2495ret KMI_DEV_ERR_WRONG_STATE
2496end
2497if eq get dtab add off 6 0
2498ret KMI_DEV_ERR_NO_WINDOW
2499end
2500rid = get dtab add off 3
2501sid = get dtab add off 4
2502iova = get dtab add off 7
2503pages = get dtab add off 8
2504kmi_hw_record hw KMI_EFF_INSTALL sid or iova pages
2505if not kmi_hw_step_check hw KMI_HW_INSTALL
2506ign = kmi_dev_teardown dtab ulong idx hw
2507put hw 4 KMI_HW_INSTALL
2508put hw 5 ulong 0xdead
2509ret KMI_DEV_ERR_HARDWARE
2510end
2511put hw 3 ulong 0
2512put dtab add off 5 KMI_DMA_TRANSLATION_INSTALLED
2513kmi_hw_record hw KMI_EFF_BME_ON rid ulong 1
2514if not kmi_hw_step_check hw KMI_HW_ENABLE_BME
2515ign2 = kmi_dev_teardown dtab ulong idx hw
2516put hw 4 KMI_HW_ENABLE_BME
2517put hw 5 ulong 0xdead
2518ret KMI_DEV_ERR_HARDWARE
2519end
2520put hw 2 ulong 1
2521put dtab add off 5 KMI_DMA_ACTIVE
2522put dtab add off 9 ulong 1
2523put dtab add off 10 get ct 0
2524put dtab add off 11 handle
2525ret KMI_DEV_OK
2526end
2527
2528note req[3]: [target_tid, handle, has_c1(0/1)]
2529
2530fn kmi_dev_revoke req view ulong 3 c0 edit ulong 64 c1 edit ulong 64 dtab edit ulong 128 hw edit ulong 128 to int
2531res_b = new ulong 1 0
2532tid = get req 0
2533handle = get req 1
2534has_c1 = ne get req 2 0
2535m0 = eq get c0 0 tid
2536m1 = false
2537if has_c1
2538m1 = eq get c1 0 tid
2539end
2540if m0
2541if m1
2542ret KMI_DEV_ERR_CAP_DUP_TABLE
2543end
2544end
2545if not m0
2546if not m1
2547ret KMI_DEV_ERR_CAP_DUP_TABLE
2548end
2549end
2550rc = KMI_DEV_OK
2551if m0
2552rc = kmi_cap_lookup c0 handle KMI_RIGHT_REVOKE res_b
2553else
2554rc = kmi_cap_lookup c1 handle KMI_RIGHT_REVOKE res_b
2555end
2556if ne rc KMI_DEV_OK
2557ret rc
2558end
2559idx = kmi_dtab_find dtab get res_b 0
2560if lt idx 0
2561ret KMI_DEV_ERR_NOT_DEVICE
2562end
2563hw_rc = kmi_dev_teardown dtab ulong idx hw
2564rev_rc = kmi_cap_revoke2 tid handle c0 c1 has_c1
2565if ne hw_rc KMI_DEV_OK
2566ret hw_rc
2567end
2568ret rev_rc
2569end
2570
2571fn kmi_dtab_force_quiesce dtab edit ulong 128 res ulong hw edit ulong 128 to int
2572idx = kmi_dtab_find dtab res
2573if lt idx 0
2574ret KMI_DEV_ERR_NOT_DEVICE
2575end
2576ret kmi_dev_teardown dtab ulong idx hw
2577end
2578
2579note ntables: 0, 1 (c0), or 2 (c0 and c1)
2580
2581fn kmi_dtab_auth_live dtab view ulong 128 off ulong ntables ulong c0 view ulong 64 c1 view ulong 64 to bool
2582res_b = new ulong 1 0
2583if eq get dtab add off 9 0
2584ret false
2585end
2586atab = get dtab add off 10
2587m0 = false
2588m1 = false
2589if ge ntables 1
2590m0 = eq get c0 0 atab
2591end
2592if ge ntables 2
2593m1 = eq get c1 0 atab
2594end
2595if m0
2596if not m1
2597if eq kmi_cap_lookup c0 get dtab add off 11 KMI_RIGHT_MAP res_b KMI_DEV_OK
2598ret eq get res_b 0 get dtab add off 1
2599end
2600end
2601end
2602if m1
2603if not m0
2604if eq kmi_cap_lookup c1 get dtab add off 11 KMI_RIGHT_MAP res_b KMI_DEV_OK
2605ret eq get res_b 0 get dtab add off 1
2606end
2607end
2608end
2609ret false
2610end
2611
2612fn kmi_dtab_sweep dtab edit ulong 128 ntables ulong c0 view ulong 64 c1 view ulong 64 hw edit ulong 128 to int
2613cap_s = get dtab 0
2614first_rc = KMI_DEV_OK
2615for i ulong cap_s bound 8
2616off = add 4 mul i 12
2617if eq get dtab off 1
2618if ne get dtab add off 5 KMI_DMA_QUIESCED
2619if not kmi_dtab_auth_live dtab off ntables c0 c1
2620trc = kmi_dev_teardown dtab i hw
2621if eq first_rc KMI_DEV_OK
2622first_rc = trc
2623end
2624end
2625end
2626end
2627end
2628ret first_rc
2629end
2630
2631note ========================================================================
2632note 7. EDU DMA Device State Model (edu.rs)
2633note ========================================================================
2634
2635const KMI_EDU_VENDOR_ID ushort 0x1234
2636const KMI_EDU_DEVICE_ID ushort 0x11e8
2637const KMI_EDU_BAR0_SIZE ulong 0x100000
2638const KMI_EDU_DMA_BUF_ADDR ulong 0x40000
2639const KMI_EDU_DMA_BUF_SIZE ulong 4096
2640const KMI_EDU_DEFAULT_DMA_MASK ulong 0x0fffffff
2641const KMI_EDU_ID_SIG uint 0x00ed
2642const KMI_EDU_DMA_START ulong 1
2643const KMI_EDU_DMA_TO_RAM ulong 2
2644const KMI_EDU_DMA_IRQ ulong 4
2645const KMI_EDU_IRQ_BIT uint 0x100
2646
2647const KMI_EDU_OK int 0
2648const KMI_EDU_ERR_NOT_EDU int 1
2649const KMI_EDU_ERR_LIVENESS int 2
2650const KMI_EDU_ERR_EMPTY int 3
2651const KMI_EDU_ERR_TOO_LONG int 4
2652const KMI_EDU_ERR_IOVA_RANGE int 5
2653const KMI_EDU_ERR_BUSY int 6
2654const KMI_EDU_ERR_TIMEOUT int 7
2655
2656note edu_st[24]:
2657note 0: id 1: liveness 2: irq_status 3: src
2658note 4: dst 5: count 6: cmd 7: drv_dma_mask
2659note 8: spin_limit 9: ram_base 10: blk_start 11: blk_end
2660note 12: has_delay 13: delay 14: polls_left 15: transfers
2661note 16: inv_live 17: dev_mask 18: ver_major 19: ver_minor
2662
2663fn kmi_edu_init st edit ulong 24 to void
2664for i ulong 24
2665put st i ulong 0
2666end
2667put st 0 ulong 0x010000ed
2668put st 7 KMI_EDU_DEFAULT_DMA_MASK
2669put st 8 ulong 1000
2670put st 9 ulong 0x100000
2671put st 12 ulong 1
2672put st 13 ulong 3
2673put st 16 ulong 1
2674put st 17 KMI_EDU_DEFAULT_DMA_MASK
2675end
2676
2677fn kmi_edu_probe st edit ulong 24 to int
2678id = uint and get st 0 0xffffffff
2679if ne and id 0xffff KMI_EDU_ID_SIG
2680ret KMI_EDU_ERR_NOT_EDU
2681end
2682p0 = uint 0x5a5aa5a5
2683inv = ne get st 16 0
2684if inv
2685put st 1 ulong xor p0 0xffffffff
2686else
2687put st 1 ulong p0
2688end
2689r0 = uint and get st 1 0xffffffff
2690if ne r0 xor p0 0xffffffff
2691ret KMI_EDU_ERR_LIVENESS
2692end
2693p1 = uint 0x01234567
2694if inv
2695put st 1 ulong xor p1 0xffffffff
2696else
2697put st 1 ulong p1
2698end
2699r1 = uint and get st 1 0xffffffff
2700if ne r1 xor p1 0xffffffff
2701ret KMI_EDU_ERR_LIVENESS
2702end
2703put st 18 ulong and shr id 24 0xff
2704put st 19 ulong and shr id 16 0xff
2705ret KMI_EDU_OK
2706end
2707
2708fn kmi_edu_check st view ulong 24 iova ulong xlen ulong to int
2709if eq xlen 0
2710ret KMI_EDU_ERR_EMPTY
2711end
2712if gt xlen KMI_EDU_DMA_BUF_SIZE
2713ret KMI_EDU_ERR_TOO_LONG
2714end
2715if not kmi_range_end_ok iova sub xlen 1
2716ret KMI_EDU_ERR_IOVA_RANGE
2717end
2718last = add iova sub xlen 1
2719not_mask = xor get st 7 0xffffffffffffffff
2720if ne and iova not_mask 0
2721ret KMI_EDU_ERR_IOVA_RANGE
2722end
2723if ne and last not_mask 0
2724ret KMI_EDU_ERR_IOVA_RANGE
2725end
2726ret KMI_EDU_OK
2727end
2728
2729fn kmi_edu_copy_buf dev_buf edit uchar 4096 ram_buf edit uchar 16384 off ulong ram_off ulong xlen ulong to_ram bool to void
2730for i ulong xlen bound 4096
2731if to_ram
2732put ram_buf add ram_off i get dev_buf add off i
2733else
2734put dev_buf add off i get ram_buf add ram_off i
2735end
2736end
2737end
2738
2739fn kmi_edu_run_dma st edit ulong 24 dev_buf edit uchar 4096 ram_buf edit uchar 16384 to void
2740cmd = get st 6
2741to_ram = ne and cmd KMI_EDU_DMA_TO_RAM 0
2742dev_a = get st 4
2743ram_a = get st 3
2744if to_ram
2745dev_a = get st 3
2746ram_a = get st 4
2747end
2748blocked = false
2749if ge ram_a get st 10
2750if lt ram_a get st 11
2751blocked = true
2752end
2753end
2754if not blocked
2755kmi_edu_copy_buf dev_buf ram_buf sub dev_a KMI_EDU_DMA_BUF_ADDR sub ram_a get st 9 get st 5 to_ram
2756end
2757put st 15 add get st 15 1
2758put st 6 and cmd xor KMI_EDU_DMA_START 0xffffffffffffffff
2759if ne and cmd KMI_EDU_DMA_IRQ 0
2760put st 2 or get st 2 ulong KMI_EDU_IRQ_BIT
2761end
2762end
2763
2764fn kmi_edu_poll_cmd st edit ulong 24 dev_buf edit uchar 4096 ram_buf edit uchar 16384 to ulong
2765cmd = get st 6
2766if ne and cmd KMI_EDU_DMA_START 0
2767if ne get st 12 0
2768if ge get st 14 get st 13
2769kmi_edu_run_dma st dev_buf ram_buf
2770else
2771put st 14 add get st 14 1
2772end
2773end
2774end
2775ret get st 6
2776end
2777
2778fn kmi_edu_transfer st edit ulong 24 dev_buf edit uchar 4096 ram_buf edit uchar 16384 iova ulong xlen ulong to_ram bool to int
2779rc = kmi_edu_check st iova xlen
2780if ne rc KMI_EDU_OK
2781ret rc
2782end
2783if ne and kmi_edu_poll_cmd st dev_buf ram_buf KMI_EDU_DMA_START 0
2784ret KMI_EDU_ERR_BUSY
2785end
2786if to_ram
2787put st 3 KMI_EDU_DMA_BUF_ADDR
2788put st 4 iova
2789put st 5 xlen
2790put st 6 or KMI_EDU_DMA_START KMI_EDU_DMA_TO_RAM
2791else
2792put st 3 iova
2793put st 4 KMI_EDU_DMA_BUF_ADDR
2794put st 5 xlen
2795put st 6 KMI_EDU_DMA_START
2796end
2797put st 14 ulong 0
2798lim = get st 8
2799for s ulong lim bound 65536
2800c = kmi_edu_poll_cmd st dev_buf ram_buf
2801if eq and c KMI_EDU_DMA_START 0
2802ret KMI_EDU_OK
2803end
2804end
2805ret KMI_EDU_ERR_TIMEOUT
2806end
2807
2808fn kmi_edu_irq_ack st edit ulong 24 bits uint to void
2809cur = uint and get st 2 0xffffffff
2810put st 2 ulong and cur xor bits 0xffffffff
2811end
2812
2813note ========================================================================
2814note 8. Block Device Geometry and RamDisk (block.rs)
2815note ========================================================================
2816
2817const KMI_BLK_OK int 0
2818const KMI_BLK_INVALID_INPUT int 1
2819const KMI_BLK_OUT_OF_RANGE int 2
2820const KMI_BLK_DEVICE_ERROR int 3
2821const KMI_BLK_TIMEOUT int 4
2822
2823note blk_st[12]:
2824note 0: block_size 1: block_count 2: total_bytes 3: has_fail_after
2825note 4: fail_after 5: has_fail_wr 6: fail_wr_num 7: fail_wr_bytes
2826note 8: write_count 9: failed
2827
2828fn kmi_ramdisk_init st edit ulong 12 block_size ulong block_count ulong buf_cap ulong to int
2829if lt block_size 64
2830ret KMI_BLK_INVALID_INPUT
2831end
2832if gt block_size 0xffffffff
2833ret KMI_BLK_INVALID_INPUT
2834end
2835if lt block_count 4
2836ret KMI_BLK_INVALID_INPUT
2837end
2838if gt block_count div 0xffffffffffffffff block_size
2839ret KMI_BLK_INVALID_INPUT
2840end
2841tot = mul block_size block_count
2842if gt tot buf_cap
2843ret KMI_BLK_INVALID_INPUT
2844end
2845for i ulong 12
2846put st i ulong 0
2847end
2848put st 0 block_size
2849put st 1 block_count
2850put st 2 tot
2851ret KMI_BLK_OK
2852end
2853
2854fn kmi_ramdisk_fail_after st edit ulong 12 has_lim bool count ulong to void
2855if has_lim
2856put st 3 ulong 1
2857put st 4 count
2858else
2859put st 3 ulong 0
2860put st 4 ulong 0
2861end
2862put st 5 ulong 0
2863put st 8 ulong 0
2864put st 9 ulong 0
2865end
2866
2867fn kmi_ramdisk_tear_write st edit ulong 12 wr_num ulong tear_bytes ulong to void
2868put st 3 ulong 0
2869put st 5 ulong 1
2870wn = wr_num
2871if lt wn 1
2872wn = 1
2873end
2874put st 6 wn
2875put st 7 tear_bytes
2876put st 8 ulong 0
2877put st 9 ulong 0
2878end
2879
2880fn kmi_ramdisk_corrupt st view ulong 12 disk edit uchar * off ulong val uchar to int
2881if ge off get st 2
2882ret KMI_BLK_OUT_OF_RANGE
2883end
2884put disk off val
2885ret KMI_BLK_OK
2886end
2887
2888fn kmi_ramdisk_range st view ulong 12 lba ulong rlen ulong start_out edit ulong 1 to int
2889bs = get st 0
2890if ne rem rlen bs 0
2891ret KMI_BLK_INVALID_INPUT
2892end
2893if gt lba div 0xffffffffffffffff bs
2894ret KMI_BLK_OUT_OF_RANGE
2895end
2896s = mul lba bs
2897if not kmi_range_end_ok s rlen
2898ret KMI_BLK_OUT_OF_RANGE
2899end
2900if gt add s rlen get st 2
2901ret KMI_BLK_OUT_OF_RANGE
2902end
2903put start_out 0 s
2904ret KMI_BLK_OK
2905end
2906
2907fn kmi_ramdisk_read st view ulong 12 disk view uchar * lba ulong dst edit uchar * to int
2908sb = new ulong 1 0
2909rlen = len dst
2910rc = kmi_ramdisk_range st lba rlen sb
2911if ne rc KMI_BLK_OK
2912ret rc
2913end
2914s = get sb 0
2915for i ulong rlen bound 65536
2916put dst i get disk add s i
2917end
2918ret KMI_BLK_OK
2919end
2920
2921fn kmi_disk_copy_in disk edit uchar * dst_off ulong src view uchar * n ulong to void
2922for i ulong n bound 65536
2923put disk add dst_off i get src i
2924end
2925end
2926
2927fn kmi_ramdisk_write st edit ulong 12 disk edit uchar * lba ulong src view uchar * to int
2928sb = new ulong 1 0
2929rc = kmi_ramdisk_range st lba len src sb
2930if ne rc KMI_BLK_OK
2931ret rc
2932end
2933if ne get st 9 0
2934ret KMI_BLK_DEVICE_ERROR
2935end
2936if ne get st 3 0
2937bcnt = div len src get st 0
2938allow_b = get st 4
2939if gt allow_b bcnt
2940allow_b = bcnt
2941end
2942kmi_disk_copy_in disk get sb 0 src mul allow_b get st 0
2943if lt get st 4 bcnt
2944put st 4 ulong 0
2945ret KMI_BLK_DEVICE_ERROR
2946end
2947put st 4 sub get st 4 bcnt
2948ret KMI_BLK_OK
2949end
2950wc = add get st 8 1
2951put st 8 wc
2952if ne get st 5 0
2953if eq wc get st 6
2954ab = get st 7
2955if gt ab len src
2956ab = len src
2957end
2958kmi_disk_copy_in disk get sb 0 src ab
2959put st 9 ulong 1
2960ret KMI_BLK_DEVICE_ERROR
2961end
2962end
2963kmi_disk_copy_in disk get sb 0 src len src
2964ret KMI_BLK_OK
2965end
2966
2967note ========================================================================
2968note 9. NVMe Power-Fail Atomicity (nvme/atomicity.rs)
2969note ========================================================================
2970
2971const KMI_NVME_ID_CTRL_AWUPF_OFF ulong 528
2972const KMI_NVME_ID_NS_NAWUPF_OFF ulong 36
2973const KMI_NVME_ID_NS_NABO_OFF ulong 42
2974const KMI_NVME_ID_NS_NABSPF_OFF ulong 44
2975const KMI_NVME_STORE_UNIT_BYTES ulong 4096
2976
2977const KMI_ATOM_ATOMIC int 0
2978const KMI_ATOM_EMPTY int 1
2979const KMI_ATOM_EXCEEDS int 2
2980const KMI_ATOM_CROSSES int 3
2981const KMI_ATOM_NO_GUARANTEE int 4
2982const KMI_ATOM_BAD_GEOMETRY int 5
2983
2984fn kmi_read_u16_le bytes view uchar * off ulong val_out edit ushort 1 to bool
2985if gt off sub ulong 0xffffffffffffffff 2
2986ret false
2987end
2988if gt add off 2 len bytes
2989ret false
2990end
2991lo = ushort get bytes off
2992hi = ushort get bytes add off 1
2993put val_out 0 or lo shl hi 8
2994ret true
2995end
2996
2997note af[8]: [lba_bytes, has_awupf, awupf_raw, has_nawupf, nawupf_raw, has_nabspf, nabspf_raw, nabo_blocks]
2998
2999fn kmi_nvme_effective_pf af view ulong 8 to ulong
3000if ne get af 3 0
3001if gt get af 4 0
3002ret add get af 4 1
3003end
3004end
3005if ne get af 1 0
3006ret add get af 2 1
3007end
3008ret 0
3009end
3010
3011fn kmi_nvme_boundary_blocks af view ulong 8 to ulong
3012if ne get af 5 0
3013if gt get af 6 0
3014ret add get af 6 1
3015end
3016end
3017ret 0
3018end
3019
3020fn kmi_nvme_same_region b1 ulong b2 ulong bsize ulong nabo ulong to bool
3021off = rem nabo bsize
3022if lt b1 off
3023ret lt b2 off
3024end
3025if lt b2 off
3026ret false
3027end
3028r1 = div sub b1 off bsize
3029r2 = div sub b2 off bsize
3030ret eq r1 r2
3031end
3032
3033note info[4]: [effective_or_boundary, required_or_start, guaranteed_or_count, reserved]
3034
3035fn kmi_nvme_write_atomic af view ulong 8 start_lba ulong lba_count ulong info edit ulong 4 to int
3036for i ulong 4
3037put info i ulong 0
3038end
3039if eq lba_count 0
3040ret KMI_ATOM_EMPTY
3041end
3042guar = kmi_nvme_effective_pf af
3043if eq guar 0
3044ret KMI_ATOM_NO_GUARANTEE
3045end
3046if gt lba_count guar
3047put info 1 lba_count
3048put info 2 guar
3049ret KMI_ATOM_EXCEEDS
3050end
3051bsize = kmi_nvme_boundary_blocks af
3052if gt bsize 0
3053if not kmi_range_end_ok start_lba sub lba_count 1
3054put info 0 bsize
3055put info 1 start_lba
3056put info 2 lba_count
3057ret KMI_ATOM_CROSSES
3058end
3059last = add start_lba sub lba_count 1
3060if not kmi_nvme_same_region start_lba last bsize get af 7
3061put info 0 bsize
3062put info 1 start_lba
3063put info 2 lba_count
3064ret KMI_ATOM_CROSSES
3065end
3066end
3067put info 0 guar
3068ret KMI_ATOM_ATOMIC
3069end
3070
3071note out_lba[2]: [start_lba, lba_count]
3072
3073fn kmi_nvme_store_unit_lba af view ulong 8 slot uint out_lba edit ulong 2 to bool
3074bs = get af 0
3075if eq bs 0
3076ret false
3077end
3078if ne rem KMI_NVME_STORE_UNIT_BYTES bs 0
3079ret false
3080end
3081blks = div KMI_NVME_STORE_UNIT_BYTES bs
3082sl = ulong slot
3083if gt sl div 0xffffffffffffffff blks
3084ret false
3085end
3086put out_lba 0 mul sl blks
3087put out_lba 1 blks
3088ret true
3089end
3090
3091fn kmi_nvme_store_root_decision af view ulong 8 slot uint info edit ulong 4 to int
3092lba_pair = new ulong 2 0
3093ok = kmi_nvme_store_unit_lba af slot lba_pair
3094if not ok
3095ret KMI_ATOM_BAD_GEOMETRY
3096end
3097ret kmi_nvme_write_atomic af get lba_pair 0 get lba_pair 1 info
3098end