ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-hexagon / htp / main.c
| 1 | #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" |
| 2 | #pragma clang diagnostic ignored "-Wunused-function" |
| 3 | #pragma clang diagnostic ignored "-Wunused-variable" |
| 4 | #pragma clang diagnostic ignored "-Wunused-but-set-variable" |
| 5 | |
| 6 | #include <HAP_farf.h> |
| 7 | #include <HAP_perf.h> |
| 8 | #include <AEEStdErr.h> |
| 9 | #include <dspqueue.h> |
| 10 | #include <HAP_compute_res.h> |
| 11 | #include <HAP_etm_config.h> |
| 12 | #include <HAP_mem.h> |
| 13 | #include <HAP_power.h> |
| 14 | #include <HAP_ps.h> |
| 15 | #include <HAP_dcvs.h> |
| 16 | #include <qurt.h> |
| 17 | #include <qurt_thread.h> |
| 18 | #include <qurt_memory.h> |
| 19 | #include <remote.h> |
| 20 | #include <string.h> |
| 21 | #include <stdatomic.h> |
| 22 | |
| 23 | #include "hex-utils.h" |
| 24 | #include "dma-queue.h" |
| 25 | #include "hmx-queue.h" |
| 26 | |
| 27 | #define GGML_COMMON_DECL_C |
| 28 | #include "ggml-common.h" |
| 29 | #include "hex-bitmap.h" |
| 30 | #include "htp-ctx.h" |
| 31 | #include "htp-dcache.h" |
| 32 | #include "htp-ops.h" |
| 33 | #include "htp_iface.h" |
| 34 | #include "work-queue.h" |
| 35 | #include "hex-profile.h" |
| 36 | #include "allreduce-ops.h" |
| 37 | #include "dsv4-hc-ops.h" |
| 38 | #include "htp-fence.h" |
| 39 | |
| 40 | #define HMX_QUEUE_CAPACITY 128 |
| 41 | #define HMX_QUEUE_STACK_SIZE 16384 |
| 42 | #define WORK_QUEUE_CAPACITY 16 |
| 43 | #define WORK_QUEUE_STACK_SIZE 16384 |
| 44 | #define MAIN_THREAD_STACK_SIZE 32768 |
| 45 | |
| 46 | _Static_assert(WORK_QUEUE_MAX_N_THREADS >= HTP_MAX_NTHREADS, |
| 47 | "work-queue thread cap must be >= HTP_MAX_NTHREADS"); |
| 48 | |
| 49 | struct htp_handle { |
| 50 | struct htp_context * ctx; |
| 51 | }; |
| 52 | |
| 53 | static inline uint64_t htp_mmap(uint32_t fd, uint64_t size, uint32_t flags) { |
| 54 | #if __HVX_ARCH__ > 79 |
| 55 | if (flags & HTP_BUF_EXTENDED) { |
| 56 | HAP_mem_req_payload_t payload; |
| 57 | memset(&payload, 0, sizeof(payload)); |
| 58 | payload.request_id = HAP_MEM_MAP; |
| 59 | payload.mmap.len = size; |
| 60 | payload.mmap.prot = HAP_MEM_CACHE_NON_SHARED | HAP_PROT_READ; |
| 61 | payload.mmap.flags = HAP_MEM_FLAGS_EXTENDED_MAP; |
| 62 | payload.mmap.fd = fd; |
| 63 | |
| 64 | if (HAP_mem_request(&payload) != 0) { |
| 65 | FARF(ERROR, "extended mmap failed : fd %u size %llu", fd, (unsigned long long) size); |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | return payload.mmap.dsp_va; |
| 70 | } |
| 71 | #else |
| 72 | if (flags & HTP_BUF_EXTENDED) { |
| 73 | FARF(ERROR, "extended mmap is unsupported on v%d", __HVX_ARCH__); |
| 74 | return 0; |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | if (size > UINT32_MAX) { |
| 79 | FARF(ERROR, "mmap failed : size %llu exceeds 32-bit limit", (unsigned long long) size); |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | void * va = (void *)-1; |
| 84 | for (int retry = 0; retry < 2; retry++) { |
| 85 | #if __HVX_ARCH__ > 73 |
| 86 | va = HAP_mmap2(NULL, (size_t) size, HAP_PROT_READ | HAP_PROT_WRITE, 0, fd, 0); |
| 87 | #else |
| 88 | if (size > HTP_MMAP_MAX_VMEM) { |
| 89 | FARF(ERROR, "mmap failed : size %llu exceeds 2GB limit for HAP_mmap", (unsigned long long) size); |
| 90 | abort(); |
| 91 | } |
| 92 | va = HAP_mmap(NULL, (int) size, HAP_PROT_READ | HAP_PROT_WRITE, 0, fd, 0); |
| 93 | #endif |
| 94 | if (va != (void *)-1 && va != NULL) { |
| 95 | return (uint64_t) (uintptr_t) va; |
| 96 | } |
| 97 | if (retry == 0) { |
| 98 | FARF(HIGH, "mmap failed first try (va %p fd %u size %llu), retrying...", va, fd, (unsigned long long) size); |
| 99 | } |
| 100 | } |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static inline void htp_munmap(uint64_t va, uint64_t size, uint32_t flags) { |
| 105 | #if __HVX_ARCH__ > 79 |
| 106 | if (flags & HTP_BUF_EXTENDED) { |
| 107 | HAP_mem_req_payload_t payload; |
| 108 | memset(&payload, 0, sizeof(payload)); |
| 109 | payload.request_id = HAP_MEM_UNMAP; |
| 110 | payload.munmap.dsp_va = va; |
| 111 | payload.munmap.len = size; |
| 112 | HAP_mem_request(&payload); |
| 113 | return; |
| 114 | } |
| 115 | #endif |
| 116 | |
| 117 | #if __HVX_ARCH__ > 73 |
| 118 | HAP_munmap2((void *) (uintptr_t) va, (size_t) size); |
| 119 | #else |
| 120 | HAP_munmap((void *) (uintptr_t) va, (int) size); |
| 121 | #endif |
| 122 | } |
| 123 | |
| 124 | AEEResult htp_iface_open(const char * uri, remote_handle64 * handle) { |
| 125 | (void) uri; |
| 126 | struct htp_handle * h = calloc(1, sizeof(*h)); |
| 127 | if (h == NULL) { |
| 128 | return AEE_ENOMEMORY; |
| 129 | } |
| 130 | |
| 131 | *handle = (remote_handle64) h; |
| 132 | return AEE_SUCCESS; |
| 133 | } |
| 134 | |
| 135 | AEEResult htp_iface_etm(remote_handle64 handle, uint32_t enable) { |
| 136 | struct htp_handle * h = (struct htp_handle *) handle; |
| 137 | if (!h) { |
| 138 | return AEE_EBADPARM; |
| 139 | } |
| 140 | |
| 141 | int err = enable ? HAP_user_etm_enable() : HAP_user_etm_disable(); |
| 142 | if (err) { |
| 143 | if (err == AEE_EVERSIONNOTSUPPORT) { |
| 144 | FARF(ERROR, "API HAP_user_etm_enable/disable is not supported\n"); |
| 145 | } else { |
| 146 | FARF(ERROR, "Error executing HAP_user_etm_enable/disable with error code : 0x%x\n", err); |
| 147 | } |
| 148 | } |
| 149 | return err; |
| 150 | } |
| 151 | |
| 152 | AEEResult htp_iface_profiler(remote_handle64 handle, uint32_t mode, const htp_iface_pmu_conf* pmu_conf) { |
| 153 | struct htp_handle * h = (struct htp_handle *) handle; |
| 154 | if (!h || !h->ctx) { |
| 155 | return AEE_EBADPARM; |
| 156 | } |
| 157 | struct htp_context * ctx = h->ctx; |
| 158 | |
| 159 | if (mode == HTP_PROF_PMU) { |
| 160 | const uint32_t* events = pmu_conf->events; |
| 161 | |
| 162 | // Pack 4 event IDs (low 8 bits) into each 32-bit config register |
| 163 | uint32_t evtcfg = 0, evtcfg1 = 0, cfg = 0, i = 0; |
| 164 | for (; i < HEX_NUM_PMU_COUNTERS/2; i++) { |
| 165 | evtcfg |= ((events[i + 0] & 0xFF) << (i * 8)); |
| 166 | evtcfg1 |= ((events[i + 4] & 0xFF) << (i * 8)); |
| 167 | } |
| 168 | |
| 169 | // For events >255 pack high 2 bits of all 8 event IDs into cfg register |
| 170 | // 2 bits per counter: bits [1:0] for counter 0, [3:2] for counter 1, etc. |
| 171 | for (i = 0; i < HEX_NUM_PMU_COUNTERS; i++) { |
| 172 | cfg |= (((events[i] >> 8) & 3) << (i * 2)); |
| 173 | } |
| 174 | |
| 175 | FARF(ALWAYS, "Configuring PMU registers: evtcfg = 0x%x, evtcfg1 = 0x%x, pmucfg = 0x%x", evtcfg, evtcfg1, cfg); |
| 176 | |
| 177 | // Configure PMU registers |
| 178 | qurt_pmu_set(QURT_PMUCFG, cfg); |
| 179 | qurt_pmu_set(QURT_PMUEVTCFG, evtcfg); |
| 180 | qurt_pmu_set(QURT_PMUEVTCFG1, evtcfg1); |
| 181 | qurt_pmu_enable(1); |
| 182 | } |
| 183 | |
| 184 | ctx->profiler = mode; |
| 185 | |
| 186 | return AEE_SUCCESS; |
| 187 | } |
| 188 | |
| 189 | AEEResult htp_iface_close(remote_handle64 handle) { |
| 190 | struct htp_handle * h = (struct htp_handle *) handle; |
| 191 | if (!h) { |
| 192 | return AEE_EBADPARM; |
| 193 | } |
| 194 | |
| 195 | struct htp_context * ctx = h->ctx; |
| 196 | if (ctx) { |
| 197 | if (ctx->dsp_queue) { |
| 198 | FARF(ERROR, "Closing handle with queue still open"); |
| 199 | return AEE_EITEMBUSY; |
| 200 | } |
| 201 | |
| 202 | // release the mmaps (if any) |
| 203 | for (uint32_t i=0; i<HTP_MAX_MMAPS; i++) { |
| 204 | if (ctx->mmap[i].size) { |
| 205 | htp_munmap(ctx->mmap[i].base, ctx->mmap[i].size, ctx->mmap[i].flags); |
| 206 | ctx->mmap[i].size = 0; |
| 207 | ctx->mmap[i].base = 0; |
| 208 | ctx->mmap[i].fd = -1; |
| 209 | ctx->mmap[i].flags = 0; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if (ctx->profiler) { |
| 214 | qurt_pmu_enable(1); |
| 215 | } |
| 216 | |
| 217 | if (ctx->etm) { |
| 218 | HAP_user_etm_disable(); |
| 219 | } |
| 220 | |
| 221 | // Free the unified block (ctx is the base address of the block) |
| 222 | free(ctx); |
| 223 | h->ctx = NULL; |
| 224 | } |
| 225 | |
| 226 | free(h); |
| 227 | return AEE_SUCCESS; |
| 228 | } |
| 229 | |
| 230 | AEEResult htp_iface_mmap(remote_handle64 handle, uint32_t fd, uint64_t size) { |
| 231 | struct htp_handle * h = (struct htp_handle *) handle; |
| 232 | if (!h || !h->ctx) { |
| 233 | return AEE_EBADPARM; |
| 234 | } |
| 235 | struct htp_context * ctx = h->ctx; |
| 236 | |
| 237 | // See if we already have this mapping |
| 238 | for (uint32_t i=0; i<HTP_MAX_MMAPS; i++) { |
| 239 | struct htp_mmap *m = &ctx->mmap[i]; |
| 240 | if (m->fd == fd) { |
| 241 | return AEE_SUCCESS; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // Add new mapping |
| 246 | for (uint32_t i=0; i<HTP_MAX_MMAPS; i++) { |
| 247 | struct htp_mmap *m = &ctx->mmap[i]; |
| 248 | if (!m->size) { |
| 249 | FARF(HIGH, "mmap : fd %u size %llu", fd, (unsigned long long) size); |
| 250 | uint64_t va = htp_mmap(fd, size, 0); |
| 251 | if (va == 0) { |
| 252 | FARF(ERROR, "mmap failed : fd %u size %llu", fd, (unsigned long long) size); |
| 253 | return AEE_EFAILED; |
| 254 | } |
| 255 | |
| 256 | m->base = va; |
| 257 | m->fd = fd; |
| 258 | m->size = size; |
| 259 | m->flags = 0; |
| 260 | |
| 261 | return AEE_SUCCESS; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | return AEE_ENOMEMORY; |
| 266 | } |
| 267 | |
| 268 | AEEResult htp_iface_munmap(remote_handle64 handle, uint32 fd) { |
| 269 | struct htp_handle * h = (struct htp_handle *) handle; |
| 270 | if (!h || !h->ctx) { |
| 271 | return AEE_EBADPARM; |
| 272 | } |
| 273 | struct htp_context * ctx = h->ctx; |
| 274 | |
| 275 | for (uint32_t i=0; i<HTP_MAX_MMAPS; i++) { |
| 276 | struct htp_mmap *m = &ctx->mmap[i]; |
| 277 | if (fd < 0 || m->fd == fd) { |
| 278 | FARF(HIGH, "unmmap : base 0x%llx fd %u size %llu", (unsigned long long) m->base, m->fd, (unsigned long long) m->size); |
| 279 | htp_munmap(m->base, m->size, m->flags); |
| 280 | m->size = 0; |
| 281 | m->base = NULL; |
| 282 | m->fd = -1; |
| 283 | m->flags = 0; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | return AEE_SUCCESS; |
| 288 | } |
| 289 | |
| 290 | static void vtcm_acquire(struct htp_context * ctx) { |
| 291 | if (!atomic_load(&ctx->vtcm_valid)) { |
| 292 | int err = HAP_compute_res_acquire_cached(ctx->vtcm_rctx, 10000000u); |
| 293 | if (err != 0) { |
| 294 | FARF(ERROR, "ggml-hex: failed to acquire VTCM: 0x%08x", (unsigned)err); |
| 295 | abort(); |
| 296 | } |
| 297 | |
| 298 | atomic_store(&ctx->vtcm_needs_release, false); |
| 299 | atomic_store(&ctx->vtcm_valid, true); |
| 300 | |
| 301 | // Drop the priority to make sure we get the release callback from other GGML-HTP and QNN-HTP sessions |
| 302 | HAP_compute_res_update_priority(ctx->vtcm_rctx, ctx->thread_prio + 10); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | static void vtcm_release(struct htp_context * ctx) { |
| 307 | if (atomic_load(&ctx->vtcm_valid)) { |
| 308 | atomic_store(&ctx->vtcm_valid, false); |
| 309 | atomic_store(&ctx->vtcm_needs_release, false); |
| 310 | HAP_compute_res_release_cached(ctx->vtcm_rctx); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | static int vtcm_release_callback(unsigned int rctx, void * state) { |
| 315 | struct htp_context * ctx = (struct htp_context *) state; |
| 316 | atomic_store(&ctx->vtcm_needs_release, true); |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static unsigned int vtcm_query_size(void) { |
| 321 | unsigned int vtcm_size = 8 * 1024 * 1024; // 8MB default |
| 322 | unsigned int vtcm_avail; |
| 323 | compute_res_vtcm_page_t total_layout, avail_layout; |
| 324 | // some images (e.g. FuSa cDSP) fail the query when the layout out-params are NULL |
| 325 | HAP_compute_res_query_VTCM(0, &vtcm_size, &total_layout, &vtcm_avail, &avail_layout); |
| 326 | return vtcm_size; |
| 327 | } |
| 328 | |
| 329 | static int vtcm_alloc(struct htp_context * ctx, uint64_t size_override) { |
| 330 | unsigned int vtcm_size = vtcm_query_size(); |
| 331 | if (size_override) { |
| 332 | vtcm_size = (unsigned int) size_override; |
| 333 | } |
| 334 | |
| 335 | compute_res_attr_t attr; |
| 336 | HAP_compute_res_attr_init(&attr); |
| 337 | HAP_compute_res_attr_set_serialize(&attr, 0); |
| 338 | HAP_compute_res_attr_set_cache_mode(&attr, 1); |
| 339 | HAP_compute_res_attr_set_vtcm_param_v2(&attr, vtcm_size, vtcm_size, vtcm_size); // single page |
| 340 | HAP_compute_res_attr_set_release_callback(&attr, vtcm_release_callback, (void *) ctx); |
| 341 | HAP_compute_res_attr_set_hmx_param(&attr, 1); |
| 342 | |
| 343 | // Allocate VTCM for scratch pads |
| 344 | uint32_t rctx = HAP_compute_res_acquire(&attr, 1000000 /* timeout */); |
| 345 | if (!rctx) { |
| 346 | FARF(ERROR, "failed to allocate %zu bytes VTCM\n", ctx->vtcm_size); |
| 347 | return AEE_ENOMEMORY; |
| 348 | } |
| 349 | |
| 350 | void * vtcm_ptr; |
| 351 | if (HAP_compute_res_attr_get_vtcm_ptr_v2(&attr, &vtcm_ptr, &vtcm_size) != 0) { |
| 352 | HAP_compute_res_release(rctx); |
| 353 | FARF(ERROR, "failed to allocate %zu bytes VTCM (new)\n", ctx->vtcm_size); |
| 354 | return AEE_ENOMEMORY; |
| 355 | } |
| 356 | |
| 357 | ctx->vtcm_base = (uint8_t *) vtcm_ptr; |
| 358 | ctx->vtcm_size = vtcm_size; |
| 359 | ctx->vtcm_rctx = rctx; |
| 360 | atomic_store(&ctx->vtcm_valid, false); |
| 361 | atomic_store(&ctx->vtcm_needs_release, false); |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | static void vtcm_free(struct htp_context * ctx) { |
| 367 | if (ctx->vtcm_rctx) { |
| 368 | HAP_compute_res_release(ctx->vtcm_rctx); |
| 369 | ctx->vtcm_base = 0; |
| 370 | ctx->vtcm_rctx = 0; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | static void htp_main_thread(void * context); |
| 375 | static void htp_packet_callback(dspqueue_t queue, int error, void * context); |
| 376 | static void htp_error_callback(dspqueue_t queue, int error, void * context); |
| 377 | |
| 378 | // Non-BURST modes leave DCVS enabled and keep the HTP awake. |
| 379 | // BURST pins all corners to MAX and disables DCVS. |
| 380 | // Corner constants are from HAP_power.h (HAP_dcvs_voltage_corner_t / HAP_dcvs_exp_vcorner_t). |
| 381 | struct htp_power_profile { |
| 382 | boolean dcvs_enable; |
| 383 | uint32_t bus_min_corner; |
| 384 | uint32_t bus_max_corner; |
| 385 | uint32_t bus_target_corner; |
| 386 | uint32_t core_min_corner; |
| 387 | uint32_t core_max_corner; |
| 388 | uint32_t core_target_corner; |
| 389 | boolean sleep_disable; |
| 390 | uint32_t hmx_min_corner; // only used when __HVX_ARCH__ >= 75 |
| 391 | uint32_t hmx_max_corner; |
| 392 | uint32_t hmx_target_corner; |
| 393 | uint32_t hmx_perf_mode; |
| 394 | }; |
| 395 | |
| 396 | #define HTP_POWER_PROFILE_ENTRY(min_c, max_c, hmx_c) \ |
| 397 | { \ |
| 398 | .dcvs_enable = TRUE, \ |
| 399 | .bus_min_corner = (min_c), \ |
| 400 | .bus_max_corner = (max_c), \ |
| 401 | .bus_target_corner = (max_c), \ |
| 402 | .core_min_corner = (min_c), \ |
| 403 | .core_max_corner = (max_c), \ |
| 404 | .core_target_corner = (max_c), \ |
| 405 | .sleep_disable = TRUE, \ |
| 406 | .hmx_min_corner = (hmx_c), \ |
| 407 | .hmx_max_corner = (hmx_c), \ |
| 408 | .hmx_target_corner = (hmx_c), \ |
| 409 | .hmx_perf_mode = HAP_CLK_PERF_HIGH, \ |
| 410 | } |
| 411 | |
| 412 | static const struct htp_power_profile htp_power_profiles[HTP_POWER_MODE_COUNT] = { |
| 413 | [HTP_POWER_MODE_LOW_POWER_SAVER] = |
| 414 | HTP_POWER_PROFILE_ENTRY(HAP_DCVS_VCORNER_SVS2, HAP_DCVS_VCORNER_SVS2, HAP_DCVS_EXP_VCORNER_MIN_SVS), |
| 415 | [HTP_POWER_MODE_POWER_SAVER] = |
| 416 | HTP_POWER_PROFILE_ENTRY(HAP_DCVS_VCORNER_SVS, HAP_DCVS_VCORNER_SVS_PLUS, HAP_DCVS_EXP_VCORNER_SVS), |
| 417 | [HTP_POWER_MODE_HIGH_POWER_SAVER] = |
| 418 | HTP_POWER_PROFILE_ENTRY(HAP_DCVS_VCORNER_SVS_PLUS, HAP_DCVS_VCORNER_NOM, HAP_DCVS_EXP_VCORNER_SVS_L2), |
| 419 | [HTP_POWER_MODE_LOW_BALANCED] = |
| 420 | HTP_POWER_PROFILE_ENTRY(HAP_DCVS_VCORNER_NOM, HAP_DCVS_VCORNER_NOM_PLUS, HAP_DCVS_EXP_VCORNER_NOM), |
| 421 | [HTP_POWER_MODE_BALANCED] = |
| 422 | HTP_POWER_PROFILE_ENTRY(HAP_DCVS_VCORNER_NOM_PLUS, HAP_DCVS_VCORNER_TURBO, HAP_DCVS_EXP_VCORNER_NOM_L1), |
| 423 | [HTP_POWER_MODE_HIGH_PERFORMANCE] = |
| 424 | HTP_POWER_PROFILE_ENTRY(HAP_DCVS_VCORNER_TURBO, HAP_DCVS_VCORNER_TURBO_PLUS, HAP_DCVS_EXP_VCORNER_TUR_L2), |
| 425 | [HTP_POWER_MODE_SUSTAINED_HIGH_PERFORMANCE] = |
| 426 | HTP_POWER_PROFILE_ENTRY(HAP_DCVS_VCORNER_NOM_PLUS, HAP_DCVS_VCORNER_TURBO_PLUS, HAP_DCVS_EXP_VCORNER_TUR_L2), |
| 427 | [HTP_POWER_MODE_BURST] = { |
| 428 | .dcvs_enable = FALSE, |
| 429 | .bus_min_corner = HAP_DCVS_VCORNER_MAX, |
| 430 | .bus_max_corner = HAP_DCVS_VCORNER_MAX, |
| 431 | .bus_target_corner = HAP_DCVS_VCORNER_MAX, |
| 432 | .core_min_corner = HAP_DCVS_VCORNER_MAX, |
| 433 | .core_max_corner = HAP_DCVS_VCORNER_MAX, |
| 434 | .core_target_corner = HAP_DCVS_VCORNER_MAX, |
| 435 | .sleep_disable = TRUE, |
| 436 | .hmx_min_corner = HAP_DCVS_EXP_VCORNER_MAX, |
| 437 | .hmx_max_corner = HAP_DCVS_EXP_VCORNER_MAX, |
| 438 | .hmx_target_corner = HAP_DCVS_EXP_VCORNER_MAX, |
| 439 | .hmx_perf_mode = HAP_CLK_PERF_HIGH, |
| 440 | }, |
| 441 | }; |
| 442 | |
| 443 | #undef HTP_POWER_PROFILE_ENTRY |
| 444 | |
| 445 | AEEResult htp_iface_start(remote_handle64 handle, uint32_t sess_id, uint64_t dsp_queue_id, uint32_t n_hvx, uint32_t n_hmx, uint64_t vtcm_size, uint64_t max_vmem, uint32_t power_mode) { |
| 446 | struct htp_handle * h = (struct htp_handle *) handle; |
| 447 | if (!h) { |
| 448 | return AEE_EBADPARM; |
| 449 | } |
| 450 | |
| 451 | if (power_mode >= HTP_POWER_MODE_COUNT) { |
| 452 | FARF(ERROR, "invalid power-mode %u, falling back to burst", (unsigned) power_mode); |
| 453 | power_mode = HTP_POWER_MODE_BURST; |
| 454 | } |
| 455 | const struct htp_power_profile * pwr = &htp_power_profiles[power_mode]; |
| 456 | FARF(ALWAYS, "power-mode %u : dcvs-enable %d bus[%u,%u,%u] core[%u,%u,%u] sleep-disable %d hmx[%u,%u,%u]", |
| 457 | (unsigned) power_mode, (int) pwr->dcvs_enable, |
| 458 | (unsigned) pwr->bus_min_corner, (unsigned) pwr->bus_max_corner, (unsigned) pwr->bus_target_corner, |
| 459 | (unsigned) pwr->core_min_corner, (unsigned) pwr->core_max_corner, (unsigned) pwr->core_target_corner, |
| 460 | (int) pwr->sleep_disable, |
| 461 | (unsigned) pwr->hmx_min_corner, (unsigned) pwr->hmx_max_corner, (unsigned) pwr->hmx_target_corner); |
| 462 | |
| 463 | if (h->ctx) { |
| 464 | FARF(ERROR, "Queue already open"); |
| 465 | return AEE_EITEMBUSY; |
| 466 | } |
| 467 | |
| 468 | // Cache the original FastRPC thread priority, then calculate compute priority |
| 469 | int fastrpc_tid = qurt_thread_get_id(); |
| 470 | int fastrpc_prio = qurt_thread_get_priority(fastrpc_tid); |
| 471 | int main_prio = fastrpc_prio - 10; |
| 472 | if (main_prio < 1) main_prio = 1; |
| 473 | |
| 474 | dspqueue_t dsp_queue = NULL; |
| 475 | bool use_callbacks = false; |
| 476 | |
| 477 | // Import queue with NULL callbacks to avoid starting dspueue internal threads |
| 478 | int err = dspqueue_import(dsp_queue_id, NULL, NULL, (void *) h, &dsp_queue); |
| 479 | if (err == AEE_EBADPARM) { |
| 480 | // Fallback for devices that don't support NULL callbacks |
| 481 | FARF(HIGH, "dspqueue import with NULL callbacks failed, trying with callbacks"); |
| 482 | use_callbacks = true; |
| 483 | err = dspqueue_import(dsp_queue_id, htp_packet_callback, htp_error_callback, (void *) h, &dsp_queue); |
| 484 | } |
| 485 | |
| 486 | if (err) { |
| 487 | FARF(ERROR, "Queue import failed with 0x%08x", (unsigned) err); |
| 488 | return err; |
| 489 | } |
| 490 | |
| 491 | qurt_sysenv_max_hthreads_t hw_threads; |
| 492 | qurt_sysenv_get_max_hw_threads(&hw_threads); |
| 493 | uint32_t hw_nhvx = (qurt_hvx_get_units() >> 8) & 0xFF; |
| 494 | |
| 495 | if (n_hvx == 0) { |
| 496 | n_hvx = hw_nhvx; |
| 497 | } |
| 498 | if (n_hvx > hw_threads.max_hthreads) { |
| 499 | n_hvx = hw_threads.max_hthreads; |
| 500 | } |
| 501 | if (n_hvx > HTP_MAX_NTHREADS) { |
| 502 | n_hvx = HTP_MAX_NTHREADS; |
| 503 | } |
| 504 | |
| 505 | // layout segments of our contiguous block |
| 506 | |
| 507 | // 1. htp_context : sits at the base (block is 4K-aligned via memalign below) |
| 508 | size_t offset = sizeof(struct htp_context); |
| 509 | |
| 510 | // 2. main_stack |
| 511 | size_t offset_main_stack = 0; |
| 512 | size_t size_main_stack = 0; |
| 513 | if (!use_callbacks) { |
| 514 | offset_main_stack = hex_align_up(offset, 4096); |
| 515 | size_main_stack = MAIN_THREAD_STACK_SIZE; |
| 516 | offset = offset_main_stack + size_main_stack; |
| 517 | } |
| 518 | |
| 519 | // 3. work_queue |
| 520 | size_t wq_align = work_queue_alignof(); |
| 521 | size_t offset_wq = hex_align_up(offset, wq_align); |
| 522 | size_t size_wq = work_queue_sizeof(n_hvx, WORK_QUEUE_CAPACITY, WORK_QUEUE_STACK_SIZE); |
| 523 | offset = offset_wq + size_wq; |
| 524 | |
| 525 | // 4. dma_queue |
| 526 | size_t dma_align = dma_queue_alignof(); |
| 527 | size_t offset_dma = hex_align_up(offset, dma_align); |
| 528 | size_t size_dma = 0; |
| 529 | for (uint32_t i = 0; i < n_hvx; i++) { |
| 530 | size_dma = hex_align_up(size_dma, dma_queue_alignof()); |
| 531 | size_dma += dma_queue_sizeof(256); |
| 532 | } |
| 533 | offset = offset_dma + size_dma; |
| 534 | |
| 535 | // 5. hmx_queue |
| 536 | size_t offset_hmx = 0; |
| 537 | size_t size_hmx = 0; |
| 538 | if (n_hmx) { |
| 539 | size_t hmx_align = hmx_queue_alignof(); |
| 540 | offset_hmx = hex_align_up(offset, hmx_align); |
| 541 | size_hmx = hmx_queue_sizeof(HMX_QUEUE_CAPACITY, HMX_QUEUE_STACK_SIZE); |
| 542 | offset = offset_hmx + size_hmx; |
| 543 | } |
| 544 | |
| 545 | size_t footprint = hex_align_up(offset, 128); |
| 546 | |
| 547 | void * block = memalign(4096, footprint); |
| 548 | if (!block) { |
| 549 | FARF(ERROR, "Unable to allocate unified block of size %zu\n", footprint); |
| 550 | dspqueue_close(dsp_queue); |
| 551 | return AEE_ENOMEMORY; |
| 552 | } |
| 553 | memset(block, 0, footprint); |
| 554 | |
| 555 | h->ctx = (struct htp_context *) block; |
| 556 | struct htp_context * ctx = h->ctx; |
| 557 | ctx->footprint = footprint; |
| 558 | |
| 559 | ctx->thread_id = fastrpc_tid; |
| 560 | ctx->thread_prio = main_prio; |
| 561 | ctx->max_vmem = max_vmem; |
| 562 | ctx->dsp_queue = dsp_queue; |
| 563 | |
| 564 | err = vtcm_alloc(ctx, vtcm_size); |
| 565 | if (err != AEE_SUCCESS) { |
| 566 | FARF(ERROR, "Unable to allocate VTCM"); |
| 567 | htp_iface_stop(handle); |
| 568 | return AEE_ENOMEMORY; |
| 569 | } |
| 570 | |
| 571 | HAP_setFARFRuntimeLoggingParams(0xffff, NULL, 0); |
| 572 | |
| 573 | // Set client class |
| 574 | { |
| 575 | HAP_power_request_t request; |
| 576 | memset(&request, 0, sizeof(HAP_power_request_t)); |
| 577 | request.type = HAP_power_set_apptype; |
| 578 | request.apptype = HAP_POWER_COMPUTE_CLIENT_CLASS; |
| 579 | |
| 580 | if ((err = HAP_power_set((void *) ctx, &request)) != 0) { |
| 581 | htp_iface_stop(handle); |
| 582 | return err; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | // DCVS setup |
| 587 | { |
| 588 | HAP_power_request_t request; |
| 589 | memset(&request, 0, sizeof(request)); |
| 590 | |
| 591 | request.type = HAP_power_set_DCVS_v3; |
| 592 | request.dcvs_v3.set_dcvs_enable = TRUE; |
| 593 | request.dcvs_v3.dcvs_enable = pwr->dcvs_enable; |
| 594 | request.dcvs_v3.set_bus_params = TRUE; |
| 595 | request.dcvs_v3.bus_params.min_corner = pwr->bus_min_corner; |
| 596 | request.dcvs_v3.bus_params.max_corner = pwr->bus_max_corner; |
| 597 | request.dcvs_v3.bus_params.target_corner = pwr->bus_target_corner; |
| 598 | request.dcvs_v3.set_core_params = TRUE; |
| 599 | request.dcvs_v3.core_params.min_corner = pwr->core_min_corner; |
| 600 | request.dcvs_v3.core_params.max_corner = pwr->core_max_corner; |
| 601 | request.dcvs_v3.core_params.target_corner = pwr->core_target_corner; |
| 602 | request.dcvs_v3.set_sleep_disable = TRUE; |
| 603 | request.dcvs_v3.sleep_disable = pwr->sleep_disable; |
| 604 | |
| 605 | #if (__HEXAGON_ARCH__ >= 79) |
| 606 | HAP_set_dcvs_v3_protected_bus_corners(&request, 1); |
| 607 | #endif |
| 608 | if ((err = HAP_power_set((void *) ctx, &request)) != 0) { |
| 609 | htp_iface_stop(handle); |
| 610 | return err; |
| 611 | } |
| 612 | |
| 613 | memset(&request, 0, sizeof(request)); |
| 614 | request.type = HAP_power_set_HVX; |
| 615 | request.hvx.power_up = TRUE; |
| 616 | if ((err = HAP_power_set((void *) ctx, &request)) != 0) { |
| 617 | htp_iface_stop(handle); |
| 618 | return err; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | #if __HVX_ARCH__ >= 75 |
| 623 | { |
| 624 | // Power on HMX and set HMX clock |
| 625 | HAP_power_request_t request; |
| 626 | memset(&request, 0, sizeof(HAP_power_request_t)); |
| 627 | request.type = HAP_power_set_HMX_v2; |
| 628 | request.hmx_v2.set_power = TRUE; |
| 629 | request.hmx_v2.power_up = TRUE; |
| 630 | request.hmx_v2.set_clock = TRUE; |
| 631 | request.hmx_v2.target_corner = pwr->hmx_target_corner; |
| 632 | request.hmx_v2.min_corner = pwr->hmx_min_corner; |
| 633 | request.hmx_v2.max_corner = pwr->hmx_max_corner; |
| 634 | request.hmx_v2.perf_mode = pwr->hmx_perf_mode; |
| 635 | FARF(ALWAYS, "Setting HMX clock\n"); |
| 636 | err = HAP_power_set((void *) ctx, &request); |
| 637 | if (err != AEE_SUCCESS) { |
| 638 | FARF(ERROR, "ggml-hex: error setting HMX clock."); |
| 639 | htp_iface_stop(handle); |
| 640 | return err; |
| 641 | } |
| 642 | } |
| 643 | #else |
| 644 | { |
| 645 | // Power on HMX |
| 646 | HAP_power_request_t request; |
| 647 | memset(&request, 0, sizeof(HAP_power_request_t)); |
| 648 | request.type = HAP_power_set_HMX; |
| 649 | request.hmx.power_up = TRUE; |
| 650 | FARF(ALWAYS, "Powering HMX on\n"); |
| 651 | err = HAP_power_set((void *) ctx, &request); |
| 652 | if (err != AEE_SUCCESS) { |
| 653 | FARF(ERROR, "ggml-hex: error powering on HMX."); |
| 654 | htp_iface_stop(handle); |
| 655 | return err; |
| 656 | } |
| 657 | } |
| 658 | #endif |
| 659 | |
| 660 | ctx->hmx_enabled = n_hmx; |
| 661 | ctx->hmx_queue = NULL; |
| 662 | if (n_hmx) { |
| 663 | void * hmx_ptr = (void *) ((uintptr_t) block + offset_hmx); |
| 664 | ctx->hmx_queue = hmx_queue_init(hmx_ptr, HMX_QUEUE_CAPACITY, HMX_QUEUE_STACK_SIZE, ctx->vtcm_rctx, &ctx->trace[HTP_MAX_NTHREADS]); |
| 665 | } |
| 666 | FARF(HIGH, "HMX %s (n_hmx=%d)", ctx->hmx_enabled ? "enabled" : "disabled", n_hmx); |
| 667 | |
| 668 | ctx->n_threads = n_hvx; |
| 669 | ctx->n_threads_div = init_fastdiv_values(ctx->n_threads); |
| 670 | |
| 671 | // Initialize DMA queues |
| 672 | uint8_t * dma_ptr_curr = (uint8_t *) ((uintptr_t) block + offset_dma); |
| 673 | size_t size_dma_q = dma_queue_sizeof(256); |
| 674 | |
| 675 | for (int i = 0; i < ctx->n_threads; i++) { |
| 676 | dma_ptr_curr = (uint8_t *) hex_align_up((uintptr_t) dma_ptr_curr, dma_queue_alignof()); |
| 677 | ctx->dma[i] = dma_queue_init(dma_ptr_curr, 256, &ctx->trace[i]); |
| 678 | dma_ptr_curr += size_dma_q; |
| 679 | } |
| 680 | |
| 681 | ctx->ddr_spad_size = HTP_DDR_SPAD_SIZE; |
| 682 | ctx->ddr_spad_base = memalign(128, ctx->ddr_spad_size); |
| 683 | |
| 684 | void * wq_ptr = (void *) ((uintptr_t) block + offset_wq); |
| 685 | ctx->work_queue = work_queue_init(wq_ptr, n_hvx, WORK_QUEUE_CAPACITY, WORK_QUEUE_STACK_SIZE); |
| 686 | |
| 687 | ctx->main_stack = NULL; |
| 688 | ctx->main_thread = 0; |
| 689 | atomic_store(&ctx->killed, false); |
| 690 | |
| 691 | if (!use_callbacks) { |
| 692 | // Start main compute thread |
| 693 | ctx->main_stack = (void *) ((uintptr_t) block + offset_main_stack); |
| 694 | |
| 695 | qurt_thread_attr_t attr; |
| 696 | qurt_thread_attr_init(&attr); |
| 697 | qurt_thread_attr_set_stack_addr(&attr, ctx->main_stack); |
| 698 | qurt_thread_attr_set_stack_size(&attr, size_main_stack); |
| 699 | qurt_thread_attr_set_priority(&attr, main_prio); |
| 700 | qurt_thread_attr_set_name(&attr, "htp-main"); |
| 701 | |
| 702 | int err_thread = qurt_thread_create(&ctx->main_thread, &attr, htp_main_thread, ctx); |
| 703 | if (err_thread) { |
| 704 | FARF(ERROR, "Unable to create htp main thread: %d", err_thread); |
| 705 | htp_iface_stop(handle); |
| 706 | return AEE_ENOMEMORY; |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | FARF(HIGH, "session %u started: n-hvx %u vtcm-size %zu vtcm-rctx %u n-threads %u thread-id %d thread-prio %d \n", |
| 711 | sess_id, hw_nhvx, ctx->vtcm_size, ctx->vtcm_rctx, ctx->n_threads, ctx->thread_id, ctx->thread_prio); |
| 712 | |
| 713 | return AEE_SUCCESS; |
| 714 | } |
| 715 | |
| 716 | AEEResult htp_iface_stop(remote_handle64 handle) { |
| 717 | struct htp_handle * h = (struct htp_handle *) handle; |
| 718 | if (!h || !h->ctx) { |
| 719 | return AEE_EBADPARM; |
| 720 | } |
| 721 | struct htp_context * ctx = h->ctx; |
| 722 | |
| 723 | if (ctx->main_thread) { |
| 724 | atomic_store(&ctx->killed, true); |
| 725 | int status; |
| 726 | (void) qurt_thread_join(ctx->main_thread, &status); |
| 727 | ctx->main_thread = 0; |
| 728 | } |
| 729 | |
| 730 | int err = dspqueue_close(ctx->dsp_queue); ctx->dsp_queue = NULL; |
| 731 | if (err != 0) { |
| 732 | FARF(ERROR, "Queue close failed with 0x%08x", (unsigned) err); |
| 733 | return err; |
| 734 | } |
| 735 | |
| 736 | work_queue_free(ctx->work_queue); |
| 737 | |
| 738 | for (int i = 0; i < ctx->n_threads; i++) { |
| 739 | dma_queue_free(ctx->dma[i]); |
| 740 | } |
| 741 | |
| 742 | if (ctx->hmx_queue) { |
| 743 | hmx_queue_free(ctx->hmx_queue); |
| 744 | ctx->hmx_queue = NULL; |
| 745 | } |
| 746 | ctx->hmx_enabled = false; |
| 747 | |
| 748 | vtcm_free(ctx); |
| 749 | |
| 750 | if (ctx->ddr_spad_base) { |
| 751 | free(ctx->ddr_spad_base); |
| 752 | ctx->ddr_spad_base = NULL; |
| 753 | ctx->ddr_spad_size = 0; |
| 754 | } |
| 755 | |
| 756 | free(ctx); |
| 757 | h->ctx = NULL; |
| 758 | |
| 759 | return AEE_SUCCESS; |
| 760 | } |
| 761 | |
| 762 | AEEResult htp_iface_hwinfo(remote_handle64 handle, uint32_t * n_threads, uint32_t * n_hvx, uint32_t * n_hmx, uint64_t * vtcm_size) { |
| 763 | (void)handle; |
| 764 | if (!n_threads || !n_hvx || !n_hmx || !vtcm_size) { |
| 765 | return AEE_EBADPARM; |
| 766 | } |
| 767 | |
| 768 | qurt_sysenv_max_hthreads_t hw_threads; |
| 769 | qurt_sysenv_get_max_hw_threads(&hw_threads); |
| 770 | uint32_t hw_nhvx = (qurt_hvx_get_units() >> 8) & 0xFF; |
| 771 | |
| 772 | uint32_t n_hvx_val = hw_nhvx; |
| 773 | if (n_hvx_val > hw_threads.max_hthreads) { |
| 774 | n_hvx_val = hw_threads.max_hthreads; |
| 775 | } |
| 776 | if (n_hvx_val > HTP_MAX_NTHREADS) { |
| 777 | n_hvx_val = HTP_MAX_NTHREADS; |
| 778 | } |
| 779 | |
| 780 | // for now we force n_threads == n_hvx |
| 781 | *n_threads = n_hvx_val; |
| 782 | *n_hvx = n_hvx_val; |
| 783 | *n_hmx = 1; |
| 784 | |
| 785 | *vtcm_size = vtcm_query_size(); |
| 786 | |
| 787 | return AEE_SUCCESS; |
| 788 | } |
| 789 | |
| 790 | static void htp_error_callback(dspqueue_t queue, int error, void * context) { |
| 791 | // No errors expected on the DSP. |
| 792 | FARF(ERROR, "Error callback: 0x%08x", (unsigned) error); |
| 793 | } |
| 794 | |
| 795 | struct profile_data { |
| 796 | uint64_t usecs; |
| 797 | uint64_t cycles_start; |
| 798 | uint64_t cycles_stop; |
| 799 | uint32_t pmu_counters[HEX_NUM_PMU_COUNTERS]; |
| 800 | }; |
| 801 | |
| 802 | static inline void profile_start(uint32_t mode, struct profile_data * d) { |
| 803 | switch (mode) { |
| 804 | case HTP_PROF_PMU: |
| 805 | hex_get_pmu(d->pmu_counters); |
| 806 | // fallthrough |
| 807 | case HTP_PROF_BASIC: |
| 808 | case HTP_PROF_TRACE: |
| 809 | d->usecs = HAP_perf_get_qtimer_count(); |
| 810 | d->cycles_start = hex_get_cycles(); |
| 811 | break; |
| 812 | default: |
| 813 | break; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | static inline void profile_stop(uint32_t mode, struct profile_data * d) { |
| 818 | uint32_t pmu_counters[HEX_NUM_PMU_COUNTERS]; |
| 819 | switch (mode) { |
| 820 | case HTP_PROF_PMU: |
| 821 | hex_get_pmu(pmu_counters); |
| 822 | for (int i = 0; i < HEX_NUM_PMU_COUNTERS; i++) { |
| 823 | d->pmu_counters[i] = pmu_counters[i] - d->pmu_counters[i]; |
| 824 | } |
| 825 | // fallthrough |
| 826 | case HTP_PROF_BASIC: |
| 827 | case HTP_PROF_TRACE: |
| 828 | d->usecs = HAP_perf_qtimer_count_to_us(HAP_perf_get_qtimer_count() - d->usecs); |
| 829 | d->cycles_stop = hex_get_cycles(); |
| 830 | break; |
| 831 | default: |
| 832 | break; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | static int op_fence(struct htp_ops_context * octx) { |
| 837 | struct htp_context *ctx = octx->ctx; |
| 838 | struct htp_thread_trace * tr = &ctx->trace[0]; |
| 839 | const uint32_t seq = (uint32_t) octx->op_params[0]; |
| 840 | const uint32_t mode = (uint32_t) octx->op_params[1]; |
| 841 | |
| 842 | htp_trace_event_start(tr, HTP_TRACE_EVT_FENCE, (uint16_t) seq); |
| 843 | |
| 844 | const struct htp_tensor * sync = octx->src[0]; |
| 845 | atomic_uint * sync_fence = (atomic_uint *) (uintptr_t) sync->data; |
| 846 | |
| 847 | if (mode == 1) { |
| 848 | htp_dcache_flush_dirty_ranges(ctx); |
| 849 | |
| 850 | htp_mdev_group_barrier(octx); |
| 851 | |
| 852 | if (ctx->mdev.idx == 0) { |
| 853 | htp_fence_write(sync_fence, seq, octx->status); |
| 854 | } |
| 855 | htp_trace_event_stop(tr, HTP_TRACE_EVT_FENCE, (uint16_t) seq); |
| 856 | FARF(HIGH, "ggml-hex: sync-signal : fence %p seq 0x%x status %d\n", sync_fence, seq, octx->status); |
| 857 | return octx->status; |
| 858 | } |
| 859 | |
| 860 | int status = HTP_STATUS_OK; |
| 861 | uint64_t spins = 0; |
| 862 | while (1) { |
| 863 | uint32_t sync_seq; |
| 864 | uint32_t sync_status; |
| 865 | htp_fence_read(sync_fence, &sync_seq, &sync_status); |
| 866 | if ((int32_t)(sync_seq - seq) >= 0) { |
| 867 | if (sync_status > HTP_STATUS_OK) { |
| 868 | FARF(ERROR, "ggml-hex: sync-wait peer failed with status %u : fence %p seq 0x%x\n", sync_status, sync_fence, seq); |
| 869 | status = sync_status; |
| 870 | } |
| 871 | break; |
| 872 | } |
| 873 | if (++spins > HTP_FENCE_TIMEOUT) { |
| 874 | FARF(ERROR, "ggml-hex: sync-wait TIMEOUT : fence %p spins %llu seq 0x%x\n", sync_fence, spins, seq); |
| 875 | status = HTP_STATUS_INTERNAL_ERR; |
| 876 | break; |
| 877 | } |
| 878 | hex_pause(); |
| 879 | } |
| 880 | |
| 881 | htp_trace_event_stop(tr, HTP_TRACE_EVT_FENCE, (uint16_t) seq); |
| 882 | |
| 883 | FARF(HIGH, "ggml-hex: sync-done : fence %p spins %llu seq 0x%x\n", sync_fence, spins, seq); |
| 884 | return status; |
| 885 | } |
| 886 | |
| 887 | static int op_mdev_group(struct htp_ops_context * octx) { |
| 888 | struct htp_context * ctx = octx->ctx; |
| 889 | const struct htp_tensor * sync = octx->src[0]; |
| 890 | ctx->mdev.idx = (uint16_t) octx->op_params[0]; |
| 891 | ctx->mdev.count = (uint16_t) sync->ne[1]; |
| 892 | if (ctx->mdev.count > 1) { |
| 893 | ctx->mdev.count_div = init_fastdiv_values(ctx->mdev.count); |
| 894 | ctx->mdev.fence_base = (uint8_t *) sync->data; |
| 895 | } |
| 896 | return HTP_STATUS_OK; |
| 897 | } |
| 898 | |
| 899 | static int execute_op(struct htp_ops_context * octx) { |
| 900 | switch (octx->op) { |
| 901 | case HTP_OP_MDEV_GROUP: |
| 902 | return op_mdev_group(octx); |
| 903 | |
| 904 | case HTP_OP_FENCE: |
| 905 | return op_fence(octx); |
| 906 | |
| 907 | case HTP_OP_ALLREDUCE: |
| 908 | case HTP_OP_ALLREDUCE_ADD: |
| 909 | return op_allreduce(octx); |
| 910 | |
| 911 | case HTP_OP_MUL_MAT: |
| 912 | case HTP_OP_MUL_MAT_ADD: |
| 913 | return op_matmul(octx); |
| 914 | |
| 915 | case HTP_OP_MUL_MAT_ID: |
| 916 | return op_matmul_id(octx); |
| 917 | |
| 918 | case HTP_OP_MUL_MAT_ID_NX: |
| 919 | return op_matmul_id_nx(octx); |
| 920 | |
| 921 | case HTP_OP_MUL_MAT_NX: |
| 922 | return op_matmul_nx(octx); |
| 923 | |
| 924 | case HTP_OP_MUL: |
| 925 | case HTP_OP_ADD: |
| 926 | case HTP_OP_SUB: |
| 927 | case HTP_OP_DIV: |
| 928 | case HTP_OP_ADD_ID: |
| 929 | return op_binary(octx); |
| 930 | |
| 931 | case HTP_OP_NORM: |
| 932 | case HTP_OP_RMS_NORM: |
| 933 | case HTP_OP_RMS_NORM_MUL: |
| 934 | case HTP_OP_SCALE: |
| 935 | case HTP_OP_CLAMP: |
| 936 | case HTP_OP_LEAKY_RELU: |
| 937 | case HTP_OP_SQR: |
| 938 | case HTP_OP_SQRT: |
| 939 | case HTP_OP_UNARY_SOFTPLUS: |
| 940 | case HTP_OP_UNARY_SIGMOID: |
| 941 | case HTP_OP_UNARY_SILU: |
| 942 | case HTP_OP_UNARY_GELU: |
| 943 | case HTP_OP_UNARY_GELU_QUICK: |
| 944 | case HTP_OP_UNARY_GELU_ERF: |
| 945 | case HTP_OP_UNARY_NEG: |
| 946 | case HTP_OP_UNARY_EXP: |
| 947 | case HTP_OP_UNARY_TANH: |
| 948 | case HTP_OP_UNARY_ABS: |
| 949 | case HTP_OP_UNARY_LOG: |
| 950 | case HTP_OP_UNARY_RELU: |
| 951 | case HTP_OP_UNARY_STEP: |
| 952 | case HTP_OP_UNARY_CEIL: |
| 953 | case HTP_OP_UNARY_SGN: |
| 954 | case HTP_OP_L2_NORM: |
| 955 | case HTP_OP_HADAMARD: |
| 956 | return op_unary(octx); |
| 957 | |
| 958 | case HTP_OP_GLU_SWIGLU: |
| 959 | case HTP_OP_GLU_SWIGLU_OAI: |
| 960 | case HTP_OP_GLU_SWIGLU_CLAMP: |
| 961 | case HTP_OP_GLU_GEGLU: |
| 962 | case HTP_OP_GLU_GEGLU_QUICK: |
| 963 | case HTP_OP_GLU_GEGLU_ERF: |
| 964 | case HTP_OP_GLU_REGLU: |
| 965 | case HTP_OP_GLU_SIGMOID: |
| 966 | return op_glu(octx); |
| 967 | |
| 968 | case HTP_OP_SOFTMAX: |
| 969 | return op_softmax(octx); |
| 970 | |
| 971 | case HTP_OP_ROPE: |
| 972 | return op_rope(octx); |
| 973 | |
| 974 | case HTP_OP_FLASH_ATTN_EXT: |
| 975 | return op_flash_attn_ext(octx); |
| 976 | |
| 977 | case HTP_OP_SET_ROWS: |
| 978 | return op_set_rows(octx); |
| 979 | |
| 980 | case HTP_OP_GET_ROWS: |
| 981 | return op_get_rows(octx); |
| 982 | |
| 983 | case HTP_OP_SUM: |
| 984 | return op_sum(octx); |
| 985 | |
| 986 | case HTP_OP_SUM_ROWS: |
| 987 | return op_sum_rows(octx); |
| 988 | |
| 989 | case HTP_OP_CPY: |
| 990 | case HTP_OP_CPY_FENCE: |
| 991 | return op_cpy(octx); |
| 992 | |
| 993 | case HTP_OP_REPEAT: |
| 994 | return op_repeat(octx); |
| 995 | |
| 996 | case HTP_OP_ARGSORT: |
| 997 | return op_argsort(octx); |
| 998 | |
| 999 | case HTP_OP_TOP_K: |
| 1000 | return op_top_k(octx); |
| 1001 | |
| 1002 | case HTP_OP_ARGMAX: |
| 1003 | return op_argmax(octx); |
| 1004 | |
| 1005 | case HTP_OP_SSM_CONV: |
| 1006 | return op_ssm_conv(octx); |
| 1007 | |
| 1008 | case HTP_OP_CUMSUM: |
| 1009 | return op_cumsum(octx); |
| 1010 | |
| 1011 | case HTP_OP_TOP_K_MASK: |
| 1012 | return op_top_k_mask(octx); |
| 1013 | |
| 1014 | case HTP_OP_FILL: |
| 1015 | return op_fill(octx); |
| 1016 | |
| 1017 | case HTP_OP_DIAG: |
| 1018 | return op_diag(octx); |
| 1019 | |
| 1020 | case HTP_OP_SOLVE_TRI: |
| 1021 | return op_solve_tri(octx); |
| 1022 | |
| 1023 | case HTP_OP_PAD: |
| 1024 | return op_pad(octx); |
| 1025 | |
| 1026 | case HTP_OP_IM2COL: |
| 1027 | return op_im2col(octx); |
| 1028 | |
| 1029 | case HTP_OP_IM2COL_3D: |
| 1030 | return op_im2col_3d(octx); |
| 1031 | |
| 1032 | case HTP_OP_CONV_2D: |
| 1033 | return op_conv_2d(octx); |
| 1034 | |
| 1035 | case HTP_OP_CONV_2D_DW: |
| 1036 | return op_conv_2d_dw(octx); |
| 1037 | |
| 1038 | case HTP_OP_ROLL: |
| 1039 | return op_roll(octx); |
| 1040 | |
| 1041 | case HTP_OP_POOL_2D: |
| 1042 | return op_pool_2d(octx); |
| 1043 | |
| 1044 | case HTP_OP_POOL_1D: |
| 1045 | return op_pool_1d(octx); |
| 1046 | |
| 1047 | case HTP_OP_DSV4_HC_PRE: |
| 1048 | return op_dsv4_hc_pre(octx); |
| 1049 | |
| 1050 | case HTP_OP_DSV4_HC_POST: |
| 1051 | return op_dsv4_hc_post(octx); |
| 1052 | |
| 1053 | case HTP_OP_CONCAT: |
| 1054 | return op_concat(octx); |
| 1055 | |
| 1056 | case HTP_OP_GATED_DELTA_NET: |
| 1057 | return op_gated_delta_net(octx); |
| 1058 | |
| 1059 | case HTP_OP_LIGHTNING_INDEXER: |
| 1060 | return op_lightning_indexer(octx); |
| 1061 | |
| 1062 | case HTP_OP_TRI: |
| 1063 | return op_unary(octx); |
| 1064 | |
| 1065 | case HTP_OP_UPSCALE: |
| 1066 | return op_upscale(octx); |
| 1067 | |
| 1068 | case HTP_OP_INVALID: |
| 1069 | break; |
| 1070 | } |
| 1071 | |
| 1072 | FARF(ERROR, "Unknown Op %u", octx->op); |
| 1073 | return HTP_STATUS_NO_SUPPORT; |
| 1074 | } |
| 1075 | |
| 1076 | static inline bool reuse_buf(struct htp_context *ctx, uint32_t *m_reuse, struct htp_buf_desc *b) { |
| 1077 | b->base = NULL; |
| 1078 | |
| 1079 | for (uint32_t i=0; i<HTP_MAX_MMAPS; i++) { |
| 1080 | struct htp_mmap *m = ctx->mmap + i; |
| 1081 | if (m->size && m->fd == b->fd && m->flags == b->flags) { |
| 1082 | b->base = m->base; |
| 1083 | *m_reuse |= (1 << i); |
| 1084 | return true; |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | return false; |
| 1089 | } |
| 1090 | |
| 1091 | static inline void drop_mmap(struct htp_context *ctx, struct htp_mmap *m) { |
| 1092 | if (m->size) { |
| 1093 | FARF(ALWAYS, "unmap : fd %u base 0x%llx size %llu", m->fd, (unsigned long long) m->base, (unsigned long long) m->size); |
| 1094 | htp_munmap(m->base, m->size, m->flags); |
| 1095 | m->size = 0; |
| 1096 | m->base = 0; |
| 1097 | m->fd = -1; |
| 1098 | m->flags = 0; |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | static inline bool mmap_buf(struct htp_context *ctx, struct htp_buf_desc *b) { |
| 1103 | if (b->base) return true; // already mapped |
| 1104 | |
| 1105 | // find unused mapping |
| 1106 | for (uint32_t i=0; i < HTP_MAX_MMAPS; i++) { |
| 1107 | struct htp_mmap *m = &ctx->mmap[i]; |
| 1108 | if (!m->size) { |
| 1109 | uint64_t va = htp_mmap(b->fd, b->size, b->flags); |
| 1110 | if (va == 0) { |
| 1111 | FARF(HIGH, "mmap failed (will attempt defrag) : fd %u size %llu", b->fd, (unsigned long long) b->size); |
| 1112 | return false; |
| 1113 | } |
| 1114 | |
| 1115 | m->base = b->base = va; |
| 1116 | m->fd = b->fd; |
| 1117 | m->size = b->size; |
| 1118 | m->flags = b->flags; |
| 1119 | |
| 1120 | FARF(ALWAYS, "mmap : fd %u base 0x%llx size %llu flags 0x%x", m->fd, (unsigned long long) m->base, (unsigned long long) m->size, m->flags); |
| 1121 | return true; |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | FARF(ERROR, "mmap failed : exceeded mapping capacity limit of %u", HTP_MAX_MMAPS); |
| 1126 | return false; |
| 1127 | } |
| 1128 | |
| 1129 | static void prep_op_bufs(struct htp_context *ctx, struct htp_buf_desc *bufs, uint32_t n_bufs) { |
| 1130 | uint32_t m_reuse = 0; // mmap reuse mask (index from ctx->mmap array) |
| 1131 | uint32_t b_reuse = 0; // buf reuse count |
| 1132 | |
| 1133 | uint64_t m_vmem = 0; // mapped vmem |
| 1134 | uint64_t e_vmem = 0; // extra vmem |
| 1135 | |
| 1136 | // See what we can reuse |
| 1137 | for (uint32_t i=0; i < n_bufs; i++) { |
| 1138 | struct htp_buf_desc *b = bufs + i; |
| 1139 | if (reuse_buf(ctx, &m_reuse, b)) { |
| 1140 | b_reuse++; |
| 1141 | } else if (!(b->flags & HTP_BUF_EXTENDED)) { |
| 1142 | e_vmem += b->size; |
| 1143 | } |
| 1144 | FARF(HIGH, "prep-buf #%u : pass0 fd %u base 0x%llx size %llu flags 0x%x", i, b->fd, (unsigned long long) b->base, (unsigned long long) b->size, b->flags); |
| 1145 | } |
| 1146 | |
| 1147 | if (b_reuse == n_bufs) return; // all bufs reuse existing mappings |
| 1148 | |
| 1149 | // See how much vmem we have mmaped right now |
| 1150 | for (uint32_t i=0; i<HTP_MAX_MMAPS; i++) { |
| 1151 | if (!(ctx->mmap[i].flags & HTP_BUF_EXTENDED)) { |
| 1152 | m_vmem += ctx->mmap[i].size; |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | FARF(HIGH, "prep-bufs : pass1 mmap-vmem %zu extra-vmem %zu max-vmem %zu : n-bufs %u b-reuse %u", |
| 1157 | (size_t) m_vmem, (size_t) e_vmem, (size_t) ctx->max_vmem, n_bufs, b_reuse); |
| 1158 | |
| 1159 | if ((m_vmem + e_vmem) > ctx->max_vmem) { |
| 1160 | // Drop unused mappings |
| 1161 | for (uint32_t i=0; i < HTP_MAX_MMAPS; i++) { |
| 1162 | bool used = m_reuse & (1<<i); |
| 1163 | if (!used && !(ctx->mmap[i].flags & HTP_BUF_EXTENDED)) { |
| 1164 | drop_mmap(ctx, ctx->mmap + i); |
| 1165 | } |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | // Create missing mappings (pass 1) |
| 1170 | bool mmap_ok = true; |
| 1171 | for (uint32_t i=0; i < n_bufs; i++) { |
| 1172 | struct htp_buf_desc *b = bufs + i; |
| 1173 | if (!mmap_buf(ctx, b)) { |
| 1174 | mmap_ok = false; |
| 1175 | break; |
| 1176 | } |
| 1177 | FARF(HIGH, "prep-buf #%u : pass1 fd %u base 0x%llx size %llu flags 0x%x", i, b->fd, (unsigned long long) b->base, (unsigned long long) b->size, b->flags); |
| 1178 | } |
| 1179 | |
| 1180 | if (!mmap_ok) { |
| 1181 | // Attempt defragmentation: drop 32-bit mappings and remap (pass 2) |
| 1182 | FARF(HIGH, "prep-bufs : dropping 32-bit mappings to defragment address space"); |
| 1183 | for (uint32_t i=0; i < HTP_MAX_MMAPS; i++) { |
| 1184 | if (!(ctx->mmap[i].flags & HTP_BUF_EXTENDED)) { |
| 1185 | drop_mmap(ctx, ctx->mmap + i); |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | for (uint32_t i=0; i < n_bufs; i++) { |
| 1190 | struct htp_buf_desc *b = bufs + i; |
| 1191 | if (!(b->flags & HTP_BUF_EXTENDED)) { |
| 1192 | b->base = 0; |
| 1193 | } |
| 1194 | if (!mmap_buf(ctx, b)) { |
| 1195 | FARF(ERROR, "prep-bufs : mmap failed after defragmentation (fd %u size %llu)", b->fd, (unsigned long long) b->size); |
| 1196 | abort(); |
| 1197 | } |
| 1198 | FARF(HIGH, "prep-buf #%u : pass2 fd %u base 0x%llx size %llu flags 0x%x", i, b->fd, (unsigned long long) b->base, (unsigned long long) b->size, b->flags); |
| 1199 | } |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | static void prep_tensor(struct htp_context *ctx, struct htp_buf_desc *bufs, struct htp_tensor *tens, uint32_t idx, struct htp_tensor *t) { |
| 1204 | uint64_t offset = t->data; |
| 1205 | uint32_t bi = t->bi; |
| 1206 | |
| 1207 | t->data = bufs[bi].base + offset; // update data to the actual pointer |
| 1208 | |
| 1209 | FARF(HIGH, "prep-tensor #%u: bi %u offset %llu size %u data 0x%llx : %u:%u:%u:%u", idx, t->bi, (unsigned long long) offset, t->size, (unsigned long long) t->data, |
| 1210 | t->ne[0], t->ne[1], t->ne[2], t->ne[3]); |
| 1211 | } |
| 1212 | |
| 1213 | static void prep_tensors(struct htp_context *ctx, struct htp_buf_desc *bufs, struct htp_tensor *tens, uint32_t n_tens) { |
| 1214 | for (uint32_t i=0; i < n_tens; i++) { |
| 1215 | prep_tensor(ctx, bufs, tens, i, tens + i); |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | static void mdev_group_init(struct htp_context * ctx, const struct htp_opbatch_req * req) { |
| 1220 | memset(&ctx->mdev, 0, sizeof(ctx->mdev)); |
| 1221 | ctx->mdev.fence_seq = (uint32_t)((req->seq & 0xfffff) << 12); |
| 1222 | } |
| 1223 | |
| 1224 | static int proc_op_req(struct htp_ops_context * octx, struct htp_buf_desc * bufs, uint32_t n_bufs, |
| 1225 | struct htp_tensor * tens, uint32_t idx, struct htp_op_desc * op) { |
| 1226 | memcpy(octx->op_params, op->params, sizeof(octx->op_params)); |
| 1227 | memcpy(octx->kernel_params, op->kernel_params, sizeof(octx->kernel_params)); |
| 1228 | octx->flags = op->flags; |
| 1229 | octx->op = op->opcode; |
| 1230 | octx->n_threads = octx->ctx->n_threads; |
| 1231 | octx->n_threads_div = octx->ctx->n_threads_div; |
| 1232 | |
| 1233 | FARF(HIGH, "proc-op #%u: opcode %u flags 0x%x", idx, octx->op, octx->flags); |
| 1234 | |
| 1235 | // Prep input tensors |
| 1236 | for (uint32_t i=0; i<HTP_OP_MAX_INPUTS; i++) { |
| 1237 | uint16_t src_idx = op->src[i]; |
| 1238 | if (src_idx == 0xffff) { |
| 1239 | octx->src[i] = NULL; |
| 1240 | continue; |
| 1241 | } |
| 1242 | |
| 1243 | struct htp_tensor *src = tens + src_idx; |
| 1244 | octx->src[i] = src; |
| 1245 | |
| 1246 | FARF(HIGH, "prep-src #%u: data 0x%llx size %u : %u:%u:%u:%u", op->src[i], (unsigned long long) src->data, src->size, |
| 1247 | src->ne[0], src->ne[1], src->ne[2], src->ne[3]); |
| 1248 | } |
| 1249 | |
| 1250 | htp_dcache_flush_dirty_tensors(octx->ctx, octx->src, HTP_OP_MAX_INPUTS); |
| 1251 | |
| 1252 | // Prep output tensors |
| 1253 | for (uint32_t i = 0; i < HTP_OP_MAX_OUTPUTS; i++) { |
| 1254 | uint16_t dst_idx = op->dst[i]; |
| 1255 | if (dst_idx == 0xffff) { |
| 1256 | octx->dsts[i] = NULL; |
| 1257 | continue; |
| 1258 | } |
| 1259 | struct htp_tensor *dst = tens + dst_idx; |
| 1260 | octx->dsts[i] = dst; |
| 1261 | |
| 1262 | FARF(HIGH, "prep-dst[%u] #%u: data 0x%llx size %u : %u:%u:%u:%u", i, dst_idx, (unsigned long long) dst->data, dst->size, |
| 1263 | dst->ne[0], dst->ne[1], dst->ne[2], dst->ne[3]); |
| 1264 | } |
| 1265 | |
| 1266 | htp_dcache_mark_dirty_tensors(octx->ctx, octx->dsts, HTP_OP_MAX_OUTPUTS); |
| 1267 | |
| 1268 | htp_mdev_group_barrier(octx); |
| 1269 | |
| 1270 | int status = execute_op(octx); |
| 1271 | |
| 1272 | htp_ops_context_set_status(octx, status); |
| 1273 | |
| 1274 | octx->src0_spad.src = NULL; |
| 1275 | octx->src1_spad.src = NULL; |
| 1276 | octx->dst_spad.src = NULL; |
| 1277 | |
| 1278 | return octx->status; |
| 1279 | } |
| 1280 | |
| 1281 | static void process_opbatch(struct htp_context * ctx, const struct htp_opbatch_req * req, const struct dspqueue_buffer * dbuf) { |
| 1282 | dspqueue_t queue = ctx->dsp_queue; |
| 1283 | int err; |
| 1284 | |
| 1285 | const uint32_t n_bufs = req->n_bufs; |
| 1286 | const uint32_t n_tens = req->n_tensors; |
| 1287 | const uint32_t n_ops = req->n_ops; |
| 1288 | |
| 1289 | const uint32_t b_size = sizeof(struct htp_buf_desc) * n_bufs; |
| 1290 | const uint32_t t_size = sizeof(struct htp_tensor) * n_tens; |
| 1291 | const uint32_t o_size = sizeof(struct htp_op_desc) * n_ops; |
| 1292 | const uint32_t p_size = sizeof(struct htp_prof_desc) * n_ops; |
| 1293 | const uint32_t tr_size = (HTP_MAX_NTHREADS + 1) * req->n_traces * sizeof(struct htp_trace_desc); |
| 1294 | |
| 1295 | if (dbuf->size < b_size + t_size + o_size + p_size + tr_size) { |
| 1296 | FARF(ERROR, "invalid opbatch memory block size %u (req %u)", dbuf->size, b_size + t_size + o_size + p_size + tr_size); |
| 1297 | return; |
| 1298 | } |
| 1299 | |
| 1300 | FARF(HIGH, "processing opbatch #%llu: n-bufs %u n-tensors %u n-ops %u n-traces %u : m-size %u b-size %u t-size %u o-size %u", (unsigned long long) req->seq, |
| 1301 | n_bufs, n_tens, n_ops, req->n_traces, dbuf->size, b_size, t_size, o_size); |
| 1302 | |
| 1303 | // Setup descriptor pointers |
| 1304 | uint8_t * m_ptr = dbuf->ptr; |
| 1305 | struct htp_buf_desc* bufs = (struct htp_buf_desc*) m_ptr; m_ptr += b_size; |
| 1306 | struct htp_tensor* tens = (struct htp_tensor*) m_ptr; m_ptr += t_size; |
| 1307 | struct htp_op_desc* ops = (struct htp_op_desc*) m_ptr; m_ptr += o_size; |
| 1308 | struct htp_prof_desc* pds = (struct htp_prof_desc*) m_ptr; |
| 1309 | |
| 1310 | struct profile_data batch_prof; |
| 1311 | profile_start(HTP_PROF_BASIC, &batch_prof); |
| 1312 | |
| 1313 | memset(ctx->trace, 0, sizeof(ctx->trace)); |
| 1314 | if (ctx->profiler == HTP_PROF_TRACE) { |
| 1315 | struct htp_trace_desc * trace_events = (struct htp_trace_desc *) (m_ptr + p_size); |
| 1316 | for (int t = 0; t <= HTP_MAX_NTHREADS; t++) { |
| 1317 | ctx->trace[t].events = &trace_events[t * req->n_traces]; |
| 1318 | ctx->trace[t].max_events = req->n_traces; |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | // Clean cache at the start of the batch |
| 1323 | htp_trace_event_start(&ctx->trace[0], HTP_TRACE_EVT_L2FLUSH, 0); |
| 1324 | qurt_mem_cache_clean((qurt_addr_t) 0, 0, QURT_MEM_CACHE_FLUSH_INVALIDATE_ALL, QURT_MEM_DCACHE); |
| 1325 | hex_l2fetch_block(ctx, ctx->footprint); |
| 1326 | memset(ctx->dirty_ranges, 0, sizeof(ctx->dirty_ranges)); |
| 1327 | htp_trace_event_stop(&ctx->trace[0], HTP_TRACE_EVT_L2FLUSH, 0); |
| 1328 | |
| 1329 | htp_trace_event_start(&ctx->trace[0], HTP_TRACE_EVT_BUFF, 0); |
| 1330 | prep_op_bufs(ctx, bufs, n_bufs); |
| 1331 | htp_trace_event_stop(&ctx->trace[0], HTP_TRACE_EVT_BUFF, 0); |
| 1332 | |
| 1333 | prep_tensors(ctx, bufs, tens, n_tens); |
| 1334 | |
| 1335 | struct htp_ops_context *octx = &ctx->octx; |
| 1336 | htp_ops_context_reset(octx, ctx); |
| 1337 | |
| 1338 | mdev_group_init(ctx, req); |
| 1339 | |
| 1340 | work_queue_wakeup(ctx->work_queue); |
| 1341 | if (ctx->hmx_queue) { |
| 1342 | hmx_queue_wakeup(ctx->hmx_queue); |
| 1343 | } |
| 1344 | |
| 1345 | int op_status = HTP_STATUS_OK; |
| 1346 | octx->status = HTP_STATUS_OK; |
| 1347 | for (uint32_t i = 0; i < n_ops; i++) { |
| 1348 | struct profile_data prof; |
| 1349 | |
| 1350 | profile_start(ctx->profiler, &prof); |
| 1351 | |
| 1352 | op_status = proc_op_req(octx, bufs, n_bufs, tens, i, &ops[i]); |
| 1353 | |
| 1354 | profile_stop(ctx->profiler, &prof); |
| 1355 | |
| 1356 | htp_ops_context_set_status(octx, op_status); |
| 1357 | |
| 1358 | if (ctx->profiler) { |
| 1359 | pds[i].opcode = ops[i].opcode; |
| 1360 | pds[i].usecs = prof.usecs; |
| 1361 | pds[i].cycles_start = prof.cycles_start; |
| 1362 | pds[i].cycles_stop = prof.cycles_stop; |
| 1363 | for (int j = 0; j < HEX_NUM_PMU_COUNTERS; j++) { |
| 1364 | pds[i].pmu[j] = prof.pmu_counters[j]; |
| 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | if (ctx->hmx_queue) { |
| 1370 | hmx_queue_suspend(ctx->hmx_queue); |
| 1371 | hmx_queue_flush(ctx->hmx_queue); |
| 1372 | } |
| 1373 | work_queue_suspend(ctx->work_queue); |
| 1374 | |
| 1375 | // Flush remaining dirty tensors at the end of the batch |
| 1376 | htp_trace_event_start(&ctx->trace[0], HTP_TRACE_EVT_L2FLUSH, 0); |
| 1377 | qurt_mem_cache_clean((qurt_addr_t) 0, 0, QURT_MEM_CACHE_FLUSH_INVALIDATE_ALL, QURT_MEM_DCACHE); |
| 1378 | htp_trace_event_stop(&ctx->trace[0], HTP_TRACE_EVT_L2FLUSH, 0); |
| 1379 | |
| 1380 | htp_mdev_group_barrier(octx); |
| 1381 | |
| 1382 | profile_stop(HTP_PROF_BASIC, &batch_prof); |
| 1383 | |
| 1384 | struct htp_opbatch_rsp rsp; |
| 1385 | memset(&rsp, 0, sizeof(rsp)); |
| 1386 | rsp.seq = req->seq; |
| 1387 | rsp.status = octx->status; |
| 1388 | rsp.n_bufs = n_bufs; |
| 1389 | rsp.n_tensors = n_tens; |
| 1390 | rsp.n_ops = n_ops; |
| 1391 | rsp.usecs = batch_prof.usecs; |
| 1392 | rsp.cycles_start = batch_prof.cycles_start; |
| 1393 | rsp.cycles_stop = batch_prof.cycles_stop; |
| 1394 | |
| 1395 | if (ctx->profiler == HTP_PROF_TRACE) { |
| 1396 | for (int t = 0; t <= HTP_MAX_NTHREADS; t++) { |
| 1397 | rsp.n_traces[t] = ctx->trace[t].count; |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | struct dspqueue_buffer write_dbuf = *dbuf; |
| 1402 | write_dbuf.flags = DSPQUEUE_BUFFER_FLAG_FLUSH_SENDER | DSPQUEUE_BUFFER_FLAG_INVALIDATE_RECIPIENT; |
| 1403 | |
| 1404 | err = dspqueue_write(queue, 0, 1, &write_dbuf, sizeof(rsp), (const uint8_t *) &rsp, DSPQUEUE_TIMEOUT_NONE); |
| 1405 | if (err != 0) { |
| 1406 | FARF(ERROR, "dspqueue_write failed: 0x%08x", (unsigned) err); |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | #define DSPQUEUE_READ_TIMEOUT_USEC 5000 |
| 1411 | #define DSPQUEUE_POLL_TIMEOUT_USEC 100 |
| 1412 | #define DSPQUEUE_POLL_COUNT 100 |
| 1413 | |
| 1414 | static void process_ops(struct htp_context * ctx) { |
| 1415 | dspqueue_t queue = ctx->dsp_queue; |
| 1416 | int err; |
| 1417 | |
| 1418 | uint32_t poll_count = DSPQUEUE_POLL_COUNT; |
| 1419 | |
| 1420 | vtcm_acquire(ctx); |
| 1421 | |
| 1422 | while (!atomic_load(&ctx->vtcm_needs_release) && !atomic_load(&ctx->killed)) { |
| 1423 | struct htp_opbatch_req req; |
| 1424 | uint32_t r_size = sizeof(req); |
| 1425 | |
| 1426 | struct dspqueue_buffer dbuf; |
| 1427 | uint32_t n_dbufs = 1; |
| 1428 | uint32_t flags = 0; |
| 1429 | |
| 1430 | err = dspqueue_read_noblock(queue, &flags, n_dbufs, &n_dbufs, &dbuf, r_size, &r_size, (uint8_t *) &req); |
| 1431 | if (err == AEE_EWOULDBLOCK) { |
| 1432 | if (--poll_count) { |
| 1433 | qurt_sleep(DSPQUEUE_POLL_TIMEOUT_USEC); |
| 1434 | continue; |
| 1435 | } |
| 1436 | break; |
| 1437 | } |
| 1438 | |
| 1439 | if (err != 0) { |
| 1440 | FARF(ERROR, "dspqueue_read_noblock failed: 0x%08x", (unsigned) err); |
| 1441 | break; |
| 1442 | } |
| 1443 | |
| 1444 | if (r_size < sizeof(req) || n_dbufs != 1) { |
| 1445 | FARF(ERROR, "invalid request : size %u n-dbufs %u", r_size, n_dbufs); |
| 1446 | continue; |
| 1447 | } |
| 1448 | |
| 1449 | // Reset poll count for valid requests |
| 1450 | poll_count = DSPQUEUE_POLL_COUNT; |
| 1451 | |
| 1452 | process_opbatch(ctx, &req, &dbuf); |
| 1453 | } |
| 1454 | |
| 1455 | vtcm_release(ctx); |
| 1456 | } |
| 1457 | |
| 1458 | static void htp_packet_callback(dspqueue_t queue, int error, void * context) { |
| 1459 | (void) queue; |
| 1460 | (void) error; |
| 1461 | struct htp_handle * h = (struct htp_handle *) context; |
| 1462 | if (h && h->ctx) { |
| 1463 | process_ops(h->ctx); |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | static void htp_main_thread(void * context) { |
| 1468 | struct htp_context * ctx = (struct htp_context *) context; |
| 1469 | |
| 1470 | FARF(HIGH, "htp-main-thread: started"); |
| 1471 | |
| 1472 | while (!atomic_load(&ctx->killed)) { |
| 1473 | uint32_t flags = 0; |
| 1474 | uint32_t num_buffers = 0; |
| 1475 | uint32_t message_length = 0; |
| 1476 | |
| 1477 | int err = dspqueue_peek(ctx->dsp_queue, &flags, &num_buffers, &message_length, 50000); |
| 1478 | if (err == 0) { |
| 1479 | process_ops(ctx); |
| 1480 | } else if (err == AEE_EWOULDBLOCK || err == AEE_EEXPIRED) { |
| 1481 | continue; |
| 1482 | } else { |
| 1483 | FARF(ERROR, "dspqueue_peek failed: 0x%08x", (unsigned) err); |
| 1484 | break; |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | FARF(HIGH, "htp-main-thread: stopped"); |
| 1489 | } |