Snitch Runtime
Loading...
Searching...
No Matches
alloc_v2.h
Go to the documentation of this file.
1// Copyright 2023 ETH Zurich and University of Bologna.
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4
15//================================================================================
16// L1 allocation
17//================================================================================
18
19extern __thread snrt_allocator_t l1_allocator_v2;
20
26inline snrt_allocator_t *snrt_l1_allocator_v2() { return &l1_allocator_v2; }
27
33inline void *snrt_l1_next_v2() { return (void *)snrt_l1_allocator_v2()->next; }
34
40inline void snrt_l1_update_next_v2(void *next) {
41 snrt_l1_allocator_v2()->next = (uint32_t)next;
42}
43
48static inline void snrt_l1_alloc_check_bounds() {
49 if (snrt_l1_allocator_v2()->next > snrt_l1_allocator_v2()->end)
50 asm volatile("ecall \n");
51}
52
65inline void *snrt_l1_alloc_cluster_local(size_t size,
66 const size_t alignment = 1) {
67 snrt_l1_allocator_v2()->next =
68 snrt_align_up(snrt_l1_allocator_v2()->next, alignment);
69 void *retval = snrt_l1_next_v2();
70 snrt_l1_allocator_v2()->next += size;
72 return retval;
73}
74
88inline void *snrt_l1_alloc_compute_core_local(size_t size,
89 const size_t alignment = 1) {
90 snrt_l1_allocator_v2()->next =
91 snrt_align_up(snrt_l1_allocator_v2()->next, alignment);
92 void *retval =
93 ((uint8_t *)snrt_l1_next_v2()) + size * snrt_cluster_core_idx();
94 snrt_l1_allocator_v2()->next += size * snrt_cluster_compute_core_num();
96 return retval;
97}
98
108inline void snrt_l1_init() {
109 // Calculate end address of the heap. The top of the TCDM address space is
110 // reserved for the cluster-local storage (CLS) and the stack of every
111 // core. We further provision a safety margin of 128B. The rest of the
112 // TCDM is reserved for the heap.
113 uint32_t heap_end_addr = snrt_cls_base_addr();
114 heap_end_addr -= (1 << SNRT_LOG2_STACK_SIZE) * snrt_cluster_core_num();
115 heap_end_addr -= 128;
116 // Initialize L1 allocator
117 uintptr_t l1_start_addr = (uintptr_t)(snrt_cluster()->tcdm.mem);
118 snrt_l1_allocator_v2()->base = snrt_align_up(l1_start_addr, MIN_CHUNK_SIZE);
119 snrt_l1_allocator_v2()->end = heap_end_addr;
121}
122
123//================================================================================
124// L3 allocation
125//================================================================================
126
127extern __thread snrt_allocator_t l3_allocator_v2;
128
134inline snrt_allocator_t *snrt_l3_allocator_v2() { return &l3_allocator_v2; }
135
141inline void *snrt_l3_next_v2() { return (void *)snrt_l3_allocator_v2()->next; }
142
147static inline void snrt_l3_alloc_check_bounds() {
148 if (snrt_l3_allocator_v2()->next >= snrt_l3_allocator_v2()->end)
149 asm volatile("ecall \n");
150}
151
164inline void *snrt_l3_alloc_v2(size_t size, const size_t alignment = 1) {
165 snrt_l3_allocator_v2()->next =
166 snrt_align_up(snrt_l3_allocator_v2()->next, alignment);
167 void *retval = snrt_l3_next_v2();
168 snrt_l3_allocator_v2()->next += size;
170 return retval;
171}
172
182inline void snrt_l3_init() {
183 extern uint32_t _edram;
184 snrt_l3_allocator_v2()->base =
185 snrt_align_up((uint32_t)&_edram, MIN_CHUNK_SIZE);
186 snrt_l3_allocator_v2()->end = SNRT_L3_END_ADDR;
188}
189
190//================================================================================
191// Pointer translation functions
192//================================================================================
193
207inline void *snrt_compute_core_local_ptr(void *ptr, uint32_t core_idx,
208 size_t size) {
209 size_t offset = (core_idx - snrt_cluster_core_idx()) * size;
210 return (void *)((uintptr_t)ptr + offset);
211}
212
225inline void *snrt_remote_l1_ptr(void *ptr, uint32_t src_cluster_idx,
226 uint32_t dst_cluster_idx) {
227 return (void *)((uintptr_t)ptr +
228 (dst_cluster_idx - src_cluster_idx) * SNRT_CLUSTER_OFFSET);
229}
void snrt_l3_init()
Initialize the L3 allocator.
Definition alloc_v2.h:182
void * snrt_l3_alloc_v2(size_t size, const size_t alignment=1)
Allocate space for a variable in L3 memory.
Definition alloc_v2.h:164
void snrt_l1_update_next_v2(void *next)
Override the L1 allocator next pointer.
Definition alloc_v2.h:40
static void snrt_l1_alloc_check_bounds()
Check if the allocation exceeds the allocator bounds and raise an exception if it does.
Definition alloc_v2.h:48
void snrt_l1_init()
Initialize the L1 allocator.
Definition alloc_v2.h:108
void * snrt_l1_next_v2()
Get the next pointer of the L1 allocator.
Definition alloc_v2.h:33
void * snrt_l1_alloc_compute_core_local(size_t size, const size_t alignment=1)
Allocate space for N variables in the cluster's L1 memory.
Definition alloc_v2.h:88
void * snrt_l3_next_v2()
Get the next pointer of the L3 allocator.
Definition alloc_v2.h:141
static void snrt_l3_alloc_check_bounds()
Check if the allocation exceeds the allocator bounds and raise an exception if it does.
Definition alloc_v2.h:147
snrt_allocator_t * snrt_l3_allocator_v2()
Get a pointer to the L3 allocator.
Definition alloc_v2.h:134
void * snrt_remote_l1_ptr(void *ptr, uint32_t src_cluster_idx, uint32_t dst_cluster_idx)
Get a pointer to the same offset in another cluster's L1 memory.
Definition alloc_v2.h:225
void * snrt_l1_alloc_cluster_local(size_t size, const size_t alignment=1)
Allocate space for a variable in the cluster's L1 memory.
Definition alloc_v2.h:65
void * snrt_compute_core_local_ptr(void *ptr, uint32_t core_idx, size_t size)
Get a pointer to the same variable allocated by another core.
Definition alloc_v2.h:207
snrt_allocator_t * snrt_l1_allocator_v2()
Get a pointer to the L1 allocator.
Definition alloc_v2.h:26
Definition alloc_decls.h:10