interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / omega_numeric_ldst_gb10.c

390 lines · 22678 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* E1 row 2: general global load/store kernels for the GB10. See omega_numeric_ldst_gb10.h. */
18#include "omega_numeric_ldst_gb10.h"
19#include "omega_numeric.h"
20#include "omega_numeric_divsqrt_gb10.h"
21#include "omega_blackwell_encoder.h"
22#include "omega_blackwell_qmd.h"
23
24#include <stdarg.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
29#ifndef OMEGA_NUMERIC_CPU_ONLY
30#include "omega_blackwell_submit.h"
31#include "omega_numeric_native.h"
32#include "omega_gpu_code_alloc.h"
33#endif
34
35#define NINSN 24u
36#define PRO_INSNS 17u
37#define I_LD_DST 12u /* the load's destination register: aligned for 128-bit */
38#define I_IMAD_IN 12u /* prologue instruction indices patched */
39#define I_LDG_A 13u
40#define I_IMAD_OUT 16u
41#define I_IADD3 17u
42#define I_STG 18u
43#define I_EXIT 19u
44#define I_BRA 20u
45#define I_NOP 21u
46
47static const size_t LD_BYTES[OMEGA_LD_COUNT] = { 1, 1, 2, 2, 4, 8, 16 };
48static const size_t ST_BYTES[OMEGA_ST_COUNT] = { 1, 2, 4, 8, 16 };
49static const unsigned LD_SIZE[OMEGA_LD_COUNT] = { 0, 1, 2, 3, 4, 5, 6 };
50static const unsigned ST_SIZE[OMEGA_ST_COUNT] = { 0, 2, 4, 5, 6 };
51static const char *const LD_SUFFIX[OMEGA_LD_COUNT] = { ".E.U8", ".E.S8", ".E.U16", ".E.S16", ".E", ".E.64", ".E.128" };
52static const char *const ST_SUFFIX[OMEGA_ST_COUNT] = { ".E.U8", ".E.U16", ".E", ".E.64", ".E.128" };
53static const char *const LD_NAME[OMEGA_LD_COUNT] = { "U8", "S8", "U16", "S16", "B32", "B64", "B128" };
54static const char *const ST_NAME[OMEGA_ST_COUNT] = { "B8", "B16", "B32", "B64", "B128" };
55
56/* Texts of prologue instructions 0..16 (same as src/omega_numeric_divsqrt_gb10.c PROLOGUE_TEXT). */
57static const char *const PROLOGUE_TEXT[PRO_INSNS] = {
58 "LDC R1, c[0x0][0x37c]", "S2R R9, SR_CTAID.X", "LDCU UR4, c[0x0][0x360]", "S2R R0, SR_TID.X",
59 "LDCU UR5, c[0x0][0x398]", "IMAD R9, R9, UR4, R0", "ISETP.GE.U32.AND P0, PT, R9, UR5, PT", "@P0 EXIT",
60 "LDC.64 R2, c[0x0][0x380]", "LDCU.64 UR4, c[0x0][0x358]", "LDC.64 R4, c[0x0][0x388]",
61 "LDC.64 R6, c[0x0][0x390]", "IMAD.WIDE.U32 R2, R9, 0x4, R2", "LDG.E R2, desc[UR4][R2.64]",
62 "IMAD.WIDE.U32 R4, R9, 0x4, R4", "LDG.E R5, desc[UR4][R4.64]", "IMAD.WIDE.U32 R6, R9, 0x4, R6",
63};
64
65size_t omega_ld_bytes(OmegaLdKind k) { return k < OMEGA_LD_COUNT ? LD_BYTES[k] : 0; }
66size_t omega_st_bytes(OmegaStKind k) { return k < OMEGA_ST_COUNT ? ST_BYTES[k] : 0; }
67
68const char *omega_ldst_describe(const OmegaLdstSpec *s, char *buf, size_t len) {
69 snprintf(buf, len, "%s@%u%+d -> %s@%u%+d", s->ld < OMEGA_LD_COUNT ? LD_NAME[s->ld] : "?", s->in_stride, (int)s->in_off,
70 s->st < OMEGA_ST_COUNT ? ST_NAME[s->st] : "?", s->out_stride, (int)s->out_off);
71 return buf;
72}
73
74static int refuse(char *why, size_t n, const char *fmt, ...) {
75 if (why && n) { va_list ap; va_start(ap, fmt); vsnprintf(why, n, fmt, ap); va_end(ap); }
76 return -1;
77}
78
79int omega_ldst_check_spec(const OmegaLdstSpec *s, size_t count, char *why, size_t why_len) {
80 if (!s) return refuse(why, why_len, "no spec");
81 if (s->ld >= OMEGA_LD_COUNT) return refuse(why, why_len, "unknown load kind %u", s->ld);
82 if (s->st >= OMEGA_ST_COUNT) return refuse(why, why_len, "unknown store kind %u", s->st);
83 size_t lb = LD_BYTES[s->ld], sb = ST_BYTES[s->st], fill = lb < 4 ? 4 : lb;
84 if (sb > fill) return refuse(why, why_len, "store of %zu bytes is wider than the %zu bytes the load fills", sb, fill);
85 if (s->in_stride < lb || s->in_stride % lb) return refuse(why, why_len, "input stride %u is not a multiple of %zu >= %zu", s->in_stride, lb, lb);
86 if (s->out_stride < sb || s->out_stride % sb) return refuse(why, why_len, "output stride %u is not a multiple of %zu >= %zu", s->out_stride, sb, sb);
87 if (s->in_stride >= (1u << 24) || s->out_stride >= (1u << 24)) return refuse(why, why_len, "stride must be below 2^24");
88 if (s->in_off < -(1 << 23) || s->in_off >= (1 << 23) || s->out_off < -(1 << 23) || s->out_off >= (1 << 23))
89 return refuse(why, why_len, "offset outside the signed 24-bit field");
90 if (s->in_off % (int32_t)lb) return refuse(why, why_len, "input offset %d is not a multiple of %zu", (int)s->in_off, lb);
91 if (s->out_off % (int32_t)sb) return refuse(why, why_len, "output offset %d is not a multiple of %zu", (int)s->out_off, sb);
92 if (count == 0 || count > OMEGA_DS_MAX_BATCH) return refuse(why, why_len, "count %zu outside 1..%u", count, OMEGA_DS_MAX_BATCH);
93 return 0;
94}
95
96int omega_ldst_extent(const OmegaLdstSpec *s, size_t count, int64_t *in_lo, int64_t *in_hi, int64_t *out_lo, int64_t *out_hi) {
97 if (omega_ldst_check_spec(s, count, NULL, 0) != 0) return -1;
98 int64_t n1 = (int64_t)count - 1;
99 *in_lo = s->in_off;
100 *in_hi = (int64_t)s->in_off + n1 * (int64_t)s->in_stride + (int64_t)LD_BYTES[s->ld];
101 *out_lo = s->out_off;
102 *out_hi = (int64_t)s->out_off + n1 * (int64_t)s->out_stride + (int64_t)ST_BYTES[s->st];
103 return 0;
104}
105
106/* ---- kernel -------------------------------------------------------------------- */
107
108static void put_words(uint8_t *p, const uint32_t w[4]) {
109 for (int i = 0; i < 4; i++) { p[4 * i] = (uint8_t)w[i]; p[4 * i + 1] = (uint8_t)(w[i] >> 8); p[4 * i + 2] = (uint8_t)(w[i] >> 16); p[4 * i + 3] = (uint8_t)(w[i] >> 24); }
110}
111
112int omega_ldst_build_kernel(const OmegaLdstSpec *s, uint8_t *code, size_t max, size_t *out_len) {
113 uint8_t va[OMEGA_BW_VECADD_CODE_SIZE];
114 size_t vl = 0;
115 if (!code || max < OMEGA_LDST_CODE_BYTES || omega_ldst_check_spec(s, 1, NULL, 0) != 0) return -1;
116 if (omega_blackwell_encode_vecadd(va, sizeof(va), &vl) != 0 || vl != OMEGA_BW_VECADD_CODE_SIZE) return -1;
117 memset(code, 0, OMEGA_LDST_CODE_BYTES);
118 for (size_t k = 0; k < I_EXIT + 2; k++) memcpy(code + 16 * k, va + 16 * k, 16); /* 0..16 prologue; 17,18 overwritten below */
119 memcpy(code + 16 * I_EXIT, va + 16 * 19, 16); /* EXIT */
120 memcpy(code + 16 * I_BRA, va + 16 * 20, 16); /* BRA to self */
121 for (size_t k = I_NOP; k < NINSN; k++) memcpy(code + 16 * k, va + 16 * 21, 16); /* NOP */
122 uint32_t w[4];
123 /* IMAD.WIDE.U32 R2, R9, in_stride, R2 */
124 for (int i = 0; i < 4; i++) { const uint8_t *p = va + 16 * I_IMAD_IN + 4 * i; w[i] = p[0] | p[1] << 8 | p[2] << 16 | (uint32_t)p[3] << 24; }
125 w[1] = s->in_stride;
126 put_words(code + 16 * I_IMAD_IN, w);
127 /* LDG.<kind> R12, desc[UR4][R2.64+off] */
128 w[0] = 0x7981u | (I_LD_DST << 16) | (2u << 24);
129 w[1] = 4u | (((uint32_t)s->in_off & 0xffffffu) << 8);
130 w[2] = 0x0c1e1100u | (LD_SIZE[s->ld] << 9);
131 w[3] = 0x002f2200u;
132 put_words(code + 16 * I_LDG_A, w);
133 /* IMAD.WIDE.U32 R6, R9, out_stride, R6 */
134 for (int i = 0; i < 4; i++) { const uint8_t *p = va + 16 * I_IMAD_OUT + 4 * i; w[i] = p[0] | p[1] << 8 | p[2] << 16 | (uint32_t)p[3] << 24; }
135 w[1] = s->out_stride;
136 put_words(code + 16 * I_IMAD_OUT, w);
137 /* IADD3 R9, PT, PT, R12, R5, RZ : waits on both load barriers (control word of vecadd's IADD3) */
138 w[0] = 0x7210u | (9u << 16) | (I_LD_DST << 24);
139 w[1] = 5u;
140 w[2] = 0x07ffe0ffu;
141 w[3] = 0x010fca00u;
142 put_words(code + 16 * I_IADD3, w);
143 /* STG.<kind> desc[UR4][R6.64+off], R12 */
144 w[0] = 0x7986u | (6u << 24);
145 w[1] = I_LD_DST | (((uint32_t)s->out_off & 0xffffffu) << 8);
146 w[2] = 0x0c101104u | (ST_SIZE[s->st] << 9);
147 w[3] = 0x000fe200u;
148 put_words(code + 16 * I_STG, w);
149 if (out_len) *out_len = OMEGA_LDST_CODE_BYTES;
150 return 0;
151}
152
153static int signed_off(char *dst, size_t n, int32_t off) {
154 if (off == 0) { dst[0] = '\0'; return 0; }
155 return snprintf(dst, n, "+%s0x%x", off < 0 ? "-" : "", (unsigned)(off < 0 ? -(int64_t)off : off));
156}
157
158int omega_ldst_listing(const OmegaLdstSpec *s, char *buf, size_t len) {
159 if (!buf || omega_ldst_check_spec(s, 1, NULL, 0) != 0) return -1;
160 size_t pos = 0;
161 char io[24], oo[24];
162 signed_off(io, sizeof io, s->in_off);
163 signed_off(oo, sizeof oo, s->out_off);
164#define LINE(...) do { int w_ = snprintf(buf + pos, pos < len ? len - pos : 0, __VA_ARGS__); \
165 if (w_ < 0 || pos + (size_t)w_ >= len) { return -1; } \
166 pos += (size_t)w_; } while (0)
167 for (size_t k = 0; k < PRO_INSNS; k++) {
168 if (k == I_IMAD_IN) LINE("%04zx IMAD.WIDE.U32 R2, R9, 0x%x, R2 ;\n", k * 16, s->in_stride);
169 else if (k == I_LDG_A) LINE("%04zx LDG%s R%u, desc[UR4][R2.64%s] ;\n", k * 16, LD_SUFFIX[s->ld], I_LD_DST, io);
170 else if (k == I_IMAD_OUT) LINE("%04zx IMAD.WIDE.U32 R6, R9, 0x%x, R6 ;\n", k * 16, s->out_stride);
171 else LINE("%04zx %s ;\n", k * 16, PROLOGUE_TEXT[k]);
172 }
173 LINE("%04x IADD3 R9, PT, PT, R%u, R5, RZ ;\n", I_IADD3 * 16, I_LD_DST);
174 LINE("%04x STG%s desc[UR4][R6.64%s], R%u ;\n", I_STG * 16, ST_SUFFIX[s->st], oo, I_LD_DST);
175 LINE("%04x EXIT ;\n", I_EXIT * 16);
176 LINE("%04x BRA 0x%x;\n", I_BRA * 16, I_BRA * 16);
177 for (size_t k = I_NOP; k < NINSN; k++) LINE("%04zx NOP;\n", k * 16);
178#undef LINE
179 return 0;
180}
181
182static void get_words(const uint8_t *p, uint32_t w[4]) {
183 for (int i = 0; i < 4; i++) w[i] = p[4 * i] | p[4 * i + 1] << 8 | p[4 * i + 2] << 16 | (uint32_t)p[4 * i + 3] << 24;
184}
185
186int omega_ldst_check_kernel(const OmegaLdstSpec *s, const uint8_t *code, size_t len, char *why, size_t why_len) {
187 uint8_t want[OMEGA_LDST_CODE_BYTES];
188 size_t wl = 0;
189 if (!code || len != OMEGA_LDST_CODE_BYTES) return refuse(why, why_len, "ldst kernel length %zu != %u", len, OMEGA_LDST_CODE_BYTES);
190 /* decode the fields back out of the words, then require a spec-for-spec rebuild */
191 uint32_t w[4];
192 OmegaLdstSpec d = *s;
193 get_words(code + 16 * I_LDG_A, w);
194 unsigned lsz = (w[2] >> 9) & 7u;
195 int found = 0;
196 for (unsigned k = 0; k < OMEGA_LD_COUNT; k++) if (LD_SIZE[k] == lsz) { d.ld = (uint8_t)k; found = 1; }
197 if (!found) return refuse(why, why_len, "ldst: LDG size field %u unknown", lsz); /* CHECK:ldst_ldsize */
198 d.in_off = (int32_t)(w[1] & 0xffffff00u) >> 8;
199 get_words(code + 16 * I_STG, w);
200 unsigned ssz = (w[2] >> 9) & 7u;
201 found = 0;
202 for (unsigned k = 0; k < OMEGA_ST_COUNT; k++) if (ST_SIZE[k] == ssz) { d.st = (uint8_t)k; found = 1; }
203 if (!found) return refuse(why, why_len, "ldst: STG size field %u unknown", ssz); /* CHECK:ldst_stsize */
204 d.out_off = (int32_t)(w[1] & 0xffffff00u) >> 8;
205 get_words(code + 16 * I_IMAD_IN, w);
206 d.in_stride = w[1];
207 get_words(code + 16 * I_IMAD_OUT, w);
208 d.out_stride = w[1];
209 if (omega_ldst_check_spec(&d, 1, why, why_len) != 0) return -1; /* CHECK:ldst_spec */
210 if (d.ld != s->ld || d.st != s->st || d.in_stride != s->in_stride || d.out_stride != s->out_stride ||
211 d.in_off != s->in_off || d.out_off != s->out_off)
212 return refuse(why, why_len, "ldst: kernel encodes a different spec than requested"); /* CHECK:ldst_same_spec */
213 if (omega_ldst_build_kernel(&d, want, sizeof want, &wl) != 0 || wl != len || memcmp(want, code, len) != 0)
214 return refuse(why, why_len, "ldst: kernel words differ from the rebuilt kernel"); /* CHECK:ldst_rebuild */
215 return 0;
216}
217
218/* ---- host model ---------------------------------------------------------------- */
219
220int omega_ldst_host_run(const OmegaLdstSpec *s, const uint8_t *in_buf, size_t in_len, uint8_t *out_buf, size_t out_len,
221 size_t pad, size_t count) {
222 int64_t il, ih, ol, oh;
223 if (!in_buf || !out_buf || omega_ldst_extent(s, count, &il, &ih, &ol, &oh) != 0) return -1;
224 if (il < -(int64_t)pad || ih > (int64_t)in_len - (int64_t)pad || ol < -(int64_t)pad || oh > (int64_t)out_len - (int64_t)pad) return -1;
225 size_t lb = LD_BYTES[s->ld], sb = ST_BYTES[s->st];
226 for (size_t i = 0; i < count; i++) {
227 uint8_t reg[16] = { 0 };
228 const uint8_t *src = in_buf + pad + (int64_t)s->in_off + (int64_t)i * s->in_stride;
229 uint8_t *dst = out_buf + pad + (int64_t)s->out_off + (int64_t)i * s->out_stride;
230 if (s->ld == OMEGA_LD_S8) { int32_t v = (int8_t)src[0]; memcpy(reg, &v, 4); }
231 else if (s->ld == OMEGA_LD_S16) { int32_t v = (int16_t)(src[0] | src[1] << 8); memcpy(reg, &v, 4); }
232 else { for (size_t b = 0; b < lb; b++) reg[b] = src[b]; } /* U8 U16 zero extend: the rest of reg[0..3] is 0 */
233 for (size_t b = 0; b < sb; b++) dst[b] = reg[b];
234 }
235 return 0;
236}
237
238/* ---- chip ---------------------------------------------------------------------- */
239
240static int g_spec_id = -1;
241void omega_ldst_gb10_set_spec_id(int id) { g_spec_id = id; }
242/* One stderr line saying which device step failed, then the device error code. No other behaviour. */
243#define LDST_DEVERR(step, rc) (fprintf(stderr, "OMEGA_DEVERR step=%s rc=%d spec=%d\n", (step), (int)(rc), g_spec_id), OMEGA_NUMERIC_ERR_DEVICE)
244
245int omega_ldst_gb10_run(const OmegaLdstSpec *s, const uint8_t *in_buf, size_t in_len, uint8_t *out_buf, size_t out_len,
246 size_t pad, size_t count) {
247 char err[256];
248 int64_t il, ih, ol, oh;
249 if (!in_buf || !out_buf || omega_ldst_check_spec(s, count, err, sizeof err) != 0) {
250 fprintf(stderr, "omega_ldst_gb10_run: refused: %s\n", err);
251 return OMEGA_NUMERIC_ERR_BAD_ARGS;
252 }
253 if (omega_ldst_extent(s, count, &il, &ih, &ol, &oh) != 0 || il < -(int64_t)pad || ih > (int64_t)in_len - (int64_t)pad ||
254 ol < -(int64_t)pad || oh > (int64_t)out_len - (int64_t)pad || pad > OMEGA_LDST_PAD) {
255 fprintf(stderr, "omega_ldst_gb10_run: refused: access outside the buffers\n");
256 return OMEGA_NUMERIC_ERR_OPERANDS;
257 }
258 uint8_t code[OMEGA_LDST_CODE_BYTES]; /* per call: built before the numeric owner is acquired */
259 size_t code_len = 0;
260 if (omega_ldst_build_kernel(s, code, sizeof code, &code_len) != 0 || omega_ldst_check_kernel(s, code, code_len, err, sizeof err) != 0) {
261 fprintf(stderr, "omega_ldst_gb10_run: refused: %s\n", err);
262 return OMEGA_NUMERIC_ERR_OPERANDS;
263 }
264 {
265 uint32_t q[OMEGA_BW_QMD_WORDS];
266 OmegaBlackwellQmdConfig c = { .code_va = 0x200000000ull, .cbank_va = 0x200100000ull, .scratch_va = 0x200204000ull,
267 .sem_va = 0x200202000ull, .qmd0_va = 0x200200000ull, .qmd1_va = 0x200201000ull,
268 .num_elements = (uint32_t)count, .gpr_count = OMEGA_DS_GPR_COUNT };
269 omega_numeric_launch_shape(count, &c.threads_per_block, &c.grid_width);
270 if (omega_blackwell_build_qmd1(q, &c) != 0 || omega_ds_check_qmd(q, c.code_va, err, sizeof err) != OMEGA_NUMERIC_OK) {
271 fprintf(stderr, "omega_ldst_gb10_run: refused: %s\n", err);
272 return OMEGA_NUMERIC_ERR_OPERANDS;
273 }
274 }
275#ifdef OMEGA_NUMERIC_CPU_ONLY
276 return LDST_DEVERR("cpu_only", OMEGA_NUMERIC_ERR_DEVICE);
277#else
278 static const uint32_t SETUP[18] = {
279 0x20012061, 0x0000cec0, 0x20012092, 0x00000001, 0x200120a8, 0x0000000f, 0x2001255d, 0x00000003,
280 0x2001255e, 0x20000000, 0x2001255f, 0x000fffff, 0x20012557, 0x00000003, 0x20012558, 0x22000000,
281 0x20012559, 0x00000000,
282 };
283 M16NativeContext ctx;
284 int drc;
285 if ((drc = omega_numeric_native_open(&ctx)) != 0) return LDST_DEVERR("open", drc);
286 if ((drc = m16_native_create_channel(&ctx)) != 0) { omega_numeric_native_close(&ctx); return LDST_DEVERR("open", drc); }
287 NvrmMem large_pb;
288 if ((drc = nvrm_alloc(&ctx.rm, 0x10000, &large_pb)) != 0) { omega_numeric_native_close(&ctx); return LDST_DEVERR("alloc", drc); }
289 ctx.pb_mem = large_pb;
290 /* the prologue also reads 4 bytes at index i of the "b" buffer: give it the input, zero padded to 4*count */
291 size_t in_dev = in_len > count * 4 ? in_len : count * 4;
292 size_t in_bytes = (in_dev + 0xfffULL) & ~0xfffULL, out_bytes = (out_len + 0xfffULL) & ~0xfffULL;
293 NvrmMem code_mem, cbank_mem, a_mem, out_mem, marker_mem, qmd_mem;
294 if ((drc = nvrm_alloc(&ctx.rm, omega_gpu_code_alloc_bytes(OMEGA_DS_MAX_CODE_BYTES), &code_mem) /* + 2 KB prefetch tail */) != 0 || (drc = nvrm_alloc(&ctx.rm, 0x1000, &cbank_mem)) != 0 ||
295 (drc = nvrm_alloc(&ctx.rm, in_bytes, &a_mem)) != 0 || (drc = nvrm_alloc(&ctx.rm, out_bytes, &out_mem)) != 0 ||
296 (drc = nvrm_alloc_gpu_uncached(&ctx.rm, 0x1000, &marker_mem)) != 0 || (drc = nvrm_alloc(&ctx.rm, 0x10000, &qmd_mem)) != 0) {
297 omega_numeric_native_close(&ctx);
298 return LDST_DEVERR("alloc", drc);
299 }
300 memset(a_mem.cpu, 0, in_bytes);
301 memcpy(a_mem.cpu, in_buf, in_len);
302 memcpy(out_mem.cpu, out_buf, out_len);
303 memcpy(code_mem.cpu, code, code_len);
304
305 uint32_t cbank_data[OMEGA_BW_CBANK_DRIVER_WORDS];
306 omega_blackwell_build_cbank_driver(cbank_data, cbank_mem.va);
307 cbank_data[223] = 0;
308 uint64_t ain = a_mem.va + pad, aout = out_mem.va + pad;
309 uint32_t args[10] = { (uint32_t)ain, (uint32_t)(ain >> 32), (uint32_t)a_mem.va, (uint32_t)(a_mem.va >> 32),
310 (uint32_t)aout, (uint32_t)(aout >> 32), (uint32_t)count, 0, 0, 0 };
311 memcpy(cbank_mem.cpu, cbank_data, sizeof(cbank_data));
312 memcpy((uint8_t *)cbank_mem.cpu + 0x380, args, sizeof(args));
313
314 uint64_t qmd0_va = qmd_mem.va, qmd1_va = qmd_mem.va + 0x1000, sem_va = qmd_mem.va + 0x2000;
315 OmegaBlackwellQmdConfig cfg = { .code_va = code_mem.va, .cbank_va = cbank_mem.va, .scratch_va = qmd_mem.va + 0x4000,
316 .sem_va = sem_va, .qmd0_va = qmd0_va, .qmd1_va = qmd1_va,
317 .num_elements = (uint32_t)count, .gpr_count = OMEGA_DS_GPR_COUNT };
318 omega_numeric_launch_shape(count, &cfg.threads_per_block, &cfg.grid_width);
319 uint32_t qmd0[OMEGA_BW_QMD_WORDS], qmd1[OMEGA_BW_QMD_WORDS];
320 if (omega_blackwell_build_qmd0(qmd0, qmd0_va, qmd1_va) != 0 || omega_blackwell_build_qmd1(qmd1, &cfg) != 0 ||
321 omega_ds_check_qmd(qmd1, code_mem.va, err, sizeof err) != OMEGA_NUMERIC_OK ||
322 omega_ldst_check_kernel(s, code_mem.cpu, code_len, err, sizeof err) != 0) {
323 fprintf(stderr, "omega_ldst_gb10_run: %s\n", err);
324 omega_numeric_native_close(&ctx);
325 return OMEGA_NUMERIC_ERR_OPERANDS;
326 }
327 memcpy(qmd_mem.cpu, qmd0, sizeof(qmd0));
328 memcpy((uint8_t *)qmd_mem.cpu + 0x1000, qmd1, sizeof(qmd1));
329 volatile uint32_t *hsem = (volatile uint32_t *)((uint8_t *)qmd_mem.cpu + 0x2000);
330 volatile uint32_t *hmarker = (volatile uint32_t *)marker_mem.cpu;
331 *hsem = 0;
332 *hmarker = 0;
333#ifndef OMEGA_C3_PROTECT_OFF /* C3 hardening, same as omega_ds_gb10_run (-DOMEGA_C3_PROTECT_OFF = A/B control arm) */
334 *(volatile uint32_t *)((uint8_t *)marker_mem.cpu + 0x10) = 0;
335#endif
336 __asm__ volatile("dsb sy" ::: "memory");
337
338 static uint32_t pb[1024];
339 size_t n = 0;
340 memcpy(&pb[n], SETUP, sizeof(SETUP)); n += 18;
341 pb[n++] = nvrm_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)(cbank_mem.va >> 32); pb[n++] = (uint32_t)cbank_mem.va;
342 pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = 0x00000380; pb[n++] = 0x00000001;
343 pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041;
344 pb[n++] = (224 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28);
345 memcpy(&pb[n], cbank_data, 224 * 4); n += 224;
346 pb[n++] = nvrm_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)((cbank_mem.va + 0x380) >> 32); pb[n++] = (uint32_t)(cbank_mem.va + 0x380);
347 pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = 0x00000028; pb[n++] = 0x00000001;
348 pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041;
349 pb[n++] = (10 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28);
350 memcpy(&pb[n], args, 10 * 4); n += 10;
351 pb[n++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28);
352 pb[n++] = (1u << 30) | (uint32_t)((qmd0_va >> 40) & 0x1ff);
353 pb[n++] = (uint32_t)(qmd0_va >> 8);
354 memcpy(&pb[n], qmd0, 96 * 4); n += 96;
355 pb[n++] = nvrm_mthd(1, 0x0188, 2); pb[n++] = (uint32_t)(sem_va >> 32); pb[n++] = (uint32_t)sem_va;
356 pb[n++] = nvrm_mthd(1, 0x0180, 2); pb[n++] = 0x00000004; pb[n++] = 0x00000001;
357 pb[n++] = nvrm_mthd(1, 0x01b0, 1); pb[n++] = 0x00000041;
358 pb[n++] = (1 << 16) | (1 << 13) | (0x01b4 >> 2) | (6u << 28);
359 pb[n++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT;
360 pb[n++] = (98 << 16) | (1 << 13) | (0x0318 >> 2) | (2u << 28);
361 pb[n++] = (1u << 30) | (uint32_t)((qmd1_va >> 40) & 0x1ff);
362 pb[n++] = (uint32_t)(qmd1_va >> 8);
363 memcpy(&pb[n], qmd1, 96 * 4); n += 96;
364 pb[n++] = nvrm_mthd(0, 0x005c, 5);
365 pb[n++] = (uint32_t)marker_mem.va; pb[n++] = (uint32_t)(marker_mem.va >> 32);
366 pb[n++] = OMEGA_BW_MARKER_COMPLETION_PAYLOAD; pb[n++] = 0; pb[n++] = 0x1 | (1u << 20);
367#ifndef OMEGA_C3_PROTECT_OFF
368 /* C3 hardening, copied from omega_ds_gb10_run (src/omega_numeric_divsqrt_gb10.c): after the WFI marker
369 * flush GPU L2 dirty lines to memory (NVC96F_MEM_OP_A..D = 0x28..0x34, D bits 31:27 OPERATION =
370 * L2_FLUSH_DIRTY 0x10), then a second WFI marker the host waits for before the readback. */
371 pb[n++] = nvrm_mthd(0, 0x0028, 4); pb[n++] = 0; pb[n++] = 0; pb[n++] = 0; pb[n++] = (0x10u << 27);
372 pb[n++] = nvrm_mthd(0, 0x005c, 5);
373 pb[n++] = (uint32_t)(marker_mem.va + 0x10); pb[n++] = (uint32_t)((marker_mem.va + 0x10) >> 32);
374 pb[n++] = 0x46464646u; pb[n++] = 0; pb[n++] = 0x1 | (1u << 20);
375#endif
376
377 if ((drc = m16_native_submit_methods(&ctx, pb, n)) != 0) { omega_numeric_native_close(&ctx); return LDST_DEVERR("submit", drc); }
378 /* Wait long: closing the channel under a running kernel jams the seat. */
379 if ((drc = omega_numeric_native_wait(hmarker, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, 600000)) != 0) return LDST_DEVERR("wait", drc);
380#ifndef OMEGA_C3_PROTECT_OFF
381 volatile uint32_t *hmarker2 = (volatile uint32_t *)((uint8_t *)marker_mem.cpu + 0x10);
382 if ((drc = omega_numeric_native_wait(hmarker2, 0x46464646u, 600000)) != 0) return LDST_DEVERR("marker2_wait", drc);
383#endif
384 if ((drc = omega_numeric_native_wait(hsem, OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE, 600000)) != 0) return LDST_DEVERR("sem_wait", drc);
385 __asm__ volatile("dsb sy" ::: "memory");
386 memcpy(out_buf, out_mem.cpu, out_len);
387 if ((drc = omega_numeric_native_close(&ctx)) != 0) return LDST_DEVERR("close", drc);
388 return OMEGA_NUMERIC_OK;
389#endif
390}