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
15extern __thread snrt_allocator_t l1_allocator_v2;
16
22inline snrt_allocator_t *snrt_l1_allocator_v2() { return &l1_allocator_v2; }
23
29inline void *snrt_l1_next_v2() { return (void *)snrt_l1_allocator_v2()->next; }
30
36inline void snrt_l1_update_next_v2(void *next) {
37 snrt_l1_allocator_v2()->next = (uint32_t)next;
38}
39
44static inline void snrt_l1_alloc_check_bounds() {
45 if (snrt_l1_allocator_v2()->next > snrt_l1_allocator_v2()->end)
46 asm volatile("ecall \n");
47}
48
61inline void *snrt_l1_alloc_cluster_local(size_t size, const size_t alignment) {
62 snrt_l1_allocator_v2()->next =
63 snrt_align_up(snrt_l1_allocator_v2()->next, alignment);
64 void *retval = snrt_l1_next_v2();
65 snrt_l1_allocator_v2()->next += size;
66 snrt_l1_alloc_check_bounds();
67 return retval;
68}
69
83inline void *snrt_l1_alloc_compute_core_local(size_t size,
84 const size_t alignment) {
85 snrt_l1_allocator_v2()->next =
86 snrt_align_up(snrt_l1_allocator_v2()->next, alignment);
87 void *retval =
88 ((uint8_t *)snrt_l1_next_v2()) + size * snrt_cluster_core_idx();
89 snrt_l1_allocator_v2()->next += size * snrt_cluster_compute_core_num();
90 snrt_l1_alloc_check_bounds();
91 return retval;
92}
93
107inline void *snrt_compute_core_local_ptr(void *ptr, uint32_t core_idx,
108 size_t size) {
109 size_t offset = (core_idx - snrt_cluster_core_idx()) * size;
110 return (void *)((uintptr_t)ptr + offset);
111}
112
125inline void *snrt_remote_l1_ptr(void *ptr, uint32_t src_cluster_idx,
126 uint32_t dst_cluster_idx) {
127 return (void *)((uintptr_t)ptr +
128 (dst_cluster_idx - src_cluster_idx) * SNRT_CLUSTER_OFFSET);
129}
130
140inline void snrt_alloc_init_v2() {
141 // Calculate end address of the heap. The top of the TCDM address space is
142 // reserved for the cluster-local storage (CLS) and the stack of every
143 // core. We further provision a safety margin of 128B. The rest of the
144 // TCDM is reserved for the heap.
145 uint32_t heap_end_addr = snrt_cls_base_addr();
146 heap_end_addr -= (1 << SNRT_LOG2_STACK_SIZE) * snrt_cluster_core_num();
147 heap_end_addr -= 128;
148 // Initialize L1 allocator
149 snrt_l1_allocator_v2()->base =
150 snrt_align_up(snrt_l1_start_addr(), MIN_CHUNK_SIZE);
151 snrt_l1_allocator_v2()->end = heap_end_addr;
153}
void * snrt_l1_alloc_cluster_local(size_t size, const size_t alignment)
Allocate space for a variable in the cluster's L1 memory.
Definition alloc_v2.h:61
void snrt_l1_update_next_v2(void *next)
Override the L1 allocator next pointer.
Definition alloc_v2.h:36
void snrt_alloc_init_v2()
Initialize the L1 allocator.
Definition alloc_v2.h:140
void * snrt_l1_next_v2()
Get the next pointer of the L1 allocator.
Definition alloc_v2.h:29
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:125
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:107
void * snrt_l1_alloc_compute_core_local(size_t size, const size_t alignment)
Allocate space for N variables in the cluster's L1 memory.
Definition alloc_v2.h:83
snrt_allocator_t * snrt_l1_allocator_v2()
Get a pointer to the L1 allocator.
Definition alloc_v2.h:22
Definition alloc_decls.h:10