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 / kernel / hal / iort.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/kernel/hal/iort.om: Pure-Omega ACPI SDT checksum and IORT SMMUv3 route parser. |
| 3 | note Converted from aienux native/kernel/core/acpi.{h,c} (ck_acpi_sum_ok, ck_iort_parse, |
| 4 | note ck_iort_stream_id, ck_iort_other_stream, ck_iort_route_rid, ck_iort_named, RMR ranges). |
| 5 | note Compose with: si/directives/om2.om aienux/kernel/hal/iort.om ... |
| 6 | |
| 7 | const IORT_SDT_HDR ulong 36 |
| 8 | const IORT_TABLE_HDR ulong 48 |
| 9 | const IORT_NODE_HDR ulong 16 |
| 10 | const IORT_MAP_BYTES ulong 20 |
| 11 | const IORT_RC_SEGMENT ulong 28 |
| 12 | const IORT_RMR_HDR ulong 28 |
| 13 | const IORT_RMR_DESC_BYTES ulong 20 |
| 14 | const IORT_NAMED_NAME_OFF ulong 29 |
| 15 | |
| 16 | const IORT_NODE_NAMED uchar 1 |
| 17 | const IORT_NODE_RC uchar 2 |
| 18 | const IORT_NODE_SMMUV3 uchar 4 |
| 19 | const IORT_NODE_RMR uchar 6 |
| 20 | const IORT_MAP_SINGLE uint 1 |
| 21 | |
| 22 | const IORT_MAX_SMMUS ulong 8 |
| 23 | const IORT_MAX_MAPS ulong 16 |
| 24 | const IORT_MAX_OTHER ulong 8 |
| 25 | const IORT_MAX_RMR ulong 4 |
| 26 | const IORT_MAX_RMR_RANGES ulong 4 |
| 27 | |
| 28 | note Metadata slots: 0=nsmmus, 1=nmaps, 2=nother, 3=nrmr, 4=primary_base, 5=primary_off, |
| 29 | note 6=mutant_route_first (0=off, 1=route all to smmu 0), 7=mutant_rmr_excl (0=inclusive, 1=exclusive) |
| 30 | global iort_meta ulong 8 |
| 31 | global iort_smmu_base ulong 8 |
| 32 | global iort_smmu_off uint 8 |
| 33 | |
| 34 | global iort_map_seg uint 16 |
| 35 | global iort_map_in uint 16 |
| 36 | global iort_map_cnt uint 16 |
| 37 | global iort_map_out uint 16 |
| 38 | |
| 39 | global iort_oth_seg uint 8 |
| 40 | global iort_oth_in uint 8 |
| 41 | global iort_oth_cnt uint 8 |
| 42 | global iort_oth_out uint 8 |
| 43 | global iort_oth_base ulong 8 |
| 44 | global iort_oth_off uint 8 |
| 45 | |
| 46 | global iort_rmr_off uint 4 |
| 47 | global iort_rmr_sidx uint 4 |
| 48 | global iort_rmr_slo uint 4 |
| 49 | global iort_rmr_shi uint 4 |
| 50 | global iort_rmr_flags uint 4 |
| 51 | global iort_rmr_nrng uint 4 |
| 52 | global iort_rmr_rbase ulong 16 |
| 53 | global iort_rmr_rlen ulong 16 |
| 54 | global iort_tmp_rb ulong 4 |
| 55 | global iort_tmp_rl ulong 4 |
| 56 | |
| 57 | note Route query result slots: 0=smmu_index, 1=smmu_base, 2=smmu_off, 3=sid, 4=rmr_hits, |
| 58 | note 5=rmr_first_flags, 6=named_node_off, 7=named_sid, 8=route CCA (iort_route_rid) |
| 59 | global iort_qres ulong 9 |
| 60 | note Root-complex Memory Access Properties CCA (IORT RC node +16, u32; 1 = fully IO-coherent), |
| 61 | note recorded per primary/other map; iort_route_rid reports it in iort_qres[8]. |
| 62 | global iort_map_cca uint 16 |
| 63 | global iort_oth_cca uint 8 |
| 64 | global iort_cur_cca uint 1 |
| 65 | |
| 66 | fn iort_rd16 buf view uchar * off ulong to uint |
| 67 | b0 = uint get buf off |
| 68 | b1 = uint get buf add off 1 |
| 69 | ret or b0 shl b1 8 |
| 70 | end |
| 71 | |
| 72 | fn iort_rd32 buf view uchar * off ulong to uint |
| 73 | b0 = uint get buf off |
| 74 | b1 = uint get buf add off 1 |
| 75 | b2 = uint get buf add off 2 |
| 76 | b3 = uint get buf add off 3 |
| 77 | lo = or b0 shl b1 8 |
| 78 | hi = or shl b2 16 shl b3 24 |
| 79 | ret or lo hi |
| 80 | end |
| 81 | |
| 82 | fn iort_rd64 buf view uchar * off ulong to ulong |
| 83 | lo = ulong iort_rd32 buf off |
| 84 | hi = ulong iort_rd32 buf add off 4 |
| 85 | ret or lo shl hi 32 |
| 86 | end |
| 87 | |
| 88 | fn acpi_sum_ok buf view uchar * n ulong to bool |
| 89 | if or lt n IORT_SDT_HDR gt n len buf |
| 90 | ret false |
| 91 | end |
| 92 | acc = uint 0 |
| 93 | i = ulong 0 |
| 94 | note ACPI tables are bounded in size; 65536 covers all IORT/MADT/SDT tables. |
| 95 | while lt i n bound 65536 |
| 96 | acc = and add acc uint get buf i 0xff |
| 97 | i = add i 1 |
| 98 | end |
| 99 | ret eq acc 0 |
| 100 | end |
| 101 | |
| 102 | fn iort_reset to void |
| 103 | for i ulong 8 |
| 104 | put iort_meta i 0 |
| 105 | put iort_smmu_base i 0 |
| 106 | put iort_smmu_off i 0 |
| 107 | put iort_oth_seg i 0 |
| 108 | put iort_oth_in i 0 |
| 109 | put iort_oth_cnt i 0 |
| 110 | put iort_oth_out i 0 |
| 111 | put iort_oth_base i 0 |
| 112 | put iort_oth_off i 0 |
| 113 | put iort_oth_cca i 0 |
| 114 | put iort_qres i 0 |
| 115 | end |
| 116 | put iort_qres 8 0 |
| 117 | for j ulong 16 |
| 118 | put iort_map_seg j 0 |
| 119 | put iort_map_in j 0 |
| 120 | put iort_map_cnt j 0 |
| 121 | put iort_map_out j 0 |
| 122 | put iort_map_cca j 0 |
| 123 | put iort_rmr_rbase j 0 |
| 124 | put iort_rmr_rlen j 0 |
| 125 | end |
| 126 | for k ulong 4 |
| 127 | put iort_rmr_off k 0 |
| 128 | put iort_rmr_sidx k 0 |
| 129 | put iort_rmr_slo k 0 |
| 130 | put iort_rmr_shi k 0 |
| 131 | put iort_rmr_flags k 0 |
| 132 | put iort_rmr_nrng k 0 |
| 133 | end |
| 134 | end |
| 135 | |
| 136 | fn iort_set_mutants route_first ulong rmr_excl ulong to void |
| 137 | put iort_meta 6 route_first |
| 138 | put iort_meta 7 rmr_excl |
| 139 | end |
| 140 | |
| 141 | fn iort_find_smmu_idx off uint to ulong |
| 142 | ns = get iort_meta 0 |
| 143 | i = ulong 0 |
| 144 | while lt i ns bound 8 |
| 145 | if eq get iort_smmu_off i off |
| 146 | ret i |
| 147 | end |
| 148 | i = add i 1 |
| 149 | end |
| 150 | ret IORT_MAX_SMMUS |
| 151 | end |
| 152 | |
| 153 | fn iort_u32_add_overflow a uint b uint to bool |
| 154 | s = add a b |
| 155 | ret lt s a |
| 156 | end |
| 157 | |
| 158 | fn iort_u64_add_overflow a ulong b ulong to bool |
| 159 | s = add a b |
| 160 | ret lt s a |
| 161 | end |
| 162 | |
| 163 | fn iort_ranges_overlap a_lo uint a_cnt uint b_lo uint b_cnt uint to bool |
| 164 | a_hi = add a_lo a_cnt |
| 165 | b_hi = add b_lo b_cnt |
| 166 | if lt a_hi b_lo |
| 167 | ret false |
| 168 | end |
| 169 | if lt b_hi a_lo |
| 170 | ret false |
| 171 | end |
| 172 | ret true |
| 173 | end |
| 174 | |
| 175 | fn iort_check_hdr buf view uchar * to long |
| 176 | blen = len buf |
| 177 | if lt blen IORT_TABLE_HDR |
| 178 | ret -1 |
| 179 | end |
| 180 | note Signature "IORT" = 0x49 0x4f 0x52 0x54 |
| 181 | if or ne get buf 0 73 ne get buf 1 79 |
| 182 | ret -1 |
| 183 | end |
| 184 | if or ne get buf 2 82 ne get buf 3 84 |
| 185 | ret -1 |
| 186 | end |
| 187 | tlen = ulong iort_rd32 buf 4 |
| 188 | if or lt tlen IORT_TABLE_HDR gt tlen blen |
| 189 | ret -1 |
| 190 | end |
| 191 | if not acpi_sum_ok buf tlen |
| 192 | ret -1 |
| 193 | end |
| 194 | ncnt = ulong iort_rd32 buf 36 |
| 195 | noff = ulong iort_rd32 buf 40 |
| 196 | if eq ncnt 0 |
| 197 | ret -1 |
| 198 | end |
| 199 | if or lt noff IORT_TABLE_HDR gt noff tlen |
| 200 | ret -1 |
| 201 | end |
| 202 | ret long tlen |
| 203 | end |
| 204 | |
| 205 | fn iort_load_rmr_descs buf view uchar * start ulong rcount ulong to int |
| 206 | d = ulong 0 |
| 207 | while lt d rcount bound 4 |
| 208 | doff = add start mul d IORT_RMR_DESC_BYTES |
| 209 | b = iort_rd64 buf doff |
| 210 | l = iort_rd64 buf add doff 8 |
| 211 | if eq l 0 |
| 212 | ret -1 |
| 213 | end |
| 214 | if or ne and b 0xfff 0 ne and l 0xfff 0 |
| 215 | ret -1 |
| 216 | end |
| 217 | if iort_u64_add_overflow b l |
| 218 | ret -1 |
| 219 | end |
| 220 | put iort_tmp_rb d b |
| 221 | put iort_tmp_rl d l |
| 222 | d = add d 1 |
| 223 | end |
| 224 | ret 0 |
| 225 | end |
| 226 | |
| 227 | fn iort_bind_rmr_one off ulong sidx ulong ob uint cnt uint flags uint rcount ulong to int |
| 228 | nr = get iort_meta 3 |
| 229 | if ge nr IORT_MAX_RMR |
| 230 | ret -1 |
| 231 | end |
| 232 | if iort_u32_add_overflow ob cnt |
| 233 | ret -1 |
| 234 | end |
| 235 | shi = add ob cnt |
| 236 | if ne get iort_meta 7 0 |
| 237 | note Mutant: treat id_count as exclusive count instead of inclusive (number of IDs - 1) |
| 238 | if gt cnt 0 |
| 239 | shi = sub add ob cnt 1 |
| 240 | else |
| 241 | shi = ob |
| 242 | end |
| 243 | end |
| 244 | put iort_rmr_off nr uint off |
| 245 | put iort_rmr_sidx nr uint sidx |
| 246 | put iort_rmr_slo nr ob |
| 247 | put iort_rmr_shi nr shi |
| 248 | put iort_rmr_flags nr flags |
| 249 | put iort_rmr_nrng nr uint rcount |
| 250 | k = ulong 0 |
| 251 | while lt k rcount bound 4 |
| 252 | rslot = add mul nr 4 k |
| 253 | put iort_rmr_rbase rslot get iort_tmp_rb k |
| 254 | put iort_rmr_rlen rslot get iort_tmp_rl k |
| 255 | k = add k 1 |
| 256 | end |
| 257 | put iort_meta 3 add nr 1 |
| 258 | ret 0 |
| 259 | end |
| 260 | |
| 261 | fn iort_bind_rmr_maps buf view uchar * off ulong mcount ulong moff ulong to int |
| 262 | flags = iort_rd32 buf add off 16 |
| 263 | rcount = ulong iort_rd32 buf add off 20 |
| 264 | m = ulong 0 |
| 265 | while lt m mcount bound 64 |
| 266 | eoff = add add off moff mul m IORT_MAP_BYTES |
| 267 | ref = iort_rd32 buf add eoff 12 |
| 268 | sidx = iort_find_smmu_idx ref |
| 269 | if lt sidx IORT_MAX_SMMUS |
| 270 | cnt = iort_rd32 buf add eoff 4 |
| 271 | ob = iort_rd32 buf add eoff 8 |
| 272 | if ne iort_bind_rmr_one off sidx ob cnt flags rcount 0 |
| 273 | ret -1 |
| 274 | end |
| 275 | end |
| 276 | m = add m 1 |
| 277 | end |
| 278 | ret 0 |
| 279 | end |
| 280 | |
| 281 | fn iort_parse_rmr buf view uchar * off ulong nlen ulong mcount ulong moff ulong to int |
| 282 | if lt nlen IORT_RMR_HDR |
| 283 | ret -1 |
| 284 | end |
| 285 | rcount = ulong iort_rd32 buf add off 20 |
| 286 | roff = ulong iort_rd32 buf add off 24 |
| 287 | if or eq rcount 0 gt rcount IORT_MAX_RMR_RANGES |
| 288 | ret -1 |
| 289 | end |
| 290 | if or lt roff IORT_RMR_HDR gt roff nlen |
| 291 | ret -1 |
| 292 | end |
| 293 | if gt mul rcount IORT_RMR_DESC_BYTES sub nlen roff |
| 294 | ret -1 |
| 295 | end |
| 296 | if ne iort_load_rmr_descs buf add off roff rcount 0 |
| 297 | ret -1 |
| 298 | end |
| 299 | ret iort_bind_rmr_maps buf off mcount moff |
| 300 | end |
| 301 | |
| 302 | fn iort_add_primary_map seg uint inb uint cnt uint ob uint to int |
| 303 | if or iort_u32_add_overflow inb cnt iort_u32_add_overflow ob cnt |
| 304 | ret -1 |
| 305 | end |
| 306 | nm = get iort_meta 1 |
| 307 | if ge nm IORT_MAX_MAPS |
| 308 | ret -1 |
| 309 | end |
| 310 | i = ulong 0 |
| 311 | while lt i nm bound 16 |
| 312 | if eq get iort_map_seg i seg |
| 313 | if iort_ranges_overlap get iort_map_in i get iort_map_cnt i inb cnt |
| 314 | ret -1 |
| 315 | end |
| 316 | end |
| 317 | i = add i 1 |
| 318 | end |
| 319 | put iort_map_seg nm seg |
| 320 | put iort_map_in nm inb |
| 321 | put iort_map_cnt nm cnt |
| 322 | put iort_map_out nm ob |
| 323 | put iort_map_cca nm get iort_cur_cca 0 |
| 324 | put iort_meta 1 add nm 1 |
| 325 | ret 0 |
| 326 | end |
| 327 | |
| 328 | fn iort_add_other_map seg uint inb uint cnt uint ob uint sidx ulong to int |
| 329 | if or iort_u32_add_overflow inb cnt iort_u32_add_overflow ob cnt |
| 330 | ret -1 |
| 331 | end |
| 332 | no = get iort_meta 2 |
| 333 | if ge no IORT_MAX_OTHER |
| 334 | ret -1 |
| 335 | end |
| 336 | i = ulong 0 |
| 337 | while lt i no bound 8 |
| 338 | if eq get iort_oth_seg i seg |
| 339 | if iort_ranges_overlap get iort_oth_in i get iort_oth_cnt i inb cnt |
| 340 | ret -1 |
| 341 | end |
| 342 | end |
| 343 | i = add i 1 |
| 344 | end |
| 345 | put iort_oth_seg no seg |
| 346 | put iort_oth_in no inb |
| 347 | put iort_oth_cnt no cnt |
| 348 | put iort_oth_out no ob |
| 349 | put iort_oth_base no get iort_smmu_base sidx |
| 350 | put iort_oth_off no get iort_smmu_off sidx |
| 351 | put iort_oth_cca no get iort_cur_cca 0 |
| 352 | put iort_meta 2 add no 1 |
| 353 | ret 0 |
| 354 | end |
| 355 | |
| 356 | fn iort_parse_rc_map buf view uchar * eoff ulong seg uint to int |
| 357 | inb = iort_rd32 buf eoff |
| 358 | cnt = iort_rd32 buf add eoff 4 |
| 359 | ob = iort_rd32 buf add eoff 8 |
| 360 | ref = iort_rd32 buf add eoff 12 |
| 361 | prim_off = uint get iort_meta 5 |
| 362 | if eq ref prim_off |
| 363 | ret iort_add_primary_map seg inb cnt ob |
| 364 | end |
| 365 | sidx = iort_find_smmu_idx ref |
| 366 | if lt sidx IORT_MAX_SMMUS |
| 367 | ret iort_add_other_map seg inb cnt ob sidx |
| 368 | end |
| 369 | ret 0 |
| 370 | end |
| 371 | |
| 372 | fn iort_parse_rc buf view uchar * off ulong nlen ulong mcount ulong moff ulong to int |
| 373 | if lt nlen add IORT_RC_SEGMENT 4 |
| 374 | ret -1 |
| 375 | end |
| 376 | seg = iort_rd32 buf add off IORT_RC_SEGMENT |
| 377 | put iort_cur_cca 0 iort_rd32 buf add off 16 |
| 378 | m = ulong 0 |
| 379 | while lt m mcount bound 64 |
| 380 | eoff = add add off moff mul m IORT_MAP_BYTES |
| 381 | if ne iort_parse_rc_map buf eoff seg 0 |
| 382 | ret -1 |
| 383 | end |
| 384 | m = add m 1 |
| 385 | end |
| 386 | ret 0 |
| 387 | end |
| 388 | |
| 389 | fn iort_parse_pass0 buf view uchar * tlen ulong ncnt ulong noff ulong to int |
| 390 | off = noff |
| 391 | i = ulong 0 |
| 392 | while lt i ncnt bound 128 |
| 393 | if or lt off IORT_TABLE_HDR gt off tlen |
| 394 | ret -1 |
| 395 | end |
| 396 | if lt sub tlen off IORT_NODE_HDR |
| 397 | ret -1 |
| 398 | end |
| 399 | ntype = get buf off |
| 400 | nlen = ulong iort_rd16 buf add off 1 |
| 401 | if or lt nlen IORT_NODE_HDR gt nlen sub tlen off |
| 402 | ret -1 |
| 403 | end |
| 404 | if eq ntype IORT_NODE_SMMUV3 |
| 405 | if lt nlen 24 |
| 406 | ret -1 |
| 407 | end |
| 408 | base = iort_rd64 buf add off 16 |
| 409 | if eq base 0 |
| 410 | ret -1 |
| 411 | end |
| 412 | ns = get iort_meta 0 |
| 413 | if ge ns IORT_MAX_SMMUS |
| 414 | ret -1 |
| 415 | end |
| 416 | put iort_smmu_base ns base |
| 417 | put iort_smmu_off ns uint off |
| 418 | if eq ns 0 |
| 419 | put iort_meta 4 base |
| 420 | put iort_meta 5 off |
| 421 | end |
| 422 | put iort_meta 0 add ns 1 |
| 423 | end |
| 424 | off = add off nlen |
| 425 | i = add i 1 |
| 426 | end |
| 427 | if eq get iort_meta 0 0 |
| 428 | ret 0 |
| 429 | end |
| 430 | ret 1 |
| 431 | end |
| 432 | |
| 433 | fn iort_parse_pass1 buf view uchar * tlen ulong ncnt ulong noff ulong to int |
| 434 | off = noff |
| 435 | i = ulong 0 |
| 436 | while lt i ncnt bound 128 |
| 437 | ntype = get buf off |
| 438 | nlen = ulong iort_rd16 buf add off 1 |
| 439 | mcount = ulong iort_rd32 buf add off 8 |
| 440 | moff = ulong iort_rd32 buf add off 12 |
| 441 | if gt mcount 0 |
| 442 | if or lt moff IORT_NODE_HDR gt moff nlen |
| 443 | ret -1 |
| 444 | end |
| 445 | if gt mul mcount IORT_MAP_BYTES sub nlen moff |
| 446 | ret -1 |
| 447 | end |
| 448 | end |
| 449 | if eq ntype IORT_NODE_RC |
| 450 | if ne iort_parse_rc buf off nlen mcount moff 0 |
| 451 | ret -1 |
| 452 | end |
| 453 | end |
| 454 | if eq ntype IORT_NODE_RMR |
| 455 | if ne iort_parse_rmr buf off nlen mcount moff 0 |
| 456 | ret -1 |
| 457 | end |
| 458 | end |
| 459 | off = add off nlen |
| 460 | i = add i 1 |
| 461 | end |
| 462 | ret 1 |
| 463 | end |
| 464 | |
| 465 | note Parse an ACPI IORT table buffer. Preserves mutant flags across reset. |
| 466 | note Returns 1 if at least one SMMUv3 node was found, 0 if none, -1 if malformed. |
| 467 | fn iort_parse buf view uchar * to int |
| 468 | m0 = get iort_meta 6 |
| 469 | m1 = get iort_meta 7 |
| 470 | iort_reset |
| 471 | put iort_meta 6 m0 |
| 472 | put iort_meta 7 m1 |
| 473 | tl = iort_check_hdr buf |
| 474 | if lt tl 0 |
| 475 | ret -1 |
| 476 | end |
| 477 | tlen = ulong tl |
| 478 | ncnt = ulong iort_rd32 buf 36 |
| 479 | noff = ulong iort_rd32 buf 40 |
| 480 | p0 = iort_parse_pass0 buf tlen ncnt noff |
| 481 | if le p0 0 |
| 482 | ret p0 |
| 483 | end |
| 484 | ret iort_parse_pass1 buf tlen ncnt noff |
| 485 | end |
| 486 | |
| 487 | fn iort_count_rmr_for sidx ulong sid uint to ulong |
| 488 | nr = get iort_meta 3 |
| 489 | hits = ulong 0 |
| 490 | i = ulong 0 |
| 491 | while lt i nr bound 4 |
| 492 | if eq ulong get iort_rmr_sidx i sidx |
| 493 | lo = get iort_rmr_slo i |
| 494 | hi = get iort_rmr_shi i |
| 495 | if and ge sid lo le sid hi |
| 496 | if eq hits 0 |
| 497 | put iort_qres 5 ulong get iort_rmr_flags i |
| 498 | end |
| 499 | hits = add hits 1 |
| 500 | end |
| 501 | end |
| 502 | i = add i 1 |
| 503 | end |
| 504 | ret hits |
| 505 | end |
| 506 | |
| 507 | note Route (segment, rid) across primary and other SMMUv3 nodes. |
| 508 | note Returns 0 on match (results in iort_qres[0..5], RC CCA in [8]), -1 if uncovered. |
| 509 | fn iort_route_rid seg uint rid uint to int |
| 510 | for z ulong 6 |
| 511 | put iort_qres z 0 |
| 512 | end |
| 513 | put iort_qres 8 0 |
| 514 | nm = get iort_meta 1 |
| 515 | i = ulong 0 |
| 516 | while lt i nm bound 16 |
| 517 | if eq get iort_map_seg i seg |
| 518 | inb = get iort_map_in i |
| 519 | if ge rid inb |
| 520 | rel = sub rid inb |
| 521 | if le rel get iort_map_cnt i |
| 522 | sid = add get iort_map_out i rel |
| 523 | put iort_qres 0 0 |
| 524 | put iort_qres 1 get iort_meta 4 |
| 525 | put iort_qres 2 get iort_meta 5 |
| 526 | put iort_qres 3 ulong sid |
| 527 | put iort_qres 4 iort_count_rmr_for 0 sid |
| 528 | put iort_qres 8 ulong get iort_map_cca i |
| 529 | ret 0 |
| 530 | end |
| 531 | end |
| 532 | end |
| 533 | i = add i 1 |
| 534 | end |
| 535 | no = get iort_meta 2 |
| 536 | j = ulong 0 |
| 537 | while lt j no bound 8 |
| 538 | if eq get iort_oth_seg j seg |
| 539 | inb = get iort_oth_in j |
| 540 | if ge rid inb |
| 541 | rel = sub rid inb |
| 542 | if le rel get iort_oth_cnt j |
| 543 | sid = add get iort_oth_out j rel |
| 544 | sidx = iort_find_smmu_idx get iort_oth_off j |
| 545 | if ge sidx IORT_MAX_SMMUS |
| 546 | ret -1 |
| 547 | end |
| 548 | if ne get iort_meta 6 0 |
| 549 | note Mutant: route every other-SMMU requester to SMMU 0 |
| 550 | sidx = ulong 0 |
| 551 | end |
| 552 | put iort_qres 0 sidx |
| 553 | put iort_qres 1 get iort_smmu_base sidx |
| 554 | put iort_qres 2 ulong get iort_smmu_off sidx |
| 555 | put iort_qres 3 ulong sid |
| 556 | put iort_qres 4 iort_count_rmr_for sidx sid |
| 557 | put iort_qres 8 ulong get iort_oth_cca j |
| 558 | ret 0 |
| 559 | end |
| 560 | end |
| 561 | end |
| 562 | j = add j 1 |
| 563 | end |
| 564 | ret -1 |
| 565 | end |
| 566 | |
| 567 | fn iort_last_seg4 buf view uchar * start ulong lim ulong dest edit uchar 4 to int |
| 568 | seg_start = start |
| 569 | q = start |
| 570 | while lt q lim bound 64 |
| 571 | c = get buf q |
| 572 | if eq c 0 |
| 573 | break |
| 574 | end |
| 575 | if or eq c 46 or eq c 92 eq c 94 |
| 576 | seg_start = add q 1 |
| 577 | end |
| 578 | q = add q 1 |
| 579 | end |
| 580 | i = ulong 0 |
| 581 | s = seg_start |
| 582 | while and lt s lim lt i 4 bound 5 |
| 583 | c = get buf s |
| 584 | if eq c 0 |
| 585 | break |
| 586 | end |
| 587 | put dest i c |
| 588 | i = add i 1 |
| 589 | s = add s 1 |
| 590 | end |
| 591 | if eq i 0 |
| 592 | ret -1 |
| 593 | end |
| 594 | if lt s lim |
| 595 | if ne get buf s 0 |
| 596 | ret -1 |
| 597 | end |
| 598 | end |
| 599 | while lt i 4 bound 4 |
| 600 | put dest i 95 |
| 601 | i = add i 1 |
| 602 | end |
| 603 | ret 0 |
| 604 | end |
| 605 | |
| 606 | fn iort_named_bind_smmu buf view uchar * map_start ulong mcount ulong to int |
| 607 | tlen = ulong iort_rd32 buf 4 |
| 608 | m = ulong 0 |
| 609 | while lt m mcount bound 64 |
| 610 | eoff = add map_start mul m IORT_MAP_BYTES |
| 611 | ref = ulong iort_rd32 buf add eoff 12 |
| 612 | if or lt ref IORT_TABLE_HDR gt ref tlen |
| 613 | ret -1 |
| 614 | end |
| 615 | if lt sub tlen ref IORT_NODE_HDR |
| 616 | ret -1 |
| 617 | end |
| 618 | if eq get buf ref IORT_NODE_SMMUV3 |
| 619 | is_single = ne and iort_rd32 buf add eoff 16 IORT_MAP_SINGLE 0 |
| 620 | is_one = eq iort_rd32 buf add eoff 4 0 |
| 621 | if or is_single is_one |
| 622 | put iort_qres 7 ulong iort_rd32 buf add eoff 8 |
| 623 | t_nlen = ulong iort_rd16 buf add ref 1 |
| 624 | if and ge t_nlen 24 le t_nlen sub tlen ref |
| 625 | put iort_qres 1 iort_rd64 buf add ref 16 |
| 626 | end |
| 627 | ret 0 |
| 628 | end |
| 629 | end |
| 630 | m = add m 1 |
| 631 | end |
| 632 | ret 0 |
| 633 | end |
| 634 | |
| 635 | fn iort_named_match buf view uchar * off ulong nlen ulong mcount ulong moff ulong to int |
| 636 | if le nlen IORT_NAMED_NAME_OFF |
| 637 | ret -1 |
| 638 | end |
| 639 | nend = add off nlen |
| 640 | if gt mcount 0 |
| 641 | nend = add off moff |
| 642 | end |
| 643 | nstart = add off IORT_NAMED_NAME_OFF |
| 644 | if le nend nstart |
| 645 | ret -1 |
| 646 | end |
| 647 | s4 = new uchar 4 0 |
| 648 | if ne iort_last_seg4 buf nstart nend s4 0 |
| 649 | ret 0 |
| 650 | end |
| 651 | for k ulong 4 |
| 652 | if ne ulong get s4 k get iort_tmp_rb k |
| 653 | ret 0 |
| 654 | end |
| 655 | end |
| 656 | ret 1 |
| 657 | end |
| 658 | |
| 659 | fn iort_named_scan buf view uchar * tlen ulong ncnt ulong noff ulong to int |
| 660 | found = ulong 0 |
| 661 | off = noff |
| 662 | i = ulong 0 |
| 663 | while lt i ncnt bound 128 |
| 664 | if or lt off IORT_TABLE_HDR gt off tlen |
| 665 | ret -1 |
| 666 | end |
| 667 | if lt sub tlen off IORT_NODE_HDR |
| 668 | ret -1 |
| 669 | end |
| 670 | ntype = get buf off |
| 671 | nlen = ulong iort_rd16 buf add off 1 |
| 672 | if or lt nlen IORT_NODE_HDR gt nlen sub tlen off |
| 673 | ret -1 |
| 674 | end |
| 675 | mcount = ulong iort_rd32 buf add off 8 |
| 676 | moff = ulong iort_rd32 buf add off 12 |
| 677 | if gt mcount 0 |
| 678 | if or lt moff IORT_NODE_HDR gt moff nlen |
| 679 | ret -1 |
| 680 | end |
| 681 | if gt mul mcount IORT_MAP_BYTES sub nlen moff |
| 682 | ret -1 |
| 683 | end |
| 684 | end |
| 685 | if eq ntype IORT_NODE_NAMED |
| 686 | mr = iort_named_match buf off nlen mcount moff |
| 687 | if lt mr 0 |
| 688 | ret -1 |
| 689 | end |
| 690 | if eq mr 1 |
| 691 | if ne found 0 |
| 692 | ret -2 |
| 693 | end |
| 694 | found = ulong 1 |
| 695 | put iort_qres 6 off |
| 696 | if ne iort_named_bind_smmu buf add off moff mcount 0 |
| 697 | ret -1 |
| 698 | end |
| 699 | end |
| 700 | end |
| 701 | off = add off nlen |
| 702 | i = add i 1 |
| 703 | end |
| 704 | if eq found 0 |
| 705 | ret 0 |
| 706 | end |
| 707 | ret 1 |
| 708 | end |
| 709 | |
| 710 | note Lookup a Named Component node by its final 4-char ACPI segment (padded with '_'). |
| 711 | note Returns 1 if found (iort_qres[6]=node_off, [7]=stream_id, [1]=target_base), |
| 712 | note 0 if not found, -1 if malformed, -2 if duplicate named component matches. |
| 713 | fn iort_named_route buf view uchar * want view uchar * to int |
| 714 | put iort_qres 6 0 |
| 715 | put iort_qres 7 0 |
| 716 | put iort_qres 1 0 |
| 717 | tl = iort_check_hdr buf |
| 718 | if lt tl 0 |
| 719 | ret -1 |
| 720 | end |
| 721 | w4 = new uchar 4 0 |
| 722 | if ne iort_last_seg4 want 0 len want w4 0 |
| 723 | ret -1 |
| 724 | end |
| 725 | for k ulong 4 |
| 726 | put iort_tmp_rb k ulong get w4 k |
| 727 | end |
| 728 | tlen = ulong tl |
| 729 | ncnt = ulong iort_rd32 buf 36 |
| 730 | noff = ulong iort_rd32 buf 40 |
| 731 | ret iort_named_scan buf tlen ncnt noff |
| 732 | end |