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