stl_accel.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #ifndef STL_ACCEL_H
  2. #define STL_ACCEL_H
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. #include <sys/types.h>
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. /* =========================================================================
  10. * 1. ISA Tiers & Runtime CPUID / XGETBV Dispatch
  11. * ========================================================================= */
  12. #define STL_ISA_SSE2 0
  13. #define STL_ISA_SSE42_POPCNT 1
  14. #define STL_ISA_AVX2_BMI 2
  15. int stl_cpu_tier_detect(void);
  16. int stl_active_isa_tier(void);
  17. void stl_force_isa_tier(int tier); /* -1 restores auto-detected tier */
  18. const char *stl_isa_tier_name(int tier);
  19. /* Assembly entry points (scan_asm.S) */
  20. uint64_t asm_xgetbv_eax0(void);
  21. size_t asm_base64_encode_sse2(const uint8_t *src, size_t len, uint8_t *dst);
  22. size_t asm_base64_encode_avx2(const uint8_t *src, size_t len, uint8_t *dst);
  23. /* Dispatched Base64 encoder */
  24. size_t stl_base64_encode(const uint8_t *src, size_t len, uint8_t *dst);
  25. /* =========================================================================
  26. * 2. Single-Pass Zero-Allocation Serial Clean + UEFI Fold + Split + Classify
  27. * ========================================================================= */
  28. #define STL_TAG_BRACKET (1u << 0) /* '[' banner / stage / tag */
  29. #define STL_TAG_PROMPT (1u << 1) /* '>' or '#' prompt candidate */
  30. #define STL_TAG_ERR_HINT (1u << 2) /* 'E','F','W','U' error/warning hint */
  31. #define STL_TAG_DIGIT (1u << 3) /* '0'..'9' numeric / coord / progress */
  32. #define STL_TAG_COLON_EQ (1u << 4) /* ':' or '=' key-value / table row */
  33. #define STL_HALF_LINE_CAP 1024
  34. typedef struct {
  35. uint32_t offset;
  36. uint32_t len;
  37. uint32_t tag_mask;
  38. uint8_t first_nonws;
  39. uint8_t term_kind; /* 1=LF, 2=CR, 4=ROW_JUMP, 5=WRAP */
  40. uint16_t reserved;
  41. } StlCleanLineSpan;
  42. typedef struct {
  43. uint32_t esc_state;
  44. uint32_t csi_p1;
  45. uint32_t csi_p2;
  46. uint32_t csi_param_idx;
  47. uint32_t cur_row;
  48. uint32_t cur_col;
  49. uint32_t prev_was_cr;
  50. uint32_t half_len;
  51. uint32_t tag_mask;
  52. uint8_t first_nonws;
  53. uint8_t csi_priv;
  54. uint16_t pad0;
  55. uint8_t half_buf[STL_HALF_LINE_CAP];
  56. } StlCleanStreamState;
  57. typedef struct {
  58. uint8_t *clean_buf;
  59. size_t clean_cap;
  60. size_t clean_written;
  61. StlCleanLineSpan *spans;
  62. size_t spans_cap;
  63. size_t spans_count;
  64. size_t src_consumed;
  65. } StlCleanOutput;
  66. _Static_assert(sizeof(StlCleanLineSpan) == 16, "StlCleanLineSpan must be 16 bytes");
  67. _Static_assert(offsetof(StlCleanStreamState, half_buf) == 40, "half_buf must be at offset 40");
  68. _Static_assert(sizeof(StlCleanOutput) == 56, "StlCleanOutput must be 56 bytes");
  69. void asm_clean_split_classify_sse2(
  70. StlCleanStreamState *state, const uint8_t *src, size_t src_len, StlCleanOutput *out);
  71. void asm_clean_split_classify_sse42(
  72. StlCleanStreamState *state, const uint8_t *src, size_t src_len, StlCleanOutput *out);
  73. void asm_clean_split_classify_avx2(
  74. StlCleanStreamState *state, const uint8_t *src, size_t src_len, StlCleanOutput *out);
  75. void stl_clean_stream_init(StlCleanStreamState *state);
  76. void stl_clean_stream_clear_halfline(StlCleanStreamState *state);
  77. void stl_clean_split_classify(
  78. StlCleanStreamState *state, const uint8_t *src, size_t src_len, StlCleanOutput *out);
  79. /* =========================================================================
  80. * 3. Monotonic 64-Bit mmap Ring Buffer (Hugepage + 64B Tail Pad + Guard Page)
  81. * ========================================================================= */
  82. typedef struct {
  83. uint8_t *buf; /* mmap base: [0..cap) ring, [cap..cap+64) zero pad, then PROT_NONE */
  84. size_t cap; /* power-of-two capacity (default 8 MiB) */
  85. size_t mask; /* cap - 1 */
  86. size_t mmap_total; /* total mmap length including guard page */
  87. uint64_t base_offset; /* oldest retained absolute byte offset (monotonic, never decreases) */
  88. uint64_t write_offset; /* next write absolute byte offset (monotonic, never decreases) */
  89. } StlRingBuf;
  90. int stl_ring_init(StlRingBuf *rb, size_t requested_cap);
  91. void stl_ring_destroy(StlRingBuf *rb);
  92. uint64_t stl_ring_append(StlRingBuf *rb, const uint8_t *data, size_t len);
  93. size_t stl_ring_read(
  94. const StlRingBuf *rb,
  95. uint64_t req_offset,
  96. uint8_t *dst,
  97. size_t max_len,
  98. uint64_t *out_actual_start,
  99. uint64_t *out_next_offset);
  100. void stl_ring_clear_monotonic(StlRingBuf *rb);
  101. /* =========================================================================
  102. * 4. Deterministic re2c Line Lexer (parser_re2c.re -> parser_re2c.c)
  103. * ========================================================================= */
  104. typedef enum {
  105. STL_LK_NONE = 0,
  106. STL_LK_ICT_BANNER_HOVSEE = 1,
  107. STL_LK_ICT_BANNER_CXMT = 2,
  108. STL_LK_ICT_START_STAGE1 = 3,
  109. STL_LK_ICT_START_STAGE2 = 4,
  110. STL_LK_ICT_ENTER_HINT = 5,
  111. STL_LK_ICT_PROMPT = 6,
  112. STL_LK_ICT_EXIT = 7,
  113. STL_LK_MRC_START = 8,
  114. STL_LK_MRC_DONE = 9,
  115. STL_LK_DIMM_INVENTORY = 10,
  116. STL_LK_RMT_START = 11,
  117. STL_LK_RMT_ROW = 12,
  118. STL_LK_RMT_STOP = 13,
  119. STL_LK_TRAINING_ERROR = 14,
  120. STL_LK_AMT_BANNER = 15,
  121. STL_LK_AMT_PROMPT = 16,
  122. STL_LK_AMT_PROGRESS = 17,
  123. STL_LK_AMT_SUMMARY = 18,
  124. STL_LK_AMT_ERROR = 19,
  125. STL_LK_UEFI_BANNER = 20,
  126. STL_LK_UEFI_PROMPT_ROOT = 21,
  127. STL_LK_UEFI_PROMPT_EFI = 22,
  128. STL_LK_PXE_BOOT = 23,
  129. STL_LK_MT_BANNER = 24,
  130. STL_LK_MT_PROGRESS = 25,
  131. STL_LK_MT_ERROR = 26,
  132. STL_LK_MT_PASS_COMPLETE = 27,
  133. STL_LK_MT_FAIL_COMPLETE = 28,
  134. STL_LK_INLINE_TEMP = 29,
  135. STL_LK_BIOS_BOOT = 30
  136. } StlLineKind;
  137. typedef struct {
  138. uint32_t kind; /* StlLineKind */
  139. uint8_t fw_family; /* 0=unknown, 1=Hovsee/GNR-4.3.3(24-algo), 2=CxmtDdrTest/GNR-4.3.0(19-algo) */
  140. int8_t cpu; /* -1 if unspecified, 0 or 1 */
  141. int8_t channel; /* -1 if unspecified, 0..11 */
  142. int8_t dimm; /* -1 if unspecified, 0..1 */
  143. int8_t rank; /* -1 if unspecified, 0..3 */
  144. int8_t bank; /* -1 if unspecified, validated 0..31 (never >= 32!) */
  145. int8_t subkind; /* 0=normal, 1=pass, 2=fail, 3=disabled, 4=present, 5=coord_cont */
  146. uint8_t fs_index; /* 0..9 for FSx:\ prompt */
  147. int32_t val0; /* algo_id / progress_pct / margin_rx_lo / error_cnt */
  148. int32_t val1; /* loop_or_total / margin_rx_hi */
  149. int32_t val2; /* step / margin_tx_lo */
  150. int32_t val3; /* margin_tx_hi / temp_c_x10 */
  151. uint64_t addr; /* parsed hex address if present */
  152. } StlRe2cEvent;
  153. _Static_assert(sizeof(StlRe2cEvent) == 40, "StlRe2cEvent must be 40 bytes");
  154. void stl_re2c_parse_line(const uint8_t *line, size_t len, uint32_t tag_mask, StlRe2cEvent *ev);
  155. int stl_re2c_detect_halfline_prompt(const uint8_t *buf, size_t len, StlRe2cEvent *ev);
  156. /* =========================================================================
  157. * 5. Linux epoll / timerfd / eventfd / Non-Blocking Serial & Socket Helpers
  158. * ========================================================================= */
  159. int stl_epoll_create(void);
  160. int stl_epoll_ctl_fd(int epfd, int op, int fd, uint32_t events, uint64_t token);
  161. int stl_epoll_wait_tokens(
  162. int epfd, uint64_t *out_tokens, uint32_t *out_events, int max_ev, int timeout_ms);
  163. int stl_timerfd_create_ms(uint64_t initial_ms, uint64_t interval_ms);
  164. int stl_timerfd_arm_ms(int tfd, uint64_t initial_ms, uint64_t interval_ms);
  165. void stl_timerfd_drain(int tfd);
  166. int stl_eventfd_create(void);
  167. void stl_eventfd_notify(int efd);
  168. void stl_eventfd_drain(int efd);
  169. int stl_set_nonblocking_nodelay(int fd, int is_tcp);
  170. int stl_open_tty_115200_raw(const char *path);
  171. int64_t stl_ws_writev2(int fd, const uint8_t *hdr, size_t hdr_len, const uint8_t *body, size_t body_len);
  172. void stl_pin_thread_to_core(int core_index);
  173. int c_pty_open_serial_115200(const char *path);
  174. int c_open_tty_baud_raw(const char *path, int baud);
  175. int c_open_mcu_tty_baud_raw(const char *path, int baud);
  176. int c_pty_open_tcp_serial(const char *host, int port, int timeout_ms);
  177. int c_pty_open_tcp_listen(int port);
  178. int c_pty_accept_tcp_nonblock(int listen_fd);
  179. int c_pty_spawn_ssh_console(
  180. const char *host,
  181. int port,
  182. const char *user,
  183. const char *password,
  184. const char *known_hosts_path,
  185. int *out_master_fd,
  186. int *out_child_pid);
  187. int c_pty_spawn_ipmi_sol(
  188. const char *host,
  189. const char *user,
  190. const char *password,
  191. int *out_master_fd,
  192. int *out_child_pid);
  193. ssize_t c_pty_read_nonblock(int master_fd, uint8_t *buf, size_t cap);
  194. ssize_t c_mcu_tty_read_nonblock(int fd, uint8_t *buf, size_t cap, int is_serial);
  195. ssize_t c_pty_write_bytes(int master_fd, const uint8_t *buf, size_t len);
  196. int c_pty_is_alive(int child_pid);
  197. void c_pty_close(int master_fd, int child_pid);
  198. /* In-process libssh exec + SFTP atomic file transfer */
  199. int c_ssh_exec_capture(
  200. const char *host,
  201. int port,
  202. const char *user,
  203. const char *password,
  204. const char *cmd,
  205. char *out_buf,
  206. size_t out_cap,
  207. size_t *out_len,
  208. int timeout_ms);
  209. int c_ssh_sftp_put_file(
  210. const char *host,
  211. int port,
  212. const char *user,
  213. const char *password,
  214. const char *local_path,
  215. const char *remote_path,
  216. int timeout_ms);
  217. int c_ssh_sftp_get_file(
  218. const char *host,
  219. int port,
  220. const char *user,
  221. const char *password,
  222. const char *remote_path,
  223. const char *local_path,
  224. int timeout_ms);
  225. /* In-process libfreeipmi persistent session & telemetry */
  226. int c_ipmi_open(
  227. const char *host,
  228. const char *user,
  229. const char *password,
  230. int cipher_suite,
  231. int timeout_ms);
  232. void c_ipmi_close(int handle);
  233. int c_ipmi_is_alive(int handle);
  234. int c_ipmi_chassis_status(int handle, int *out_power_on, char *out_buf, size_t out_cap);
  235. int c_ipmi_chassis_control(int handle, int op);
  236. int c_ipmi_warm_reset(int handle);
  237. int c_ipmi_raw(
  238. int handle,
  239. uint8_t net_fn,
  240. uint8_t cmd,
  241. const uint8_t *payload,
  242. size_t payload_len,
  243. uint8_t *out_buf,
  244. size_t out_cap,
  245. size_t *out_len);
  246. int c_ipmi_sdr_temp_list(int handle, char *out_buf, size_t out_cap, size_t *out_len);
  247. int c_ipmi_device_info(
  248. int handle,
  249. const char *kind,
  250. char *out_buf,
  251. size_t out_cap,
  252. size_t *out_len);
  253. /* =========================================================================
  254. * 6. OpenCL Gen7.5-Gen9.5 Zero-Copy Batch Offload (Auto CPU Fallback)
  255. * ========================================================================= */
  256. typedef struct {
  257. int available; /* 1 if Intel/OpenCL iGPU initialized */
  258. int zero_copy_llc; /* 1 if unified memory CL_MEM_USE_HOST_PTR supported */
  259. uint32_t compute_units;
  260. uint64_t break_even_bytes; /* S* threshold (default 262144 = 256 KiB) */
  261. char device_name[96];
  262. } StlOpenClInfo;
  263. void stl_opencl_query_info(StlOpenClInfo *out_info);
  264. int stl_opencl_has_tdp_headroom(void);
  265. /* Batch historical log newline/error-marker pre-index; auto-falls back to CPU SIMD below S* or low TDP */
  266. size_t stl_batch_rescan_logs(
  267. const uint8_t *buf_4k_aligned,
  268. size_t len,
  269. uint32_t *out_line_offsets,
  270. uint8_t *out_line_tags,
  271. size_t max_lines,
  272. int *used_gpu);
  273. /* Multi-station RMT margin matrix min/max/mean & 16x16 eye-diagram bilinear interpolation */
  274. int stl_batch_rmt_eye_reduce(
  275. const float *margins_in, /* [station_count * 24 * 4] (rx_lo, rx_hi, tx_lo, tx_hi) */
  276. size_t station_count,
  277. float *eye_grid_out, /* [station_count * 24 * 256] (16x16 normalized eye surface) */
  278. float *summary_out, /* [station_count * 4] (min_timing, min_vref, mean_timing, mean_vref) */
  279. int *used_gpu);
  280. #ifdef __cplusplus
  281. }
  282. #endif
  283. #endif /* STL_ACCEL_H */