Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ jobs:
CLOUDTPU_MOCK=1 "$LLAMA_BUILD/bin/test-backend-ops" test -b CLOUDTPU -o ADD
CLOUDTPU_MOCK=1 "$LLAMA_BUILD/bin/test-backend-ops" test -b CLOUDTPU -o RMS_NORM
CLOUDTPU_MOCK=1 "$LLAMA_BUILD/bin/test-backend-ops" test -b CLOUDTPU -o SCALE
for bad_spmd in 1x 9 17; do
if CLOUDTPU_MOCK=1 CLOUDTPU_MOCK_PROFILE=tpu7x-4t CLOUDTPU_MOCK_DEVICES=8 CLOUDTPU_SPMD_CHIPS="$bad_spmd" "$LLAMA_BUILD/bin/test-backend-ops" test -b CLOUDTPU -o ADD; then
echo "ERROR: test-backend-ops unexpectedly accepted CLOUDTPU_SPMD_CHIPS=$bad_spmd" >&2
exit 1
fi
done

- name: Build & test libopenbuenos (Make)
run: |
Expand Down Expand Up @@ -109,6 +115,8 @@ jobs:
python3 ops/tpu-cdi/freetpu_cdi.py --help >/dev/null
python3 ops/tpu-exporter/test_tpu_exporter.py -v
python3 ops/tpu-cdi/test_freetpu_cdi.py -v
for s in tools/ironwood-day1/*.sh; do bash -n "$s"; done
python3 tools/ironwood-day1/test_ironwood_day1.py -v

- name: Run FreeTPU Router self-tests
run: |
Expand Down
9 changes: 5 additions & 4 deletions docs/cloud-tpu/ironwood-tpu7x-day1-runbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

**Offline Host Mock profiles (local dev/testing only, not live GCP VM shapes):**
- `tpu7x-1t` (`CLOUDTPU_MOCK_PROFILE=tpu7x-1t`, `OPENTPU_MOCK_TOPOLOGY=tpu7x-1t`): `2`-chiplet single-package mock (`192 GiB`, D2D pair `(0, 1)`)
- `tpu7x-4t` (`CLOUDTPU_MOCK_PROFILE=tpu7x-4t`, `OPENTPU_MOCK_TOPOLOGY=tpu7x-4t`): `8`-chiplet quad-package mock (`768 GiB`)
- `tpu7x-4t` / `tpu7x-4t-single` (`OPENTPU_MOCK_TOPOLOGY=tpu7x-4t` / `tpu7x-4t-single`): `8`-chiplet quad-package mock (`4` PCI functions × `2` chiplets = `8` logical devices, `768 GiB`)
- `tpu7x-4t-2fn` (`OPENTPU_MOCK_TOPOLOGY=tpu7x-4t-2fn`): `8`-chiplet `2`-function-per-package quad-package mock (`8` PCI functions × `1` chiplet = `8` logical devices, `768 GiB`)

**Goal:** Zero wasted DWS Calendar Mode clock minutes during the 24-hour evaluation window by running four reproducible, non-destructive/benchmark scripts under [`tools/ironwood-day1/`](../../tools/ironwood-day1/) (`00` -> `01` -> `02` -> `03`).

Expand Down Expand Up @@ -46,12 +47,12 @@ On VMs without JAX installed (or running Python 3.10 where `pip install libtpu`
```

Record from `runs/ironwood-day1/00_pci_vfio_survey.txt`:
- PCI Vendor/Device ID (`0x1ae0:0x0076` guest VFIO ID / `0x0075` or observed Silicon DID) and physical PCI function count (`4` on `tpu7x-standard-4t`).
- PCI Vendor/Device ID (provisional `0x1ae0:0x0075` / `0x0076` candidates pending live Day-1 `lspci -nn` capture), verified physical package count (`4` on `tpu7x-standard-4t`), and PCI function count (`4` in `1-fn` dual-chiplet mode or `8` across `4` physical packages in `2-fn` mode).
- `BAR0`, `BAR2`, `BAR4` window sizes and PCIe **Resizable BAR** (`0x0015`) capability fields (`current size:` and `supported:` lines).
- `MSI-X` vector count, `/dev/iommu` (`iommufd`) availability, and `/dev/vfio/*` group viability.
- Confirm physical-to-logical mapping in PJRT (native C++ `GetPjrtApi` probe with automatic `jax.local_devices(backend="tpu")` fallback preserving any native failure reason in the log):
- `tpu7x-standard-4t` -> `8` logical `PJRT_Device*` in serpentine 2×2 Hamiltonian package walk with consecutive D2D pairs `(0,1), (2,3), (4,5), (6,7)` (each package pair sharing `coords` with `core_on_chip = 0, 1`).
- Confirm `STATUS: PJRT_TOPOLOGY_CAPTURED` (the script flags `WARN: TOPOLOGY_COUNT_MISMATCH` / `STATUS: PJRT_TOPOLOGY_MISMATCH` whenever live counts disagree with the expected `4` packages / `8` logical devices).
- Confirm `STATUS: PJRT_TOPOLOGY_CAPTURED` (the script compares verified physical packages `pci_tpu_packages` and live PJRT devices against `4` packages / `8` logical devices, reporting `pci_tpu_functions` separately so both `4`-function `1-fn` and `8`-function `2-fn` layouts validate cleanly).

---

Expand Down Expand Up @@ -102,7 +103,7 @@ What this captures:

Wait for:
- `ot_chip_geometry_for_device_id()` & Resizable BAR clamp verification passing (`ALL_HOST_TESTS_PASSED`).
- Live `/dev/vfio/*` probe confirming **`MAPS_LIBTPU=0` and `MAPS_LIBPYTHON=0`** (`CLEANROOM_MAPS_STATUS=PASS`) and either `LIVE_VFIO_PROBE_STATUS=PASS` on validated `0x0063` or explicit pre-reset refusal (`Refusing to reset/program unsupported VFIO PCI device` -> `LIVE_VFIO_PROBE_STATUS=EXPECTED_UNVALIDATED_DEVICE_REFUSAL`) on unvalidated PCI DIDs until `0x0076` / `0x0075` (or observed DID) doorbell registers are documented cleanly.
- Live `/dev/vfio/*` probe confirming **`MAPS_LIBTPU=0` and `MAPS_LIBPYTHON=0`** (`CLEANROOM_MAPS_STATUS=PASS`) and either `LIVE_VFIO_PROBE_STATUS=PASS` on validated `0x0063` or explicit pre-open/pre-reset refusal (`Unsupported TPU PCI device` or `Refusing to reset/program unsupported VFIO PCI device` -> `LIVE_VFIO_PROBE_STATUS=EXPECTED_UNVALIDATED_DEVICE_REFUSAL`) on unvalidated PCI DIDs until `0x0076` / `0x0075` (or observed DID) doorbell registers are documented cleanly.

---

Expand Down
10 changes: 5 additions & 5 deletions docs/cloud-tpu/ops-container-telemetry.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Cloud TPU Ops Plumbing: Container CDI & Prometheus Telemetry

FreeTPU Pillar 6 brings GPU-grade operational ergonomics (`dcgm-exporter` + `nvidia-container-toolkit` + Kubernetes device plumbing) to VFIO-backed Google Cloud TPU VMs (`v5e`, `v5p`, `v6e` Trillium) using zero-dependency Python tooling over standard Linux sysfs (`vendor=0x1ae0`) and VFIO (`/dev/vfio/*`).
FreeTPU Pillar 6 brings GPU-grade operational ergonomics (`dcgm-exporter` + `nvidia-container-toolkit` + Kubernetes device plumbing) to VFIO-backed Google Cloud TPU VMs (`v5e`, `v5p`, `v6e` Trillium, and provisional `tpu7x` Ironwood `0x0075`/`0x0076` candidates) using zero-dependency Python tooling over standard Linux sysfs (`vendor=0x1ae0`) and VFIO (`/dev/vfio/*`).

---

Expand All @@ -12,9 +12,9 @@ FreeTPU Pillar 6 brings GPU-grade operational ergonomics (`dcgm-exporter` + `nvi
| `nvidia-ctk cdi generate` | `freetpu-cdi generate` (CNCF CDI `v0.6.0` default, `--cdi-version 1.1.0` opt-in) | [`ops/tpu-cdi/freetpu_cdi.py`](../../ops/tpu-cdi/freetpu_cdi.py) |
| `k8s-device-plugin` / GPU Operator | K8s DaemonSet + DRA / CDI Pod Manifests | [`ops/tpu-cdi/manifests/`](../../ops/tpu-cdi/manifests/k8s-tpu-cdi-daemonset.yaml) |

VFIO-backed Cloud TPU VMs (`v5e`, `v5p`, `v6e` Trillium) expose each TPU chip as a PCI function (`vendor=0x1ae0`) bound to the standard Linux `vfio-pci` kernel driver, with per-IOMMU-group character nodes at `/dev/vfio/<group>` and the shared VFIO container at `/dev/vfio/vfio`.
VFIO-backed Cloud TPU VMs (`v5e`, `v5p`, `v6e` Trillium, and provisional `tpu7x` Ironwood `0x0075`/`0x0076`) expose each TPU chip (or chiplet pair in 2-fn layouts) as PCI function(s) (`vendor=0x1ae0`) bound to the standard Linux `vfio-pci` kernel driver, with per-IOMMU-group character nodes at `/dev/vfio/<group>` and the shared VFIO container at `/dev/vfio/vfio`.

> **Cloud TPU v2 / v3 / v4 (`/dev/accel*`) Scope Note:** Google Cloud TPU `v2`, `v3`, and `v4` hosts use Google's `/dev/accel*` character device interface rather than `vfio-pci` + `/dev/vfio/*`. `tpu-exporter` still recognizes `v2/v3` and `v4` PCI IDs for `freetpu_tpu_info` inventory discovery, but `freetpu-cdi` passthrough, VFIO readiness (`freetpu_tpu_health_status`), and `/dev/vfio/*` PID tracking are scoped strictly to VFIO-backed hosts (`v5e`, `v5p`, `v6e` Trillium).
> **Cloud TPU v2 / v3 / v4 (`/dev/accel*`) Scope Note:** Google Cloud TPU `v2`, `v3`, and `v4` hosts use Google's `/dev/accel*` character device interface rather than `vfio-pci` + `/dev/vfio/*`. `tpu-exporter` still recognizes `v2/v3` and `v4` PCI IDs for `freetpu_tpu_info` inventory discovery, but `freetpu-cdi` passthrough, VFIO readiness (`freetpu_tpu_health_status`), and `/dev/vfio/*` PID tracking are scoped strictly to VFIO-backed hosts (`v5e`, `v5p`, `v6e` Trillium, and provisional `tpu7x` Ironwood `0x0075`/`0x0076` pending live Day-1 confirmation).

### Supported Google TPU PCI Device IDs (`vendor=0x1ae0`)

Expand All @@ -27,8 +27,8 @@ VFIO-backed Cloud TPU VMs (`v5e`, `v5p`, `v6e` Trillium) expose each TPU chip as
| `0x0063` | `v5e` | TPU v5e | 16 GiB | `vfio-pci` (`/dev/vfio/*`, full exporter + CDI) |
| `0x006e` | `v6e-pf` | TPU v6e (PF) | 32 GiB | `vfio-pci` (`/dev/vfio/*`, sysfs/VFIO inventory + CDI; upstream `tpu-info` CLI discovery omits `0x006e`, while `tpu_exporter.py` maps `0x006e` to `TpuChip.V6E` and queries a reachable `libtpu` endpoint via direct gRPC or `tpu_info.metrics` fallback) |
| `0x006f` | `v6e` | TPU v6e (Trillium) | 32 GiB | `vfio-pci` (`/dev/vfio/*`, full exporter + CDI) |
| `0x0075` | `tpu7x` | TPU tpu7x (Ironwood, pending live hardware confirmation `#21`) | 192 GiB | `vfio-pci` (`/dev/vfio/*` or `iommufd`, pending live HW confirmation) |
| `0x0076` | `tpu7x` | TPU tpu7x (Ironwood) | 192 GiB | `vfio-pci` (`/dev/vfio/*` or `iommufd`, matches upstream `tpu-info` Ironwood ID) |
| `0x0075` | `tpu7x` | TPU tpu7x (Ironwood, pending live hardware confirmation `#21`) | 192 GiB | `vfio-pci` (`/dev/vfio/*` or `iommufd`, provisional candidate pending live Day-1 `lspci -nn`) |
| `0x0076` | `tpu7x` | TPU tpu7x (Ironwood, pending live hardware confirmation `#21`) | 192 GiB | `vfio-pci` (`/dev/vfio/*` or `iommufd`, provisional upstream `tpu-info` Ironwood ID pending live Day-1 `lspci -nn`) |

> **TPU v6e PF (`0x006e`) note:** Upstream `tpu-info` CLI discovery recognizes the v6e virtual function (`0x006f`) and omits the physical function (`0x006e`), exiting before querying `libtpu` if only `0x006e` is present. On PF-only `0x006e` hosts, `tpu_exporter.py` maps `0x006e` directly to `TpuChip.V6E` and falls back to the Python `tpu_info.metrics` gRPC client against `--runtime-metrics-addr` (default `127.0.0.1:8431`) when the `tpu-info` Python package is installed, so live HBM and duty-cycle telemetry works on PF-only hosts without reconfiguring PCI devices (while PCI/VFIO inventory, readiness, active PIDs, and `freetpu-cdi` work without `tpu-info`).

Expand Down
2 changes: 1 addition & 1 deletion libopentpu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ Outputs in `libopentpu/build/host/`:
- **M3 (Self-Contained Bare-Metal `128x128` BF16 MXU Matmul over `/dev/vfio/0`)**: Complete clean-room 120-bundle (`7,680 B`) direct-in-IMEM resident boot/poller + 58-bundle `512`-bit VLIW MXU matmul loop (`ot::emit_v5e_resident_matmul_imem()` in `src/ot_vliw.h`), direct Host -> IMEM DMA (`op=0x04`), core start (`op=0x03 d[8]=0x121`), slot-0 `OtV5eLaunchDesc` doorbell (`0x3fff00000`), and transparent `{1,0:T(2,128)(2,1)}` HBM layout handling in `ot_device.cpp` + `ot_pjrt.cpp`. See [imem_boot_stub_spec.md](../docs/cloud-tpu-vfio/imem_boot_stub_spec.md).
- **M4 (`GetPjrtApi()` `0.116`)**: Verified via `dlopen("./build/host/libopentpu.so")` -> `GetPjrtApi()` over live `/dev/vfio/0` (`platform=tpu`, `devices=1`, `PJRT_Client_BufferFromHostBuffer` -> `PJRT_Client_Compile` -> `PJRT_LoadedExecutable_Execute` -> `PJRT_Buffer_ToHostBuffer`, `MAPS_LIBTPU=0`, `MAPS_LIBPYTHON=0`). Because `libopentpu` is intentionally zero-dependency (no LLVM/MLIR/StableHLO runtime linked), `PJRT_Client_Compile` lowers single-op **textual** StableHLO `bf16` `dot_general` (`format="mlir"`) for both live VFIO (`128x128x128`) and mock clients, and returns `PJRT_Error_Code_UNIMPLEMENTED` for binary MLIR bytecode (`ML\xefR`) pending a zero-dependency bytecode decoder.
- **M5 (`FreeTile`-Aligned Multi-Tile BF16 & Bare-Metal `Q8_0`/`Q4_0` Quantized GEMM)**: Added C11/C++17 `FreeTile` Backend B descriptor and VMEM double-buffer planner (`include/opentpu_freetile.hpp`, `ot_matmul_bf16_ex`, `ot_matmul_quant`), bare-metal IMEM multi-tile `BF16` GEMM with ping-pong DMA + FP32 accumulation (`128x512x512`, `256x512x256`, `128x4096x4096`), and bare-metal VPU/MXU `Q8_0`/`Q4_0` + per-channel `INT8`/`INT4` dequant+matmul (`0` mismatches vs FP32 reference on live `v5e-1`, `MAPS_LIBTPU=0`, `MAPS_LIBPYTHON=0`). Run `./build/host/opentpu_matmul_proof --mock --multitile --quant` offline or omit `--mock` on live `/dev/vfio/0`. See [freetile_multitile_quant_spec.md](../docs/cloud-tpu-vfio/freetile_multitile_quant_spec.md).
- **Multi-Gen HAL & Ironwood (`tpu7x`) Prep**: `ot_chip_geometry_for_device_id()` / `ot_device_get_geometry()` parameterize MXU tiles (`128x128` for `0x0063` v5e, `2x 256x256` for `0x006e`/`0x006f` v6e, and `0x0076`/`0x0075` Ironwood `tpu7x` with `(4,1)` FP8 inner tile, `64 MiB` VMEM/chiplet, and dual-chiplet `96 GiB/chiplet`), `clamp_bar_mmap_len()` / `parse_pcie_rebar_cap()` guard 64-bit Resizable BAR mappings, and `GetPjrtApi()` enumerates dual-chiplet `coords` + `core_on_chip` attributes (`OPENTPU_MOCK_TOPOLOGY=tpu7x-1t` / `tpu7x-4t`). See [`docs/cloud-tpu/ironwood-tpu7x-day1-runbook.md`](../docs/cloud-tpu/ironwood-tpu7x-day1-runbook.md).
- **Multi-Gen HAL & Ironwood (`tpu7x`) Prep**: `ot_chip_geometry_for_device_id()` / `ot_device_get_geometry()` parameterize MXU tiles (`128x128` for `0x0063` v5e, `2x 256x256` for `0x006e`/`0x006f` v6e, and provisional `0x0075`/`0x0076` Ironwood `tpu7x` candidates pending live Day-1 `lspci -nn`, with `(4,1)` FP8 inner tile, `64 MiB` VMEM/chiplet, and dual-chiplet `96 GiB/chiplet`), `clamp_bar_mmap_len()` / `parse_pcie_rebar_cap()` guard 64-bit Resizable BAR mappings, and `GetPjrtApi()` enumerates dual-chiplet `coords` + `core_on_chip` attributes (`OPENTPU_MOCK_TOPOLOGY=tpu7x-1t` / `tpu7x-4t`). See [`docs/cloud-tpu/ironwood-tpu7x-day1-runbook.md`](../docs/cloud-tpu/ironwood-tpu7x-day1-runbook.md).
2 changes: 1 addition & 1 deletion libopentpu/include/opentpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ typedef enum ot_chip_gen {
OT_CHIP_UNKNOWN = 0,
OT_CHIP_V5E = 1, /* PCI 0x1ae0:0x0063 (128x128 MXU, 1 chiplet/package) */
OT_CHIP_V6E = 2, /* PCI 0x1ae0:0x006f (256x256 MXU, 1 chiplet/package) */
OT_CHIP_TPU7X = 3 /* PCI 0x1ae0:0x0076 / 0x0075 Ironwood (256x256 MXU, dual-chiplet D2D, 64 MiB VMEM/chiplet) */
OT_CHIP_TPU7X = 3 /* Provisional 0x1ae0:0x0075 / 0x0076 Ironwood candidates pending Day-1 lspci -nn (256x256 MXU, dual-chiplet D2D, 64 MiB VMEM/chiplet) */
} ot_chip_gen_t;

typedef struct ot_device_geometry {
Expand Down
36 changes: 32 additions & 4 deletions libopentpu/src/ot_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,27 @@ ot_status_t ot_context_create(const ot_config_t* config, ot_context_t** out_ctx)
delete ctx;
return OT_ERR_DEVICE;
}
// Anchor cfg.bdf to context-owned info.bdf so callers passing temporary/stack BDF buffers
// (e.g. multi-device PJRT mock enumeration) never leave ctx->cfg.bdf dangling.
ctx->cfg.bdf = (ctx->info.bdf[0] != '\0') ? ctx->info.bdf : nullptr;

if (ctx->info.chip_gen == OT_CHIP_TPU7X) {
const size_t bdf_len = std::strlen(ctx->info.bdf);
const bool non_zero_fn = bdf_len >= 2 &&
ctx->info.bdf[bdf_len - 2] == '.' &&
ctx->info.bdf[bdf_len - 1] != '0';
if (ctx->info.device_id == 0x0076u || non_zero_fn) {
ctx->logical_chiplets_per_pci_func_override = 1u;
} else if (cfg.mock_mode) {
ot::MockProfileSpec mock_spec = {};
std::string mock_err;
if (ot::resolve_mock_profile(&mock_spec, &mock_err) && mock_spec.is_tpu7x_two_fn) {
ctx->logical_chiplets_per_pci_func_override = 1u;
}
} else if (ot::probe_linux_tpu7x_two_fn_layout()) {
ctx->logical_chiplets_per_pci_func_override = 1u;
}
}

if (!cfg.mock_mode) {
ctx->info.is_prebooted = 0u;
Expand Down Expand Up @@ -572,6 +593,10 @@ ot_status_t ot_chip_geometry_for_device_id(uint16_t pci_device_id, ot_device_geo
ot_status_t ot_device_get_geometry(const ot_context_t* ctx, ot_device_geometry_t* out_geom) {
if (!ctx || !out_geom) return OT_ERR_INVALID_ARG;
if (ot::chip_geometry_for_device_id(ctx->info.device_id, out_geom)) {
if (ctx->info.chip_gen == OT_CHIP_TPU7X &&
ctx->logical_chiplets_per_pci_func_override > 0) {
out_geom->logical_chiplets_per_pci_func = ctx->logical_chiplets_per_pci_func_override;
}
return OT_OK;
}
std::memset(out_geom, 0, sizeof(*out_geom));
Expand All @@ -581,7 +606,10 @@ ot_status_t ot_device_get_geometry(const ot_context_t* ctx, ot_device_geometry_t
out_geom->vmem_bytes_per_logical_chiplet = ctx->info.vmem_bytes;
out_geom->smem_bytes_per_logical_chiplet = ctx->info.smem_bytes;
out_geom->num_mxus_per_logical_chiplet = ctx->info.num_mxus;
out_geom->logical_chiplets_per_pci_func = (ctx->info.chip_gen == OT_CHIP_TPU7X) ? 2u : 1u;
out_geom->logical_chiplets_per_pci_func =
ctx->logical_chiplets_per_pci_func_override > 0
? ctx->logical_chiplets_per_pci_func_override
: ((ctx->info.chip_gen == OT_CHIP_TPU7X) ? 2u : 1u);
return OT_OK;
}

Expand Down Expand Up @@ -698,14 +726,14 @@ bool chip_geometry_for_device_id(uint16_t device_id, ot_device_geometry_t* out)
out->vmem_bytes_per_logical_chiplet = 128ULL << 20;
out->smem_bytes_per_logical_chiplet = 1ULL << 20;
return true;
case 0x0075u: // Ironwood tpu7x (dual-chiplet, 256x256 MXU + (4,1) FP8 inner tile, 96 GiB HBM, 64 MiB VMEM/chiplet)
case 0x0076u: // Documented Cloud TPU v7x (Ironwood) guest VM VFIO PCI Device ID
case 0x0075u: // Provisional Ironwood tpu7x ID pending Day-1 lspci (1-fn dual-chiplet default: 2 chiplets/fn, 96 GiB/chiplet, 64 MiB VMEM/chiplet)
case 0x0076u: // Provisional Ironwood tpu7x ID pending Day-1 lspci (2-fn candidate: 1 chiplet/fn, 96 GiB/chiplet)
out->chip_gen = OT_CHIP_TPU7X;
out->mxu_rows = 256u;
out->mxu_cols = 256u;
out->fp8_inner_rows = 4u;
out->fp8_inner_cols = 1u;
out->logical_chiplets_per_pci_func = 2u;
out->logical_chiplets_per_pci_func = (device_id == 0x0076u) ? 1u : 2u;
out->num_mxus_per_logical_chiplet = 2u;
out->supports_native_fp8 = 1u;
out->hbm_bytes_per_logical_chiplet = 96ULL << 30;
Expand Down
29 changes: 29 additions & 0 deletions libopentpu/src/ot_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ struct RebarCapabilityInfo {
// Parse a 0x1234 hex sysfs attribute safely (no exceptions).
bool parse_hex_u64(const std::string& text, uint64_t* out);

// Parse an unsigned 16-bit non-zero PCI device ID (no sign/overflow/trailing junk).
bool parse_mock_device_id(const char* raw, uint16_t* out);

struct MockProfileSpec {
uint16_t device_id = 0x0063u;
ot_chip_gen_t chip_gen = OT_CHIP_V5E;
size_t pci_funcs = 1;
size_t chiplets_per_func = 1;
bool is_tpu7x_two_fn = false;
bool has_named_topo = false;
bool single_fn_only = false;
};

// Shared validation/resolution of OPENTPU_MOCK_TOPOLOGY / _DEVICE_ID / _CHIP
// used identically by both HAL ot_context_create() and PJRT_Client_Create().
bool resolve_mock_profile(MockProfileSpec* out, std::string* err);

// Inspect PCI sysfs directory for 2-fn Ironwood evidence (0x0076, non-.0 functions,
// >4 TPU7x functions, multifunction slot siblings, or same-domain shared chip_id).
bool probe_tpu7x_two_fn_layout_from_sysfs(const char* sysfs_pci_root);
bool probe_linux_tpu7x_two_fn_layout();

// HAL geometry lookup across Google Cloud TPU generations (0x0063 v5e, 0x006e/0x006f v6e,
// 0x0075/0x0076 Ironwood tpu7x dual-chiplet). Returns false on unknown device_id.
bool chip_geometry_for_device_id(uint16_t device_id, ot_device_geometry_t* out);
Expand All @@ -56,6 +78,12 @@ bool parse_pcie_rebar_cap(const uint8_t* cfg_bytes, size_t cfg_len, RebarCapabil
// Enumerate all visible 0x1ae0 Google Cloud TPU PCI functions on Linux sysfs (sorted by BDF).
bool enumerate_linux_tpus(std::vector<PciDiscoveryEntry>* out, std::string* err);

// Inspect a PCI sysfs directory for a supported vfio-pci Google Cloud TPU (0x1ae0:0x0063,
// class=0xff0000), failing closed with an explicit pre-open refusal if preferred_bdf selects
// an unvalidated 0x1ae0 vfio-pci accelerator device or class.
bool discover_vfio_tpu_in_sysfs(const char* sysfs_pci_root, const char* preferred_bdf,
PciDiscoveryEntry* out, std::string* err);

// Simple monotonic IOVA bump/free-list allocator over valid IOMMU ranges.
struct IovaBlock {
uint64_t iova = 0;
Expand Down Expand Up @@ -238,6 +266,7 @@ struct ot_context {
std::mutex mu;
ot_config_t cfg{};
ot_device_info_t info{};
uint32_t logical_chiplets_per_pci_func_override = 0;
ot::VfioTransport transport{};
ot::Bar4Descriptor bar4_desc{};
std::vector<ot_buffer_t*> live_buffers;
Expand Down
Loading
Loading