Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/physics-mv/blob/main/m15/physics_accel.c # physics-mv FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `physics-mv / m15 / physics_accel.c` 457 lines ยท 18153 bytes Raw Blame History 1 #include "physics_accel.h" 2 #include 3 #include 4 5 /* Forward declaration from sha256_clean.c */ 6 void sha256_compute(const uint8_t *data, uint64_t len, uint8_t scratch_buf[128], uint32_t out_digest[8]); 7 8 /* Internal helper to compute SHA-256 and store canonical big-endian 32-byte digest */ 9 static void compute_sha256_be(const void *data, uint64_t len, uint8_t out[32]) { 10 uint8_t scratch[128] __attribute__((aligned(16))); 11 uint32_t st[8]; 12 sha256_compute((const uint8_t *)data, len, scratch, st); 13 for (int i = 0; i < 8; i++) { 14 out[4 * i + 0] = (uint8_t)(st[i] >> 24); 15 out[4 * i + 1] = (uint8_t)(st[i] >> 16); 16 out[4 * i + 2] = (uint8_t)(st[i] >> 8); 17 out[4 * i + 3] = (uint8_t)(st[i]); 18 } 19 } 20 21 int physics_accel_init(PhysicsAcceleratorLink *link, 22 uint64_t dram_base, 23 uint64_t dram_size, 24 uint64_t kernel_reserved_base, 25 uint64_t kernel_reserved_size) { 26 if (!link) return -1; 27 memset(link, 0, sizeof(*link)); 28 29 link->link_version = PHYSICS_ACCEL_LINK_VERSION; 30 link->device_state = ACCEL_STATE_PROBED; 31 link->stream_id = 0x0100; /* Observed hardware Stream ID for GB10 (000f:01:00.0) on DGX Spark */ 32 link->coherent_dram_base = dram_base; 33 link->coherent_dram_size = dram_size; 34 link->kernel_reserved_base = kernel_reserved_base; 35 link->kernel_reserved_size = kernel_reserved_size; 36 link->mmio_doorbell_base = 0x24000000; /* Hardware BAR0 base */ 37 link->mmio_doorbell_size = 0x04000000; /* 64 MiB */ 38 link->machine_generation = 1; 39 40 /* Initialize rolling cryptographic receipt chain with genesis zero */ 41 memset(link->last_receipt_digest, 0, 32); 42 return 0; 43 } 44 45 int physics_accel_configure_smmu(PhysicsAcceleratorLink *link, 46 const Smmuv3StreamConfig *config) { 47 if (!link || !config) return -1; 48 if (link->device_state != ACCEL_STATE_PROBED && 49 link->device_state != ACCEL_STATE_RECOVERED && 50 link->device_state != ACCEL_STATE_CONFIGURED) { 51 return -2; 52 } 53 54 /* Validate SMMU geometry */ 55 if (config->granule_size != 4096) return -3; /* Mandate 4 KiB granule */ 56 if (config->t0sz != 16 && config->t0sz != 24) return -4; /* 48-bit or 40-bit IOVA */ 57 58 link->smmu_config = *config; 59 link->stream_id = config->stream_id; 60 link->device_state = ACCEL_STATE_CONFIGURED; 61 return 0; 62 } 63 64 int physics_accel_validate_capability(const PhysicsAcceleratorLink *link, 65 const AcceleratorCapability *cap, 66 uint32_t required_op, 67 uint64_t target_base, 68 uint64_t target_size) { 69 if (!link || !cap) return DEC_REJECTED_STRUCTURAL; 70 if (cap->resource_type != RES_ACCELERATOR) return DEC_REJECTED_RESOURCE; 71 if (cap->revocation_state != 1) return DEC_REJECTED_REVOKED; 72 if ((cap->allowed_ops & required_op) == 0) return DEC_REJECTED_OPERATION; 73 74 /* Check bounds if applicable */ 75 if (target_size > 0) { 76 if (target_base < cap->iova_bound_base) return DEC_REJECTED_BOUNDS; 77 if ((target_base + target_size) > (cap->iova_bound_base + cap->iova_bound_size)) { 78 return DEC_REJECTED_BOUNDS; 79 } 80 } 81 82 return DEC_ADMITTED; 83 } 84 85 void physics_accel_commit_receipt_measured(PhysicsAcceleratorLink *link, 86 const EffectIntent *intent, 87 uint32_t decision, 88 uint32_t rejection_reason, 89 uint64_t actual_effect, 90 uint64_t output, 91 uint64_t measurement, 92 EffectReceipt *out_receipt) { 93 if (!out_receipt) return; 94 memset(out_receipt, 0, sizeof(*out_receipt)); 95 96 out_receipt->version = 1; 97 out_receipt->length = sizeof(EffectReceipt); 98 out_receipt->decision = decision; 99 out_receipt->rejection_reason = rejection_reason; 100 out_receipt->request_id = intent ? intent->request_id : 0; 101 102 if (intent) { 103 compute_sha256_be(intent, sizeof(EffectIntent), out_receipt->intent_digest); 104 out_receipt->capability_slot = intent->capability_slot; 105 out_receipt->capability_generation = intent->capability_generation; 106 } 107 108 out_receipt->actual_effect = actual_effect; 109 out_receipt->output = output; 110 out_receipt->machine_generation = link ? ++link->machine_generation : 0; 111 out_receipt->measurement = measurement; 112 113 if (link) { 114 memcpy(out_receipt->previous_receipt_digest, link->last_receipt_digest, 32); 115 } 116 117 /* Compute seal over bytes 0..127 + previous_receipt_digest */ 118 uint8_t hash_input[160]; 119 memcpy(hash_input, out_receipt, 128); 120 memcpy(hash_input + 128, out_receipt->previous_receipt_digest, 32); 121 compute_sha256_be(hash_input, 160, out_receipt->receipt_digest); 122 123 /* Update rolling chain in link */ 124 if (link) { 125 memcpy(link->last_receipt_digest, out_receipt->receipt_digest, 32); 126 } 127 } 128 129 void physics_accel_commit_receipt(PhysicsAcceleratorLink *link, 130 const EffectIntent *intent, 131 uint32_t decision, 132 uint32_t rejection_reason, 133 uint64_t actual_effect, 134 uint64_t output, 135 EffectReceipt *out_receipt) { 136 uint64_t gen = link ? (link->machine_generation + 1) : 0; 137 uint64_t synthetic_meas = 1000 + gen; 138 physics_accel_commit_receipt_measured(link, intent, decision, rejection_reason, 139 actual_effect, output, synthetic_meas, out_receipt); 140 } 141 142 int physics_accel_grant_dma_window(PhysicsAcceleratorLink *link, 143 const AcceleratorCapability *cap, 144 const DmaWindowDescriptor *window, 145 EffectReceipt *out_receipt) { 146 if (!link || !window) return -1; 147 148 EffectIntent intent; 149 memset(&intent, 0, sizeof(intent)); 150 intent.version = 1; 151 intent.length = sizeof(intent); 152 intent.request_id = 101; 153 intent.principal_id = cap ? cap->principal_id : 0; 154 intent.capability_slot = cap ? cap->slot : 0; 155 intent.capability_generation = cap ? cap->generation : 0; 156 intent.resource_type = RES_ACCELERATOR; 157 intent.operation = ACCEL_OP_MAP_DMA; 158 intent.target_base = window->iova_base; 159 intent.target_size = window->size_bytes; 160 intent.param0 = window->phys_base; 161 162 int val = physics_accel_validate_capability(link, cap, ACCEL_OP_MAP_DMA, 163 window->iova_base, window->size_bytes); 164 if (val != DEC_ADMITTED) { 165 physics_accel_commit_receipt(link, &intent, DEC_REJECTED, val, 0, 0, out_receipt); 166 return -1; 167 } 168 169 /* Verify device state */ 170 if (link->device_state != ACCEL_STATE_CONFIGURED && 171 link->device_state != ACCEL_STATE_ACTIVE) { 172 physics_accel_commit_receipt(link, &intent, DEC_REJECTED, DEC_REJECTED_STRUCTURAL, 0, 0, out_receipt); 173 return -2; 174 } 175 176 /* Verify physical memory bounds against DRAM envelope */ 177 if (window->phys_base < link->coherent_dram_base || 178 (window->phys_base + window->size_bytes) > (link->coherent_dram_base + link->coherent_dram_size)) { 179 physics_accel_commit_receipt(link, &intent, DEC_REJECTED, DEC_REJECTED_BOUNDS, 0, 0, out_receipt); 180 return -3; 181 } 182 183 /* Verify physical memory does NOT overlap protected Physics kernel memory */ 184 if (window->phys_base < (link->kernel_reserved_base + link->kernel_reserved_size) && 185 (window->phys_base + window->size_bytes) > link->kernel_reserved_base) { 186 physics_accel_commit_receipt(link, &intent, DEC_REJECTED, DEC_REJECTED_CONSTRAINT, 0, 0, out_receipt); 187 return -4; 188 } 189 190 /* Check window capacity */ 191 if (link->active_window_count >= PHYSICS_ACCEL_MAX_WINDOWS) { 192 physics_accel_commit_receipt(link, &intent, DEC_REJECTED, DEC_REJECTED_EXHAUSTED, 0, 0, out_receipt); 193 return -5; 194 } 195 196 /* Check for overlapping IOVA windows */ 197 for (uint32_t i = 0; i < link->active_window_count; i++) { 198 const DmaWindowDescriptor *w = &link->active_windows[i]; 199 if (window->iova_base < (w->iova_base + w->size_bytes) && 200 (window->iova_base + window->size_bytes) > w->iova_base) { 201 physics_accel_commit_receipt(link, &intent, DEC_REJECTED, DEC_REJECTED_REPLAY_CONFLICT, 0, 0, out_receipt); 202 return -6; 203 } 204 } 205 206 /* Register active window */ 207 DmaWindowDescriptor *new_win = &link->active_windows[link->active_window_count++]; 208 *new_win = *window; 209 new_win->window_id = link->active_window_count; 210 new_win->stream_id = link->stream_id; 211 212 physics_accel_commit_receipt(link, &intent, DEC_ADMITTED, 0, window->phys_base, window->size_bytes, out_receipt); 213 return 0; 214 } 215 216 int physics_accel_revoke_dma_window(PhysicsAcceleratorLink *link, 217 const AcceleratorCapability *cap, 218 uint64_t iova_base, 219 EffectReceipt *out_receipt) { 220 if (!link) return -1; 221 222 EffectIntent intent; 223 memset(&intent, 0, sizeof(intent)); 224 intent.version = 1; 225 intent.length = sizeof(intent); 226 intent.request_id = 102; 227 intent.principal_id = cap ? cap->principal_id : 0; 228 intent.capability_slot = cap ? cap->slot : 0; 229 intent.capability_generation = cap ? cap->generation : 0; 230 intent.resource_type = RES_ACCELERATOR; 231 intent.operation = ACCEL_OP_UNMAP_DMA; 232 intent.target_base = iova_base; 233 234 int val = physics_accel_validate_capability(link, cap, ACCEL_OP_UNMAP_DMA, 0, 0); 235 if (val != DEC_ADMITTED) { 236 physics_accel_commit_receipt(link, &intent, DEC_REJECTED, val, 0, 0, out_receipt); 237 return -1; 238 } 239 240 int found_idx = -1; 241 for (uint32_t i = 0; i < link->active_window_count; i++) { 242 if (link->active_windows[i].iova_base == iova_base) { 243 found_idx = (int)i; 244 break; 245 } 246 } 247 248 if (found_idx < 0) { 249 physics_accel_commit_receipt(link, &intent, DEC_REJECTED, DEC_REJECTED_BOUNDS, 0, 0, out_receipt); 250 return -2; 251 } 252 253 /* Shift remaining windows */ 254 for (uint32_t i = (uint32_t)found_idx; i + 1 < link->active_window_count; i++) { 255 link->active_windows[i] = link->active_windows[i + 1]; 256 } 257 link->active_window_count--; 258 259 physics_accel_commit_receipt(link, &intent, DEC_ADMITTED, 0, iova_base, 0, out_receipt); 260 return 0; 261 } 262 263 int physics_accel_alloc_queue(PhysicsAcceleratorLink *link, 264 const AcceleratorCapability *cap, 265 uint32_t queue_id, 266 EffectReceipt *out_receipt) { 267 if (!link) return -1; 268 269 EffectIntent intent; 270 memset(&intent, 0, sizeof(intent)); 271 intent.version = 1; 272 intent.length = sizeof(intent); 273 intent.request_id = 103; 274 intent.principal_id = cap ? cap->principal_id : 0; 275 intent.capability_slot = cap ? cap->slot : 0; 276 intent.capability_generation = cap ? cap->generation : 0; 277 intent.resource_type = RES_ACCELERATOR; 278 intent.operation = ACCEL_OP_ALLOC_QUEUE; 279 intent.target_base = queue_id; 280 281 int val = physics_accel_validate_capability(link, cap, ACCEL_OP_ALLOC_QUEUE, 0, 0); 282 if (val != DEC_ADMITTED) { 283 physics_accel_commit_receipt(link, &intent, DEC_REJECTED, val, 0, 0, out_receipt); 284 return -1; 285 } 286 287 if (queue_id >= PHYSICS_ACCEL_MAX_QUEUES) { 288 physics_accel_commit_receipt(link, &intent, DEC_REJECTED, DEC_REJECTED_BOUNDS, 0, 0, out_receipt); 289 return -2; 290 } 291 292 AcceleratorQueue *q = &link->queues[queue_id]; 293 if (q->active) { 294 physics_accel_commit_receipt(link, &intent, DEC_REJECTED, DEC_REJECTED_REPLAY_CONFLICT, 0, 0, out_receipt); 295 return -3; 296 } 297 298 q->queue_id = queue_id; 299 q->head_index = 0; 300 q->tail_index = 0; 301 q->slot_count = PHYSICS_ACCEL_RING_SLOTS; 302 q->ring_phys_base = link->coherent_dram_base + 0x100000 + (queue_id * 0x10000); 303 q->doorbell_mmio_reg = link->mmio_doorbell_base + (queue_id * 0x1000); 304 q->active = true; 305 memset(q->ring_memory, 0, sizeof(q->ring_memory)); 306 307 link->active_queues++; 308 link->device_state = ACCEL_STATE_ACTIVE; 309 310 physics_accel_commit_receipt(link, &intent, DEC_ADMITTED, 0, q->ring_phys_base, q->slot_count, out_receipt); 311 return 0; 312 } 313 314 int physics_accel_smmu_translate(const PhysicsAcceleratorLink *link, 315 uint64_t iova, 316 uint64_t size, 317 uint32_t access_perms, 318 uint64_t *out_phys_addr) { 319 if (!link || size == 0) return SMMU_FAULT_BAD_STE; 320 321 if (!link->smmu_config.stage1_enabled) { 322 return SMMU_FAULT_STREAM_DISABLED; 323 } 324 325 for (uint32_t i = 0; i < link->active_window_count; i++) { 326 const DmaWindowDescriptor *w = &link->active_windows[i]; 327 if (iova >= w->iova_base && (iova + size) <= (w->iova_base + w->size_bytes)) { 328 /* Check permission match */ 329 if ((access_perms & DMA_PERM_WRITE) && !(w->permissions & DMA_PERM_WRITE)) { 330 return SMMU_FAULT_PERMISSION; 331 } 332 if (out_phys_addr) { 333 uint64_t offset = iova - w->iova_base; 334 *out_phys_addr = w->phys_base + offset; 335 } 336 return SMMU_FAULT_NONE; 337 } 338 } 339 340 return SMMU_FAULT_TRANSLATION; 341 } 342 343 int physics_accel_submit_command(PhysicsAcceleratorLink *link, 344 const AcceleratorCapability *cap, 345 const EffectIntent *intent, 346 EffectReceipt *out_receipt) { 347 if (!link || !intent) return -1; 348 349 int val = physics_accel_validate_capability(link, cap, ACCEL_OP_SUBMIT, 0, 0); 350 if (val != DEC_ADMITTED) { 351 physics_accel_commit_receipt(link, intent, DEC_REJECTED, val, 0, 0, out_receipt); 352 return -1; 353 } 354 355 if (link->device_state != ACCEL_STATE_ACTIVE) { 356 physics_accel_commit_receipt(link, intent, DEC_REJECTED, DEC_REJECTED_STRUCTURAL, 0, 0, out_receipt); 357 return -2; 358 } 359 360 uint32_t q_id = (uint32_t)intent->target_base; 361 if (q_id >= PHYSICS_ACCEL_MAX_QUEUES || !link->queues[q_id].active) { 362 physics_accel_commit_receipt(link, intent, DEC_REJECTED, DEC_REJECTED_BOUNDS, 0, 0, out_receipt); 363 return -3; 364 } 365 366 /* Translate and validate command descriptor IOVA */ 367 uint64_t phys_cmd = 0; 368 int trans = physics_accel_smmu_translate(link, intent->param0, intent->target_size, 369 DMA_PERM_READ, &phys_cmd); 370 if (trans != SMMU_FAULT_NONE) { 371 /* SMMU fault triggered */ 372 physics_accel_handle_fault(link, trans, intent->param0); 373 physics_accel_commit_receipt(link, intent, DEC_REJECTED, DEC_REJECTED_CONSTRAINT, 0, 0, out_receipt); 374 return -4; 375 } 376 377 AcceleratorQueue *q = &link->queues[q_id]; 378 uint32_t slot = q->tail_index; 379 q->tail_index = (q->tail_index + 1) % q->slot_count; 380 381 /* Write command token into ring memory */ 382 uint8_t *slot_ptr = q->ring_memory + (slot * PHYSICS_ACCEL_SLOT_SIZE_BYTES); 383 memcpy(slot_ptr, &phys_cmd, sizeof(phys_cmd)); 384 memcpy(slot_ptr + 8, &intent->target_size, sizeof(intent->target_size)); 385 386 /* Sequential consistency memory barrier ensuring descriptor visibility */ 387 atomic_thread_fence(memory_order_seq_cst); 388 389 link->total_submissions++; 390 physics_accel_commit_receipt(link, intent, DEC_ADMITTED, 0, phys_cmd, slot, out_receipt); 391 return 0; 392 } 393 394 int physics_accel_handle_fault(PhysicsAcceleratorLink *link, 395 uint32_t fault_syndrome, 396 uint64_t fault_iova) { 397 (void)fault_iova; 398 if (!link) return -1; 399 400 link->device_state = ACCEL_STATE_FAULTED; 401 link->fault_count++; 402 403 /* Isolate all active queues */ 404 for (uint32_t i = 0; i < PHYSICS_ACCEL_MAX_QUEUES; i++) { 405 if (link->queues[i].active) { 406 link->queues[i].active = false; 407 } 408 } 409 link->active_queues = 0; 410 411 return (int)fault_syndrome; 412 } 413 414 int physics_accel_reset_device(PhysicsAcceleratorLink *link, 415 const AcceleratorCapability *cap, 416 EffectReceipt *out_receipt) { 417 if (!link) return -1; 418 419 EffectIntent intent; 420 memset(&intent, 0, sizeof(intent)); 421 intent.version = 1; 422 intent.length = sizeof(intent); 423 intent.request_id = 104; 424 intent.principal_id = cap ? cap->principal_id : 0; 425 intent.capability_slot = cap ? cap->slot : 0; 426 intent.capability_generation = cap ? cap->generation : 0; 427 intent.resource_type = RES_ACCELERATOR; 428 intent.operation = ACCEL_OP_RESET; 429 430 int val = physics_accel_validate_capability(link, cap, ACCEL_OP_RESET, 0, 0); 431 if (val != DEC_ADMITTED) { 432 physics_accel_commit_receipt(link, &intent, DEC_REJECTED, val, 0, 0, out_receipt); 433 return -1; 434 } 435 436 link->device_state = ACCEL_STATE_RESETTING; 437 438 /* Reset software rings and queues */ 439 for (uint32_t i = 0; i < PHYSICS_ACCEL_MAX_QUEUES; i++) { 440 link->queues[i].active = false; 441 link->queues[i].head_index = 0; 442 link->queues[i].tail_index = 0; 443 memset(link->queues[i].ring_memory, 0, sizeof(link->queues[i].ring_memory)); 444 } 445 link->active_queues = 0; 446 447 /* Clear windows */ 448 link->active_window_count = 0; 449 memset(link->active_windows, 0, sizeof(link->active_windows)); 450 451 /* State transition: RECOVERED -> CONFIGURED */ 452 link->device_state = ACCEL_STATE_RECOVERED; 453 link->device_state = ACCEL_STATE_CONFIGURED; 454 455 physics_accel_commit_receipt(link, &intent, DEC_ADMITTED, 0, 0x1, 0, out_receipt); 456 return 0; 457 }