File size: 7,441 Bytes
b7e49b8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | // 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
|