physics-mv
FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.
Brought by drakestapleton
physics-mv / m16 / m16_gpu_wait.h
| 1 | #ifndef M16_GPU_WAIT_H |
| 2 | #define M16_GPU_WAIT_H |
| 3 | |
| 4 | /* Shared completion wait for words the GPU writes to host-visible memory. |
| 5 | * |
| 6 | * Pure C, no NVRM dependency, so the host unit test can drive it with fake |
| 7 | * memory and a fake clock. |
| 8 | * |
| 9 | * Two wait kinds, never one ambiguous wait: |
| 10 | * m16_gpu_wait_fixed the word must equal `expected` exactly. A value |
| 11 | * that overshoots is NOT success. |
| 12 | * m16_gpu_wait_sequence the word is a free-running u32 counter (for |
| 13 | * example dispatch ids). Success when it has reached |
| 14 | * or passed `target` in serial-number order, i.e. |
| 15 | * (int32_t)(observed - target) >= 0, so it is |
| 16 | * correct across the 0xffffffff -> 0 wrap. Valid |
| 17 | * while the counter is less than 2^31 ahead. |
| 18 | * |
| 19 | * Two bounds, both on CLOCK_MONOTONIC in nanoseconds: |
| 20 | * total_timeout_ms hard deadline from the start; never extended. A device |
| 21 | * that advances occasionally still hits it (TIMEOUT). |
| 22 | * progress_timeout_ms stall deadline; restarts only when a watched word |
| 23 | * (word, marker2, snapshot words) changes (STALLED). |
| 24 | * |
| 25 | * Memory visibility: a barrier (dsb sy on AArch64, a full fence elsewhere) runs |
| 26 | * before every read of the watched words and once more, after the success |
| 27 | * observation, before returning PASS. After PASS, the CPU loads issued by the |
| 28 | * caller are ordered after the marker load. That is ordering only. It does not |
| 29 | * make a cached CPU mapping coherent with GPU writes and it does not flush the |
| 30 | * GPU L2: the marker page must still be GPU-uncached and the producing work must |
| 31 | * flush (WFI marker, L2_FLUSH_DIRTY mem-op, second WFI marker; see |
| 32 | * m16/m16_concurrent.c). The timer does not fix visibility. |
| 33 | * |
| 34 | * Same contract as omega's src/omega_gpu_wait.h (omega 6fa4d97, PR omega#232). |
| 35 | * Physics cannot include omega code, so this is the canonical copy; omega's |
| 36 | * becomes a thin wrapper in a later cut. m16_native_wait_marker(_ge) is not |
| 37 | * converted yet (follow-up). |
| 38 | * |
| 39 | * If cfg->marker2 is set, PASS also requires *marker2 == marker2_want (read |
| 40 | * output only after both). */ |
| 41 | |
| 42 | #include <stdbool.h> |
| 43 | #include <stdint.h> |
| 44 | |
| 45 | typedef enum { |
| 46 | M16_GPU_WAIT_FIXED = 1, |
| 47 | M16_GPU_WAIT_SEQUENCE = 2 |
| 48 | } m16_gpu_wait_kind_t; |
| 49 | |
| 50 | typedef enum { |
| 51 | M16_GPU_WAIT_PASS = 0, |
| 52 | M16_GPU_WAIT_TIMEOUT = 1, /* hard total deadline */ |
| 53 | M16_GPU_WAIT_STALLED = 2, /* no progress for progress_timeout_ms */ |
| 54 | M16_GPU_WAIT_INVALID_STATE = 3, /* cfg->valid rejected an observed value */ |
| 55 | M16_GPU_WAIT_ABORTED = 4 /* cfg->abort asked to stop */ |
| 56 | } m16_gpu_wait_result_t; |
| 57 | |
| 58 | typedef struct { |
| 59 | m16_gpu_wait_kind_t wait_kind; |
| 60 | m16_gpu_wait_result_t result; |
| 61 | uint32_t expected; /* fixed value or sequence target */ |
| 62 | uint32_t last_observed; /* last value read from the watched word */ |
| 63 | uint32_t last_marker2; /* last value read from marker2 (0 when unused) */ |
| 64 | uint32_t snap[2]; /* last values from the snapshot callback */ |
| 65 | uint64_t start_ns; |
| 66 | uint64_t elapsed_ns; |
| 67 | uint64_t last_progress_ns; /* time of the last observed change (start if none) */ |
| 68 | uint64_t progress_count; /* observed changes after the first read */ |
| 69 | uint64_t polls; |
| 70 | } m16_gpu_wait_report_t; |
| 71 | |
| 72 | typedef struct { |
| 73 | uint32_t total_timeout_ms; /* 0 means the default, 600000 */ |
| 74 | uint32_t progress_timeout_ms; /* 0 means the default, 5000 */ |
| 75 | volatile uint32_t *marker2; |
| 76 | uint32_t marker2_want; |
| 77 | /* Optional: fill two extra words that count as progress when they change |
| 78 | * (for example GP_PUT and a semaphore). Called once per poll. */ |
| 79 | void (*snapshot)(void *ctx, uint32_t out[2]); |
| 80 | void *snapshot_ctx; |
| 81 | /* Optional: return false to declare the observed word out of order. */ |
| 82 | bool (*valid)(void *ctx, uint32_t observed); |
| 83 | /* Optional: return true to stop the wait (ABORTED). */ |
| 84 | bool (*abort)(void *ctx); |
| 85 | /* Test hooks. NULL means the real ones (CLOCK_MONOTONIC, dsb, yield). |
| 86 | * hook_ctx is passed to now_ns, barrier, relax, valid and abort. */ |
| 87 | uint64_t (*now_ns)(void *ctx); |
| 88 | void (*barrier)(void *ctx); |
| 89 | void (*relax)(void *ctx); |
| 90 | void *hook_ctx; |
| 91 | } m16_gpu_wait_cfg_t; |
| 92 | |
| 93 | #define M16_GPU_WAIT_DEFAULT_TOTAL_MS 600000u |
| 94 | #define M16_GPU_WAIT_DEFAULT_PROGRESS_MS 5000u |
| 95 | |
| 96 | /* Both return true on PASS. rpt and cfg may be NULL (cfg NULL means defaults). */ |
| 97 | bool m16_gpu_wait_fixed(volatile uint32_t *word, uint32_t expected, |
| 98 | m16_gpu_wait_report_t *rpt, const m16_gpu_wait_cfg_t *cfg); |
| 99 | bool m16_gpu_wait_sequence(volatile uint32_t *word, uint32_t target, |
| 100 | m16_gpu_wait_report_t *rpt, const m16_gpu_wait_cfg_t *cfg); |
| 101 | |
| 102 | /* Failure class of a failed wait, for tagged test output (additive, not in omega's copy): |
| 103 | * "wrong_marker" INVALID_STATE; or, for a FIXED wait, the word holds a nonzero |
| 104 | * value other than the expected one; or marker2 holds a nonzero |
| 105 | * value other than marker2_want. (A SEQUENCE word below its |
| 106 | * target is progress, never wrong.) Independent of TIMEOUT vs STALLED. |
| 107 | * "late_progress" something moved (progress_count > 0, or the word already held |
| 108 | * its value but marker2 never arrived) yet the wait did not finish |
| 109 | * (hard TIMEOUT while still moving, or stalled after moving) |
| 110 | * "no_progress" word and marker2 never left 0 |
| 111 | * Call only on a report whose result is not PASS. marker2_want is 0 when unused. */ |
| 112 | const char *m16_gpu_wait_failure_class(const m16_gpu_wait_report_t *r, uint32_t marker2_want); |
| 113 | |
| 114 | const char *m16_gpu_wait_result_name(m16_gpu_wait_result_t r); |
| 115 | const char *m16_gpu_wait_kind_name(m16_gpu_wait_kind_t k); |
| 116 | |
| 117 | #endif |