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_concurrent.c

262 lines · 11694 bytes Raw Blame History
1/*
2 * m16_concurrent.c -- M16 concurrent multi-channel qualification test.
3 *
4 * Sequence:
5 * 1. Initialize two independent M16NativeContext instances (ctxA, ctxB).
6 * 2. Allocate separate channels, GPFIFO rings, USERD, and tokens.
7 * 3. Allocate independent GPU-uncached marker pages (nvrm_alloc_gpu_uncached,
8 * nvrm/nvrm.c:473): markerA, markerB. Each page also holds marker2 at +0x10.
9 * 4. Submit independent work:
10 * Channel A -> 0x16000001, L2 flush, then 0x46464646 at +0x10
11 * Channel B -> 0x16000002, L2 flush, then 0x46464646 at +0x10
12 * 5. Verify independent causality:
13 * Both withheld -> markerA=0, markerB=0
14 * Ring A -> markerA=0x16000001, markerB=0
15 * Ring B -> markerB=0x16000002
16 * 6. Prove non-collision and dynamic resource independence.
17 *
18 * Waits use the canonical primitive m16/m16_gpu_wait.c: monotonic clock, hard
19 * 2000 ms deadline that is never extended, 2000 ms stall deadline (equal, so the
20 * old 2 s pass window is unchanged) that restarts only on progress, barrier before every read, PASS needs marker AND marker2.
21 * Every failure prints one line tagged
22 * class=<submission_fail|no_progress|late_progress|wrong_marker|output_parity>
23 * followed by the full wait report.
24 *
25 * Exit codes (unchanged unless marked NEW):
26 * 1 init failure or enqueue failure (class submission_fail)
27 * 2 premature execution on withheld doorbells (wrong_marker)
28 * 3 markerA wait failed (no_progress / late_progress / wrong_marker)
29 * 4 cross-talk: B executed on A's doorbell (wrong_marker)
30 * 5 markerB wait failed (no_progress / late_progress / wrong_marker)
31 * 6 root handle collision
32 * 7 NEW: output_parity. Either a marker re-read right after its PASS disagrees
33 * with the value just accepted (nearly a tautology, it only catches a torn
34 * or unstable word), or, new tightening, the final check that both pages still
35 * hold their payload and 0x46464646 after B completed fails.
36 * This test has no output buffer; the markers are the output.
37 *
38 * The doorbell is a plain store (nvrm_ring, nvrm/nvrm.c:742, returns void), so a
39 * doorbell write that fails cannot be detected; one that never reaches the chip
40 * shows up as no_progress.
41 */
42#include "m16_native.h"
43#include "m16_gpu_wait.h"
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <unistd.h>
48
49#define PAYLOAD_A 0x16000001u
50#define PAYLOAD_B 0x16000002u
51#define MARKER2_PAYLOAD 0x46464646u
52#define MARKER2_OFF 0x10u
53#define WAIT_TOTAL_MS 2000u /* hard deadline: same 2 s window as the old wait */
54#define WAIT_STALL_MS 2000u /* >= hard bound so the pass window is not narrower than the old wait */
55
56static void fence(void) { __asm__ volatile("dsb sy" ::: "memory"); }
57
58/* Marker stream: bind class, WFI release of payload at marker_va, L2_FLUSH_DIRTY
59 * mem-op (NVC96F_MEM_OP_A..D = 0x28..0x34, D bits 31:27 = 0x10), then a second WFI
60 * release of 0x46464646 at marker_va + 0x10. Same block as omega's
61 * src/omega_numeric_gb10.c (the C3 hardening block, "L2_FLUSH_DIRTY" comment
62 * above the second 0x005c packet); nvrm_mthd is nvrm/nvrm.h:112. */
63static int build_release_flushed(uint32_t *w, uint64_t marker_va, uint32_t payload, uint32_t cls) {
64 int n = m16_native_build_release(w, marker_va, payload, cls);
65 if (n < 0) return -1;
66 w[n++] = nvrm_mthd(0, 0x0028, 4);
67 w[n++] = 0;
68 w[n++] = 0;
69 w[n++] = 0;
70 w[n++] = (0x10u << 27);
71 uint64_t m2 = marker_va + MARKER2_OFF;
72 w[n++] = nvrm_mthd(0, 0x005c, 5);
73 w[n++] = (uint32_t)m2;
74 w[n++] = (uint32_t)(m2 >> 32);
75 w[n++] = MARKER2_PAYLOAD;
76 w[n++] = 0;
77 w[n++] = 0x1 | (1u << 20);
78 return n;
79}
80
81static void print_report(const char *cls, const char *stage, int code, const m16_gpu_wait_report_t *r) {
82 fprintf(stderr,
83 "FAIL class=%s stage=%s exit=%d kind=%s result=%s expected=0x%08x last_marker=0x%08x "
84 "last_marker2=0x%08x elapsed_ms=%llu last_progress_ms=%llu "
85 "progress_count=%llu polls=%llu\n",
86 cls, stage, code, m16_gpu_wait_kind_name(r->wait_kind), m16_gpu_wait_result_name(r->result),
87 r->expected, r->last_observed, r->last_marker2, (unsigned long long)(r->elapsed_ns / 1000000ull),
88 (unsigned long long)((r->last_progress_ns - r->start_ns) / 1000000ull),
89 (unsigned long long)r->progress_count, (unsigned long long)r->polls);
90}
91
92static void print_simple(const char *cls, const char *stage, int code, const char *what) {
93 fprintf(stderr, "FAIL class=%s stage=%s exit=%d %s\n", cls, stage, code, what);
94}
95
96/* Wait marker==want and marker2==MARKER2_PAYLOAD; on failure print the tagged
97 * line and return the class's exit code, on PASS re-read both (parity). */
98static int wait_marker(volatile uint32_t *marker, uint32_t want, const char *stage, int fail_exit) {
99 volatile uint32_t *marker2 = (volatile uint32_t *)((volatile uint8_t *)marker + MARKER2_OFF);
100 m16_gpu_wait_cfg_t cfg;
101 m16_gpu_wait_report_t rpt;
102 memset(&cfg, 0, sizeof cfg);
103 cfg.total_timeout_ms = WAIT_TOTAL_MS;
104 cfg.progress_timeout_ms = WAIT_STALL_MS;
105 cfg.marker2 = marker2;
106 cfg.marker2_want = MARKER2_PAYLOAD;
107 if (!m16_gpu_wait_fixed(marker, want, &rpt, &cfg)) {
108 print_report(m16_gpu_wait_failure_class(&rpt, MARKER2_PAYLOAD), stage, fail_exit, &rpt);
109 return fail_exit;
110 }
111 fence();
112 uint32_t m = *marker, m2 = *marker2;
113 if (m != want || m2 != MARKER2_PAYLOAD) {
114 char what[160];
115 snprintf(what, sizeof what, "reread_marker=0x%08x want=0x%08x reread_marker2=0x%08x want2=0x%08x",
116 m, want, m2, MARKER2_PAYLOAD);
117 print_simple("output_parity", stage, 7, what);
118 return 7;
119 }
120 return 0;
121}
122
123int main(void) {
124 printf("=== M16 CONCURRENT MULTI-CHANNEL QUALIFICATION ===\n");
125
126 M16NativeContext ctxA, ctxB;
127
128 printf("\n--- INITIALIZING CONTEXT A ---\n");
129 if (m16_native_open(&ctxA) != 0 || m16_native_create_channel(&ctxA) != 0) {
130 fprintf(stderr, "FATAL: Failed to initialize Context A: %s\n", ctxA.rm.err);
131 return 1;
132 }
133 printf("[OBSERVED] Context A: token=0x%08x gpfifo=0x%08x fifo_va=0x%lx\n",
134 ctxA.rm.token, ctxA.rm.gpfifo, (unsigned long)ctxA.rm.fifo.va);
135
136 printf("\n--- INITIALIZING CONTEXT B ---\n");
137 if (m16_native_open(&ctxB) != 0 || m16_native_create_channel(&ctxB) != 0) {
138 fprintf(stderr, "FATAL: Failed to initialize Context B: %s\n", ctxB.rm.err);
139 m16_native_close(&ctxA);
140 return 1;
141 }
142 printf("[OBSERVED] Context B: token=0x%08x gpfifo=0x%08x fifo_va=0x%lx\n",
143 ctxB.rm.token, ctxB.rm.gpfifo, (unsigned long)ctxB.rm.fifo.va);
144
145 /* Allocate markers: GPU-uncached, so a GPU store reaches memory instead of
146 * sitting in GPU L2 (was nvrm_alloc via m16_native_alloc_memory: cacheable). */
147 NvrmMem memA, memB;
148 if (nvrm_alloc_gpu_uncached(&ctxA.rm, 0x1000, &memA) != 0 ||
149 nvrm_alloc_gpu_uncached(&ctxB.rm, 0x1000, &memB) != 0) {
150 fprintf(stderr, "FATAL: Failed to allocate marker memories\n");
151 m16_native_close(&ctxA);
152 m16_native_close(&ctxB);
153 return 1;
154 }
155 uint64_t vaA = memA.va, vaB = memB.va;
156 volatile uint32_t *markerA = (volatile uint32_t *)memA.cpu;
157 volatile uint32_t *markerB = (volatile uint32_t *)memB.cpu;
158 volatile uint32_t *marker2A = (volatile uint32_t *)((volatile uint8_t *)memA.cpu + MARKER2_OFF);
159 volatile uint32_t *marker2B = (volatile uint32_t *)((volatile uint8_t *)memB.cpu + MARKER2_OFF);
160 *markerA = 0;
161 *markerB = 0;
162 *marker2A = 0;
163 *marker2B = 0;
164 fence();
165
166 printf("[OBSERVED] Marker A: VA=0x%lx CPU=%p (initial=0, GPU-uncached)\n", (unsigned long)vaA, memA.cpu);
167 printf("[OBSERVED] Marker B: VA=0x%lx CPU=%p (initial=0, GPU-uncached)\n", (unsigned long)vaB, memB.cpu);
168
169 /* Build methods */
170 uint32_t mthdA[32], mthdB[32];
171 int nA = build_release_flushed(mthdA, vaA, PAYLOAD_A, ctxA.rm.compute_class);
172 int nB = build_release_flushed(mthdB, vaB, PAYLOAD_B, ctxB.rm.compute_class);
173
174 /* Enqueue to both without ringing */
175 printf("\n--- ENQUEUING WORK WITHOUT DOORBELLS ---\n");
176 if (nA < 0 || nB < 0 ||
177 m16_native_enqueue_methods(&ctxA, mthdA, (size_t)nA) != 0 ||
178 m16_native_enqueue_methods(&ctxB, mthdB, (size_t)nB) != 0) {
179 print_simple("submission_fail", "enqueue", 1, "enqueue of method stream failed");
180 m16_native_close(&ctxA);
181 m16_native_close(&ctxB);
182 return 1;
183 }
184 usleep(50000);
185 fence();
186 printf("[OBSERVED] Withheld doorbells: markerA=0x%08x markerB=0x%08x\n", *markerA, *markerB);
187 if (*markerA != 0 || *markerB != 0 || *marker2A != 0 || *marker2B != 0) {
188 char what[160];
189 snprintf(what, sizeof what, "premature markerA=0x%08x markerB=0x%08x marker2A=0x%08x marker2B=0x%08x",
190 *markerA, *markerB, *marker2A, *marker2B);
191 print_simple("wrong_marker", "withheld", 2, what);
192 m16_native_close(&ctxA);
193 m16_native_close(&ctxB);
194 return 2;
195 }
196
197 /* Ring Doorbell A only */
198 printf("\n--- RINGING DOORBELL A ONLY ---\n");
199 m16_native_ring_doorbell(&ctxA);
200 int rc = wait_marker(markerA, PAYLOAD_A, "A", 3);
201 if (rc != 0) {
202 m16_native_close(&ctxA);
203 m16_native_close(&ctxB);
204 return rc;
205 }
206 printf("[OBSERVED] After Doorbell A: markerA=0x%08x markerB=0x%08x (expected A=0x16000001, B=0x00000000)\n",
207 *markerA, *markerB);
208 fence();
209 if (*markerB != 0 || *marker2B != 0) {
210 char what[120];
211 snprintf(what, sizeof what, "cross-talk markerB=0x%08x marker2B=0x%08x with only doorbell A rung",
212 *markerB, *marker2B);
213 print_simple("wrong_marker", "crosstalk", 4, what);
214 m16_native_close(&ctxA);
215 m16_native_close(&ctxB);
216 return 4;
217 }
218
219 /* Ring Doorbell B */
220 printf("\n--- RINGING DOORBELL B ---\n");
221 m16_native_ring_doorbell(&ctxB);
222 rc = wait_marker(markerB, PAYLOAD_B, "B", 5);
223 if (rc != 0) {
224 m16_native_close(&ctxA);
225 m16_native_close(&ctxB);
226 return rc;
227 }
228 printf("[OBSERVED] After Doorbell B: markerA=0x%08x markerB=0x%08x (expected A=0x16000001, B=0x16000002)\n",
229 *markerA, *markerB);
230 /* output_parity, final: B's work must not have disturbed A's page. The old log
231 * printed this as "expected" but never enforced it (NEW tightening). */
232 fence();
233 if (*markerA != PAYLOAD_A || *marker2A != MARKER2_PAYLOAD || *markerB != PAYLOAD_B || *marker2B != MARKER2_PAYLOAD) {
234 char what[200];
235 snprintf(what, sizeof what, "final markerA=0x%08x marker2A=0x%08x markerB=0x%08x marker2B=0x%08x",
236 *markerA, *marker2A, *markerB, *marker2B);
237 print_simple("output_parity", "final", 7, what);
238 m16_native_close(&ctxA);
239 m16_native_close(&ctxB);
240 return 7;
241 }
242
243 /* Verify distinct resource handles */
244 printf("\n--- VERIFYING RESOURCE INDEPENDENCE ---\n");
245 printf("[OBSERVED] Handle check: Root A=0x%x B=0x%x, Channel A=0x%x B=0x%x, Token A=0x%x B=0x%x\n",
246 ctxA.rm.root, ctxB.rm.root, ctxA.rm.gpfifo, ctxB.rm.gpfifo, ctxA.rm.token, ctxB.rm.token);
247
248 if (ctxA.rm.root == ctxB.rm.root) {
249 fprintf(stderr, "FATAL: Root handles collided between sessions!\n");
250 m16_native_close(&ctxA);
251 m16_native_close(&ctxB);
252 return 6;
253 }
254
255 printf("CONCURRENT_CHANNEL_PASS: 2 independent channels executed without collision.\n");
256
257 nvrm_retire(&ctxA.rm, ctxA.rm.put);
258 nvrm_retire(&ctxB.rm, ctxB.rm.put);
259 m16_native_close(&ctxA);
260 m16_native_close(&ctxB);
261 return 0;
262}