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_gpu_wait.h

116 lines · 5468 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#ifndef OMEGA_GPU_WAIT_H
18#define OMEGA_GPU_WAIT_H
19
20/* Shared completion wait for words the GPU writes to host-visible memory.
21 *
22 * Pure C, no NVRM dependency, so the host unit test can drive it with fake
23 * memory and a fake clock.
24 *
25 * Two wait kinds, never one ambiguous wait:
26 * omega_gpu_wait_fixed the word must equal `expected` exactly. A value
27 * that overshoots is NOT success.
28 * omega_gpu_wait_sequence the word is a free-running u32 counter (for
29 * example dispatch ids). Success when it has reached
30 * or passed `target` in serial-number order, i.e.
31 * (int32_t)(observed - target) >= 0, so it is
32 * correct across the 0xffffffff -> 0 wrap. Valid
33 * while the counter is less than 2^31 ahead.
34 *
35 * Two bounds, both on CLOCK_MONOTONIC in nanoseconds:
36 * total_timeout_ms hard deadline from the start; never extended. A device
37 * that advances occasionally still hits it (TIMEOUT).
38 * progress_timeout_ms stall deadline; restarts only when a watched word
39 * (word, marker2, snapshot words) changes (STALLED).
40 *
41 * Memory visibility: a barrier (dsb sy on AArch64, a full fence elsewhere) runs
42 * before every read of the watched words and once more, after the success
43 * observation, before returning PASS. After PASS, the CPU loads issued by the
44 * caller are ordered after the marker load. That is ordering only. It does not
45 * make a cached CPU mapping coherent with GPU writes and it does not flush the
46 * GPU L2: the marker page must still be GPU-uncached and the producing work must
47 * flush (the omega_numeric_*_gb10.c pattern). The timer does not fix visibility.
48 *
49 * If cfg->marker2 is set, PASS also requires *marker2 == marker2_want (read
50 * output only after both). */
51
52#include <stdbool.h>
53#include <stdint.h>
54
55typedef enum {
56 OMEGA_GPU_WAIT_FIXED = 1,
57 OMEGA_GPU_WAIT_SEQUENCE = 2
58} omega_gpu_wait_kind_t;
59
60typedef enum {
61 OMEGA_GPU_WAIT_PASS = 0,
62 OMEGA_GPU_WAIT_TIMEOUT = 1, /* hard total deadline */
63 OMEGA_GPU_WAIT_STALLED = 2, /* no progress for progress_timeout_ms */
64 OMEGA_GPU_WAIT_INVALID_STATE = 3, /* cfg->valid rejected an observed value */
65 OMEGA_GPU_WAIT_ABORTED = 4 /* cfg->abort asked to stop */
66} omega_gpu_wait_result_t;
67
68typedef struct {
69 omega_gpu_wait_kind_t wait_kind;
70 omega_gpu_wait_result_t result;
71 uint32_t expected; /* fixed value or sequence target */
72 uint32_t last_observed; /* last value read from the watched word */
73 uint32_t last_marker2; /* last value read from marker2 (0 when unused) */
74 uint32_t snap[2]; /* last values from the snapshot callback */
75 uint64_t start_ns;
76 uint64_t elapsed_ns;
77 uint64_t last_progress_ns; /* time of the last observed change (start if none) */
78 uint64_t progress_count; /* observed changes after the first read */
79 uint64_t polls;
80} omega_gpu_wait_report_t;
81
82typedef struct {
83 uint32_t total_timeout_ms; /* 0 means the default, 600000 */
84 uint32_t progress_timeout_ms; /* 0 means the default, 5000 */
85 volatile uint32_t *marker2;
86 uint32_t marker2_want;
87 /* Optional: fill two extra words that count as progress when they change
88 * (for example GP_PUT and a semaphore). Called once per poll. */
89 void (*snapshot)(void *ctx, uint32_t out[2]);
90 void *snapshot_ctx;
91 /* Optional: return false to declare the observed word out of order. */
92 bool (*valid)(void *ctx, uint32_t observed);
93 /* Optional: return true to stop the wait (ABORTED). */
94 bool (*abort)(void *ctx);
95 /* Test hooks. NULL means the real ones (CLOCK_MONOTONIC, dsb, yield).
96 * hook_ctx is passed to now_ns, barrier, relax, valid and abort. The freestanding build (OMEGA_GPU_NO_POSIX)
97 * has no default clock: now_ns is required there (the platform clock) and a wait without it is INVALID_STATE. */
98 uint64_t (*now_ns)(void *ctx);
99 void (*barrier)(void *ctx);
100 void (*relax)(void *ctx);
101 void *hook_ctx;
102} omega_gpu_wait_cfg_t;
103
104#define OMEGA_GPU_WAIT_DEFAULT_TOTAL_MS 600000u
105#define OMEGA_GPU_WAIT_DEFAULT_PROGRESS_MS 5000u
106
107/* Both return true on PASS. rpt and cfg may be NULL (cfg NULL means defaults). */
108bool omega_gpu_wait_fixed(volatile uint32_t *word, uint32_t expected,
109 omega_gpu_wait_report_t *rpt, const omega_gpu_wait_cfg_t *cfg);
110bool omega_gpu_wait_sequence(volatile uint32_t *word, uint32_t target,
111 omega_gpu_wait_report_t *rpt, const omega_gpu_wait_cfg_t *cfg);
112
113const char *omega_gpu_wait_result_name(omega_gpu_wait_result_t r);
114const char *omega_gpu_wait_kind_name(omega_gpu_wait_kind_t k);
115
116#endif