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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! Cache management operations for the ET-SoC-1 Minion processor.
//!
//! The ET-SoC-1 implements a software-coherent memory model: the RISC-V
//! `fence` instruction orders CPU-visible stores but does not flush dirty L1
//! data cache lines to L2 or DDR. Cross-hart, cross-shire, and host-visible
//! coherence therefore require explicit cache management via dedicated CSRs.
//!
//! # Cache hierarchy
//!
//! Each Minion core has a private L1 data cache with 64-byte lines. The L2
//! is shared among all Minions in a shire (512 KB on aifoundry3). The L3 is
//! shared across all compute shires (32 MB on aifoundry3). Host DMA reads
//! bypass all Minion caches and observe only DDR.
//!
//! # Producer/consumer protocol
//!
//! Per PRM Section 8.1.3, software must `fence` before a cache op (to commit
//! all prior CPU stores to L1) and issue `TensorWait(CacheOp)` after (to
//! guarantee the op completed before any subsequent memory access to the
//! affected lines). The high-level functions below handle the TensorWait
//! internally; only the preceding `fence` is the caller's responsibility.
//!
//! ```text
//! // Hart A (producer):
//! // ... write data ...
//! fence(); // commit stores to L1
//! unsafe { cache_writeback(ptr as usize, len); } // flush L1 to DDR + TensorWait
//!
//! // <synchronisation, e.g. via a shared flag + fence on both sides>
//!
//! // Hart B (consumer):
//! fence(); // receive synchronisation
//! unsafe { cache_invalidate(ptr as usize, len); } // discard stale L1 + TensorWait
//! // ... read data ...
//! ```
//!
//! Use [`cache_flush`] when a region may contain both dirty (locally modified)
//! and stale lines, performing writeback then invalidation atomically at the
//! function level.
//!
//! # Cache levels
//!
//! The high-level functions [`cache_writeback`], [`cache_invalidate`], and
//! [`cache_flush`] propagate to main memory ([`CacheDest::Mem`]), which is the
//! safest choice for cross-shire and host-DMA coherence. The lower-level
//! `_to` variants accept an explicit [`CacheDest`] for intra-shire operations
//! that need only reach L2.
use asm;
// ---------------------------------------------------------------------------
// CSR addresses (cacheops.h, Ainekko SDK)
// ---------------------------------------------------------------------------
/// `evict_va` CSR: evicts cache lines by virtual address up to a target level.
pub const CSR_EVICT_VA: u16 = 0x89F;
/// `flush_va` CSR: writes back dirty cache lines by virtual address to a target level.
pub const CSR_FLUSH_VA: u16 = 0x8BF;
// ---------------------------------------------------------------------------
// Cache destination enum
// ---------------------------------------------------------------------------
/// Target cache hierarchy level for cache management operations.
///
/// Specifies how far up the cache hierarchy a writeback or eviction
/// propagates. Use [`Mem`](CacheDest::Mem) for host-DMA visibility;
/// [`L2`](CacheDest::L2) to make data visible to other Minions in the same
/// shire without a full writeback to DDR.
// ---------------------------------------------------------------------------
// Hardware primitives
// ---------------------------------------------------------------------------
/// Issues a single `evict_va` CSR write (CSR `0x89F`).
///
/// Evicts `hw_count + 1` cache lines starting at `line_addr` (64-byte
/// aligned), using a stride of 64 bytes per hardware iteration. The
/// hardware reads x31 (t6) implicitly at the moment of the CSR write; this
/// function loads t6 = 64 (stride=64, id=0) immediately before the
/// instruction to satisfy that dependency.
///
/// CSR field layout (cacheops.h `evict_va`):
/// - \[63\]: `use_tmask` = 0
/// - \[59:58\]: `dst` (`CacheDest` discriminant)
/// - \[47:6\]: VA bits \[47:6\] (`line_addr` is 64B-aligned, so bits \[5:0\] = 0;
/// the mask `0x0000_FFFF_FFFF_FFC0` preserves bits \[47:6\] only)
/// - \[3:0\]: `hw_count` (0..=15, encodes 1..=16 lines)
///
/// x31 layout: `(stride & !63) | id`. For stride=64, id=0: x31 = 64.
///
/// # Safety
/// `line_addr` must be 64-byte aligned. `hw_count` must be in `0..=15`.
unsafe
/// Issues a single `flush_va` CSR write (CSR `0x8BF`).
///
/// Writes back `hw_count + 1` dirty cache lines to `dst`; the lines remain
/// cached as clean. All parameters and the CSR field layout are identical to
/// [`evict_va_hw`], differing only in the CSR address.
///
/// # Safety
/// `line_addr` must be 64-byte aligned. `hw_count` must be in `0..=15`.
unsafe
/// Waits for all outstanding cache operations to complete.
///
/// Issues `TensorWait(ID=6)` (CSR `0x830`, xs bits \[3:0\] = 6), which stalls
/// the hart until every previously issued `evict_va`, `flush_va`,
/// `prefetch_va`, and `TensorLoadL2Scp` has completed. Required after any
/// cache management instruction and before any subsequent memory access to the
/// affected cache lines (PRM Table 9-2, event code 6; PRM Section 8.1.3).
// ---------------------------------------------------------------------------
// Range helpers
// ---------------------------------------------------------------------------
/// Number of 64-byte cache lines covering the byte range `[addr, addr + len)`.
///
/// The result accounts for a partially covered first line: if `addr` is not
/// 64-byte aligned, the first line begins at `addr & !63`.
/// Evicts all cache lines in `[addr, addr + len)` to `dst`, in batches of 16.
///
/// The hardware field `hw_count` is 0-indexed (0 = 1 line, 15 = 16 lines).
/// Each batch issues one `evict_va` CSR write covering `batch` lines.
/// Writes back all dirty cache lines in `[addr, addr + len)` to `dst`,
/// in batches of 16.
// ---------------------------------------------------------------------------
// Public API - high-level (always targets main memory)
// ---------------------------------------------------------------------------
/// Writes back dirty L1 cache lines in `[addr, addr + len)` to main memory.
///
/// Issues `flush_va` for every covered line, then stalls via `TensorWait(6)`
/// until all writeback traffic has reached DDR. After this call, the flushed
/// data is visible to host DMA and to other shires reading from DDR.
/// The lines remain cached as clean.
///
/// Callers must issue [`crate::fence`] before this function to commit all
/// prior CPU stores to L1 (PRM Section 8.1.3).
///
/// Equivalent to [`cache_writeback_to`]`(CacheDest::Mem, addr, len)`.
///
/// # Safety
/// `addr` must be a valid virtual address; `[addr, addr + len)` must lie
/// within device memory accessible to this hart.
pub unsafe
/// Invalidates (evicts) L1 cache lines in `[addr, addr + len)`.
///
/// Issues `evict_va` for every covered line, then stalls via `TensorWait(6)`
/// until all eviction traffic is complete. Subsequent loads to the range will
/// fetch fresh data from DDR. Issue on the consumer side of a cross-hart or
/// host-DMA coherence protocol after receiving the producer's synchronisation
/// signal and before reading the produced data.
///
/// Callers must issue [`crate::fence`] before this function (PRM Section
/// 8.1.3).
///
/// Equivalent to [`cache_invalidate_to`]`(CacheDest::Mem, addr, len)`.
///
/// # Safety
/// `addr` must be a valid virtual address; `[addr, addr + len)` must lie
/// within device memory accessible to this hart. Invalidating dirty lines
/// without a prior writeback discards uncommitted data; use [`cache_flush`]
/// when lines may be dirty.
pub unsafe
/// Writes back then invalidates L1 cache lines in `[addr, addr + len)`.
///
/// Issues `flush_va` for every covered line followed by `evict_va` for the
/// same lines, then stalls via `TensorWait(6)`. Use when the calling hart has
/// both dirty data to publish and potentially stale lines to discard.
///
/// Callers must issue [`crate::fence`] before this function (PRM Section
/// 8.1.3).
///
/// # Safety
/// `addr` must be a valid virtual address; `[addr, addr + len)` must lie
/// within device memory accessible to this hart.
pub unsafe
// ---------------------------------------------------------------------------
// Public API - lower-level (explicit destination)
// ---------------------------------------------------------------------------
/// Writes back dirty cache lines in `[addr, addr + len)` to `dst`.
///
/// Lower-level variant of [`cache_writeback`] with an explicit destination.
/// Issues `flush_va` then `TensorWait(6)`. Pass [`CacheDest::L2`] to make
/// data visible to other Minions in the same shire without propagating to DDR.
///
/// # Safety
/// Same constraints as [`cache_writeback`].
pub unsafe
/// Invalidates cache lines in `[addr, addr + len)`, evicting to `dst`.
///
/// Lower-level variant of [`cache_invalidate`] with an explicit destination.
/// Issues `evict_va` then `TensorWait(6)`.
///
/// # Safety
/// Same constraints as [`cache_invalidate`].
pub unsafe
// ---------------------------------------------------------------------------
// Tests (host-only; do not touch the hardware CSRs)
// ---------------------------------------------------------------------------