stl_accel.h 9.4 KB

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