aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / dev / gb10_gsp_boot.h
| 1 | /* gb10_gsp_boot.h -- GB10 (GB10B) GSP boot builder, cut C host side (aienos#286, L192). |
| 2 | * |
| 3 | * Freestanding C that lays out, in plain memory only, everything the pinned Linux driver |
| 4 | * (open-gpu-kernel-modules tag 580.173.02) hands the chip to boot GSP-RM on the GB10B through |
| 5 | * the SEC2-boots-GSP-FMC path, plus the RPC messages a native kernel sends afterwards. |
| 6 | * Nothing here touches a device: no MMIO, no register write, no firmware load, no DMA start. |
| 7 | * The caller decides where the arena lives and what physical address it has; this file only |
| 8 | * writes bytes into it and reports the addresses the chip would be given. |
| 9 | * |
| 10 | * Blocks, in arena order (every one 4 KiB aligned), with the driver function that fills each: |
| 11 | * GspFwWprMeta page kgspPopulateWprMeta_GH100 + _GB10B (gsp_fw_wpr_meta.h) |
| 12 | * libos init-argument page kgspSetupLibosInitArgs_IMPL (libos_init_args.h) |
| 13 | * 7 log buffers + PTE prefix kgspInitLogging / log registration |
| 14 | * GSP_ARGUMENTS_CACHED page kgspPopulateGspRmInitArgs_IMPL (gsp_init_args.h) |
| 15 | * RPC shared buffer (129 pg) GspMsgQueuesInit (message_queue_cpu.c), msgqTxCreate (msgq.c) |
| 16 | * radix-3 table + ELF copy kgspCreateRadix3 |
| 17 | * GspRmBoot bootloader copy kgspPrepareForBootstrap_GH100 |
| 18 | * GSP-RM signature copy kgspPrepareForBootstrap_GH100 (256-aligned, NV_ALIGN_UP 256) |
| 19 | * GSP-FMC image copy ksec2SetupGspImages_GB10B |
| 20 | * GSP_FMC_BOOT_PARAMS page kgspSetupGspFmcArgs_GH100 (gspifpub.h) |
| 21 | * and, outside the arena, the single SEC2 EMEM packet (MCTP + NVDM COT header + NVDM_PAYLOAD_COT) |
| 22 | * ksec2PrepareBootCommands_GB10B / ksec2SendMessage would write. The two RPCs the driver queues |
| 23 | * before boot (GSP_SET_SYSTEM_INFO 72, SET_REGISTRY 73) are placed in the command ring. |
| 24 | * |
| 25 | * docs/GB10_FIRMWARE_INVENTORY.md sections 10-14 cite the source line for every value. The hosted |
| 26 | * tools native/kernel/tools/ck_gsp_*_encode.c are the earlier per-block encoders; this file is |
| 27 | * the composed, freestanding form the kernel can link. */ |
| 28 | #ifndef GB10_GSP_BOOT_H |
| 29 | #define GB10_GSP_BOOT_H |
| 30 | |
| 31 | #include <stddef.h> |
| 32 | #include <stdint.h> |
| 33 | |
| 34 | /* ---- driver constants (580.173.02) ---- */ |
| 35 | #define GSP_PAGE 4096u |
| 36 | #define GSP_WPR_META_MAGIC 0xdc3aae21371a60b3ULL |
| 37 | #define GSP_WPR_META_REVISION 1ULL |
| 38 | #define GSP_WPR_META_SIZE 256u |
| 39 | #define GSP_GB10B_BOOTLOADER_SIZE 196608u |
| 40 | #define GSP_GB10B_BL_CODE_OFF 0xb000u |
| 41 | #define GSP_GB10B_BL_DATA_OFF 0xa00u |
| 42 | #define GSP_GB10B_BL_MANIFEST_OFF 0x0u |
| 43 | #define GSP_GB10B_FW_HEAP_SIZE (133ULL << 20) |
| 44 | #define GSP_GB10B_LOG_COUNT 7u |
| 45 | #define GSP_LIBOS_ENTRY_SIZE 32u |
| 46 | #define GSP_LIBOS_KIND_CONTIGUOUS 1u |
| 47 | #define GSP_LIBOS_LOC_SYSMEM 1u |
| 48 | #define GSP_ARGS_CACHED_SIZE 88u |
| 49 | #define GSP_MQ_QUEUE_SIZE 0x40000u |
| 50 | #define GSP_MQ_MSG_SIZE 4096u |
| 51 | #define GSP_MQ_NUM_PTES 129u |
| 52 | #define GSP_MQ_SHARED_SIZE (GSP_MQ_NUM_PTES * GSP_PAGE) |
| 53 | #define GSP_MQ_CMDQ_OFF GSP_PAGE |
| 54 | #define GSP_MQ_STATQ_OFF (GSP_PAGE + GSP_MQ_QUEUE_SIZE) |
| 55 | #define GSP_MQ_RXHDR_OFF 32u |
| 56 | #define GSP_MQ_ENTRY_OFF 4096u |
| 57 | #define GSP_MQ_MSG_COUNT 63u |
| 58 | #define GSP_MQ_FLAGS_SWAP_RX 1u |
| 59 | #define GSP_ELEM_HDR_SIZE 48u |
| 60 | #define GSP_ELEM_MIN_LEN 80u /* sizeof(GSP_MSG_QUEUE_ELEMENT) */ |
| 61 | #define GSP_ELEM_SIZE_MAX 65536u |
| 62 | #define GSP_RPC_HDR_SIZE 32u |
| 63 | #define GSP_RPC_SIZE_MAX (GSP_ELEM_SIZE_MAX - GSP_ELEM_HDR_SIZE) |
| 64 | #define GSP_RPC_HEADER_VERSION 0x03000000u |
| 65 | #define GSP_RPC_SIGNATURE 0x43505256u /* "VRPC" */ |
| 66 | #define GSP_RPC_PENDING 0xFFFFFFFFu |
| 67 | #define GSP_SYSTEM_INFO_SIZE 936u |
| 68 | #define GSP_FMC_PARAMS_SIZE 88u |
| 69 | #define GSP_COT_PAYLOAD_SIZE 860u |
| 70 | #define GSP_COT_VERSION 1u |
| 71 | #define GSP_COT_HASH_SIZE 48u |
| 72 | #define GSP_COT_KEY_SIZE 384u |
| 73 | #define GSP_COT_SIG_SIZE 384u |
| 74 | #define GSP_EMEM_CHANNEL_SIZE 1024u |
| 75 | #define GSP_DMA_TARGET_COHERENT 1u /* GSP_DMA_TARGET_COHERENT_SYSTEM: cached sysmem */ |
| 76 | #define GSP_DMA_TARGET_NONCOHERENT 2u /* GSP_DMA_TARGET_NONCOHERENT_SYSTEM: uncached */ |
| 77 | |
| 78 | /* RPC function numbers (rpc_global_enums.h) and the GSP events a native kernel reads back. */ |
| 79 | #define GSP_FN_FREE 10u |
| 80 | #define GSP_FN_GET_GSP_STATIC_INFO 65u |
| 81 | #define GSP_FN_CONTINUATION 71u |
| 82 | #define GSP_FN_SET_SYSTEM_INFO 72u |
| 83 | #define GSP_FN_SET_REGISTRY 73u |
| 84 | #define GSP_FN_GSP_RM_CONTROL 76u |
| 85 | #define GSP_FN_GSP_RM_ALLOC 103u |
| 86 | #define GSP_EV_INIT_DONE 0x1001u |
| 87 | #define GSP_EV_RUN_CPU_SEQUENCER 0x1002u |
| 88 | |
| 89 | /* Fixed parts of the post-init RPC payloads (g_rpc-structures.h, g_sdk-structures.h). */ |
| 90 | #define GSP_RM_ALLOC_FIXED 32u /* rpc_gsp_rm_alloc_v03_00 before params[] */ |
| 91 | #define GSP_RM_CONTROL_FIXED 40u /* rpc_gsp_rm_control_v03_00 before params[] */ |
| 92 | #define GSP_RM_FREE_SIZE 16u /* rpc_free_v03_00 = NVOS00_PARAMETERS_v03_00 */ |
| 93 | |
| 94 | /* Registry value kinds (PACKED_REGISTRY_TABLE). */ |
| 95 | #define GSP_REG_DWORD 1u |
| 96 | #define GSP_REG_BINARY 2u |
| 97 | #define GSP_REG_STRING 3u |
| 98 | |
| 99 | /* ---- natural-layout mirrors (LP64, little endian) for size/offset asserts ---- */ |
| 100 | typedef struct { |
| 101 | uint64_t magic, revision; |
| 102 | uint64_t sysmemAddrOfRadix3Elf, sizeOfRadix3Elf; |
| 103 | uint64_t sysmemAddrOfBootloader, sizeOfBootloader; |
| 104 | uint64_t bootloaderCodeOffset, bootloaderDataOffset, bootloaderManifestOffset; |
| 105 | uint64_t sysmemAddrOfSignature, sizeOfSignature; |
| 106 | uint64_t gspFwRsvdStart, nonWprHeapOffset, nonWprHeapSize, gspFwWprStart; |
| 107 | uint64_t gspFwHeapOffset, gspFwHeapSize, gspFwOffset, bootBinOffset; |
| 108 | uint64_t frtsOffset, frtsSize, gspFwWprEnd, fbSize; |
| 109 | uint64_t vgaWorkspaceOffset, vgaWorkspaceSize, bootCount; |
| 110 | uint64_t partitionRpcAddr; |
| 111 | uint16_t partitionRpcRequestOffset, partitionRpcReplyOffset; |
| 112 | uint32_t elfCodeOffset, elfDataOffset, elfCodeSize, elfDataSize, lsUcodeVersion; |
| 113 | uint8_t gspFwHeapVfPartitionCount, flags, padding[2]; |
| 114 | uint32_t pmuReservedSize; |
| 115 | uint64_t verified; |
| 116 | } gsp_mirror_wpr_meta; |
| 117 | |
| 118 | typedef struct { uint64_t id8, pa, size; uint8_t kind, loc; } gsp_mirror_libos_arg; |
| 119 | |
| 120 | typedef struct { |
| 121 | struct { uint64_t sharedMemPhysAddr; uint32_t pageTableEntryCount; uint64_t cmdQueueOffset, statQueueOffset; } mq; |
| 122 | struct { uint32_t oldLevel, flags; uint8_t bInPMTransition; } sr; |
| 123 | uint32_t gpuInstance; |
| 124 | uint8_t bDmemStack; |
| 125 | struct { uint64_t pa, size; } profilerArgs; |
| 126 | struct { uint64_t pa, size; } sysmemHeapArgs; |
| 127 | } gsp_mirror_args_cached; |
| 128 | |
| 129 | typedef struct { uint32_t version, size, msgSize, msgCount, writePtr, flags, rxHdrOff, entryOff; } gsp_mirror_msgq_tx; |
| 130 | typedef struct { uint32_t header_version, signature, length, function, rpc_result, rpc_result_private, sequence, cpuRmGfid; } gsp_mirror_rpc_hdr; |
| 131 | typedef struct { uint8_t authTag[16], aad[16]; uint32_t checkSum, seqNum, elemCount; _Alignas(8) gsp_mirror_rpc_hdr rpc; } gsp_mirror_elem; |
| 132 | typedef struct { uint32_t hClient, hParent, hObject, hClass, status, paramsSize, flags; uint8_t reserved[4]; } gsp_mirror_rm_alloc; |
| 133 | typedef struct { uint32_t hClient, hObject, cmd, status, paramsSize, rmapiRpcFlags, rmctrlFlags, rmctrlAccessRight; _Alignas(8) uint64_t reserved0; } gsp_mirror_rm_control; |
| 134 | typedef struct { uint32_t hRoot, hObjectParent, hObjectOld, status; } gsp_mirror_rm_free; |
| 135 | typedef struct { uint32_t nameOffset; uint8_t type; uint32_t data, length; } gsp_mirror_reg_entry; |
| 136 | |
| 137 | typedef struct { |
| 138 | uint32_t regkeys; |
| 139 | struct { uint32_t target, gspRmDescSize; uint64_t gspRmDescOffset, wprCarveoutOffset; uint32_t wprCarveoutSize; uint8_t bIsGspRmBoot; } boot; |
| 140 | struct { uint32_t target; uint64_t bootArgsOffset; } rm; |
| 141 | struct { uint32_t target; uint64_t payloadBufferOffset; uint32_t payloadBufferSize; } spdm; |
| 142 | struct { uint32_t flushSysmemAddrValLo, flushSysmemAddrValHi; } mem; |
| 143 | } gsp_mirror_fmc_params; |
| 144 | |
| 145 | typedef struct __attribute__((packed)) { |
| 146 | uint16_t version, size; |
| 147 | uint64_t gspFmcSysmemOffset, frtsSysmemOffset; |
| 148 | uint32_t frtsSysmemSize; |
| 149 | uint64_t frtsVidmemOffset; |
| 150 | uint32_t frtsVidmemSize; |
| 151 | uint8_t hash384[48], publicKey[384], signature[384]; |
| 152 | uint64_t gspBootArgsSysmemOffset; |
| 153 | } gsp_mirror_cot; |
| 154 | |
| 155 | /* ---- the arena: zeroed host memory whose first byte has physical address pa ---- */ |
| 156 | typedef struct { |
| 157 | uint8_t *base; |
| 158 | uint64_t pa; |
| 159 | uint64_t cap; |
| 160 | uint64_t used; |
| 161 | } gsp_arena; |
| 162 | |
| 163 | /* Fails (returns -1) on a NULL base, a base or pa that is not 4 KiB aligned, or pa + cap wrapping. */ |
| 164 | int gsp_arena_init(gsp_arena *a, void *base, uint64_t pa, uint64_t cap); |
| 165 | |
| 166 | typedef struct { |
| 167 | const char *name; /* NUL-terminated, non-empty */ |
| 168 | uint8_t type; /* GSP_REG_DWORD / _BINARY / _STRING */ |
| 169 | uint32_t dword; /* DWORD value */ |
| 170 | const uint8_t *bytes; /* BINARY / STRING value (STRING includes its NUL) */ |
| 171 | uint32_t len; |
| 172 | } gsp_reg_entry; |
| 173 | |
| 174 | typedef struct { |
| 175 | /* GSP-RM firmware: the .fwimage section and this chip's signature section (container parsed |
| 176 | * elsewhere, ck_gsp_fw_inspect). Copied into the arena; never modified. */ |
| 177 | const uint8_t *rm_elf; uint64_t rm_elf_size; |
| 178 | const uint8_t *rm_sig; uint32_t rm_sig_size; |
| 179 | /* GspRmBoot_GB10B image and its RM_RISCV_UCODE_DESC offsets (ck_gsp_bindata_extract). */ |
| 180 | const uint8_t *bootloader; uint32_t bootloader_size; |
| 181 | uint32_t bl_code_off, bl_data_off, bl_manifest_off; |
| 182 | /* GspRmFmcGfwProdSigned_GB10B: image, hash 48, public key 384, signature 384. */ |
| 183 | const uint8_t *fmc; uint32_t fmc_size; |
| 184 | const uint8_t *fmc_hash, *fmc_pubkey, *fmc_sig; |
| 185 | /* ZERO_FB sysmem heap (512 MiB on the driver), contiguous uncached; caller's carve-out. */ |
| 186 | uint64_t sysmem_heap_pa, sysmem_heap_size; |
| 187 | /* GspSystemInfo bytes (936): content comes from the real PCI/BAR/ACPI state. */ |
| 188 | const uint8_t *system_info; |
| 189 | /* Registry for SET_REGISTRY; nregs 0 = the driver's NV_WARN_NOTHING_TO_DO, RPC not queued. */ |
| 190 | const gsp_reg_entry *regs; uint32_t nregs; |
| 191 | uint32_t gpu_instance; |
| 192 | uint32_t fmc_regkeys; /* GSP_FMC_BOOT_PARAMS.initParams.regkeys (0 without CC/NVLink) */ |
| 193 | unsigned dma_bits; /* every chip-visible address must be below 1 << dma_bits */ |
| 194 | } gsp_boot_in; |
| 195 | |
| 196 | typedef struct { |
| 197 | uint64_t pa, size; |
| 198 | } gsp_region; |
| 199 | |
| 200 | typedef struct { |
| 201 | gsp_region wpr_meta, libos_args, log[GSP_GB10B_LOG_COUNT], args_cached; |
| 202 | gsp_region rpc_shared; /* page-table page + command queue + status queue */ |
| 203 | uint64_t cmdq_pa, statq_pa; |
| 204 | gsp_region radix3; /* levels 0..2 then the ELF data pages */ |
| 205 | uint64_t radix3_root_pa, elf_data_pa; |
| 206 | uint32_t radix3_level_pages[4]; |
| 207 | gsp_region bootloader, signature, fmc, fmc_params; |
| 208 | /* command ring state after the pre-boot RPCs (the driver's txSeqNum / writePtr) */ |
| 209 | uint32_t cmdq_write_ptr, tx_seq; |
| 210 | /* the SEC2 EMEM packet: header dword 0 (MCTP), dword 1 (NVDM COT), then the COT payload */ |
| 211 | uint8_t cot_packet[GSP_EMEM_CHANNEL_SIZE]; |
| 212 | uint32_t cot_packet_len; /* bytes written at EMEM offset 0 */ |
| 213 | uint32_t emem_queue_tail; /* QUEUE_TAIL value, QUEUE_HEAD = 0 */ |
| 214 | } gsp_boot_plan; |
| 215 | |
| 216 | /* Builds every block into the arena and the COT packet into *out. Returns 0, or -1 with *why set |
| 217 | * to a fixed reason string (the test oracle). On failure the arena may hold partial bytes. */ |
| 218 | int gsp_boot_build(gsp_arena *a, const gsp_boot_in *in, gsp_boot_plan *out, const char **why); |
| 219 | |
| 220 | /* ---- message queue over the RPC shared buffer (post-boot RPCs and status reads) ---- */ |
| 221 | typedef struct { |
| 222 | uint8_t *cmdq; /* host view of the command queue (producer: CPU) */ |
| 223 | uint8_t *statq; /* host view of the status queue (producer: GSP) */ |
| 224 | uint32_t tx_seq; /* next element seqNum on the command queue */ |
| 225 | uint32_t rx_seq; /* next expected seqNum on the status queue */ |
| 226 | uint32_t rpc_seq; /* next header sequence for synchronous RPCs */ |
| 227 | } gsp_mq; |
| 228 | |
| 229 | /* Attach to the queues of a built plan (arena must be the one passed to gsp_boot_build). */ |
| 230 | int gsp_mq_attach(gsp_mq *q, const gsp_arena *a, const gsp_boot_plan *plan); |
| 231 | |
| 232 | /* One piece of an RPC payload; p NULL means len zero bytes. */ |
| 233 | typedef struct { const void *p; uint32_t len; } gsp_seg; |
| 234 | #define GSP_REG_MAX_ENTRIES 32u /* builder bound: keeps the registry staging on a small stack */ |
| 235 | |
| 236 | /* Queue one message, streamed into the ring slot by slot (it may wrap the ring end, as |
| 237 | * msgqTxGetWriteBuffer allows). The payload is the segments back to back. rpc_len is the header |
| 238 | * length field minus 32 (what the driver passes to rpcWriteCommonHeader); the segments may be |
| 239 | * longer than rpc_len (the driver's GSP_RM_ALLOC leaves params outside the length) but must fit |
| 240 | * the elements the length covers. sync != 0 takes the next RPC sequence, 0 sends sequence 0 (the |
| 241 | * driver's async RPCs). Refuses a full ring. */ |
| 242 | int gsp_mq_send(gsp_mq *q, uint32_t function, const gsp_seg *segs, uint32_t nsegs, uint32_t rpc_len, |
| 243 | int sync, uint32_t *sequence_out, const char **why); |
| 244 | |
| 245 | /* Post-init RPCs, byte-for-byte as rpcRmApiAlloc_GSP / rpcRmApiControl_GSP / rpcRmApiFree_GSP / |
| 246 | * rpcGetGspStaticInfo_v14_00 (rpc.c) build them; all synchronous. */ |
| 247 | int gsp_rpc_rm_alloc(gsp_mq *q, uint32_t hClient, uint32_t hParent, uint32_t hObject, uint32_t hClass, |
| 248 | const void *params, uint32_t params_size, uint32_t *seq, const char **why); |
| 249 | int gsp_rpc_rm_control(gsp_mq *q, uint32_t hClient, uint32_t hObject, uint32_t cmd, |
| 250 | const void *params, uint32_t params_size, uint32_t *seq, const char **why); |
| 251 | int gsp_rpc_rm_free(gsp_mq *q, uint32_t hClient, uint32_t hObject, uint32_t *seq, const char **why); |
| 252 | int gsp_rpc_get_static_info(gsp_mq *q, uint32_t static_info_size, uint32_t *seq, const char **why); |
| 253 | |
| 254 | /* Read one status-queue message if one is waiting: checks writePtr bounds, elemCount, the zero |
| 255 | * checksum over 48 + rpc.length and seqNum == rx_seq, then copies 48 + rpc.length bytes to out |
| 256 | * and advances the CPU's readPtr (kept in the command queue at rxHdrOff, SWAP_RX). Returns 1 on a |
| 257 | * message, 0 when the queue is empty, -1 with *why on a corrupt queue or message. */ |
| 258 | int gsp_mq_recv(gsp_mq *q, uint8_t *out, uint32_t cap, uint32_t *function, uint32_t *rpc_result, |
| 259 | const char **why); |
| 260 | |
| 261 | /* Shared helpers, also used by the tests. */ |
| 262 | uint32_t gsp_checksum32(const uint8_t *p, uint32_t len); |
| 263 | uint64_t gsp_init_arg_id(const char *name); |
| 264 | uint32_t gsp_reg_table_size(const gsp_reg_entry *e, uint32_t n); |
| 265 | |
| 266 | #endif |