interplane.Get Omega
Read in your language

physics-mv

FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.

Brought by drakestapleton

branch main Branches Tags History

physics-mv / m16 / m16_native.c

163 lines · 6398 bytes Raw Blame History
1/*
2 * m16_native.c -- M16 libcuda-free native accelerator implementation.
3 *
4 * Implements clean, reusable submission wrappers around nvrm/ without
5 * any closed NVIDIA userspace libraries or hardcoded addresses.
6 */
7#include "m16_native.h"
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <time.h>
12#include <unistd.h>
13
14int m16_native_open(M16NativeContext *ctx) {
15 if (!ctx) return -1;
16 memset(ctx, 0, sizeof(*ctx));
17 if (nvrm_open(&ctx->rm) == 0) return 0;
18 /* nvrm_close records any teardown failure in rm.err; keep the reason the
19 * open failed first, so a caller printing rm.err names it (DRA-351). */
20 char open_err[sizeof ctx->rm.err];
21 memcpy(open_err, ctx->rm.err, sizeof open_err);
22 nvrm_close(&ctx->rm);
23 if (strcmp(open_err, ctx->rm.err) != 0) {
24 char close_err[sizeof ctx->rm.err];
25 memcpy(close_err, ctx->rm.err, sizeof close_err);
26 /* Both reasons may not fit; the open reason comes first and survives. */
27 int n = snprintf(ctx->rm.err, sizeof ctx->rm.err, "%s (then teardown: %s)", open_err, close_err);
28 if (n < 0) memcpy(ctx->rm.err, open_err, sizeof open_err);
29 }
30 return -1;
31}
32
33int m16_native_alloc_memory(M16NativeContext *ctx, size_t size, void **cpu_addr, uint64_t *gpu_va) {
34 if (!ctx || !cpu_addr || !gpu_va) return -1;
35 for (size_t i = 0; i < sizeof ctx->legacy_allocs / sizeof ctx->legacy_allocs[0]; i++) {
36 NvrmMem *mem = &ctx->legacy_allocs[i];
37 if (mem->handle) continue;
38 if (nvrm_alloc(&ctx->rm, size, mem) != 0) return -1;
39 *cpu_addr = mem->cpu;
40 *gpu_va = mem->va;
41 return 0;
42 }
43 return -1;
44}
45
46int m16_native_free_memory(M16NativeContext *ctx, void *cpu_addr) {
47 if (!ctx || !cpu_addr) return -1;
48 for (size_t i = 0; i < sizeof ctx->legacy_allocs / sizeof ctx->legacy_allocs[0]; i++) {
49 if (ctx->legacy_allocs[i].cpu == cpu_addr && ctx->legacy_allocs[i].handle)
50 return nvrm_free(&ctx->rm, &ctx->legacy_allocs[i]);
51 }
52 return -1;
53}
54
55/* Uncached sibling of m16_native_alloc_memory. nvos.h, NVOS32_ATTR2_GPU_CACHEABLE_YES:
56 * "For system memory this will not be coherent with direct CPU mappings." A word the CPU
57 * polls must live in the NO class, or the chip's store sits in its L2 and arrives about a
58 * second later, sometimes never (spark-hardware-manual rule 4.1). */
59int m16_native_alloc_memory_uncached(M16NativeContext *ctx, size_t size, void **cpu_addr, uint64_t *gpu_va) {
60 if (!ctx || !cpu_addr || !gpu_va) return -1;
61 for (size_t i = 0; i < sizeof ctx->legacy_allocs / sizeof ctx->legacy_allocs[0]; i++) {
62 NvrmMem *mem = &ctx->legacy_allocs[i];
63 if (mem->handle) continue;
64 if (nvrm_alloc_gpu_uncached(&ctx->rm, size, mem) != 0) return -1;
65 *cpu_addr = mem->cpu;
66 *gpu_va = mem->va;
67 return 0;
68 }
69 return -1;
70}
71
72/* The hardened marker stream, the same block m16/m16_concurrent.c proved on the chip:
73 * WFI release of payload at marker_va, L2_FLUSH_DIRTY mem-op (NVC96F_MEM_OP_A..D =
74 * 0x28..0x34, D bits 31:27 = 0x10), then a second WFI release of M16_MARKER2_PAYLOAD at
75 * marker_va + M16_MARKER2_OFF. Same block as omega src/omega_numeric_gb10.c (C3). */
76int m16_native_build_release_flushed(uint32_t *w, uint64_t marker_va, uint32_t payload, uint32_t cls) {
77 int n = m16_native_build_release(w, marker_va, payload, cls);
78 if (n < 0) return -1;
79 w[n++] = nvrm_mthd(0, 0x0028, 4);
80 w[n++] = 0;
81 w[n++] = 0;
82 w[n++] = 0;
83 w[n++] = (0x10u << 27);
84 uint64_t m2 = marker_va + M16_MARKER2_OFF;
85 w[n++] = nvrm_mthd(0, 0x005c, 5);
86 w[n++] = (uint32_t)m2;
87 w[n++] = (uint32_t)(m2 >> 32);
88 w[n++] = M16_MARKER2_PAYLOAD;
89 w[n++] = 0;
90 w[n++] = 0x1 | (1u << 20);
91 return n;
92}
93
94int m16_native_create_channel(M16NativeContext *ctx) {
95 if (!ctx) return -1;
96 if (nvrm_channel(&ctx->rm) != 0) return -1;
97 /* The CPU rewrites this command buffer between retired submissions.
98 * Bypass the GPU cache so the next submission reads the new commands. */
99 if (nvrm_alloc_gpu_uncached(&ctx->rm, 0x1000, &ctx->pb_mem) != 0) return -1;
100 return 0;
101}
102
103int m16_native_build_release(uint32_t *words, uint64_t marker_va, uint32_t payload, uint32_t compute_class) {
104 if (!words) return -1;
105 uint32_t n = 0;
106 /* Bind compute class to subchannel 1 */
107 words[n++] = nvrm_mthd(1, 0x0000, 1);
108 words[n++] = compute_class;
109
110 /* Semaphore release method packet on subchannel 0 */
111 words[n++] = nvrm_mthd(0, 0x005c, 5);
112 words[n++] = (uint32_t)marker_va;
113 words[n++] = (uint32_t)(marker_va >> 32);
114 words[n++] = payload;
115 words[n++] = 0;
116 words[n++] = 0x1 | (1u << 20); /* RELEASE | WFI */
117 return (int)n;
118}
119
120int m16_native_enqueue_methods(M16NativeContext *ctx, const uint32_t *methods, size_t count) {
121 if (!ctx || !methods || count == 0) return -1;
122 if (count > UINT32_MAX || count > ctx->pb_mem.size / sizeof(uint32_t)) return -1;
123 memcpy(ctx->pb_mem.cpu, methods, count * sizeof(uint32_t));
124 __asm__ volatile("dsb sy" ::: "memory");
125 return nvrm_enqueue(&ctx->rm, &ctx->pb_mem, 0, (uint32_t)count);
126}
127
128void m16_native_ring_doorbell(M16NativeContext *ctx) {
129 if (ctx) nvrm_ring(&ctx->rm);
130}
131
132int m16_native_submit_methods(M16NativeContext *ctx, const uint32_t *methods, size_t count) {
133 if (m16_native_enqueue_methods(ctx, methods, count) != 0) return -1;
134 m16_native_ring_doorbell(ctx);
135 return 0;
136}
137
138static uint64_t now_ns(void) {
139 struct timespec t;
140 clock_gettime(CLOCK_MONOTONIC, &t);
141 return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec;
142}
143
144int m16_native_wait_marker(volatile uint32_t *marker, uint32_t expected, uint64_t timeout_ms) {
145 return m16_native_wait_marker_ge(marker, expected, timeout_ms);
146}
147
148int m16_native_wait_marker_ge(volatile uint32_t *marker, uint32_t target, uint64_t timeout_ms) {
149 if (!marker) return -1;
150 uint64_t duration, end;
151 if (__builtin_mul_overflow(timeout_ms, 1000000ull, &duration) ||
152 __builtin_add_overflow(now_ns(), duration, &end)) end = UINT64_MAX;
153 while (now_ns() < end) {
154 if ((int32_t)(*marker - target) >= 0) return 0;
155 usleep(50);
156 __asm__ volatile("yield");
157 }
158 return ((int32_t)(*marker - target) >= 0) ? 0 : -1;
159}
160
161int m16_native_close(M16NativeContext *ctx) {
162 return ctx ? nvrm_close(&ctx->rm) : -1;
163}