interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / m16 / m16_gpu_wait.h

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