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 / runtime / rx_resident_gpu.c

1212 lines · 45310 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/*
18 * One persistent graphics seat. It is launched once, watches the same image
19 * the processor uses, and leaves when it sees a shutdown notice. It does not
20 * export a function for the processor to call.
21 */
22#include "rx_resident_gpu.h"
23
24#include "omega_blackwell_codegen.h"
25#include "omega_blackwell_qmd.h"
26#include "omega_blackwell_submit.h"
27#include "omega_gpu_code_alloc.h"
28#include "m16_native.h"
29
30#include <pthread.h>
31#include <sched.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <unistd.h>
36
37/* Code capacity of a seat (the code buffer adds the 2 KB prefetch tail on top). */
38#define RX_GPU_CODE_CAP 0x4000u
39
40#define LDG_CTL 0x001ea800u
41#define CRC_POLY 0x82F63B78u
42/* Safety stop if shutdown is never posted. A real shutdown leaves sooner.
43 * The launch wait is 15 seconds, so this only matters when shutdown is missed. */
44#define GIVEUP 200000000u
45/* Keep-alive: the processor bumps the lease every LEASE_TICK_US; a seat that
46 * sees no new lease for about LEASE_WINDOW_MS leaves by itself. The window is
47 * set in seat passes from the pass rate measured at launch. A faster chip
48 * later only shortens it, and even 20x faster stays far above the tick. */
49#define LEASE_TICK_US 5000u
50#define LEASE_WINDOW_MS 2000u
51#define LEASE_SAMPLE_MS 50u
52#define LEASE_MIN_BUDGET 20000u
53
54typedef struct __attribute__((packed)) {
55 uint64_t region;
56 uint32_t seat_gen; /* the only claim generation this seat takes */
57 uint32_t epoch;
58 uint32_t off_c2g;
59 uint32_t off_g2c;
60 uint32_t off_hb;
61 uint32_t off_tbl;
62 uint32_t giveup;
63} SeatArgs;
64
65typedef struct LaunchJob LaunchJob;
66
67struct RxGpuSeat {
68 M16NativeContext ctx;
69 pthread_t thread;
70 int thread_live;
71 int opened;
72 int killed; /* channel destroyed under the running seat */
73 volatile int abort; /* stop waiting for the launch marker */
74 volatile int marker_ok;
75 volatile int sem_ok;
76 volatile uint32_t marker_value; /* words the host read when the wait ended (diagnostic) */
77 volatile uint32_t sem_value;
78 char err[sizeof(((Nvrm *)0)->err) + 128]; /* our label plus nvrm's whole reason */
79 LaunchJob *job;
80 volatile uint32_t *hb_watch;
81 RxWorld *world;
82 int image_bound;
83 pthread_t lease_thread;
84 int lease_live;
85 volatile int lease_run;
86 uint32_t budget; /* seat passes in LEASE_WINDOW_MS, measured once */
87};
88
89/* The chip is leaving. The world keeps its objects: copy the image into
90 * memory the world owns before the chip memory goes away. A chip reset or
91 * exit must not take canonical identity with it. */
92static int seat_return_image(RxGpuSeat *s) {
93 if (!s->image_bound) return 0;
94 RxWorld *w = s->world;
95 uint64_t bytes = w->coherent_bytes;
96 void *own = malloc((size_t)bytes);
97 if (!own) return -1;
98 if (rx_world_bind_coherent(w, own, bytes, 0) != RX_OK) {
99 free(own);
100 return -1;
101 }
102 s->image_bound = 0;
103 return 0;
104}
105
106typedef struct {
107 int base, addr, hb_ptr, c2g, g2c, slot, dst, win;
108 int tid, one, zero, ones, give, hb, head, tail;
109 int tmp, tmp2, msg, obj, gen, seq, f0, f1;
110 int crc, poly, word, byte, nbit, wi;
111 int fifteen, thirtytwo, off_c2g, off_g2c, off_hb, off_tbl, epoch;
112 int off, slot_off, dst_off;
113 int win_b, lim, sgen, clk, lseen;
114 int uv;
115} Regs;
116
117static uint32_t crc_bitwise(const uint8_t *data, size_t len) {
118 uint32_t c = 0xFFFFFFFFu;
119 for (size_t i = 0; i < len; i++) {
120 c ^= data[i];
121 for (int b = 0; b < 8; b++)
122 c = (c & 1u) ? (CRC_POLY ^ (c >> 1)) : (c >> 1);
123 }
124 return c ^ 0xFFFFFFFFu;
125}
126
127static int crc_matches_sealer(void) {
128 OmegaSharedWorldDesc d;
129 memset(&d, 0, sizeof(d));
130 d.msg_type = RX_RING_CLAIM;
131 d.object_id = 3;
132 d.object_generation = 1;
133 d.object_length = 64;
134 d.payload_len = 24;
135 d.arg_b = 9;
136 d.payload[0] = 4;
137 d.payload[3] = 1;
138 d.payload[16] = 7;
139 rx_world_seal_descriptor(&d);
140 uint32_t want = d.checksum;
141 uint8_t buf[0x3C + 64];
142 memcpy(buf, &d, 0x3C);
143 memcpy(buf + 0x3C, d.payload, 64);
144 return crc_bitwise(buf, sizeof(buf)) == want;
145}
146
147static BlackwellIRInsn op(BlackwellIROpcode code) {
148 BlackwellIRInsn n;
149 memset(&n, 0, sizeof(n));
150 n.op = code;
151 n.dst_vreg = n.src1_vreg = n.src2_vreg = n.src3_vreg = n.ureg = -1;
152 return n;
153}
154
155/* S2R special register %globaltimer, low 32 bits (ns). Kept here so the
156 * hash-pinned codegen header stays untouched. */
157#define RX_SR_GLOBALTIMER_LO 0x52
158
159static int emit_overflow;
160
161static int em(BlackwellIRProgram *p, BlackwellIRInsn n) {
162 int idx = omega_bw_ir_append(p, &n);
163 if (idx < 0) {
164 emit_overflow = 1;
165 return -1;
166 }
167 return idx;
168}
169
170static int bra(BlackwellIRProgram *p, int pred, int neg) {
171 BlackwellIRInsn n = op(BW_IR_BRA);
172 n.predicate_p0 = pred ? true : false;
173 n.predicate_not = neg ? true : false;
174 return em(p, n);
175}
176
177static void fix(BlackwellIRProgram *p, int at, int target) {
178 if (at >= 0 && target >= 0) p->insns[at].imm = (uint32_t)(target - at);
179}
180
181static void movi(BlackwellIRProgram *p, int dst, uint32_t imm) {
182 BlackwellIRInsn n = op(BW_IR_MOV_IMM);
183 n.dst_vreg = dst;
184 n.imm = imm;
185 em(p, n);
186}
187
188static void add32(BlackwellIRProgram *p, int dst, int a, int b) {
189 BlackwellIRInsn n = op(BW_IR_IADD3);
190 n.dst_vreg = dst;
191 n.src1_vreg = a;
192 n.src2_vreg = b;
193 em(p, n);
194}
195
196static void xor_rr(BlackwellIRProgram *p, int dst, int a, int b) {
197 BlackwellIRInsn n = op(BW_IR_LOP3_XOR);
198 n.dst_vreg = dst;
199 n.src1_vreg = a;
200 n.src2_vreg = b;
201 em(p, n);
202}
203
204static void andi(BlackwellIRProgram *p, int dst, int a, uint32_t imm) {
205 BlackwellIRInsn n = op(BW_IR_LOP3_AND);
206 n.dst_vreg = dst;
207 n.src1_vreg = a;
208 n.imm = imm;
209 em(p, n);
210}
211
212static void shr(BlackwellIRProgram *p, int dst, int a, uint32_t imm) {
213 BlackwellIRInsn n = op(BW_IR_SHF_R);
214 n.dst_vreg = dst;
215 n.src1_vreg = a;
216 n.imm = imm;
217 em(p, n);
218}
219
220static void ge_rr(BlackwellIRProgram *p, int a, int b) {
221 BlackwellIRInsn n = op(BW_IR_ISETP_GE_U32);
222 n.src1_vreg = a;
223 n.src2_vreg = b;
224 em(p, n);
225}
226
227static void addk(BlackwellIRProgram *p, int dst, int base, int idx, uint32_t scale) {
228 BlackwellIRInsn n = op(BW_IR_IMAD_WIDE);
229 n.dst_vreg = dst;
230 n.src1_vreg = idx;
231 n.src3_vreg = base;
232 n.imm = scale;
233 em(p, n);
234}
235
236static void shl_n(BlackwellIRProgram *p, int dst, int src, int n) {
237 add32(p, dst, src, src);
238 for (int i = 1; i < n; i++) add32(p, dst, dst, dst);
239}
240
241/* Byte offset from the original region base. The wide add is only ever
242 * immediate 1 onto that base. A second wide add onto a computed pointer
243 * drops the high half of the address. */
244static void at_off(BlackwellIRProgram *p, const Regs *r, int dst, int base_off, uint32_t extra) {
245 if (extra == 0) {
246 add32(p, r->off, base_off, r->zero);
247 } else {
248 movi(p, r->tmp2, extra);
249 add32(p, r->off, base_off, r->tmp2);
250 }
251 addk(p, dst, r->base, r->off, 1u);
252}
253
254static void ring_slot(BlackwellIRProgram *p, Regs *r, int dest_off, int index, int ring_off) {
255 andi(p, r->tmp, index, 1023u);
256 shl_n(p, r->tmp, r->tmp, 7);
257 movi(p, r->tmp2, 192u);
258 add32(p, r->tmp, r->tmp, r->tmp2);
259 add32(p, dest_off, r->tmp, ring_off);
260}
261
262/* System-scope strong load: it goes past the chip's own cache, so a value
263 * the processor wrote is seen on the next poll. */
264static void ld(BlackwellIRProgram *p, int dst, int addr, int uv) {
265 BlackwellIRInsn n = op(BW_IR_LDG_STRONG_SYS);
266 n.dst_vreg = dst;
267 n.src1_vreg = addr;
268 n.ureg = uv;
269 n.control = LDG_CTL;
270 em(p, n);
271}
272
273static void st(BlackwellIRProgram *p, int addr, int data, int uv) {
274 BlackwellIRInsn n = op(BW_IR_STG_EF);
275 n.src1_vreg = addr;
276 n.src2_vreg = data;
277 n.ureg = uv;
278 em(p, n);
279}
280
281static void st_ef(BlackwellIRProgram *p, int addr, int data, int uv) {
282 BlackwellIRInsn n = op(BW_IR_STG_EF);
283 n.src1_vreg = addr;
284 n.src2_vreg = data;
285 n.ureg = uv;
286 em(p, n);
287}
288
289static void emit_byte(BlackwellIRProgram *p, const Regs *r) {
290 andi(p, r->byte, r->word, 0xffu);
291 xor_rr(p, r->crc, r->crc, r->byte);
292 movi(p, r->nbit, 8u);
293 int bit = (int)p->count;
294 andi(p, r->byte, r->crc, 1u);
295 shr(p, r->crc, r->crc, 1u);
296 ge_rr(p, r->byte, r->one);
297 int skip = bra(p, 1, 1);
298 xor_rr(p, r->crc, r->crc, r->poly);
299 fix(p, skip, (int)p->count);
300 add32(p, r->nbit, r->nbit, r->ones);
301 ge_rr(p, r->nbit, r->one);
302 fix(p, bra(p, 1, 0), bit);
303 shr(p, r->word, r->word, 8u);
304}
305
306/* Keep-alive check, once per pass. The processor bumps the lease word while
307 * its process lives. A new value refills the countdown from the budget word;
308 * a still lease lets the countdown run out, so a seat whose process died
309 * leaves by itself instead of spinning until the driver gives up on it. The
310 * lease value RX_SEAT_LEASE_QUIT asks the seat to leave now. Branches that
311 * leave are recorded in quit[] and fixed to the exit by the caller. */
312static void lease_check(BlackwellIRProgram *p, Regs *r, int *quit, int *nq) {
313 at_off(p, r, r->addr, r->off_hb, RX_SEAT_HB_LEASE);
314 ld(p, r->tmp, r->addr, r->uv);
315 xor_rr(p, r->tmp2, r->tmp, r->lseen);
316 ge_rr(p, r->tmp2, r->one);
317 int same = bra(p, 1, 1);
318 add32(p, r->lseen, r->tmp, r->zero);
319 xor_rr(p, r->tmp2, r->tmp, r->ones);
320 ge_rr(p, r->tmp2, r->one);
321 quit[(*nq)++] = bra(p, 1, 1);
322 at_off(p, r, r->addr, r->off_hb, RX_SEAT_HB_BUDGET);
323 ld(p, r->give, r->addr, r->uv);
324 ge_rr(p, r->give, r->one);
325 int have_budget = bra(p, 1, 0);
326 BlackwellIRInsn n = op(BW_IR_LDC);
327 n.dst_vreg = r->give;
328 n.imm = 0x3a0;
329 em(p, n);
330 fix(p, have_budget, (int)p->count);
331 fix(p, same, (int)p->count);
332}
333
334static int build_program(BlackwellIRProgram *prog, uint32_t *gpr_out) {
335 emit_overflow = 0;
336 omega_bw_ir_init(prog);
337 Regs r;
338 memset(&r, 0, sizeof(r));
339 r.base = omega_bw_ir_alloc_vreg64(prog);
340 r.addr = omega_bw_ir_alloc_vreg64(prog);
341 r.hb_ptr = omega_bw_ir_alloc_vreg64(prog);
342 r.c2g = omega_bw_ir_alloc_vreg64(prog);
343 r.g2c = omega_bw_ir_alloc_vreg64(prog);
344 r.dst = omega_bw_ir_alloc_vreg64(prog);
345 r.uv = omega_bw_ir_alloc_uvreg64(prog);
346#define A32(field) r.field = omega_bw_ir_alloc_vreg(prog)
347 A32(tid); A32(one); A32(zero); A32(ones); A32(give); A32(hb);
348 A32(head); A32(tail); A32(tmp); A32(tmp2); A32(msg); A32(obj); A32(gen);
349 A32(seq); A32(f0); A32(f1); A32(crc); A32(poly); A32(word); A32(byte);
350 A32(nbit); A32(wi); A32(fifteen); A32(thirtytwo);
351 A32(off_c2g); A32(off_g2c); A32(off_hb); A32(off_tbl);
352 A32(off); A32(slot_off); A32(dst_off); A32(win_b); A32(lim); A32(sgen); A32(clk);
353 A32(lseen);
354#undef A32
355 if (r.sgen < 0 || r.uv < 0) return -1;
356
357 BlackwellIRInsn s2 = op(BW_IR_S2R);
358 s2.dst_vreg = r.tid;
359 s2.imm = BW_SR_TID_X;
360 em(prog, s2);
361 BlackwellIRInsn cmp = op(BW_IR_ISETP_GE);
362 cmp.src1_vreg = r.tid;
363 cmp.imm = 1;
364 em(prog, cmp);
365 int to_body = bra(prog, 0, 0);
366 em(prog, op(BW_IR_NOP));
367 fix(prog, to_body, (int)prog->count);
368
369 BlackwellIRInsn du = op(BW_IR_LDCU64);
370 du.dst_vreg = r.uv;
371 du.imm = 0x358;
372 du.is_uniform = true;
373 em(prog, du);
374 BlackwellIRInsn pb = op(BW_IR_LDC64);
375 pb.dst_vreg = r.base;
376 pb.imm = 0x380;
377 em(prog, pb);
378#define LDC32(reg, off) do { BlackwellIRInsn n = op(BW_IR_LDC); n.dst_vreg = (reg); n.imm = (off); em(prog, n); } while (0)
379 LDC32(r.sgen, 0x388);
380 LDC32(r.off_c2g, 0x390);
381 LDC32(r.off_g2c, 0x394);
382 LDC32(r.off_hb, 0x398);
383 LDC32(r.off_tbl, 0x39c);
384 LDC32(r.give, 0x3a0);
385#undef LDC32
386 movi(prog, r.one, 1u);
387 movi(prog, r.zero, 0u);
388 movi(prog, r.ones, 0xffffffffu);
389 movi(prog, r.lseen, 0u);
390 movi(prog, r.poly, CRC_POLY);
391 movi(prog, r.fifteen, 15u);
392 movi(prog, r.thirtytwo, 32u);
393 /* The seat indexes the world's projected table (RxProjectedTable), which
394 * holds RX_MAX_OBJECTS records, not the frozen ABI's 64. With 64 here the
395 * chip refused every object id past 63 while the stand-in accepted it. */
396 movi(prog, r.lim, RX_MAX_OBJECTS);
397 /* The wide multiply-add drops a small offset (256 landed on the header).
398 * Copy the region base, then add 256 onto the low half with a plain add. */
399 {
400 BlackwellIRInsn n = op(BW_IR_LDC64);
401 n.dst_vreg = r.c2g;
402 n.imm = 0x380;
403 em(prog, n);
404 }
405 movi(prog, r.tmp2, 256u);
406 add32(prog, r.c2g, r.c2g, r.tmp2);
407 add32(prog, r.c2g, r.c2g, r.zero);
408 addk(prog, r.g2c, r.base, r.off_g2c, 1u);
409
410 addk(prog, r.hb_ptr, r.base, r.off_hb, 1u);
411 int loop = (int)prog->count;
412 em(prog, op(BW_IR_CCTL_IVALL)); /* drop any stale private copy before the check */
413 ld(prog, r.tail, r.c2g, r.uv);
414 add32(prog, r.tmp, r.tail, r.one);
415 st_ef(prog, r.hb_ptr, r.tmp, r.uv);
416 add32(prog, r.give, r.give, r.ones);
417 /* A count that moves on every pass: the processor can see the seat is
418 * alive, and that it has stopped. */
419 at_off(prog, &r, r.addr, r.off_hb, RX_SEAT_HB_LIVE);
420 st(prog, r.addr, r.give, r.uv);
421 ge_rr(prog, r.give, r.one);
422 int give_exit = bra(prog, 1, 1);
423 int quit[2];
424 int nq = 0;
425 lease_check(prog, &r, quit, &nq);
426
427 at_off(prog, &r, r.addr, r.off_c2g, 64u);
428 ld(prog, r.head, r.addr, r.uv);
429 ge_rr(prog, r.head, r.tail);
430 fix(prog, bra(prog, 1, 0), loop);
431
432 movi(prog, r.tmp2, 9u);
433 at_off(prog, &r, r.addr, r.off_hb, 4u);
434 st(prog, r.addr, r.tmp2, r.uv);
435
436 ring_slot(prog, &r, r.slot_off, r.head, r.off_c2g);
437 at_off(prog, &r, r.addr, r.slot_off, 4u);
438 ld(prog, r.word, r.addr, r.uv);
439 shr(prog, r.msg, r.word, 16u);
440
441 movi(prog, r.tmp2, RX_RING_SHUTDOWN);
442 xor_rr(prog, r.tmp, r.msg, r.tmp2);
443 ge_rr(prog, r.tmp, r.one);
444 int not_shutdown = bra(prog, 1, 0);
445 add32(prog, r.head, r.head, r.one);
446 at_off(prog, &r, r.addr, r.off_c2g, 64u);
447 st(prog, r.addr, r.head, r.uv);
448 int shutdown_exit = bra(prog, 0, 0);
449
450 fix(prog, not_shutdown, (int)prog->count);
451 movi(prog, r.tmp2, RX_RING_CLAIM);
452 xor_rr(prog, r.tmp, r.msg, r.tmp2);
453 ge_rr(prog, r.tmp, r.one);
454 int not_claim = bra(prog, 1, 0);
455
456 /* R15: the chip's own clock at pickup, with the claim it belongs to. */
457 at_off(prog, &r, r.addr, r.off_hb, RX_SEAT_HB_CLAIM);
458 st(prog, r.addr, r.head, r.uv);
459 {
460 BlackwellIRInsn t = op(BW_IR_S2R);
461 t.dst_vreg = r.clk;
462 t.imm = RX_SR_GLOBALTIMER_LO;
463 em(prog, t);
464 }
465 at_off(prog, &r, r.addr, r.off_hb, RX_SEAT_HB_T_PICK);
466 st(prog, r.addr, r.clk, r.uv);
467
468 /* Input object A: id and generation from the notice header. */
469 at_off(prog, &r, r.addr, r.slot_off, 0x18u);
470 ld(prog, r.obj, r.addr, r.uv);
471 at_off(prog, &r, r.addr, r.slot_off, 0x1cu);
472 ld(prog, r.gen, r.addr, r.uv);
473 int stale[8];
474 int ns = 0;
475 /* A claim from another seat generation: posted before a seat was lost. */
476 at_off(prog, &r, r.addr, r.slot_off, 0x14u);
477 ld(prog, r.f0, r.addr, r.uv);
478 xor_rr(prog, r.tmp2, r.f0, r.sgen);
479 ge_rr(prog, r.tmp2, r.one);
480 stale[ns++] = bra(prog, 1, 0);
481 ge_rr(prog, r.obj, r.lim); /* id outside the table */
482 stale[ns++] = bra(prog, 1, 0);
483 shl_n(prog, r.tmp, r.obj, 5);
484 add32(prog, r.tmp, r.tmp, r.off_tbl);
485 movi(prog, r.tmp2, 64u);
486 add32(prog, r.seq, r.tmp, r.tmp2); /* seq = A's table entry */
487 at_off(prog, &r, r.addr, r.seq, 4u);
488 ld(prog, r.f0, r.addr, r.uv);
489 xor_rr(prog, r.tmp2, r.f0, r.gen);
490 ge_rr(prog, r.tmp2, r.one); /* generation moved */
491 stale[ns++] = bra(prog, 1, 0);
492 at_off(prog, &r, r.addr, r.seq, 8u);
493 ld(prog, r.f0, r.addr, r.uv);
494 xor_rr(prog, r.tmp2, r.f0, r.one);
495 ge_rr(prog, r.tmp2, r.one); /* not active */
496 stale[ns++] = bra(prog, 1, 0);
497 at_off(prog, &r, r.addr, r.seq, 16u);
498 ld(prog, r.f0, r.addr, r.uv);
499 add32(prog, r.seq, r.f0, r.zero); /* seq = A's window */
500
501 /* Output object B: id and generation from the notice payload. */
502 at_off(prog, &r, r.addr, r.slot_off, 0x58u);
503 ld(prog, r.obj, r.addr, r.uv);
504 at_off(prog, &r, r.addr, r.slot_off, 0x5cu);
505 ld(prog, r.gen, r.addr, r.uv);
506 ge_rr(prog, r.obj, r.lim);
507 stale[ns++] = bra(prog, 1, 0);
508 shl_n(prog, r.tmp, r.obj, 5);
509 add32(prog, r.tmp, r.tmp, r.off_tbl);
510 movi(prog, r.tmp2, 64u);
511 add32(prog, r.win_b, r.tmp, r.tmp2); /* win_b = B's table entry */
512 at_off(prog, &r, r.addr, r.win_b, 4u);
513 ld(prog, r.f0, r.addr, r.uv);
514 xor_rr(prog, r.tmp2, r.f0, r.gen);
515 ge_rr(prog, r.tmp2, r.one);
516 stale[ns++] = bra(prog, 1, 0);
517 at_off(prog, &r, r.addr, r.win_b, 8u);
518 ld(prog, r.f0, r.addr, r.uv);
519 xor_rr(prog, r.tmp2, r.f0, r.one);
520 ge_rr(prog, r.tmp2, r.one);
521 stale[ns++] = bra(prog, 1, 0);
522 at_off(prog, &r, r.addr, r.win_b, 16u);
523 ld(prog, r.f0, r.addr, r.uv);
524 add32(prog, r.win_b, r.f0, r.zero); /* win_b = B's window */
525
526 /* The body: the qualified 32-bit integer add, A.field0 + A.field1. */
527 at_off(prog, &r, r.addr, r.seq, 0u);
528 ld(prog, r.f0, r.addr, r.uv);
529 at_off(prog, &r, r.addr, r.seq, 8u);
530 ld(prog, r.f1, r.addr, r.uv);
531 add32(prog, r.f1, r.f0, r.f1);
532 at_off(prog, &r, r.addr, r.win_b, 0u);
533 st(prog, r.addr, r.f1, r.uv);
534 at_off(prog, &r, r.addr, r.win_b, 4u);
535 st(prog, r.addr, r.zero, r.uv);
536 movi(prog, r.msg, 1u | (RX_RING_PUBLISH << 16));
537 int skip_fault_msg = bra(prog, 0, 0);
538 for (int i = 0; i < ns; i++) fix(prog, stale[i], (int)prog->count);
539 movi(prog, r.msg, 1u | (RX_RING_FAULT << 16));
540 fix(prog, skip_fault_msg, (int)prog->count);
541
542 /* Fault-injection hold. The seat marks which claim it holds, then waits
543 * while the processor keeps the hold word set: B's window may already
544 * hold the sum and no result is posted. The safety stop still counts. */
545 add32(prog, r.tmp, r.head, r.one);
546 at_off(prog, &r, r.addr, r.off_hb, RX_SEAT_HB_HELD);
547 st(prog, r.addr, r.tmp, r.uv);
548 int hold = (int)prog->count;
549 em(prog, op(BW_IR_CCTL_IVALL));
550 add32(prog, r.give, r.give, r.ones);
551 at_off(prog, &r, r.addr, r.off_hb, RX_SEAT_HB_LIVE);
552 st(prog, r.addr, r.give, r.uv);
553 ge_rr(prog, r.give, r.one);
554 int hold_exit = bra(prog, 1, 1);
555 lease_check(prog, &r, quit, &nq);
556 at_off(prog, &r, r.addr, r.off_hb, RX_SEAT_HB_HOLD);
557 ld(prog, r.tmp, r.addr, r.uv);
558 ge_rr(prog, r.tmp, r.one);
559 fix(prog, bra(prog, 1, 0), hold);
560
561 at_off(prog, &r, r.g2c, r.off_g2c, 0u);
562 ld(prog, r.tail, r.g2c, r.uv);
563 ring_slot(prog, &r, r.dst_off, r.tail, r.off_g2c);
564 movi(prog, r.wi, 0u);
565 int wloop = (int)prog->count;
566 ge_rr(prog, r.wi, r.thirtytwo);
567 int wdone = bra(prog, 1, 0);
568 add32(prog, r.nbit, r.wi, r.wi);
569 add32(prog, r.nbit, r.nbit, r.nbit);
570 add32(prog, r.off, r.slot_off, r.nbit);
571 addk(prog, r.addr, r.base, r.off, 1u);
572 ld(prog, r.word, r.addr, r.uv);
573 add32(prog, r.tmp, r.word, r.zero);
574 xor_rr(prog, r.tmp2, r.wi, r.one);
575 ge_rr(prog, r.tmp2, r.one);
576 int keep_word = bra(prog, 1, 0);
577 add32(prog, r.tmp, r.msg, r.zero);
578 fix(prog, keep_word, (int)prog->count);
579 xor_rr(prog, r.tmp2, r.wi, r.fifteen);
580 ge_rr(prog, r.tmp2, r.one);
581 int keep_sum = bra(prog, 1, 0);
582 add32(prog, r.tmp, r.zero, r.zero);
583 fix(prog, keep_sum, (int)prog->count);
584 add32(prog, r.off, r.dst_off, r.nbit);
585 addk(prog, r.addr, r.base, r.off, 1u);
586 st(prog, r.addr, r.tmp, r.uv);
587 add32(prog, r.wi, r.wi, r.one);
588 fix(prog, bra(prog, 0, 0), wloop);
589 fix(prog, wdone, (int)prog->count);
590
591 movi(prog, r.crc, 0xffffffffu);
592 movi(prog, r.wi, 0u);
593 int cloop = (int)prog->count;
594 ge_rr(prog, r.wi, r.thirtytwo);
595 int cdone = bra(prog, 1, 0);
596 xor_rr(prog, r.tmp, r.wi, r.fifteen);
597 ge_rr(prog, r.tmp, r.one);
598 int do_word = bra(prog, 1, 0);
599 int skip_word = bra(prog, 0, 0);
600 fix(prog, do_word, (int)prog->count);
601 add32(prog, r.nbit, r.wi, r.wi);
602 add32(prog, r.nbit, r.nbit, r.nbit);
603 add32(prog, r.off, r.dst_off, r.nbit);
604 addk(prog, r.addr, r.base, r.off, 1u);
605 ld(prog, r.word, r.addr, r.uv);
606 emit_byte(prog, &r);
607 emit_byte(prog, &r);
608 emit_byte(prog, &r);
609 emit_byte(prog, &r);
610 fix(prog, skip_word, (int)prog->count);
611 add32(prog, r.wi, r.wi, r.one);
612 fix(prog, bra(prog, 0, 0), cloop);
613 fix(prog, cdone, (int)prog->count);
614 xor_rr(prog, r.crc, r.crc, r.ones);
615 at_off(prog, &r, r.addr, r.dst_off, 0x3cu);
616 st(prog, r.addr, r.crc, r.uv);
617 /* R15: the chip's clock when the result and its notice are written,
618 * before the release that makes them visible. */
619 {
620 BlackwellIRInsn t = op(BW_IR_S2R);
621 t.dst_vreg = r.clk;
622 t.imm = RX_SR_GLOBALTIMER_LO;
623 em(prog, t);
624 }
625 at_off(prog, &r, r.addr, r.off_hb, RX_SEAT_HB_T_DONE);
626 st(prog, r.addr, r.clk, r.uv);
627 add32(prog, r.tail, r.tail, r.one);
628 /* Release: the output window and the whole notice reach memory before
629 * the processor can see the new tail. */
630 em(prog, op(BW_IR_MEMBAR_SC_SYS));
631 at_off(prog, &r, r.g2c, r.off_g2c, 0u);
632 st(prog, r.g2c, r.tail, r.uv);
633
634 fix(prog, not_claim, (int)prog->count);
635 add32(prog, r.head, r.head, r.one);
636 at_off(prog, &r, r.addr, r.off_c2g, 64u);
637 st(prog, r.addr, r.head, r.uv);
638 fix(prog, bra(prog, 0, 0), loop);
639
640 int leave = (int)prog->count;
641 fix(prog, give_exit, leave);
642 fix(prog, hold_exit, leave);
643 fix(prog, shutdown_exit, leave);
644 for (int i = 0; i < nq; i++) fix(prog, quit[i], leave);
645 em(prog, op(BW_IR_EXIT));
646 em(prog, op(BW_IR_BRA));
647
648 for (int v = 0; v < prog->regalloc.num_vregs; v++) {
649 if (prog->regalloc.intervals[v].first_def < 0)
650 prog->regalloc.intervals[v].first_def = 0;
651 prog->regalloc.intervals[v].last_use = (int)prog->count - 1;
652 }
653 if (emit_overflow || omega_bw_regalloc_solve(prog) != 0) {
654 fprintf(stderr, "seat build overflow %d instructions %zu vregs %d\n",
655 emit_overflow, prog->count, prog->regalloc.num_vregs);
656 return -1;
657 }
658 if (gpr_out) *gpr_out = prog->regalloc.peak_gpr_usage;
659 return (int)prog->count;
660}
661
662/* Instruction classes by the low opcode bits of word 0. */
663#define OPC(w0) ((w0) & 0xfffu)
664#define OPC_S2R 0x919u
665#define OPC_LDC 0xb82u
666#define OPC_LDCU 0x7acu
667#define OPC_LDG 0x981u
668#define OPC_STG 0x986u
669#define OPC_ISETP_I 0x80cu
670#define OPC_ISETP_R 0x20cu
671#define OPC_BRA 0x947u
672
673/* Control field in word 3: stall 9..12, yield 13, write barrier 14..16,
674 * read barrier 17..19, wait mask 20..25, reuse 26..29. */
675#define CTL_STALL(n) ((uint32_t)(n) << 9)
676#define CTL_YIELD (1u << 13)
677#define CTL_WB(b) ((uint32_t)(b) << 14)
678#define CTL_RB(b) ((uint32_t)(b) << 17)
679#define CTL_WAIT(m) ((uint32_t)(m) << 20)
680#define CTL_MASK 0x3ffffe00u
681
682/* The seat runs one thread and polls. Speed does not matter here; a value
683 * used before its load lands does. Every variable-latency read (constant
684 * bank, global memory, special register) sets barrier 0. Every store holds
685 * barrier 1 until its operands are read. Every instruction waits on both, and
686 * fixed-latency work stalls long enough for the next instruction to read it. */
687static void seat_scoreboard(uint8_t *code, size_t bytes) {
688 for (size_t at = 0; at + 16 <= bytes; at += 16) {
689 uint32_t w[4];
690 memcpy(w, code + at, 16);
691 uint32_t opc = OPC(w[0]);
692 uint32_t ctl;
693 if (opc == OPC_S2R || opc == OPC_LDC || opc == OPC_LDCU || opc == OPC_LDG)
694 ctl = CTL_STALL(2) | CTL_WB(0) | CTL_RB(7) | CTL_WAIT(0x3);
695 else if (opc == OPC_STG)
696 ctl = CTL_STALL(2) | CTL_WB(7) | CTL_RB(1) | CTL_WAIT(0x3);
697 else
698 ctl = CTL_STALL(13) | CTL_WB(7) | CTL_RB(7) | CTL_WAIT(0x3);
699 w[3] = (w[3] & ~CTL_MASK) | ctl;
700 memcpy(code + at, w, 16);
701 }
702}
703
704/* The seat must open with the lane check: only thread 0 continues. The caller prints the refusal. */
705static int first_is_lane_check(const uint8_t *code, size_t n) {
706 if (n < 48) return 0;
707 uint32_t w0[3];
708 for (int i = 0; i < 3; i++) memcpy(&w0[i], code + 16 * i, 4);
709 return OPC(w0[0]) == OPC_S2R &&
710 (OPC(w0[1]) == OPC_ISETP_I || OPC(w0[1]) == OPC_ISETP_R) &&
711 OPC(w0[2]) == OPC_BRA;
712}
713
714struct LaunchJob {
715 RxGpuSeat *seat;
716 uint8_t *code;
717 size_t code_bytes;
718 uint32_t gpr;
719 SeatArgs args;
720 int submit_rc;
721 /* Launch memory, kept across a channel rebuild. */
722 int have_mem;
723 NvrmMem code_mem, cbank_mem, qmd_mem, marker_mem, large_pb;
724};
725
726/* The launch thread writes s->err first and publishes submit_rc after it (release). wait_heartbeat reads s->err only
727 * after an acquire load of a nonzero submit_rc, and never writes it: while submit_rc is 0 the launch thread may still
728 * be writing s->err, so a heartbeat timeout prints its own text. */
729static void job_failed(LaunchJob *job, int rc) { __atomic_store_n(&job->submit_rc, rc, __ATOMIC_RELEASE); }
730
731/* The launch marker lands only when the seat leaves. A kill stops the wait. */
732static int wait_marker_or_abort(RxGpuSeat *s, volatile uint32_t *marker, uint32_t want,
733 int timeout_ms) {
734 for (int i = 0; i < timeout_ms * 20; i++) {
735 __asm__ volatile("dsb sy" ::: "memory");
736 if (*marker == want) return 0;
737 if (s->abort) return -1;
738 usleep(50);
739 }
740 return *marker == want ? 0 : -1;
741}
742
743static void *launch_main(void *arg) {
744 LaunchJob *job = arg;
745 RxGpuSeat *s = job->seat;
746 M16NativeContext *ctx = &s->ctx;
747 if (!job->have_mem) {
748 if (nvrm_alloc(&ctx->rm, omega_gpu_code_alloc_bytes(RX_GPU_CODE_CAP), &job->code_mem) != 0 || /* + 2 KB prefetch tail */
749 nvrm_alloc(&ctx->rm, 0x1000, &job->cbank_mem) != 0 ||
750 nvrm_alloc(&ctx->rm, 0x10000, &job->qmd_mem) != 0 ||
751 nvrm_alloc_gpu_uncached(&ctx->rm, 0x1000, &job->marker_mem) != 0 ||
752 nvrm_alloc(&ctx->rm, 0x10000, &job->large_pb) != 0) {
753 snprintf(s->err, sizeof s->err, "graphics memory was not allocated (last RM_ALLOC status 0x%x; nvrm: %s)",
754 ctx->rm.last_rm_status, ctx->rm.err);
755 job_failed(job, -1);
756 return NULL;
757 }
758 job->have_mem = 1;
759 }
760 NvrmMem code_mem = job->code_mem, cbank_mem = job->cbank_mem, qmd_mem = job->qmd_mem;
761 NvrmMem marker_mem = job->marker_mem;
762 ctx->pb_mem = job->large_pb;
763 memcpy(code_mem.cpu, job->code, job->code_bytes);
764
765 uint32_t cbank_data[OMEGA_BW_CBANK_DRIVER_WORDS];
766 omega_blackwell_build_cbank_driver(cbank_data, cbank_mem.va);
767 memcpy(cbank_mem.cpu, cbank_data, sizeof(cbank_data));
768 memcpy((uint8_t *)cbank_mem.cpu + 0x380, &job->args, sizeof(job->args));
769
770 uint64_t qmd0_va = qmd_mem.va;
771 uint64_t qmd1_va = qmd_mem.va + 0x1000;
772 /* The semaphore the host reads after the marker lives on the GPU-uncached marker
773 * page (nvos.h NVOS32_ATTR2_GPU_CACHEABLE_YES: "For system memory this will not be
774 * coherent with direct CPU mappings"). On the cached QMD page the host could read a
775 * stale word once the marker arrived promptly. Offset 0x20 keeps clear of the marker. */
776 uint64_t sem_va = marker_mem.va + 0x20;
777 uint64_t scratch_va = qmd_mem.va + 0x4000;
778 /* The chip keeps the top two registers of a thread's allocation for
779 * itself. On GB10 a value placed in R46 of a 48-register launch read back
780 * wrong. Ask for two more than the program uses, then round up. */
781 uint32_t gpr = (job->gpr + 2u + 15u) & ~15u;
782 if (gpr < 32u) gpr = 32u;
783 OmegaBlackwellQmdConfig cfg = {
784 .code_va = code_mem.va,
785 .cbank_va = cbank_mem.va,
786 .scratch_va = scratch_va,
787 .sem_va = sem_va,
788 .qmd0_va = qmd0_va,
789 .qmd1_va = qmd1_va,
790 .threads_x = 1,
791 .grid_x = 1,
792 .gpr_count = gpr
793 };
794 uint32_t qmd0[OMEGA_BW_QMD_WORDS];
795 uint32_t qmd1[OMEGA_BW_QMD_WORDS];
796 omega_blackwell_build_qmd0(qmd0, qmd0_va, qmd1_va);
797 omega_blackwell_build_qmd1(qmd1, &cfg);
798 if (omega_blackwell_verify_qmd_invariants(qmd1) != 0) {
799 snprintf(s->err, sizeof s->err, "launch record was rejected");
800 job_failed(job, -1);
801 return NULL;
802 }
803 memcpy(qmd_mem.cpu, qmd0, sizeof(qmd0));
804 memcpy((uint8_t *)qmd_mem.cpu + 0x1000, qmd1, sizeof(qmd1));
805 volatile uint32_t *hsem = (volatile uint32_t *)((uint8_t *)marker_mem.cpu + 0x20);
806 volatile uint32_t *hmarker = (volatile uint32_t *)marker_mem.cpu;
807 *hsem = 0;
808 *hmarker = 0;
809 __asm__ volatile("dsb sy" ::: "memory");
810
811 static const uint32_t setup[18] = {
812 0x20012061, 0x0000cec0, 0x20012092, 0x00000001, 0x200120a8, 0x0000000f, 0x2001255d, 0x00000003,
813 0x2001255e, 0x20000000, 0x2001255f, 0x000fffff, 0x20012557, 0x00000003, 0x20012558, 0x22000000,
814 0x20012559, 0x00000000
815 };
816 uint32_t pb[1024];
817 size_t n = 0;
818 memcpy(&pb[n], setup, sizeof(setup));
819 n += sizeof(setup) / 4;
820 pb[n++] = nvrm_mthd(1, 0x0188, 2);
821 pb[n++] = (uint32_t)(cbank_mem.va >> 32);
822 pb[n++] = (uint32_t)cbank_mem.va;
823 pb[n++] = nvrm_mthd(1, 0x0180, 2);
824 pb[n++] = 0x00000380;
825 pb[n++] = 0x00000001;
826 pb[n++] = nvrm_mthd(1, 0x01b0, 1);
827 pb[n++] = 0x00000041;
828 pb[n++] = (224u << 16) | (1u << 13) | (0x01b4u >> 2) | (6u << 28);
829 memcpy(&pb[n], cbank_data, 224 * 4);
830 n += 224;
831 uint32_t arg_words = (uint32_t)((sizeof(SeatArgs) + 3u) / 4u);
832 pb[n++] = nvrm_mthd(1, 0x0188, 2);
833 pb[n++] = (uint32_t)((cbank_mem.va + 0x380) >> 32);
834 pb[n++] = (uint32_t)(cbank_mem.va + 0x380);
835 pb[n++] = nvrm_mthd(1, 0x0180, 2);
836 pb[n++] = arg_words * 4u;
837 pb[n++] = 0x00000001;
838 pb[n++] = nvrm_mthd(1, 0x01b0, 1);
839 pb[n++] = 0x00000041;
840 pb[n++] = (arg_words << 16) | (1u << 13) | (0x01b4u >> 2) | (6u << 28);
841 memcpy(&pb[n], &job->args, sizeof(job->args));
842 n += arg_words;
843 pb[n++] = (98u << 16) | (1u << 13) | (0x0318u >> 2) | (2u << 28);
844 pb[n++] = (1u << 30) | (uint32_t)((qmd0_va >> 40) & 0x1ffu);
845 pb[n++] = (uint32_t)(qmd0_va >> 8);
846 memcpy(&pb[n], qmd0, 96 * 4);
847 n += 96;
848 pb[n++] = nvrm_mthd(1, 0x0188, 2);
849 pb[n++] = (uint32_t)(sem_va >> 32);
850 pb[n++] = (uint32_t)sem_va;
851 pb[n++] = nvrm_mthd(1, 0x0180, 2);
852 pb[n++] = 0x00000004;
853 pb[n++] = 0x00000001;
854 pb[n++] = nvrm_mthd(1, 0x01b0, 1);
855 pb[n++] = 0x00000041;
856 pb[n++] = (1u << 16) | (1u << 13) | (0x01b4u >> 2) | (6u << 28);
857 pb[n++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT;
858 pb[n++] = (98u << 16) | (1u << 13) | (0x0318u >> 2) | (2u << 28);
859 pb[n++] = (1u << 30) | (uint32_t)((qmd1_va >> 40) & 0x1ffu);
860 pb[n++] = (uint32_t)(qmd1_va >> 8);
861 memcpy(&pb[n], qmd1, 96 * 4);
862 n += 96;
863 pb[n++] = nvrm_mthd(0, 0x005c, 5);
864 pb[n++] = (uint32_t)marker_mem.va;
865 pb[n++] = (uint32_t)(marker_mem.va >> 32);
866 pb[n++] = OMEGA_BW_MARKER_COMPLETION_PAYLOAD;
867 pb[n++] = 0;
868 pb[n++] = 0x1u | (1u << 20);
869 if (n > 1024) {
870 snprintf(s->err, sizeof s->err, "launch stream does not fit");
871 job_failed(job, -1);
872 return NULL;
873 }
874 int submit_rc = m16_native_submit_methods(ctx, pb, n);
875 if (submit_rc != 0) {
876 snprintf(s->err, sizeof s->err, "the graphics seat was not submitted (rc %d; last RM_ALLOC status 0x%x; nvrm: %s)",
877 submit_rc, ctx->rm.last_rm_status, ctx->rm.err);
878 job_failed(job, submit_rc);
879 return NULL;
880 }
881 int wait = wait_marker_or_abort(s, hmarker, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, 60000);
882 s->marker_ok = wait == 0 && *hmarker == OMEGA_BW_MARKER_COMPLETION_PAYLOAD;
883 s->marker_value = *hmarker;
884 s->sem_value = *hsem;
885 s->sem_ok = s->sem_value == OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE;
886 if (!s->marker_ok && !s->abort)
887 snprintf(s->err, sizeof s->err, "the graphics seat did not finish");
888 return NULL;
889}
890
891/* Wait for the seat's first heartbeat in the image. */
892static int wait_heartbeat(RxGpuSeat *s) {
893 RxWorld *w = s->world;
894 volatile uint32_t *hb = (volatile uint32_t *)(w->coherent + rx_world_off_heartbeat());
895 s->hb_watch = hb;
896 for (int i = 0; i < 2000; i++) {
897 __asm__ volatile("dsb sy" ::: "memory");
898 if (*hb != 0) return 0;
899 if (__atomic_load_n(&s->job->submit_rc, __ATOMIC_ACQUIRE) != 0) break;
900 usleep(1000);
901 }
902 if (__atomic_load_n(&s->job->submit_rc, __ATOMIC_ACQUIRE) != 0 && s->err[0])
903 fprintf(stderr, "seat: %s\n", s->err);
904 else
905 fprintf(stderr, "seat: the graphics seat did not show a heartbeat\n");
906 return -1;
907}
908
909static uint32_t current_seat_gen(RxWorld *w) {
910 pthread_mutex_lock(&w->mu);
911 uint32_t g = w->seat_generation;
912 pthread_mutex_unlock(&w->mu);
913 return g;
914}
915
916static volatile uint32_t *hb_word_at(RxGpuSeat *s, uint32_t off) {
917 return (volatile uint32_t *)(s->world->coherent + rx_world_off_heartbeat() + off);
918}
919
920/* The lease thread is named "rx-seat-lease" so it can be found and counted.
921 * RX_SEAT_LEASE_CPU=<n> pins it to one processor, keeping its wake-ups off
922 * measured cores. */
923static void *lease_main(void *arg) {
924 RxGpuSeat *s = arg;
925 pthread_setname_np(pthread_self(), "rx-seat-lease");
926 const char *pin = getenv("RX_SEAT_LEASE_CPU");
927 if (pin && *pin) {
928 cpu_set_t set;
929 CPU_ZERO(&set);
930 CPU_SET(atoi(pin), &set);
931 if (pthread_setaffinity_np(pthread_self(), sizeof set, &set) != 0)
932 fprintf(stderr, "seat lease: could not pin to cpu %s\n", pin);
933 }
934 uint32_t v = 0;
935 while (s->lease_run) {
936 if (++v == 0 || v == RX_SEAT_LEASE_QUIT) v = 1;
937 *hb_word_at(s, RX_SEAT_HB_LEASE) = v;
938 __asm__ volatile("dsb sy" ::: "memory");
939 usleep(LEASE_TICK_US);
940 }
941 return NULL;
942}
943
944/* After the first heartbeat: measure how many passes the seat makes while it
945 * still counts down from GIVEUP, set the budget for the window, then start
946 * the lease. The pass count is the countdown the seat writes to LIVE. */
947static int lease_start(RxGpuSeat *s) {
948 if (!s->budget) {
949 __asm__ volatile("dsb sy" ::: "memory");
950 uint32_t a = *hb_word_at(s, RX_SEAT_HB_LIVE);
951 usleep(LEASE_SAMPLE_MS * 1000u);
952 __asm__ volatile("dsb sy" ::: "memory");
953 uint32_t b = *hb_word_at(s, RX_SEAT_HB_LIVE);
954 uint64_t passes = a > b ? (uint64_t)(a - b) : 0;
955 uint64_t budget = passes * (LEASE_WINDOW_MS / LEASE_SAMPLE_MS);
956 if (budget < LEASE_MIN_BUDGET) budget = LEASE_MIN_BUDGET;
957 if (budget > GIVEUP) budget = GIVEUP;
958 s->budget = (uint32_t)budget;
959 fprintf(stderr, "seat lease: %llu passes in %u ms, budget %u (%u ms window)\n",
960 (unsigned long long)passes, LEASE_SAMPLE_MS, s->budget, LEASE_WINDOW_MS);
961 }
962 *hb_word_at(s, RX_SEAT_HB_BUDGET) = s->budget;
963 __asm__ volatile("dsb sy" ::: "memory");
964 s->lease_run = 1;
965 int th_rc = pthread_create(&s->lease_thread, NULL, lease_main, s);
966 if (th_rc != 0) {
967 s->lease_run = 0;
968 snprintf(s->err, sizeof s->err, "the lease thread was not started: %s", strerror(th_rc));
969 fprintf(stderr, "seat: %s\n", s->err);
970 return -1;
971 }
972 s->lease_live = 1;
973 return 0;
974}
975
976static void lease_stop(RxGpuSeat *s, int quit) {
977 if (s->lease_live) {
978 s->lease_run = 0;
979 pthread_join(s->lease_thread, NULL);
980 s->lease_live = 0;
981 }
982 if (quit && s->image_bound) {
983 *hb_word_at(s, RX_SEAT_HB_LEASE) = RX_SEAT_LEASE_QUIT;
984 __asm__ volatile("dsb sy" ::: "memory");
985 }
986}
987
988int rx_gpu_seat_kill(RxGpuSeat *s) {
989 if (!s || !s->opened || s->killed) return -1;
990 /* Ask the seat to leave first, then free the channel group once it has.
991 * Destroying the channel under a spinning seat makes the driver's stop
992 * request time out (NV_ERR_TIMEOUT on STOP_CHANNEL) until the seat's own
993 * countdown ends, so a kill is a quit followed by the teardown a fault
994 * would cause. The image and launch memory stay allocated and mapped. */
995 lease_stop(s, 1);
996 for (int i = 0; i < 2000 && !s->marker_ok; i++) usleep(1000);
997 if (!s->marker_ok) fprintf(stderr, "seat kill: the seat did not leave on quit\n");
998 int rc = nvrm_channel_destroy(&s->ctx.rm);
999 s->abort = 1;
1000 if (s->thread_live) {
1001 pthread_join(s->thread, NULL);
1002 s->thread_live = 0;
1003 }
1004 s->killed = 1;
1005 if (rc != 0) {
1006 snprintf(s->err, sizeof s->err, "the graphics channel was not destroyed (nvrm: %s)", s->ctx.rm.err);
1007 fprintf(stderr, "seat kill: %s\n", s->err);
1008 }
1009 return rc == 0 ? 0 : -1;
1010}
1011
1012int rx_gpu_seat_relaunch(RxGpuSeat *s) {
1013 if (!s || !s->killed || !s->image_bound || !s->job) {
1014 fprintf(stderr, "seat: relaunch refused (%s)\n", !s ? "no seat" : !s->killed ? "the seat was not killed"
1015 : !s->image_bound ? "no image is bound" : "no launch job");
1016 return -1;
1017 }
1018 if (nvrm_channel(&s->ctx.rm) != 0) {
1019 snprintf(s->err, sizeof s->err, "the graphics channel was not rebuilt (last RM_ALLOC status 0x%x; nvrm: %s)",
1020 s->ctx.rm.last_rm_status, s->ctx.rm.err);
1021 fprintf(stderr, "seat: %s\n", s->err);
1022 return -1;
1023 }
1024 s->killed = 0;
1025 s->abort = 0;
1026 s->marker_ok = 0;
1027 s->sem_ok = 0;
1028 s->marker_value = 0;
1029 s->sem_value = 0;
1030 s->err[0] = 0;
1031 s->job->submit_rc = 0;
1032 s->job->args.seat_gen = current_seat_gen(s->world);
1033 memset(s->world->coherent + rx_world_off_heartbeat(), 0, 64);
1034 __asm__ volatile("dsb sy" ::: "memory");
1035 int th_rc = pthread_create(&s->thread, NULL, launch_main, s->job);
1036 if (th_rc != 0) {
1037 snprintf(s->err, sizeof s->err, "the relaunch thread was not started: %s", strerror(th_rc));
1038 fprintf(stderr, "seat: %s\n", s->err);
1039 return -1;
1040 }
1041 s->thread_live = 1;
1042 if (wait_heartbeat(s) != 0) return -1;
1043 return lease_start(s);
1044}
1045
1046int rx_gpu_seat_begin(RxWorld *w, RxGpuSeat **out) {
1047 if (out) *out = NULL;
1048 if (!w || !w->coherent || !w->resident_enabled) {
1049 fprintf(stderr, "seat: not started (%s)\n", !w ? "no world" : !w->coherent ? "the world has no shared image"
1050 : "the resident seat is not enabled");
1051 return -1;
1052 }
1053 if (!crc_matches_sealer()) {
1054 fprintf(stderr, "seat: the sealer's checksum table does not match\n");
1055 return -1;
1056 }
1057
1058 BlackwellIRProgram prog;
1059 uint32_t gpr = 0;
1060 int insns = build_program(&prog, &gpr);
1061 fprintf(stderr, "seat build insns %d gpr %u\n", insns, gpr);
1062 if (insns < 8 || insns > BW_MAX_IR_INSNS) {
1063 fprintf(stderr, "seat: program was not built (%d)\n", insns);
1064 return -1;
1065 }
1066 size_t padded = ((size_t)insns + 7u) & ~7u;
1067 if (padded < 32) padded = 32;
1068 size_t bytes = padded * 16u;
1069 uint8_t *code = calloc(1, bytes);
1070 if (!code) {
1071 fprintf(stderr, "seat: no memory for %zu bytes of code\n", bytes);
1072 return -1;
1073 }
1074 size_t emitted = 0;
1075 if (omega_bw_encode_program(&prog, code, bytes, &emitted) != 0) {
1076 fprintf(stderr, "seat: program was not encoded (%d instructions)\n", insns);
1077 free(code);
1078 return -1;
1079 }
1080 if (emitted > RX_GPU_CODE_CAP) { /* the seat's code buffer holds RX_GPU_CODE_CAP bytes; refuse rather than overrun it */
1081 fprintf(stderr, "seat: program is %zu bytes, the code buffer holds %u\n", emitted, (unsigned)RX_GPU_CODE_CAP);
1082 free(code);
1083 return -1;
1084 }
1085 seat_scoreboard(code, emitted);
1086 if (!first_is_lane_check(code, emitted)) {
1087 fprintf(stderr, "seat: program is too short (%zu bytes) or does not start with the lane check\n", emitted);
1088 free(code);
1089 return -1;
1090 }
1091
1092 RxGpuSeat *s = calloc(1, sizeof(*s));
1093 if (!s) {
1094 fprintf(stderr, "seat: no memory for the seat\n");
1095 free(code);
1096 return -1;
1097 }
1098 int open_rc = m16_native_open(&s->ctx);
1099 int chan_rc = open_rc == 0 ? m16_native_create_channel(&s->ctx) : -1;
1100 if (open_rc != 0 || chan_rc != 0) {
1101 snprintf(s->err, sizeof s->err, open_rc != 0 ? "the graphics device did not open"
1102 : "the graphics channel was not created");
1103 if (open_rc != 0)
1104 fprintf(stderr, "seat: %s (open %d, channel not tried; last RM_ALLOC status 0x%x; nvrm: %s)\n", s->err,
1105 open_rc, s->ctx.rm.last_rm_status, s->ctx.rm.err);
1106 else
1107 fprintf(stderr, "seat: %s (channel %d; last RM_ALLOC status 0x%x; nvrm: %s)\n", s->err, chan_rc,
1108 s->ctx.rm.last_rm_status, s->ctx.rm.err);
1109 /* m16_native_open already closed its own descriptors when it failed; closing again could close
1110 * a number another thread was handed meanwhile. */
1111 if (open_rc == 0) m16_native_close(&s->ctx);
1112 free(code);
1113 free(s);
1114 return -1;
1115 }
1116 s->opened = 1;
1117 s->world = w;
1118 NvrmMem image;
1119 memset(&image, 0, sizeof(image));
1120 uint64_t bytes_img = w->coherent_bytes;
1121 uint64_t alloc = (bytes_img + 0xfffu) & ~0xfffu;
1122 if (alloc < 0x100000) alloc = 0x100000;
1123 if (nvrm_alloc_gpu_uncached(&s->ctx.rm, alloc, &image) != 0) {
1124 snprintf(s->err, sizeof s->err, "the shared image was not allocated");
1125 fprintf(stderr, "seat: %s (%llu bytes; last RM_ALLOC status 0x%x; nvrm: %s)\n", s->err,
1126 (unsigned long long)alloc, s->ctx.rm.last_rm_status, s->ctx.rm.err);
1127 m16_native_close(&s->ctx);
1128 free(code);
1129 free(s);
1130 return -1;
1131 }
1132 int bind_rc = rx_world_bind_coherent(w, image.cpu, bytes_img, 1);
1133 if (bind_rc != RX_OK) {
1134 snprintf(s->err, sizeof s->err, "the world image was not bound (status %d)", (int)bind_rc);
1135 fprintf(stderr, "seat: %s\n", s->err);
1136 m16_native_close(&s->ctx);
1137 free(code);
1138 free(s);
1139 return -1;
1140 }
1141 s->image_bound = 1;
1142 memset(w->coherent + rx_world_off_heartbeat(), 0, 64);
1143
1144 LaunchJob *job = calloc(1, sizeof(*job));
1145 if (!job) {
1146 fprintf(stderr, "seat: no memory for the launch job\n");
1147 seat_return_image(s);
1148 m16_native_close(&s->ctx);
1149 free(code);
1150 free(s);
1151 return -1;
1152 }
1153 job->seat = s;
1154 job->code = code;
1155 job->code_bytes = emitted;
1156 job->gpr = gpr;
1157 job->args.region = image.va;
1158 job->args.seat_gen = current_seat_gen(w);
1159 job->args.epoch = w->world_epoch;
1160 job->args.off_c2g = (uint32_t)rx_world_off_c2g();
1161 job->args.off_g2c = (uint32_t)rx_world_off_g2c();
1162 job->args.off_hb = (uint32_t)rx_world_off_heartbeat();
1163 job->args.off_tbl = (uint32_t)rx_world_off_object_table();
1164 job->args.giveup = GIVEUP;
1165 int th_rc = pthread_create(&s->thread, NULL, launch_main, job);
1166 if (th_rc != 0) {
1167 fprintf(stderr, "seat: the launch thread was not started (%s)\n", strerror(th_rc));
1168 free(job);
1169 free(code);
1170 seat_return_image(s);
1171 m16_native_close(&s->ctx);
1172 free(s);
1173 return -1;
1174 }
1175 s->job = job;
1176 s->thread_live = 1;
1177 if (out) *out = s;
1178 if (wait_heartbeat(s) != 0) return -1;
1179 fprintf(stderr, "seat heartbeat %u instructions %d gpr %u\n", *s->hb_watch, insns, gpr);
1180 return lease_start(s);
1181}
1182
1183int rx_gpu_seat_finish(RxGpuSeat *seat) { return rx_gpu_seat_finish_ex(seat, NULL); }
1184
1185int rx_gpu_seat_finish_ex(RxGpuSeat *seat, RxGpuSeatLeave *leave) {
1186 if (!seat) return -1;
1187 /* The shutdown notice was posted before this; stopping the lease only
1188 * bounds how long a seat that missed it keeps running. */
1189 lease_stop(seat, 0);
1190 if (seat->thread_live) {
1191 pthread_join(seat->thread, NULL);
1192 seat->thread_live = 0;
1193 }
1194 if (leave) {
1195 leave->marker_ok = seat->marker_ok;
1196 leave->sem_ok = seat->sem_ok;
1197 leave->marker_value = seat->marker_value;
1198 leave->sem_value = seat->sem_value;
1199 }
1200 /* A killed seat never reaches its marker; leaving is then only the image
1201 * going back to the world and the device closing. */
1202 int ok = seat->killed || (seat->marker_ok && seat->sem_ok);
1203 if (!ok && seat->err[0]) fprintf(stderr, "seat finish: %s\n", seat->err);
1204 if (seat_return_image(seat) != 0) ok = 0;
1205 if (seat->opened) m16_native_close(&seat->ctx);
1206 if (seat->job) {
1207 free(seat->job->code);
1208 free(seat->job);
1209 }
1210 free(seat);
1211 return ok ? 0 : -1;
1212}