Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-cpu/spacemit/spine_tcm.h # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-cpu / spacemit / spine_tcm.h` 409 lines ยท 13965 bytes Raw Blame History 1 #ifndef SPINE_TCM_PUBLIC_H_ 2 #define SPINE_TCM_PUBLIC_H_ 3 4 /* 5 * spine_tcm public API 6 * 7 * Usage: 8 * 1. Direct link mode 9 * Define SPINE_TCM_DIRECT_LINK and link against libspine_tcm.so. 10 * 11 * if (spine_tcm_is_available()) { 12 * void *buffer = spine_tcm_mem_get(0); 13 * spine_tcm_mem_free(0); 14 * } 15 * 16 * 2. Header-only loader mode 17 * Include this header without linking libspine_tcm.so. The loader first 18 * tries to reuse a process-global spine_tcm instance and falls back to 19 * dlopen("libspine_tcm.so") when needed. 20 * 21 * spine_tcm_open_handle(NULL); // optional pre-bind 22 * if (spine_tcm_is_available()) { 23 * void *buffer = spine_tcm_mem_get(0); 24 * spine_tcm_mem_free(0); 25 * } 26 */ 27 28 #include 29 #include 30 #include 31 32 #if !defined(SPINE_TCM_BUILD_SHARED) && !defined(SPINE_TCM_DIRECT_LINK) 33 # include 34 #endif 35 36 #ifdef __cplusplus 37 extern "C" { 38 #endif 39 40 #if defined(_WIN32) 41 # if defined(SPINE_TCM_BUILD_SHARED) 42 # define SPINE_TCM_API __declspec(dllexport) 43 # else 44 # define SPINE_TCM_API __declspec(dllimport) 45 # endif 46 #else 47 # define SPINE_TCM_API __attribute__((visibility("default"))) 48 #endif 49 50 typedef struct spine_tcm_mem_info { 51 size_t blk_size; 52 size_t blk_num; 53 int is_fake_tcm; 54 } spine_tcm_mem_info_t; 55 56 typedef struct spine_tcm_block_info { 57 int id; 58 void * va; 59 size_t size; 60 uint64_t phys_addr; 61 uint64_t cpu_affinity_mask; 62 int owner_tid; 63 int is_acquired; 64 } spine_tcm_block_info_t; 65 66 /* Shared-library runtime ABI exported by libspine_tcm.so. */ 67 SPINE_TCM_API const char * spine_tcm_runtime_version(void); 68 SPINE_TCM_API int spine_tcm_runtime_is_available(void); 69 SPINE_TCM_API int spine_tcm_runtime_layout_info(spine_tcm_mem_info_t * info); 70 SPINE_TCM_API int spine_tcm_runtime_mem_info(int id, spine_tcm_block_info_t * info); 71 SPINE_TCM_API void * spine_tcm_runtime_mem_get(int id); 72 SPINE_TCM_API int spine_tcm_runtime_mem_free(int id); 73 SPINE_TCM_API void * spine_tcm_runtime_mem_try_wait(int id, size_t timeout_us); 74 SPINE_TCM_API int spine_tcm_runtime_mem_release(int id); 75 SPINE_TCM_API int spine_tcm_runtime_mem_force_release(int id); 76 SPINE_TCM_API int spine_tcm_runtime_mem_query(int id); 77 78 #if defined(SPINE_TCM_DIRECT_LINK) 79 /* Optional no-op in direct-link mode. */ 80 static inline int spine_tcm_open_handle(const char * so_path) { 81 (void) so_path; 82 return 0; 83 } 84 85 static inline const char * spine_tcm_version(void) { 86 return spine_tcm_runtime_version(); 87 } 88 89 /* Returns 1 when the runtime driver is available, otherwise 0. */ 90 static inline int spine_tcm_is_available(void) { 91 return spine_tcm_runtime_is_available(); 92 } 93 94 /* Returns runtime memory geometry and whether the current backend is fake TCM. */ 95 static inline int spine_tcm_mem_info(spine_tcm_mem_info_t * info) { 96 return spine_tcm_runtime_layout_info(info); 97 } 98 99 /* Returns per-block runtime metadata for the given TCM id. */ 100 static inline int spine_tcm_block_info(int id, spine_tcm_block_info_t * info) { 101 return spine_tcm_runtime_mem_info(id, info); 102 } 103 104 /* Returns a cached buffer for the given TCM id, or NULL on failure. */ 105 static inline void * spine_tcm_mem_get(int id) { 106 return spine_tcm_runtime_mem_get(id); 107 } 108 109 /* Releases one reference acquired by spine_tcm_mem_get(id). */ 110 static inline int spine_tcm_mem_free(int id) { 111 return spine_tcm_runtime_mem_free(id); 112 } 113 114 /* Waits for a TCM block handoff and returns the driver-owned buffer when available. */ 115 static inline void * spine_tcm_mem_try_wait(int id, size_t over_time) { 116 return spine_tcm_runtime_mem_try_wait(id, over_time); 117 } 118 119 /* Releases a buffer acquired by spine_tcm_mem_try_wait(id, over_time). */ 120 static inline int spine_tcm_mem_release(int id) { 121 return spine_tcm_runtime_mem_release(id); 122 } 123 124 /* Forces a release for the given TCM id when the backend supports it. */ 125 static inline int spine_tcm_mem_force_release(int id) { 126 return spine_tcm_runtime_mem_force_release(id); 127 } 128 129 /* Returns whether the given TCM id is currently acquired. */ 130 static inline int spine_tcm_mem_query(int id) { 131 return spine_tcm_runtime_mem_query(id); 132 } 133 #elif !defined(SPINE_TCM_BUILD_SHARED) 134 typedef struct spine_tcm_handle { 135 void * module_handle; 136 int use_global_scope; 137 int owns_module_handle; 138 const char * (*runtime_version)(void); 139 int (*runtime_is_available)(void); 140 int (*runtime_layout_info)(spine_tcm_mem_info_t * info); 141 int (*runtime_mem_info)(int id, spine_tcm_block_info_t * info); 142 void * (*runtime_mem_get)(int id); 143 int (*runtime_mem_free)(int id); 144 void * (*runtime_mem_try_wait)(int id, size_t over_time); 145 int (*runtime_mem_release)(int id); 146 int (*runtime_mem_force_release)(int id); 147 int (*runtime_mem_query)(int id); 148 } spine_tcm_handle_t; 149 150 static inline spine_tcm_handle_t * spine_tcm_default_handle(void) { 151 static spine_tcm_handle_t handle = { 0 }; 152 return &handle; 153 } 154 155 static inline void spine_tcm_handle_reset(spine_tcm_handle_t * handle) { 156 if (handle != NULL) { 157 memset(handle, 0, sizeof(*handle)); 158 } 159 } 160 161 static inline int spine_tcm_handle_bind(spine_tcm_handle_t * handle) { 162 void * symbol_scope = handle->use_global_scope ? RTLD_DEFAULT : handle->module_handle; 163 164 handle->runtime_version = (const char * (*) (void) ) dlsym(symbol_scope, "spine_tcm_runtime_version"); 165 handle->runtime_is_available = (int (*)(void)) dlsym(symbol_scope, "spine_tcm_runtime_is_available"); 166 handle->runtime_layout_info = 167 (int (*)(spine_tcm_mem_info_t *)) dlsym(symbol_scope, "spine_tcm_runtime_layout_info"); 168 handle->runtime_mem_info = 169 (int (*)(int, spine_tcm_block_info_t *)) dlsym(symbol_scope, "spine_tcm_runtime_mem_info"); 170 handle->runtime_mem_get = (void * (*) (int) ) dlsym(symbol_scope, "spine_tcm_runtime_mem_get"); 171 handle->runtime_mem_free = (int (*)(int)) dlsym(symbol_scope, "spine_tcm_runtime_mem_free"); 172 handle->runtime_mem_try_wait = (void * (*) (int, size_t)) dlsym(symbol_scope, "spine_tcm_runtime_mem_try_wait"); 173 handle->runtime_mem_release = (int (*)(int)) dlsym(symbol_scope, "spine_tcm_runtime_mem_release"); 174 handle->runtime_mem_force_release = (int (*)(int)) dlsym(symbol_scope, "spine_tcm_runtime_mem_force_release"); 175 handle->runtime_mem_query = (int (*)(int)) dlsym(symbol_scope, "spine_tcm_runtime_mem_query"); 176 177 return handle->runtime_version != NULL && handle->runtime_is_available != NULL && 178 handle->runtime_layout_info != NULL && handle->runtime_mem_info != NULL && 179 handle->runtime_mem_get != NULL && handle->runtime_mem_free != NULL && 180 handle->runtime_mem_try_wait != NULL && handle->runtime_mem_release != NULL && 181 handle->runtime_mem_force_release != NULL && handle->runtime_mem_query != NULL ? 182 0 : 183 -1; 184 } 185 186 /* 187 * Try to bind against an already-loaded process-global spine_tcm instance. 188 * The shared library exports spine_tcm_runtime_marker only for this probe. 189 */ 190 static inline int spine_tcm_try_bind_global(spine_tcm_handle_t * handle) { 191 if (dlsym(RTLD_DEFAULT, "spine_tcm_runtime_marker") == NULL) { 192 return -1; 193 } 194 195 handle->use_global_scope = 1; 196 return spine_tcm_handle_bind(handle); 197 } 198 199 /* 200 * Optional pre-bind entry point. 201 * 202 * Behavior: 203 * - Reuses an already-loaded global spine_tcm instance when available. 204 * - Otherwise loads the shared library from so_path or the default soname. 205 * - Repeated calls are safe and return 0 after the first successful bind. 206 */ 207 static inline int spine_tcm_open_handle(const char * so_path) { 208 spine_tcm_handle_t * resolved = spine_tcm_default_handle(); 209 const char * library = (so_path != NULL && so_path[0] != '\0') ? so_path : "libspine_tcm.so"; 210 211 if (resolved->module_handle != NULL || resolved->use_global_scope) { 212 return 0; 213 } 214 215 if (spine_tcm_try_bind_global(resolved) == 0) { 216 return 0; 217 } 218 219 spine_tcm_handle_reset(resolved); 220 221 resolved->module_handle = dlopen(library, RTLD_LAZY | RTLD_GLOBAL); 222 resolved->owns_module_handle = resolved->module_handle != NULL ? 1 : 0; 223 224 if (resolved->module_handle == NULL) { 225 spine_tcm_handle_reset(resolved); 226 return -1; 227 } 228 229 if (spine_tcm_handle_bind(resolved) != 0) { 230 if (resolved->owns_module_handle) { 231 dlclose(resolved->module_handle); 232 } 233 spine_tcm_handle_reset(resolved); 234 return -1; 235 } 236 237 return 0; 238 } 239 240 /* Returns 1 when the runtime driver is available, otherwise 0. */ 241 static inline int spine_tcm_is_available(void) { 242 spine_tcm_handle_t * resolved = spine_tcm_default_handle(); 243 244 if (resolved->module_handle == NULL && !resolved->use_global_scope) { 245 (void) spine_tcm_open_handle(NULL); 246 } 247 248 if ((resolved->module_handle == NULL && !resolved->use_global_scope) || resolved->runtime_is_available == NULL) { 249 return 0; 250 } 251 252 return resolved->runtime_is_available(); 253 } 254 255 /* Returns runtime memory geometry and whether the current backend is fake TCM. */ 256 static inline int spine_tcm_mem_info(spine_tcm_mem_info_t * info) { 257 spine_tcm_handle_t * resolved = spine_tcm_default_handle(); 258 259 if (resolved->module_handle == NULL && !resolved->use_global_scope) { 260 (void) spine_tcm_open_handle(NULL); 261 } 262 263 if ((resolved->module_handle == NULL && !resolved->use_global_scope) || resolved->runtime_layout_info == NULL) { 264 return -1; 265 } 266 267 return resolved->runtime_layout_info(info); 268 } 269 270 static inline const char * spine_tcm_version(void) { 271 spine_tcm_handle_t * resolved = spine_tcm_default_handle(); 272 273 if (resolved->module_handle == NULL && !resolved->use_global_scope) { 274 (void) spine_tcm_open_handle(NULL); 275 } 276 277 if ((resolved->module_handle == NULL && !resolved->use_global_scope) || resolved->runtime_version == NULL) { 278 return "unknown"; 279 } 280 281 return resolved->runtime_version(); 282 } 283 284 /* Returns per-block runtime metadata for the given TCM id. */ 285 static inline int spine_tcm_block_info(int id, spine_tcm_block_info_t * info) { 286 spine_tcm_handle_t * resolved = spine_tcm_default_handle(); 287 288 if (resolved->module_handle == NULL && !resolved->use_global_scope) { 289 (void) spine_tcm_open_handle(NULL); 290 } 291 292 if ((resolved->module_handle == NULL && !resolved->use_global_scope) || resolved->runtime_mem_info == NULL) { 293 return -1; 294 } 295 296 return resolved->runtime_mem_info(id, info); 297 } 298 299 /* Returns a cached buffer for the given TCM id, or NULL on failure. */ 300 static inline void * spine_tcm_mem_get(int id) { 301 spine_tcm_handle_t * resolved = spine_tcm_default_handle(); 302 303 if (resolved->module_handle == NULL && !resolved->use_global_scope) { 304 (void) spine_tcm_open_handle(NULL); 305 } 306 307 if (resolved->module_handle == NULL && !resolved->use_global_scope) { 308 return NULL; 309 } 310 311 if (resolved->runtime_mem_get == NULL) { 312 return NULL; 313 } 314 315 return resolved->runtime_mem_get(id); 316 } 317 318 /* Releases one reference acquired by spine_tcm_mem_get(id). */ 319 static inline int spine_tcm_mem_free(int id) { 320 spine_tcm_handle_t * resolved = spine_tcm_default_handle(); 321 322 if (resolved->module_handle == NULL && !resolved->use_global_scope) { 323 (void) spine_tcm_open_handle(NULL); 324 } 325 326 if ((resolved->module_handle == NULL && !resolved->use_global_scope) || resolved->runtime_mem_free == NULL) { 327 return -1; 328 } 329 330 return resolved->runtime_mem_free(id); 331 } 332 333 /* Waits for a TCM block handoff and returns the driver-owned buffer when available. */ 334 static inline void * spine_tcm_mem_try_wait(int id, size_t over_time) { 335 spine_tcm_handle_t * resolved = spine_tcm_default_handle(); 336 337 if (resolved->module_handle == NULL && !resolved->use_global_scope) { 338 (void) spine_tcm_open_handle(NULL); 339 } 340 341 if (resolved->module_handle == NULL && !resolved->use_global_scope) { 342 return NULL; 343 } 344 345 if (resolved->runtime_mem_try_wait == NULL) { 346 return NULL; 347 } 348 349 return resolved->runtime_mem_try_wait(id, over_time); 350 } 351 352 /* Releases a buffer acquired by spine_tcm_mem_try_wait(id, over_time). */ 353 static inline int spine_tcm_mem_release(int id) { 354 spine_tcm_handle_t * resolved = spine_tcm_default_handle(); 355 356 if (resolved->module_handle == NULL && !resolved->use_global_scope) { 357 (void) spine_tcm_open_handle(NULL); 358 } 359 360 if ((resolved->module_handle == NULL && !resolved->use_global_scope) || resolved->runtime_mem_release == NULL) { 361 return -1; 362 } 363 364 return resolved->runtime_mem_release(id); 365 } 366 367 /* Forces a release for the given TCM id when the backend supports it. */ 368 static inline int spine_tcm_mem_force_release(int id) { 369 spine_tcm_handle_t * resolved = spine_tcm_default_handle(); 370 371 if (resolved->module_handle == NULL && !resolved->use_global_scope) { 372 (void) spine_tcm_open_handle(NULL); 373 } 374 375 if ((resolved->module_handle == NULL && !resolved->use_global_scope) || 376 resolved->runtime_mem_force_release == NULL) { 377 return -1; 378 } 379 380 return resolved->runtime_mem_force_release(id); 381 } 382 383 /* Returns whether the given TCM id is currently acquired. */ 384 static inline int spine_tcm_mem_query(int id) { 385 spine_tcm_handle_t * resolved = spine_tcm_default_handle(); 386 387 if (resolved->module_handle == NULL && !resolved->use_global_scope) { 388 (void) spine_tcm_open_handle(NULL); 389 } 390 391 if ((resolved->module_handle == NULL && !resolved->use_global_scope) || resolved->runtime_mem_query == NULL) { 392 return -1; 393 } 394 395 return resolved->runtime_mem_query(id); 396 } 397 #else 398 static inline const char * spine_tcm_version(void) { 399 return spine_tcm_runtime_version(); 400 } 401 #endif 402 403 #define SPINE_TCM_VERSION (spine_tcm_version()) 404 405 #ifdef __cplusplus 406 } 407 #endif 408 409 #endif