changh95's picture
tt-model push depthpro2 (container)
b7e49b8 verified
Raw History Blame Contribute Delete
7.44 kB
// SPDX-License-Identifier: Apache-2.0
//
// DepthPro2 DEC megakernel: data-movement helpers shared by the BRISC and NCRISC kernels (mk_enc's me_dm.hpp).
#pragma once
#include <cstdint>
#include "api/dataflow/dataflow_api.h"
#include "api/debug/waypoint.h"
#include "md_plan.hpp"
namespace mdx {
FORCE_INLINE uint32_t rt(uint32_t i) { return get_arg_val<uint32_t>(i); }
FORCE_INLINE uint32_t ca(uint32_t i) { return get_common_arg_val<uint32_t>(i); }
// Byte address of a CB's buffer base (LocalCBInterface keeps it in 16 B units): identical on every core.
FORCE_INLINE uint32_t cb_base(uint32_t cb) {
const auto& iface = get_local_cb_interface(cb);
return (iface.fifo_limit - iface.fifo_size) << cb_addr_shift;
}
FORCE_INLINE uint32_t sync_addr(uint32_t w) { return cb_base(CB_SYNC) + w * SYNC_STRIDE; }
FORCE_INLINE volatile tt_l1_ptr uint32_t* sync_ptr(uint32_t w) {
return reinterpret_cast<volatile tt_l1_ptr uint32_t*>(sync_addr(w));
}
FORCE_INLINE uint32_t sync_read(uint32_t w) {
invalidate_l1_cache();
asm volatile("" ::: "memory");
return *sync_ptr(w);
}
// Multicast NoC address of the rectangle (x0, y0)-(x1, y1) given in NoC-0 virtual coordinates (start = min corner);
// NoC 1 addresses the rectangle end-first.
FORCE_INLINE uint64_t mcast_addr(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t addr) {
if (noc_index == 0) {
return NOC_MULTICAST_ADDR(
DYNAMIC_NOC_X(0, x0), DYNAMIC_NOC_Y(0, y0), DYNAMIC_NOC_X(0, x1), DYNAMIC_NOC_Y(0, y1), addr);
}
return NOC_MULTICAST_ADDR(
DYNAMIC_NOC_X(1, x1), DYNAMIC_NOC_Y(1, y1), DYNAMIC_NOC_X(1, x0), DYNAMIC_NOC_Y(1, y0), addr);
}
// Tiles of the weight ring the TRISC has not yet released (16-bit received / acked counters).
FORCE_INLINE uint32_t ring_outstanding_tiles() {
invalidate_l1_cache();
asm volatile("" ::: "memory");
const uint16_t received = (uint16_t)get_cb_tiles_received_ptr(CB_W)[0];
const uint16_t acked = (uint16_t)reg_read((uintptr_t)get_cb_tiles_acked_ptr(CB_W));
return (uint16_t)(received - acked);
}
// ------------------------------------------------------------------ bounded waits (hang protection)
inline uint32_t g_abort = 0;
inline uint32_t g_spin = 1u << 26;
inline uint32_t g_dbg_word = W_DBG; // BRISC: W_DBG, NCRISC: W_DBG + 2
FORCE_INLINE void abort_note(uint32_t code, uint32_t arg) {
if (!g_abort) {
g_abort = code;
*sync_ptr(g_dbg_word) = code;
*sync_ptr(g_dbg_word + 1) = arg;
*sync_ptr(W_ABORT) = ABORT_MAGIC; // releases every other RISC of this core (their waits poll it)
}
}
// polled every 4096 spins inside every wait: another RISC of this core aborted -> stop waiting too
FORCE_INLINE bool peer_abort(uint32_t it) {
if ((it & 4095) == 0 && sync_read(W_ABORT) == ABORT_MAGIC) {
abort_note(AB_PEER, 0);
return true;
}
return false;
}
__attribute__((noinline)) inline void cbw_front(int32_t cb, int32_t n) {
if (g_abort) {
return;
}
const uint32_t acked = get_cb_tiles_acked_ptr(cb)[0];
const uintptr_t rp = (uintptr_t)get_cb_tiles_received_ptr(cb);
uint32_t it = 0;
WAYPOINT("CWFW");
while ((uint16_t)(((uint16_t)reg_read(rp)) - acked) < (uint16_t)n) {
if (++it > g_spin) {
abort_note(AB_FRONT, cb);
return;
}
if (peer_abort(it)) {
return;
}
}
}
__attribute__((noinline)) inline void cbr_back(int32_t cb, int32_t n) {
if (g_abort) {
return;
}
const uintptr_t ap = (uintptr_t)get_cb_tiles_acked_ptr(cb);
const uint32_t received = get_cb_tiles_received_ptr(cb)[0];
uint32_t it = 0;
WAYPOINT("CRBW");
while (true) {
invalidate_l1_cache();
const uint16_t acked = (uint16_t)reg_read(ap);
const uint16_t fr = get_local_cb_interface(cb).fifo_num_pages - (uint16_t)(received - acked);
if ((int32_t)fr >= n) {
break;
}
if (++it > g_spin) {
abort_note(AB_BACK, cb);
return;
}
if (peer_abort(it)) {
return;
}
}
}
// spin until sync word w >= v (bounded)
__attribute__((noinline)) inline void spin_ge(uint32_t w, uint32_t v) {
if (g_abort) {
return;
}
uint32_t it = 0;
while (sync_read(w) < v) {
if (++it > g_spin) {
abort_note(AB_SYNC, w);
return;
}
if (peer_abort(it)) {
return;
}
}
}
// bounded NoC barriers (dedicated NoC mode): a transaction that never completes aborts instead of hanging
__attribute__((noinline)) inline void rd_bar() {
uint32_t it = 0;
while (!g_abort && !ncrisc_noc_reads_flushed(noc_index)) {
if (++it > g_spin) {
abort_note(AB_NOC, 1);
}
peer_abort(it);
}
invalidate_l1_cache();
}
__attribute__((noinline)) inline void wr_bar() {
uint32_t it = 0;
while (!g_abort && !ncrisc_noc_nonposted_writes_flushed(noc_index)) {
if (++it > g_spin) {
abort_note(AB_NOC, 2);
}
peer_abort(it);
}
invalidate_l1_cache();
}
__attribute__((noinline)) inline void wr_flush() {
uint32_t it = 0;
while (!g_abort && !ncrisc_noc_nonposted_writes_sent(noc_index)) {
if (++it > g_spin) {
abort_note(AB_NOC, 3);
}
peer_abort(it);
}
invalidate_l1_cache();
}
__attribute__((noinline)) inline void at_bar() {
uint32_t it = 0;
while (!g_abort && !ncrisc_noc_nonposted_atomics_flushed(noc_index)) {
if (++it > g_spin) {
abort_note(AB_NOC, 4);
}
peer_abort(it);
}
invalidate_l1_cache();
}
// exit hygiene: every read landed, every non-posted write and atomic acked, even after an abort (bounded; a stale
// transaction must not land in the next launch). bit: W_EXIT bit of this RISC if a barrier timed out
__attribute__((noinline)) inline void exit_barriers(uint32_t bit) {
uint32_t it = 0;
const uint32_t lim = g_spin << 2;
while (!(ncrisc_noc_reads_flushed(noc_index) && ncrisc_noc_nonposted_writes_flushed(noc_index) &&
ncrisc_noc_nonposted_atomics_flushed(noc_index))) {
if (++it > lim) {
*sync_ptr(W_EXIT) = *sync_ptr(W_EXIT) | bit;
break;
}
}
invalidate_l1_cache();
}
// the plan table landed in CB_PLAN (the BRISC pushes it once at boot)
FORCE_INLINE Plan wait_plan() {
WAYPOINT("PLNW");
uint32_t it = 0;
while (true) {
invalidate_l1_cache();
if ((uint16_t)get_cb_tiles_received_ptr(CB_PLAN)[0] != 0) {
break;
}
if (++it > g_spin) {
abort_note(AB_PLAN, 0);
break;
}
}
invalidate_l1_cache();
Plan p;
p.t = reinterpret_cast<const uint32_t*>(cb_base(CB_PLAN));
p.nm = p.t[H_NM];
return p;
}
// boot barrier: every core zeroed its sync words before anyone exchanges (semaphores reset at program launch)
FORCE_INLINE void wait_go(uint32_t sem_go_addr) {
volatile tt_l1_ptr uint32_t* go = reinterpret_cast<volatile tt_l1_ptr uint32_t*>(sem_go_addr);
WAYPOINT("BGO ");
uint32_t it = 0;
while (true) {
invalidate_l1_cache();
if (*go >= 1) {
break;
}
if (++it > g_spin) {
abort_note(AB_SYNC, 999);
break;
}
}
}
} // namespace mdx