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.h
| 1 | #ifndef PHYSICS_ACCEL_H |
| 2 | #define PHYSICS_ACCEL_H |
| 3 | |
| 4 | #include <stdbool.h> |
| 5 | #include <stddef.h> |
| 6 | #include <stdint.h> |
| 7 | |
| 8 | #define PHYSICS_ACCEL_LINK_VERSION 0x00010000 |
| 9 | #define PHYSICS_ACCEL_MAX_WINDOWS 16 |
| 10 | #define PHYSICS_ACCEL_MAX_QUEUES 4 |
| 11 | #define PHYSICS_ACCEL_RING_SLOTS 64 |
| 12 | #define PHYSICS_ACCEL_SLOT_SIZE_BYTES 128 |
| 13 | |
| 14 | /* Resource Type for Accelerator Capability (M3 extension) */ |
| 15 | #define RES_ACCELERATOR 0x00000005 |
| 16 | |
| 17 | /* Accelerator Operation Bitmasks */ |
| 18 | #define ACCEL_OP_PROBE 0x00000001 |
| 19 | #define ACCEL_OP_MAP_DMA 0x00000002 |
| 20 | #define ACCEL_OP_UNMAP_DMA 0x00000004 |
| 21 | #define ACCEL_OP_ALLOC_QUEUE 0x00000008 |
| 22 | #define ACCEL_OP_SUBMIT 0x00000010 |
| 23 | #define ACCEL_OP_SYNC 0x00000020 |
| 24 | #define ACCEL_OP_RESET 0x00000040 |
| 25 | |
| 26 | /* DMA Permission Flags */ |
| 27 | #define DMA_PERM_READ 0x00000001 |
| 28 | #define DMA_PERM_WRITE 0x00000002 |
| 29 | #define DMA_PERM_COHERENT 0x00000004 |
| 30 | |
| 31 | /* Decision Codes in EffectReceipt */ |
| 32 | #define DEC_ADMITTED 1 |
| 33 | #define DEC_REJECTED 2 |
| 34 | #define DEC_REJECTED_STRUCTURAL 2 |
| 35 | #define DEC_REJECTED_UNKNOWN_CAP 3 |
| 36 | #define DEC_REJECTED_STALE_GENERATION 4 |
| 37 | #define DEC_REJECTED_REVOKED 5 |
| 38 | #define DEC_REJECTED_WRONG_PRINCIPAL 6 |
| 39 | #define DEC_REJECTED_OPERATION 7 |
| 40 | #define DEC_REJECTED_RESOURCE 8 |
| 41 | #define DEC_REJECTED_BOUNDS 9 |
| 42 | #define DEC_REJECTED_CONSTRAINT 10 |
| 43 | #define DEC_REJECTED_REPLAY_CONFLICT 11 |
| 44 | #define DEC_REJECTED_EXHAUSTED 12 |
| 45 | #define DEC_EXECUTION_FAILED 13 |
| 46 | |
| 47 | /* SMMUv3 Fault Syndromes */ |
| 48 | #define SMMU_FAULT_NONE 0 |
| 49 | #define SMMU_FAULT_TRANSLATION 1 /* F_TRANSLATION: unmapped IOVA */ |
| 50 | #define SMMU_FAULT_PERMISSION 2 /* F_PERMISSION: write to read-only */ |
| 51 | #define SMMU_FAULT_STREAM_DISABLED 3 /* Stream disabled in STE */ |
| 52 | #define SMMU_FAULT_BAD_STE 4 /* C_BAD_STE: invalid STE format */ |
| 53 | #define SMMU_FAULT_BAD_CD 5 /* C_BAD_CD: invalid Context Descriptor */ |
| 54 | |
| 55 | /* Accelerator Lifecycle States */ |
| 56 | typedef enum { |
| 57 | ACCEL_STATE_UNINITIALIZED = 0, |
| 58 | ACCEL_STATE_PROBED = 1, |
| 59 | ACCEL_STATE_CONFIGURED = 2, |
| 60 | ACCEL_STATE_ACTIVE = 3, |
| 61 | ACCEL_STATE_FAULTED = 4, |
| 62 | ACCEL_STATE_RESETTING = 5, |
| 63 | ACCEL_STATE_RECOVERED = 6 |
| 64 | } AcceleratorDeviceState; |
| 65 | |
| 66 | /* DMA Window Descriptor for SMMUv3 Translation Mapping */ |
| 67 | typedef struct { |
| 68 | uint64_t iova_base; |
| 69 | uint64_t phys_base; |
| 70 | uint64_t size_bytes; |
| 71 | uint32_t permissions; /* DMA_PERM_* */ |
| 72 | uint32_t stream_id; |
| 73 | uint32_t window_id; |
| 74 | uint32_t reserved; |
| 75 | } DmaWindowDescriptor; |
| 76 | |
| 77 | /* SMMUv3 Stream Configuration */ |
| 78 | typedef struct { |
| 79 | uint32_t stream_id; |
| 80 | uint32_t ste_index; |
| 81 | uint64_t cd_table_base; |
| 82 | uint64_t ttbr0_base; |
| 83 | uint64_t mair_value; |
| 84 | uint32_t t0sz; |
| 85 | uint32_t granule_size; |
| 86 | bool stage1_enabled; |
| 87 | bool fault_trap_enabled; |
| 88 | } Smmuv3StreamConfig; |
| 89 | |
| 90 | /* Accelerator Capability Record */ |
| 91 | typedef struct { |
| 92 | uint32_t slot; |
| 93 | uint32_t generation; |
| 94 | uint64_t principal_id; |
| 95 | uint32_t resource_type; /* RES_ACCELERATOR */ |
| 96 | uint32_t allowed_ops; /* Bitmask of ACCEL_OP_* */ |
| 97 | uint64_t iova_bound_base; |
| 98 | uint64_t iova_bound_size; |
| 99 | uint32_t queue_id_mask; |
| 100 | uint32_t revocation_state; /* 1 = ACTIVE, 2 = REVOKED */ |
| 101 | uint8_t provenance_digest[32]; |
| 102 | } AcceleratorCapability; |
| 103 | |
| 104 | /* Canonical EffectIntent (64 bytes, little-endian, 64-bit aligned) */ |
| 105 | typedef struct { |
| 106 | uint16_t version; /* 1 */ |
| 107 | uint16_t length; /* 64 */ |
| 108 | uint32_t reserved0; /* 0 */ |
| 109 | uint64_t request_id; /* Caller-generated request ID */ |
| 110 | uint64_t principal_id; /* Principal ID (e.g. Omega) */ |
| 111 | uint32_t capability_slot; /* Capability slot index */ |
| 112 | uint32_t capability_generation; /* Capability generation */ |
| 113 | uint32_t resource_type; /* RES_ACCELERATOR */ |
| 114 | uint32_t operation; /* ACCEL_OP_* */ |
| 115 | uint64_t target_base; /* Target IOVA or Queue ID */ |
| 116 | uint64_t target_size; /* Byte span or command length */ |
| 117 | uint64_t param0; /* Descriptor address or doorbell parameter */ |
| 118 | } EffectIntent; |
| 119 | |
| 120 | /* Canonical EffectReceipt (192 bytes, 64-bit aligned) */ |
| 121 | typedef struct { |
| 122 | uint16_t version; /* 1 */ |
| 123 | uint16_t length; /* 192 */ |
| 124 | uint32_t decision; /* 1 = ADMITTED, 2 = REJECTED */ |
| 125 | uint32_t rejection_reason; /* Specific reason code if rejected */ |
| 126 | uint32_t reserved0; /* 0 */ |
| 127 | uint64_t request_id; /* Mirrored from EffectIntent */ |
| 128 | uint8_t intent_digest[32]; /* SHA-256 of 64-byte EffectIntent */ |
| 129 | uint64_t actual_effect; /* Physical result or mapped address */ |
| 130 | uint64_t output; /* Submitted packet count or status */ |
| 131 | uint32_t capability_slot; /* Presented slot */ |
| 132 | uint32_t capability_generation; /* Presented generation */ |
| 133 | uint64_t machine_generation; /* Monotonic machine counter */ |
| 134 | uint64_t measurement; /* Timestamp / cycle measurement */ |
| 135 | uint8_t previous_receipt_digest[32]; /* Rolling SHA-256 seal chain */ |
| 136 | uint8_t receipt_digest[32]; /* SHA-256 seal over fields + previous */ |
| 137 | uint8_t reserved1[32]; /* Padding to 192 bytes */ |
| 138 | } EffectReceipt; |
| 139 | |
| 140 | /* Command Queue State within Physics */ |
| 141 | typedef struct { |
| 142 | uint32_t queue_id; |
| 143 | uint32_t head_index; |
| 144 | uint32_t tail_index; |
| 145 | uint32_t slot_count; |
| 146 | uint64_t ring_phys_base; |
| 147 | uint64_t doorbell_mmio_reg; |
| 148 | bool active; |
| 149 | uint8_t ring_memory[PHYSICS_ACCEL_RING_SLOTS * PHYSICS_ACCEL_SLOT_SIZE_BYTES]; |
| 150 | } AcceleratorQueue; |
| 151 | |
| 152 | /* Master Physics Accelerator Link Context */ |
| 153 | typedef struct { |
| 154 | uint32_t link_version; |
| 155 | uint32_t device_state; /* AcceleratorDeviceState */ |
| 156 | uint32_t stream_id; |
| 157 | uint32_t active_queues; |
| 158 | uint64_t mmio_doorbell_base; |
| 159 | uint64_t mmio_doorbell_size; |
| 160 | uint64_t coherent_dram_base; /* 0x80000000 on DGX Spark */ |
| 161 | uint64_t coherent_dram_size; /* 128 GiB on DGX Spark */ |
| 162 | uint64_t kernel_reserved_base; /* Protected host kernel memory */ |
| 163 | uint64_t kernel_reserved_size; |
| 164 | DmaWindowDescriptor active_windows[PHYSICS_ACCEL_MAX_WINDOWS]; |
| 165 | uint32_t active_window_count; |
| 166 | AcceleratorQueue queues[PHYSICS_ACCEL_MAX_QUEUES]; |
| 167 | Smmuv3StreamConfig smmu_config; |
| 168 | uint8_t last_receipt_digest[32]; |
| 169 | uint64_t fault_count; |
| 170 | uint64_t total_submissions; |
| 171 | uint64_t machine_generation; |
| 172 | } PhysicsAcceleratorLink; |
| 173 | |
| 174 | /* ========================================================================= */ |
| 175 | /* Physics Server Authority APIs */ |
| 176 | /* ========================================================================= */ |
| 177 | |
| 178 | /* Initialize the Physics Accelerator Link with coherent DRAM boundaries */ |
| 179 | int physics_accel_init(PhysicsAcceleratorLink *link, |
| 180 | uint64_t dram_base, |
| 181 | uint64_t dram_size, |
| 182 | uint64_t kernel_reserved_base, |
| 183 | uint64_t kernel_reserved_size); |
| 184 | |
| 185 | /* Configure SMMUv3 Stream Table and Context Descriptors for accelerator */ |
| 186 | int physics_accel_configure_smmu(PhysicsAcceleratorLink *link, |
| 187 | const Smmuv3StreamConfig *config); |
| 188 | |
| 189 | /* Validate accelerator capability token */ |
| 190 | int physics_accel_validate_capability(const PhysicsAcceleratorLink *link, |
| 191 | const AcceleratorCapability *cap, |
| 192 | uint32_t required_op, |
| 193 | uint64_t target_base, |
| 194 | uint64_t target_size); |
| 195 | |
| 196 | /* Grant a bounded DMA window and install SMMUv3 Stage 1 translation */ |
| 197 | int physics_accel_grant_dma_window(PhysicsAcceleratorLink *link, |
| 198 | const AcceleratorCapability *cap, |
| 199 | const DmaWindowDescriptor *window, |
| 200 | EffectReceipt *out_receipt); |
| 201 | |
| 202 | /* Revoke a DMA window and invalidate SMMUv3 translation entries */ |
| 203 | int physics_accel_revoke_dma_window(PhysicsAcceleratorLink *link, |
| 204 | const AcceleratorCapability *cap, |
| 205 | uint64_t iova_base, |
| 206 | EffectReceipt *out_receipt); |
| 207 | |
| 208 | /* Allocate a bounded command submission queue ring */ |
| 209 | int physics_accel_alloc_queue(PhysicsAcceleratorLink *link, |
| 210 | const AcceleratorCapability *cap, |
| 211 | uint32_t queue_id, |
| 212 | EffectReceipt *out_receipt); |
| 213 | |
| 214 | /* Mediate command packet validation, ring insertion, and doorbell ringing */ |
| 215 | int physics_accel_submit_command(PhysicsAcceleratorLink *link, |
| 216 | const AcceleratorCapability *cap, |
| 217 | const EffectIntent *intent, |
| 218 | EffectReceipt *out_receipt); |
| 219 | |
| 220 | /* Perform SMMUv3 IOVA address translation and permission check */ |
| 221 | int physics_accel_smmu_translate(const PhysicsAcceleratorLink *link, |
| 222 | uint64_t iova, |
| 223 | uint64_t size, |
| 224 | uint32_t access_perms, |
| 225 | uint64_t *out_phys_addr); |
| 226 | |
| 227 | /* Trap and isolate an accelerator fault (SMMU abort, timeout, illegal insn) */ |
| 228 | int physics_accel_handle_fault(PhysicsAcceleratorLink *link, |
| 229 | uint32_t fault_syndrome, |
| 230 | uint64_t fault_iova); |
| 231 | |
| 232 | /* Perform non-disruptive device reset and recovery */ |
| 233 | int physics_accel_reset_device(PhysicsAcceleratorLink *link, |
| 234 | const AcceleratorCapability *cap, |
| 235 | EffectReceipt *out_receipt); |
| 236 | |
| 237 | /* Cryptographic receipt commit helper */ |
| 238 | void physics_accel_commit_receipt(PhysicsAcceleratorLink *link, |
| 239 | const EffectIntent *intent, |
| 240 | uint32_t decision, |
| 241 | uint32_t rejection_reason, |
| 242 | uint64_t actual_effect, |
| 243 | uint64_t output, |
| 244 | EffectReceipt *out_receipt); |
| 245 | |
| 246 | void physics_accel_commit_receipt_measured(PhysicsAcceleratorLink *link, |
| 247 | const EffectIntent *intent, |
| 248 | uint32_t decision, |
| 249 | uint32_t rejection_reason, |
| 250 | uint64_t actual_effect, |
| 251 | uint64_t output, |
| 252 | uint64_t measurement, |
| 253 | EffectReceipt *out_receipt); |
| 254 | |
| 255 | #endif /* PHYSICS_ACCEL_H */ |