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-hexagon/htp/dma-queue.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-hexagon / htp / dma-queue.h` 518 lines ยท 15461 bytes Raw Blame History 1 #ifndef HTP_DMA_H 2 #define HTP_DMA_H 3 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include "hex-utils.h" 11 12 #include "hex-profile.h" 13 14 #ifdef __cplusplus 15 extern "C" { 16 #endif 17 18 // Define the HW descriptor structs here since the ones in HexSDK are a bit out of date 19 typedef struct dma_descriptor_1d_s { 20 void * next; 21 uint32_t size:24; 22 uint32_t desc_size:2; 23 uint32_t dst_comp:1; 24 uint32_t src_comp:1; 25 uint32_t dst_bypass:1; 26 uint32_t src_bypass:1; 27 uint32_t order:1; 28 uint32_t done:1; 29 uint32_t src; 30 uint32_t dst; 31 } dma_descriptor_1d; 32 33 #if __HVX_ARCH__ < 75 34 35 typedef struct dma_descriptor_2d_s { 36 void * next; 37 uint32_t reserved0:24; 38 uint32_t desc_size:2; 39 uint32_t dst_comp:1; 40 uint32_t src_comp:1; 41 uint32_t dst_bypass:1; 42 uint32_t src_bypass:1; 43 uint32_t order:1; 44 uint32_t done:1; 45 uint32_t src; 46 uint32_t dst; 47 uint32_t desc_type:8; 48 uint32_t reserved1:24; 49 uint32_t row_size:16; 50 uint32_t nrows:16; 51 uint32_t src_stride:16; 52 uint32_t dst_stride:16; 53 uint32_t src_offset:16; 54 uint32_t dst_offset:16; 55 } dma_descriptor_2d; 56 57 #else 58 59 typedef struct dma_descriptor_2d_s { 60 void * next; 61 uint32_t dst_stride:24; 62 uint32_t desc_size:2; 63 uint32_t dst_comp:1; 64 uint32_t src_comp:1; 65 uint32_t dst_bypass:1; 66 uint32_t src_bypass:1; 67 uint32_t order:1; 68 uint32_t done:1; 69 uint32_t src; 70 uint32_t dst; 71 uint32_t desc_type:8; 72 #if __HVX_ARCH__ > 79 73 uint32_t src_upper:8; 74 uint32_t dst_upper:8; 75 uint32_t allocation:2; 76 uint32_t reserved0:2; 77 uint32_t transform:4; 78 #else 79 uint32_t reserved0:24; 80 #endif 81 uint32_t row_size:24; 82 uint32_t nrows_lo:8; 83 uint32_t nrows_hi:8; 84 uint32_t src_stride:24; 85 uint32_t offset:24; 86 uint32_t reserved1:8; 87 } dma_descriptor_2d; 88 89 #endif 90 91 #if __HVX_ARCH__ > 79 92 typedef uint64_t dma_addr_t; 93 #else 94 typedef uint32_t dma_addr_t; 95 #endif 96 97 typedef struct { 98 dma_addr_t dst; 99 dma_addr_t src; 100 } dma_data; 101 102 // Hardware descriptor field limits 103 #define DMA_MAX_NROWS 0xFFFFu // 16-bit HW descriptor limit (65535) 104 #define DMA_MAX_SIZE_16B 0xFFFFu // 16-bit HW descriptor limit for row_size (65535) 105 #define DMA_MAX_STRIDE_16B 0xFFFFu // 16-bit HW descriptor limit for strides (65535) 106 #define DMA_MAX_SIZE_24B 0x00FFFFFFu // 24-bit HW descriptor limit for row_size / 1D size (16MB - 1) 107 #define DMA_MAX_STRIDE_24B 0x00FFFFFFu // 24-bit HW descriptor limit for strides (16MB - 1) 108 #define DMA_SAFE_CHUNK_SIZE 0x00F00000u // ~15MB safe contiguous chunk size 109 110 #if __HVX_ARCH__ < 75 111 #define DMA_MAX_2D_ROW_SIZE DMA_MAX_SIZE_16B 112 #define DMA_MAX_2D_STRIDE DMA_MAX_STRIDE_16B 113 #else 114 #define DMA_MAX_2D_ROW_SIZE DMA_MAX_SIZE_24B 115 #define DMA_MAX_2D_STRIDE DMA_MAX_STRIDE_24B 116 #endif 117 118 #define DMA_FALLBACK_CAPACITY 16u // descriptors in secondary fallback ring 119 120 typedef struct dma_ring_s dma_ring; 121 struct dma_ring_s { 122 dma_descriptor_2d * desc; // descriptor pointers 123 dma_descriptor_2d * tail; // tail pointer 124 dma_data * data; // dst/src data 125 uint32_t push_idx; 126 uint32_t pop_idx; 127 uint32_t capacity; 128 uint32_t idx_mask; 129 struct htp_thread_trace * trace; 130 }; 131 132 typedef struct dma_queue_s dma_queue; 133 typedef dma_queue * dma_queue_t; 134 135 struct dma_queue_s { 136 dma_ring * ring0; // Main descriptor ring state 137 dma_ring * ring1; // Secondary fallback descriptor ring state 138 bool alias; // When set, dma_queue_delete will not free the ring 139 }; 140 141 size_t dma_queue_sizeof(size_t capacity); 142 size_t dma_queue_alignof(void); 143 dma_queue_t dma_queue_init(void * ptr, size_t capacity, struct htp_thread_trace * trace); 144 void dma_queue_free(dma_queue_t q); 145 146 size_t dma_queue_alias_sizeof(void); 147 dma_queue_t dma_queue_alias_init(void * ptr, dma_queue_t main_q); 148 void dma_queue_alias_free(dma_queue_t q); 149 150 bool dma_queue_push_fallback_2d(dma_queue * q, dma_data ddata, size_t dst_stride, size_t src_stride, size_t row_size, size_t nrows); 151 bool dma_queue_push_fallback_1d(dma_queue * q, dma_data ddata, size_t dst_stride, size_t src_stride, size_t row_size, size_t nrows); 152 153 // TODO: technically we don't need these and could use Q6_dmstart/wait/etc instead 154 // but those do not seem to always compiler properly. 155 static inline void dmstart(void * next) { 156 asm volatile(" release(%0):at" : : "r"(next)); 157 asm volatile(" dmstart(%0)" : : "r"(next)); 158 } 159 160 static inline void dmlink(void * cur, void * next) { 161 asm volatile(" release(%0):at" : : "r"(next)); 162 asm volatile(" dmlink(%0, %1)" : : "r"(cur), "r"(next)); 163 } 164 165 static inline unsigned int dmpoll(void) { 166 unsigned int ret = 0; 167 asm volatile(" %0 = dmpoll" : "=r"(ret) : : "memory"); 168 return ret; 169 } 170 171 static inline unsigned int dmwait(void) { 172 unsigned int ret = 0; 173 asm volatile(" %0 = dmwait" : "=r"(ret) : : "memory"); 174 return ret; 175 } 176 177 static inline dma_data dma_make_data_impl(dma_addr_t dst, dma_addr_t src) 178 { 179 dma_data d = { dst, src }; 180 return d; 181 } 182 183 #define dma_make_data(dst, src) dma_make_data_impl((dma_addr_t) (dst), (dma_addr_t) (src)) 184 185 static inline bool dma_ring_push_single_1d(dma_ring * r, dma_data ddata, size_t size) { 186 #if __HVX_ARCH__ > 79 187 assert(!((ddata.src | ddata.dst) >> 32) || size == 0); 188 #endif 189 190 if (((r->push_idx + 1) & r->idx_mask) == r->pop_idx) { 191 return false; 192 } 193 194 dma_descriptor_1d * desc = (dma_descriptor_1d *) &r->desc[r->push_idx]; 195 desc->src = (uint32_t) ddata.src; 196 desc->dst = (uint32_t) ddata.dst; 197 desc->size = size; 198 199 r->data[r->push_idx] = ddata; 200 201 htp_trace_event_start(r->trace, HTP_TRACE_EVT_DMA, r->push_idx); 202 203 if (size) { 204 desc->next = NULL; 205 desc->desc_size = 0; // 1D mode 206 desc->src_bypass = 1; 207 desc->dst_bypass = 1; 208 desc->order = 0; 209 desc->done = 0; 210 211 dmlink(r->tail, desc); 212 r->tail = (dma_descriptor_2d *) desc; 213 } else { 214 desc->desc_size = 0; 215 desc->done = 1; 216 } 217 218 r->push_idx = (r->push_idx + 1) & r->idx_mask; 219 return true; 220 } 221 222 static inline bool dma_ring_push_single_2d(dma_ring * r, dma_data ddata, size_t dst_stride, size_t src_stride, size_t row_size, size_t nrows) { 223 #if __HVX_ARCH__ > 79 224 assert(!((ddata.src | ddata.dst) >> 40) || nrows == 0); 225 const uint32_t src_hi = (uint32_t) (ddata.src >> 32); 226 const uint32_t dst_hi = (uint32_t) (ddata.dst >> 32); 227 const bool is_ext = (src_hi | dst_hi) != 0; 228 #endif 229 230 if (((r->push_idx + 1) & r->idx_mask) == r->pop_idx) { 231 return false; 232 } 233 234 dma_descriptor_2d * desc = &r->desc[r->push_idx]; 235 236 desc->next = NULL; 237 desc->reserved1 = 0; 238 desc->desc_size = 1; // 2d mode 239 desc->src_bypass = 1; 240 desc->dst_bypass = 1; 241 desc->src_comp = 0; 242 desc->dst_comp = 0; 243 desc->order = 0; 244 desc->done = 0; 245 desc->src_stride = src_stride; 246 desc->dst_stride = dst_stride; 247 desc->src = (uint32_t) ddata.src; 248 desc->dst = (uint32_t) ddata.dst; 249 desc->row_size = row_size; 250 251 #if __HVX_ARCH__ < 75 252 desc->reserved0 = 0; 253 desc->desc_type = 0; // 2d (16-bit) mode 254 desc->nrows = nrows; 255 desc->src_offset = 0; 256 desc->dst_offset = 0; 257 #else 258 #if __HVX_ARCH__ > 79 259 desc->src_upper = src_hi; 260 desc->dst_upper = dst_hi; 261 desc->allocation = 0; 262 desc->reserved0 = 0; 263 desc->transform = 0; 264 desc->desc_type = is_ext ? 10 : 9; // 2d 40-bit or 24-bit mode 265 #else 266 desc->reserved0 = 0; 267 desc->desc_type = 9; // 2d (24-bit) mode 268 #endif 269 desc->nrows_lo = (nrows & 0xff); 270 desc->nrows_hi = (nrows >> 8); 271 desc->offset = 0; 272 #endif 273 274 r->data[r->push_idx] = ddata; 275 276 htp_trace_event_start(r->trace, HTP_TRACE_EVT_DMA, r->push_idx); 277 278 if (nrows) { 279 dmlink(r->tail, desc); 280 r->tail = desc; 281 } else { 282 desc->done = 1; 283 } 284 285 r->push_idx = (r->push_idx + 1) & r->idx_mask; 286 return true; 287 } 288 289 #if __HVX_ARCH__ < 75 290 static inline bool dma_ring_push_single_contig(dma_ring * r, dma_data ddata, size_t size) { 291 return dma_ring_push_single_1d(r, ddata, size); 292 } 293 #else 294 static inline bool dma_ring_push_single_contig(dma_ring * r, dma_data ddata, size_t size) { 295 return dma_ring_push_single_2d(r, ddata, size, size, size, 1); 296 } 297 #endif 298 299 static inline dma_data dma_ring_pop(dma_ring * r) { 300 dma_data ddata = { 0 }; 301 302 if (r->push_idx == r->pop_idx) { 303 return ddata; 304 } 305 306 ddata = r->data[r->pop_idx]; 307 308 volatile dma_descriptor_2d * desc = &r->desc[r->pop_idx]; 309 310 // Wait for desc to complete 311 if (!desc->done) { 312 // FARF(ALWAYS, "dma-poll: idx %u dst %p src %p", r->pop_idx, ddata.dst, ddata.src); 313 while (!desc->done) { 314 dmpoll(); 315 } 316 } 317 318 htp_trace_event_stop(r->trace, HTP_TRACE_EVT_DMA, r->pop_idx); 319 320 r->pop_idx = (r->pop_idx + 1) & r->idx_mask; 321 return ddata; 322 } 323 324 static inline dma_data dma_ring_pop_nowait(dma_ring * r) { 325 dma_data ddata = { 0 }; 326 327 if (r->push_idx == r->pop_idx) { 328 return ddata; 329 } 330 331 ddata = r->data[r->pop_idx]; 332 333 htp_trace_event_stop(r->trace, HTP_TRACE_EVT_DMA, r->pop_idx); 334 335 r->pop_idx = (r->pop_idx + 1) & r->idx_mask; 336 return ddata; 337 } 338 339 static inline bool dma_ring_empty(dma_ring * r) { 340 return r->push_idx == r->pop_idx; 341 } 342 343 static inline void dma_ring_flush(dma_ring * r) { 344 while (!dma_ring_empty(r)) { 345 dma_ring_pop(r); 346 } 347 } 348 349 static inline uint32_t dma_ring_depth(dma_ring * r) { 350 return (r->push_idx - r->pop_idx) & r->idx_mask; 351 } 352 353 static inline uint32_t dma_ring_capacity(dma_ring * r) { 354 return r->capacity; 355 } 356 357 static inline bool dma_queue_push_single_1d(dma_queue * q, dma_data ddata, size_t size) { 358 return dma_ring_push_single_1d(q->ring0, ddata, size); 359 } 360 361 static inline bool dma_queue_push_single_2d(dma_queue * q, dma_data ddata, size_t dst_stride, size_t src_stride, size_t row_size, size_t nrows) { 362 return dma_ring_push_single_2d(q->ring0, ddata, dst_stride, src_stride, row_size, nrows); 363 } 364 365 static inline dma_data dma_queue_pop(dma_queue * q) { 366 return dma_ring_pop(q->ring0); 367 } 368 369 static inline dma_data dma_queue_pop_nowait(dma_queue * q) { 370 return dma_ring_pop_nowait(q->ring0); 371 } 372 373 static inline bool dma_queue_empty(dma_queue * q) { 374 return dma_ring_empty(q->ring0); 375 } 376 377 static inline void dma_queue_flush(dma_queue * q) { 378 dma_ring_flush(q->ring0); 379 } 380 381 static inline uint32_t dma_queue_depth(dma_queue * q) { 382 return dma_ring_depth(q->ring0); 383 } 384 385 static inline uint32_t dma_queue_capacity(dma_queue * q) { 386 return dma_ring_capacity(q->ring0); 387 } 388 389 static inline bool dma_queue_push(dma_queue * q, dma_data ddata, size_t dst_stride, size_t src_stride, size_t row_size, size_t nrows) { 390 if (__builtin_expect(nrows == 0, 0)) { 391 return dma_ring_push_single_1d(q->ring0, ddata, 0); 392 } 393 394 // 1. Hot path: Contiguous or single-row (80-90% of calls) 395 if (nrows == 1 || !((row_size ^ src_stride) | (row_size ^ dst_stride))) { 396 const size_t total = row_size * nrows; 397 if (__builtin_expect(total <= DMA_MAX_SIZE_24B, 1)) { 398 return dma_ring_push_single_contig(q->ring0, ddata, total); 399 } 400 return dma_queue_push_fallback_1d(q, ddata, 0, 0, total, 1); 401 } 402 403 // 2. Hot path: Standard strided 2D (10-20% of calls) 404 if (__builtin_expect(nrows <= DMA_MAX_NROWS && 405 (row_size | src_stride | dst_stride) <= DMA_MAX_2D_ROW_SIZE, 1)) { 406 return dma_ring_push_single_2d(q->ring0, ddata, dst_stride, src_stride, row_size, nrows); 407 } 408 409 // 3. Cold path: Descriptor chunking fallbacks (< 0.1%) 410 if ((row_size | src_stride | dst_stride) <= DMA_MAX_2D_ROW_SIZE) { 411 return dma_queue_push_fallback_2d(q, ddata, dst_stride, src_stride, row_size, nrows); 412 } 413 return dma_queue_push_fallback_1d(q, ddata, dst_stride, src_stride, row_size, nrows); 414 } 415 416 static inline void dma_sync_read(dma_queue * dma_q, void * dst, dma_addr_t src, size_t bytes) { 417 const uint32_t b = (uint32_t) bytes; 418 if (b > 0) { 419 dma_queue_push(dma_q, dma_make_data(dst, src), b, b, b, 1); 420 dma_queue_pop(dma_q); 421 } 422 } 423 424 static inline void dma_sync_write(dma_queue * dma_q, dma_addr_t dst, const void * src, size_t bytes) { 425 const uint32_t b = (uint32_t) bytes; 426 if (b > 0) { 427 dma_queue_push(dma_q, dma_make_data(dst, src), b, b, b, 1); 428 dma_queue_pop(dma_q); 429 } 430 } 431 432 #define DMA_CACHE_MAX_SIZE 128U 433 434 // Fully assoc LRU cache 435 typedef struct __attribute__((aligned(128))) { 436 uint8_t *base; 437 uint32_t line_size; 438 uint32_t capacity; 439 dma_addr_t src[DMA_CACHE_MAX_SIZE]; 440 uint16_t age[DMA_CACHE_MAX_SIZE]; 441 } dma_cache_fa; 442 443 static inline void dma_cache_fa_init(dma_cache_fa *c, uint8_t *base, uint32_t line_size, uint32_t capacity) 444 { 445 c->capacity = (capacity > DMA_CACHE_MAX_SIZE) ? DMA_CACHE_MAX_SIZE : capacity; 446 c->base = base; 447 c->line_size = line_size; 448 449 for (unsigned i=0; i < c->capacity; i++) { 450 c->src[i] = 0; 451 c->age[i] = 0; 452 } 453 } 454 455 static inline bool dma_cache_fa_push(dma_queue *q, dma_cache_fa *c, dma_addr_t src_addr, uint32_t dst_stride, uint32_t src_stride, uint32_t row_size, uint32_t nrows) 456 { 457 uint32_t o_idx = 0; 458 uint16_t o_age = 0; 459 uint8_t * dst = 0; 460 461 for (unsigned i=0; i < c->capacity; i++) { 462 if (c->src[i] == src_addr) { 463 c->age[i] = 0; 464 dst = c->base + (i * c->line_size); nrows = 0; // dummy dma 465 } else { 466 c->age[i]++; 467 if (c->age[i] > o_age) { o_age = c->age[i]; o_idx = i; } 468 } 469 } 470 if (!dst) { 471 c->age[o_idx] = 0; 472 c->src[o_idx] = src_addr; 473 dst = c->base + o_idx * c->line_size; // normal nrows dma 474 return dma_queue_push(q, dma_make_data(dst, src_addr), dst_stride, src_stride, row_size, nrows); 475 } 476 477 return dma_queue_push_single_1d(q, dma_make_data(dst, src_addr), 0); 478 } 479 480 // Direct mapped cache 481 typedef struct __attribute__((aligned(128))) { 482 uint8_t *base; 483 uint32_t line_size; 484 uint32_t capacity; 485 uint32_t idx_mask; 486 dma_addr_t src[DMA_CACHE_MAX_SIZE]; 487 } dma_cache_dm; 488 489 static inline void dma_cache_dm_init(dma_cache_dm *c, uint8_t *base, uint32_t line_size, uint32_t capacity) 490 { 491 c->capacity = (capacity > DMA_CACHE_MAX_SIZE) ? DMA_CACHE_MAX_SIZE : capacity; 492 c->idx_mask = c->capacity - 1; 493 c->base = base; 494 c->line_size = line_size; 495 496 for (unsigned i=0; i < c->capacity; i++) { 497 c->src[i] = 0; 498 } 499 } 500 501 static inline bool dma_cache_dm_push(dma_queue *q, dma_cache_dm *c, uint32_t slot, dma_addr_t src_addr, uint32_t dst_stride, uint32_t src_stride, uint32_t row_size, uint32_t nrows) 502 { 503 const uint32_t i = slot & c->idx_mask; 504 uint8_t * dst = c->base + (i * c->line_size); 505 506 if (c->src[i] == src_addr) { 507 return dma_queue_push_single_1d(q, dma_make_data(dst, src_addr), 0); // dummy dma 508 } 509 510 c->src[i] = src_addr; 511 return dma_queue_push(q, dma_make_data(dst, src_addr), dst_stride, src_stride, row_size, nrows); 512 } 513 514 #ifdef __cplusplus 515 } // extern "C" 516 #endif 517 518 #endif /* HTP_DMA_H */