CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* |
| 3 | * m16_native.c -- M16 libcuda-free native accelerator implementation. |
| 4 | * |
| 5 | * Implements clean, reusable submission wrappers around nvrm/ without |
| 6 | * any closed NVIDIA userspace libraries or hardcoded addresses. |
| 7 | */ |
| 8 | #include "m16_native.h" |
| 9 | #include "cpu_barrier.h" |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <time.h> |
| 14 | #include <unistd.h> |
| 15 | #if defined(CUDAPP_MIGRATION_PENDING_C_M16NATIVE) |
| 16 | #define ENG_C_PART 1 |
| 17 | #endif |
| 18 | #include "omega_eng_dec.h" |
| 19 | #include "omega_fixture_rest.h" |
| 20 | |
| 21 | int m16_native_open(M16NativeContext *ctx) { |
| 22 | if (!ctx) return -1; |
| 23 | memset(ctx, 0, sizeof(*ctx)); |
| 24 | if (nvrm_open(&ctx->rm) == 0) return 0; |
| 25 | nvrm_close(&ctx->rm); |
| 26 | return -1; |
| 27 | } |
| 28 | |
| 29 | int m16_native_alloc_memory(M16NativeContext *ctx, size_t size, void **cpu_addr, uint64_t *gpu_va) { |
| 30 | if (!ctx || !cpu_addr || !gpu_va) return -1; |
| 31 | for (size_t i = 0; i < sizeof ctx->legacy_allocs / sizeof ctx->legacy_allocs[0]; i++) { |
| 32 | NvrmMem *mem = &ctx->legacy_allocs[i]; |
| 33 | if (mem->handle) continue; |
| 34 | if (nvrm_alloc(&ctx->rm, size, mem) != 0) return -1; |
| 35 | *cpu_addr = mem->cpu; |
| 36 | *gpu_va = mem->va; |
| 37 | return 0; |
| 38 | } |
| 39 | return -1; |
| 40 | } |
| 41 | |
| 42 | int m16_native_free_memory(M16NativeContext *ctx, void *cpu_addr) { |
| 43 | if (!ctx || !cpu_addr) return -1; |
| 44 | for (size_t i = 0; i < sizeof ctx->legacy_allocs / sizeof ctx->legacy_allocs[0]; i++) { |
| 45 | if (ctx->legacy_allocs[i].cpu == cpu_addr && ctx->legacy_allocs[i].handle) |
| 46 | return nvrm_free(&ctx->rm, &ctx->legacy_allocs[i]); |
| 47 | } |
| 48 | return -1; |
| 49 | } |
| 50 | |
| 51 | int m16_native_create_channel(M16NativeContext *ctx) { |
| 52 | if (!ctx) return -1; |
| 53 | if (nvrm_channel(&ctx->rm) != 0) return -1; |
| 54 | /* The CPU rewrites this command buffer between retired submissions. |
| 55 | * Bypass the GPU cache so the next submission reads the new commands. */ |
| 56 | if (nvrm_alloc_gpu_uncached(&ctx->rm, 0x1000, &ctx->pb_mem) != 0) return -1; |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | int m16_native_build_release(uint32_t *words, uint64_t marker_va, uint32_t payload, uint32_t compute_class) { |
| 61 | if (eng_on()) { |
| 62 | uint64_t in[4], w[16]; |
| 63 | int i; |
| 64 | in[0] = words ? 1u : 0u; |
| 65 | in[1] = marker_va; |
| 66 | in[2] = payload; |
| 67 | in[3] = compute_class; |
| 68 | if (eng_ask(ENG_OP_M16REL, in, 4, w) != 0 || w[0] == 0) return -1; |
| 69 | for (i = 0; i < 8; i++) words[i] = (uint32_t)w[1 + i]; |
| 70 | return (int)w[0]; |
| 71 | } |
| 72 | if (!words) return -1; |
| 73 | uint32_t n = 0; |
| 74 | /* Bind compute class to subchannel 1 */ |
| 75 | words[n++] = nvrm_mthd(1, 0x0000, 1); |
| 76 | words[n++] = compute_class; |
| 77 | |
| 78 | /* Semaphore release method packet on subchannel 0 */ |
| 79 | words[n++] = nvrm_mthd(0, 0x005c, 5); |
| 80 | words[n++] = (uint32_t)marker_va; |
| 81 | words[n++] = (uint32_t)(marker_va >> 32); |
| 82 | words[n++] = payload; |
| 83 | words[n++] = 0; |
| 84 | words[n++] = 0x1 | (1u << 20); /* RELEASE | WFI */ |
| 85 | return (int)n; |
| 86 | } |
| 87 | |
| 88 | int m16_native_enqueue_methods(M16NativeContext *ctx, const uint32_t *methods, size_t count) { |
| 89 | if (!ctx || !methods || count == 0) return -1; |
| 90 | if (count > UINT32_MAX || count > ctx->pb_mem.size / sizeof(uint32_t)) return -1; |
| 91 | memcpy(ctx->pb_mem.cpu, methods, count * sizeof(uint32_t)); |
| 92 | nvrm_cpu_barrier(); |
| 93 | return nvrm_enqueue(&ctx->rm, &ctx->pb_mem, 0, (uint32_t)count); |
| 94 | } |
| 95 | |
| 96 | void m16_native_ring_doorbell(M16NativeContext *ctx) { |
| 97 | if (ctx) nvrm_ring(&ctx->rm); |
| 98 | } |
| 99 | |
| 100 | int m16_native_submit_methods(M16NativeContext *ctx, const uint32_t *methods, size_t count) { |
| 101 | if (m16_native_enqueue_methods(ctx, methods, count) != 0) return -1; |
| 102 | m16_native_ring_doorbell(ctx); |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static uint64_t now_ns(void) { |
| 107 | struct timespec t; |
| 108 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 109 | return omega_fixture_calc(FR_NS, (uint64_t)t.tv_sec, (uint64_t)t.tv_nsec, 0); |
| 110 | } |
| 111 | |
| 112 | int m16_native_wait_marker(volatile uint32_t *marker, uint32_t expected, uint64_t timeout_ms) { |
| 113 | return m16_native_wait_marker_ge(marker, expected, timeout_ms); |
| 114 | } |
| 115 | |
| 116 | static int marker_reached(uint32_t observed, uint32_t target) { |
| 117 | if (eng_on()) { |
| 118 | uint64_t in[2] = {observed, target}, w[16]; |
| 119 | if (eng_ask(ENG_OP_SEQGE, in, 2, w) != 0) return -1; |
| 120 | return w[0] == 1; |
| 121 | } |
| 122 | return (int32_t)(observed - target) >= 0; |
| 123 | } |
| 124 | |
| 125 | int m16_native_wait_marker_ge(volatile uint32_t *marker, uint32_t target, uint64_t timeout_ms) { |
| 126 | if (!marker) return -1; |
| 127 | uint64_t duration, end; |
| 128 | if (__builtin_mul_overflow(timeout_ms, 1000000ull, &duration) || |
| 129 | __builtin_add_overflow(now_ns(), duration, &end)) end = UINT64_MAX; |
| 130 | while (now_ns() < end) { |
| 131 | int reached = marker_reached(*marker, target); |
| 132 | if (reached != 0) return reached == 1 ? 0 : -1; |
| 133 | usleep(50); |
| 134 | nvrm_cpu_relax(); |
| 135 | } |
| 136 | return marker_reached(*marker, target) == 1 ? 0 : -1; |
| 137 | } |
| 138 | |
| 139 | int m16_native_close(M16NativeContext *ctx) { |
| 140 | return ctx ? nvrm_close(&ctx->rm) : -1; |
| 141 | } |