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 / gpu / gmmu.h
| 1 | /* gmmu.h -- GB10 GPU page tables (cut D, host side), freestanding. |
| 2 | * |
| 3 | * Format source (DOCUMENTED, open-gpu-kernel-modules tag 580.173.02): for |
| 4 | * GB10B the driver HAL picks kgmmuFmtInitLevels_GB10X and |
| 5 | * kgmmuFmtInitPte/Pde_GH10X (src/nvidia/generated/g_kern_gmmu_nvoc.c, the |
| 6 | * "else" branch after T234D/T264D, TU/GA/AD and GH100); field layouts are |
| 7 | * NV_MMU_VER3_* in src/common/inc/swref/published/hopper/gh100/dev_mmu.h. |
| 8 | * |
| 9 | * 57-bit VA. PD4 [56] PD3 [55:47] PD2 [46:38] PD1 [37:29] |
| 10 | * PD0 [28:21] (dual 16-byte PDE: big half, small half; or a 2 MiB PTE) |
| 11 | * PT_BIG [20:16] (64 KiB pages) PT_SMALL [20:12] (4 KiB pages) |
| 12 | * |
| 13 | * PD1 may hold a 512 MiB PTE and PD2 a 256 GiB PTE on GB10X; this code never |
| 14 | * writes those (it maps 4 KiB, 64 KiB and 2 MiB pages only) but the walker |
| 15 | * in gpu_sim.c decodes them so a stray big PTE is still translated the way |
| 16 | * the format says. |
| 17 | * |
| 18 | * Names: this code calls the bit-56 level PD4 and the dual level PD0, as the |
| 19 | * ASCII diagram in kern_gmmu_fmt_gh10x.c does. The driver's pageLevelIdTag |
| 20 | * runs the other way (bit 56 is MMU_FMT_PT_SURF_ID_PD0, the dual level |
| 21 | * PD4); tests assert bit ranges, not names. |
| 22 | * |
| 23 | * A 2 MiB PTE sits in the low 8 bytes of the 16-byte PD0 slot, high 8 bytes |
| 24 | * zero: the driver's VER3 PTE descriptor (kgmmuFmtInitPte_GH10X) places its |
| 25 | * fields at bits 0..63 of the entry. Driver-sourced; the chip must confirm. |
| 26 | * |
| 27 | * Whether the GB10 silicon walks exactly this format is UNKNOWN until a chip |
| 28 | * run; it is what the pinned driver programs for it. |
| 29 | */ |
| 30 | #ifndef CK_GMMU_H |
| 31 | #define CK_GMMU_H |
| 32 | |
| 33 | #include <stdint.h> |
| 34 | |
| 35 | #define CK_GMMU_VA_BITS 57u |
| 36 | #define CK_GMMU_PA_BITS 52u /* NV_MMU_VER3_PTE_ADDRESS 51:12 */ |
| 37 | #define CK_GMMU_SZ_4K 0x1000ull |
| 38 | #define CK_GMMU_SZ_64K 0x10000ull |
| 39 | #define CK_GMMU_SZ_2M 0x200000ull |
| 40 | #define CK_GMMU_PT_BYTES 4096u /* every level instance this code allocates */ |
| 41 | |
| 42 | /* PDE aperture (NV_MMU_VER3_PDE_APERTURE 2:1). */ |
| 43 | #define CK_GMMU_PDE_AP_INVALID 0u |
| 44 | #define CK_GMMU_PDE_AP_VIDMEM 1u |
| 45 | #define CK_GMMU_PDE_AP_SYS_COH 2u |
| 46 | #define CK_GMMU_PDE_AP_SYS_NC 3u |
| 47 | /* PTE aperture (NV_MMU_VER3_PTE_APERTURE 2:1); note the different numbering. */ |
| 48 | #define CK_GMMU_PTE_AP_VIDMEM 0u |
| 49 | #define CK_GMMU_PTE_AP_PEER 1u |
| 50 | #define CK_GMMU_PTE_AP_SYS_COH 2u |
| 51 | #define CK_GMMU_PTE_AP_SYS_NC 3u |
| 52 | /* PTE PCF (NV_MMU_VER3_PTE_PCF 7:3), the values this code uses. */ |
| 53 | #define CK_GMMU_PCF_RW_CACHED 0x00u /* REGULAR_RW_ATOMIC_CACHED_ACE */ |
| 54 | #define CK_GMMU_PCF_RW_UNCACHED 0x01u /* REGULAR_RW_ATOMIC_UNCACHED_ACE */ |
| 55 | #define CK_GMMU_PCF_RO_CACHED 0x04u /* REGULAR_RO_ATOMIC_CACHED_ACE */ |
| 56 | #define CK_GMMU_PCF_RO_UNCACHED 0x05u /* REGULAR_RO_ATOMIC_UNCACHED_ACE */ |
| 57 | /* PDE PCF (NV_MMU_VER3_PDE_PCF 5:3): VALID_CACHED_ATS_NOT_ALLOWED. */ |
| 58 | #define CK_GMMU_PDE_PCF_CACHED_NO_ATS 0x2u |
| 59 | |
| 60 | /* Entry encoders: pure bit packing, exactly the dev_mmu.h fields. */ |
| 61 | uint64_t ck_gmmu_pde(uint64_t pa, uint32_t aperture); |
| 62 | void ck_gmmu_dual_pde(uint64_t out[2], uint64_t big_pa, uint32_t big_ap, uint64_t small_pa, uint32_t small_ap); |
| 63 | uint64_t ck_gmmu_pte(uint64_t pa, uint32_t aperture, uint32_t pcf, uint32_t kind); |
| 64 | |
| 65 | /* The CPU's view of the GPU-visible physical window. GB10 memory is unified |
| 66 | * (one LPDDR5x pool); `cpu` is where the CPU sees the bytes at `pa_base`. |
| 67 | * The CPU address and the GPU physical address are carried as distinct |
| 68 | * values; nothing here assumes they are equal. */ |
| 69 | struct ck_gpu_window { |
| 70 | uint8_t *cpu; |
| 71 | uint64_t pa_base; |
| 72 | uint64_t size; |
| 73 | }; |
| 74 | |
| 75 | /* Register access. Offsets are absolute BAR0 offsets (the driver writes |
| 76 | * them window-relative through GPU_VREG_WR32; this code adds the window |
| 77 | * base itself). A kernel would back this with |
| 78 | * ck_mmio_map; the host tests back it with the simulator in gpu_sim.c. */ |
| 79 | struct ck_gpu_regs { |
| 80 | uint32_t (*rd)(void *ctx, uint32_t off); |
| 81 | void (*wr)(void *ctx, uint32_t off, uint32_t val); |
| 82 | void *ctx; |
| 83 | }; |
| 84 | |
| 85 | /* TLB invalidate registers as the driver programs them on GB10B |
| 86 | * (kgmmuCommitTlbInvalidate_GB100 + kgmmuSetPdbToInvalidate_TU102 + |
| 87 | * kgmmuCheckPendingInvalidates_TU102; gb100/dev_vm.h; window base |
| 88 | * gpuGetVirtRegPhysOffset_TU102 = DRF_BASE(NV_VIRTUAL_FUNCTION_FULL_PHYS_OFFSET)). */ |
| 89 | #define CK_GPU_VF_BASE 0x00B80000u |
| 90 | #define CK_GPU_REG_INVAL_PDB (CK_GPU_VF_BASE + 0x30A0u) |
| 91 | #define CK_GPU_REG_INVAL_UPPER (CK_GPU_VF_BASE + 0x30A4u) |
| 92 | #define CK_GPU_REG_INVAL (CK_GPU_VF_BASE + 0x30B0u) |
| 93 | #define CK_GPU_INVAL_ALL_VA (1u << 0) |
| 94 | #define CK_GPU_INVAL_ALL_PDB (1u << 1) |
| 95 | #define CK_GPU_INVAL_SYS_MEMBAR (1u << 6) |
| 96 | #define CK_GPU_INVAL_TRIGGER (1u << 31) |
| 97 | #define CK_GPU_INVAL_PDB_AP_SYS (1u << 1) /* MMU_INVALIDATE_PDB_APERTURE 1:1 SYS_MEM */ |
| 98 | /* Fields left 0 on purpose, matching the driver's plain invalidate: |
| 99 | * HUBTLB_ONLY, REPLAY (NONE), ACK (NONE_REQUIRED), CACHE_LEVEL (ALL), |
| 100 | * ALL_PDB (one PDB), INVAL_SCOPE 16:15 (ALL_TLBS; GB10B sets it through |
| 101 | * kgmmuSetTlbInvalidationScope_GA100), SYS_MEMBAR (set only by the Turing |
| 102 | * membar workaround; a stub on GB10B: g_kern_gmmu_nvoc.c). The PDB aperture for GB10 unified memory is taken as |
| 103 | * SYS_MEM: UNKNOWN on the chip. */ |
| 104 | |
| 105 | enum ck_gmmu_status { |
| 106 | CK_GMMU_OK = 0, |
| 107 | CK_GMMU_EINVAL, /* bad argument: size, page size, flags */ |
| 108 | CK_GMMU_EALIGN, /* VA, PA or size not aligned to the page size */ |
| 109 | CK_GMMU_ERANGE, /* VA beyond 57 bits, PA outside the window, or PA over the page-table pool */ |
| 110 | CK_GMMU_EBUSY, /* overlaps an existing mapping or a different page size */ |
| 111 | CK_GMMU_ENOMEM, /* page-table pool exhausted */ |
| 112 | CK_GMMU_ENOENT, /* unmap of a range that is not fully mapped */ |
| 113 | CK_GMMU_ETIMEDOUT, /* invalidate never completed */ |
| 114 | CK_GMMU_EDEAD, /* the address space latched a failure (invalidate timeout, or a PDE |
| 115 | * read back that names no live page of the pool); refuses everything */ |
| 116 | }; |
| 117 | |
| 118 | #define CK_GMMU_MAX_PT 64u /* page-table pages per address space (bounded) */ |
| 119 | |
| 120 | struct ck_gmmu_as { |
| 121 | const struct ck_gpu_window *win; |
| 122 | uint64_t root_pa; /* PD4 page */ |
| 123 | uint64_t pt_pa[CK_GMMU_MAX_PT]; /* page-table pool, carved by the caller */ |
| 124 | uint8_t pt_used[CK_GMMU_MAX_PT]; |
| 125 | uint32_t pt_count; |
| 126 | uint32_t dead; /* latched after an invalidate timeout or a corrupt PDE */ |
| 127 | }; |
| 128 | |
| 129 | /* pool_pa: CK_GMMU_MAX_PT * 4 KiB inside the window, 4 KiB aligned; pages |
| 130 | * are zeroed here. The first page becomes the PD4 root. */ |
| 131 | int ck_gmmu_as_init(struct ck_gmmu_as *as, const struct ck_gpu_window *win, uint64_t pool_pa, uint32_t pool_pages); |
| 132 | |
| 133 | /* Map [va, va+size) -> [pa, pa+size) with one page size (4K, 64K or 2M). |
| 134 | * Refuses: zero size, wrap, misalignment, VA >= 2^57, PA outside the window, |
| 135 | * any page already mapped (any size), a 2 MiB page over an existing page |
| 136 | * table, small/big pages under an existing 2 MiB PTE, pool exhaustion. |
| 137 | * All-or-nothing: on refusal no entry is left changed. */ |
| 138 | int ck_gmmu_map(struct ck_gmmu_as *as, uint64_t va, uint64_t pa, uint64_t size, uint64_t page, uint32_t aperture, uint32_t pcf); |
| 139 | /* Unmap a range mapped with `page`; refuses (no change) unless every page |
| 140 | * in it is mapped with that page size. Caller must invalidate after. */ |
| 141 | int ck_gmmu_unmap(struct ck_gmmu_as *as, uint64_t va, uint64_t size, uint64_t page); |
| 142 | |
| 143 | /* Invalidate this address space's TLB entries (ALL_VA, one PDB), |
| 144 | * then poll TRIGGER until clear for at most `poll_budget` reads. On timeout |
| 145 | * the address space latches dead (UNKNOWN whether the GPU still uses stale |
| 146 | * translations, so nothing mapped through it may be reused). */ |
| 147 | int ck_gmmu_invalidate(struct ck_gmmu_as *as, const struct ck_gpu_regs *r, uint32_t poll_budget); |
| 148 | |
| 149 | #endif |