physics-mv
FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.
Brought by drakestapleton
physics-mv / m15 / physics_accel_native.c
| 1 | #define _POSIX_C_SOURCE 200809L |
| 2 | #include "physics_accel_native.h" |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
| 6 | #include <unistd.h> |
| 7 | #include <stdatomic.h> |
| 8 | |
| 9 | /* ========================================================================= */ |
| 10 | /* 1. HARDWARE OBSERVATION: Live Host Device Probe Record */ |
| 11 | /* ========================================================================= */ |
| 12 | |
| 13 | int physics_accel_native_probe(Smmuv3NativeProbe *out_probe) { |
| 14 | if (!out_probe) return -1; |
| 15 | memset(out_probe, 0, sizeof(*out_probe)); |
| 16 | |
| 17 | const char *pci_path = "/sys/bus/pci/devices/000f:01:00.0"; |
| 18 | if (access(pci_path, F_OK) != 0) { |
| 19 | return -2; /* PCI device does not exist on this machine */ |
| 20 | } |
| 21 | out_probe->present = true; |
| 22 | |
| 23 | /* 1. Read Vendor ID */ |
| 24 | char path[256]; |
| 25 | snprintf(path, sizeof(path), "%s/vendor", pci_path); |
| 26 | FILE *f = fopen(path, "r"); |
| 27 | if (!f) return -3; |
| 28 | unsigned int vendor = 0; |
| 29 | if (fscanf(f, "0x%x", &vendor) != 1) { fclose(f); return -3; } |
| 30 | fclose(f); |
| 31 | out_probe->vendor_id = (uint16_t)vendor; |
| 32 | |
| 33 | /* 2. Read Device ID */ |
| 34 | snprintf(path, sizeof(path), "%s/device", pci_path); |
| 35 | f = fopen(path, "r"); |
| 36 | if (!f) return -4; |
| 37 | unsigned int device = 0; |
| 38 | if (fscanf(f, "0x%x", &device) != 1) { fclose(f); return -4; } |
| 39 | fclose(f); |
| 40 | out_probe->device_id = (uint16_t)device; |
| 41 | |
| 42 | /* 3. Read IOMMU Group */ |
| 43 | snprintf(path, sizeof(path), "%s/iommu_group", pci_path); |
| 44 | char link_target[256]; |
| 45 | ssize_t len = readlink(path, link_target, sizeof(link_target) - 1); |
| 46 | if (len > 0) { |
| 47 | link_target[len] = '\0'; |
| 48 | char *slash = strrchr(link_target, '/'); |
| 49 | if (slash) { |
| 50 | out_probe->iommu_group = (uint32_t)strtoul(slash + 1, NULL, 10); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /* 4. Verify SMMUv3 Presence */ |
| 55 | const char *smmu_path = "/sys/class/iommu/smmu3.0x0000000013000000"; |
| 56 | if (access(smmu_path, F_OK) == 0) { |
| 57 | out_probe->smmu_base = SMMU_V3_1_AUTO_BASE; |
| 58 | strncpy(out_probe->smmu_name, "arm-smmu-v3.1.auto", sizeof(out_probe->smmu_name) - 1); |
| 59 | out_probe->smmu_matched = true; |
| 60 | } |
| 61 | |
| 62 | /* 5. Read BAR0 Aperture from kernel-exported resource file */ |
| 63 | snprintf(path, sizeof(path), "%s/resource", pci_path); |
| 64 | f = fopen(path, "r"); |
| 65 | if (f) { |
| 66 | unsigned long long start = 0, end = 0, flags = 0; |
| 67 | if (fscanf(f, "0x%llx 0x%llx 0x%llx", &start, &end, &flags) == 3) { |
| 68 | out_probe->bar0_base = (uint64_t)start; |
| 69 | out_probe->bar0_size = (uint64_t)(end - start + 1); |
| 70 | } |
| 71 | fclose(f); |
| 72 | } |
| 73 | |
| 74 | /* 6. Populate verified identity fields */ |
| 75 | strncpy(out_probe->pci_bdf, GB10_PCI_BDF_STRING, sizeof(out_probe->pci_bdf) - 1); |
| 76 | strncpy(out_probe->device_name, "NVIDIA Blackwell GB10", sizeof(out_probe->device_name) - 1); |
| 77 | out_probe->stream_id = GB10_STREAM_ID; /* ACPI IORT Node 29, Segment 15, BDF 01:00.0 */ |
| 78 | out_probe->coherent_unified_dram = true; |
| 79 | |
| 80 | if (out_probe->vendor_id == GB10_PCI_VENDOR_ID && |
| 81 | out_probe->device_id == GB10_PCI_DEVICE_ID) { |
| 82 | out_probe->device_matched = true; |
| 83 | } |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | /* ========================================================================= */ |
| 89 | /* 2. HARDWARE OWNERSHIP BOUNDARIES: Explicit Subsystem Governance */ |
| 90 | /* ========================================================================= */ |
| 91 | |
| 92 | static const HardwareOwnershipBoundary g_ownership_boundaries[] = { |
| 93 | { |
| 94 | .subsystem = "SMMUv3 Hardware Programming", |
| 95 | .current_owner = "Linux kernel (arm_smmu_v3) / active device stack", |
| 96 | .physics_status = "Stage 1 policy model implemented; native hardware takeover not yet performed", |
| 97 | .transfer_plan = "Future milestone / bare-metal isolated environment" |
| 98 | }, |
| 99 | { |
| 100 | .subsystem = "Device Address / IOVA Mappings", |
| 101 | .current_owner = "Linux kernel (dma-iommu / uvm)", |
| 102 | .physics_status = "Stage 1 window translation & permission bounds implemented; hardware page-table walk not taken over", |
| 103 | .transfer_plan = "Future milestone / bare-metal isolated environment" |
| 104 | }, |
| 105 | { |
| 106 | .subsystem = "GPU BAR Mappings", |
| 107 | .current_owner = "Linux kernel PCI subsystem & nvidia.ko (0x24000000-0x27ffffff)", |
| 108 | .physics_status = "Aperture bounds observed; direct userspace mmap restricted by CONFIG_IO_STRICT_DEVMEM", |
| 109 | .transfer_plan = "M16 (Blackwell native submission characterization)" |
| 110 | }, |
| 111 | { |
| 112 | .subsystem = "GPU Submission Queues", |
| 113 | .current_owner = "nvidia.ko / user channel pushbuffers", |
| 114 | .physics_status = "Circular ring queue authority model implemented; native Blackwell channel format unknown", |
| 115 | .transfer_plan = "M16 (empirical discovery of command packet & queue structure)" |
| 116 | }, |
| 117 | { |
| 118 | .subsystem = "GPU Completion Handling", |
| 119 | .current_owner = "nvidia.ko interrupt handler & semaphores", |
| 120 | .physics_status = "Completion receipt model implemented; native Blackwell completion protocol unknown", |
| 121 | .transfer_plan = "M16 (empirical discovery of completion signaling & fences)" |
| 122 | }, |
| 123 | { |
| 124 | .subsystem = "Device Reset", |
| 125 | .current_owner = "Linux kernel PCI core / GPU driver reset handler", |
| 126 | .physics_status = "Monotonic device state machine implemented; raw control register reset not executed to preserve running host", |
| 127 | .transfer_plan = "Future milestone / bare-metal isolated environment" |
| 128 | } |
| 129 | }; |
| 130 | |
| 131 | const HardwareOwnershipBoundary *physics_accel_get_hardware_ownership_boundaries(size_t *out_count) { |
| 132 | if (out_count) { |
| 133 | *out_count = sizeof(g_ownership_boundaries) / sizeof(g_ownership_boundaries[0]); |
| 134 | } |
| 135 | return g_ownership_boundaries; |
| 136 | } |
| 137 | |
| 138 | /* ========================================================================= */ |
| 139 | /* 3. HARDWARE BOUNDARY CHARACTERIZATION */ |
| 140 | /* ========================================================================= */ |
| 141 | |
| 142 | int physics_accel_native_verify_hardware_boundary(Smmuv3NativeProbe *out_probe) { |
| 143 | if (!out_probe) return -1; |
| 144 | |
| 145 | int rc = physics_accel_native_probe(out_probe); |
| 146 | if (rc != 0) return rc; |
| 147 | |
| 148 | /* Verify physical reality matches declared constants */ |
| 149 | if (!out_probe->present) return -2; |
| 150 | if (out_probe->vendor_id != GB10_PCI_VENDOR_ID) return -3; |
| 151 | if (out_probe->device_id != GB10_PCI_DEVICE_ID) return -4; |
| 152 | if (out_probe->iommu_group != 20) return -5; |
| 153 | if (out_probe->smmu_base != SMMU_V3_1_AUTO_BASE) return -6; |
| 154 | if (out_probe->stream_id != GB10_STREAM_ID) return -7; |
| 155 | if (out_probe->bar0_base != GB10_BAR0_BASE) return -8; |
| 156 | if (out_probe->bar0_size != GB10_BAR0_SIZE) return -9; |
| 157 | if (!out_probe->coherent_unified_dram) return -10; |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | int physics_accel_native_commit_boundary_receipt(PhysicsAcceleratorLink *link, |
| 163 | const Smmuv3NativeProbe *probe, |
| 164 | EffectReceipt *out_receipt) { |
| 165 | if (!link || !probe || !out_receipt) return -1; |
| 166 | |
| 167 | EffectIntent boundary_intent = { |
| 168 | .version = 1, |
| 169 | .length = sizeof(EffectIntent), |
| 170 | .request_id = 0x000000000000000F, |
| 171 | .principal_id = 1, |
| 172 | .capability_slot = 1, |
| 173 | .capability_generation = 1, |
| 174 | .resource_type = RES_ACCELERATOR, |
| 175 | .operation = ACCEL_OP_MAP_DMA, |
| 176 | .target_base = probe->bar0_base, |
| 177 | .target_size = probe->bar0_size, |
| 178 | .param0 = probe->smmu_base |
| 179 | }; |
| 180 | |
| 181 | /* Bind observed hardware boundary parameters into EffectReceipt */ |
| 182 | physics_accel_commit_receipt_measured(link, |
| 183 | &boundary_intent, |
| 184 | DEC_ADMITTED, |
| 185 | 0, |
| 186 | probe->bar0_base, |
| 187 | probe->stream_id, |
| 188 | probe->smmu_base, |
| 189 | out_receipt); |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | /* ========================================================================= */ |
| 195 | /* 4. MODEL / DISCOVERY PLACEHOLDERS (Intentionally deferred to M16) */ |
| 196 | /* ========================================================================= */ |
| 197 | |
| 198 | int physics_accel_model_map_dma(const Smmuv3NativeProbe *probe, |
| 199 | uint64_t iova, |
| 200 | uint64_t size_bytes, |
| 201 | uint32_t perms, |
| 202 | DmaModelMapping *out_map) { |
| 203 | (void)probe; |
| 204 | if (!out_map || size_bytes == 0) return -1; |
| 205 | memset(out_map, 0, sizeof(*out_map)); |
| 206 | |
| 207 | void *buf = NULL; |
| 208 | if (posix_memalign(&buf, 4096, size_bytes) != 0 || !buf) { |
| 209 | return -2; |
| 210 | } |
| 211 | memset(buf, 0, size_bytes); |
| 212 | |
| 213 | out_map->iova = iova; |
| 214 | /* In software model simulation, map to a synthetic PA within DRAM envelope */ |
| 215 | out_map->phys_base = DGX_SPARK_DRAM_BASE + (iova & 0x0FFFFFFFULL); |
| 216 | out_map->size_bytes = size_bytes; |
| 217 | out_map->permissions = perms; |
| 218 | out_map->coherent = true; |
| 219 | out_map->vaddr = buf; |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | int physics_accel_model_unmap_dma(DmaModelMapping *map) { |
| 225 | if (!map) return -1; |
| 226 | if (map->vaddr) { |
| 227 | free(map->vaddr); |
| 228 | map->vaddr = NULL; |
| 229 | } |
| 230 | map->iova = 0; |
| 231 | map->phys_base = 0; |
| 232 | map->size_bytes = 0; |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | int physics_accel_model_ring_init(DmaModelMapping *map, |
| 237 | ModelCommandRing *out_ring) { |
| 238 | if (!map || !out_ring) return -1; |
| 239 | memset(out_ring, 0, sizeof(*out_ring)); |
| 240 | |
| 241 | out_ring->ring_phys_base = map->phys_base; |
| 242 | out_ring->ring_vaddr = (uint8_t *)map->vaddr; |
| 243 | out_ring->slot_count = 64; |
| 244 | out_ring->slot_size = 128; |
| 245 | out_ring->head_index = 0; |
| 246 | out_ring->tail_index = 0; |
| 247 | out_ring->active = true; |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | int physics_accel_model_ring_doorbell(uint64_t doorbell_phys_reg, uint32_t token) { |
| 253 | (void)doorbell_phys_reg; |
| 254 | (void)token; |
| 255 | /* CLASSIFICATION: MODEL / PLACEHOLDER |
| 256 | * In M15, we do NOT perform unverified MMIO doorbell writes to guessed |
| 257 | * registers. Empirical doorbell discovery is deferred to Milestone 16. */ |
| 258 | atomic_thread_fence(memory_order_seq_cst); |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | int physics_accel_model_observe_completion(ModelCommandRing *ring) { |
| 263 | if (!ring) return -1; |
| 264 | /* CLASSIFICATION: MODEL / PLACEHOLDER |
| 265 | * In M15, completion is a software ring update. Actual hardware completion |
| 266 | * signaling and fence discovery are deferred to Milestone 16. */ |
| 267 | atomic_thread_fence(memory_order_seq_cst); |
| 268 | ring->head_index = ring->tail_index; |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | int physics_accel_model_reset(Smmuv3NativeProbe *probe, |
| 273 | ModelCommandRing *ring, |
| 274 | DmaModelMapping *map) { |
| 275 | (void)probe; |
| 276 | if (ring) { |
| 277 | ring->active = false; |
| 278 | ring->head_index = 0; |
| 279 | ring->tail_index = 0; |
| 280 | } |
| 281 | if (map) { |
| 282 | physics_accel_model_unmap_dma(map); |
| 283 | } |
| 284 | atomic_thread_fence(memory_order_seq_cst); |
| 285 | return 0; |
| 286 | } |