Snitch Runtime
Loading...
Searching...
No Matches
alloc.h
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
5#pragma once
6
7#define ALIGN_UP(addr, size) (((addr) + (size)-1) & ~((size)-1))
8#define ALIGN_DOWN(addr, size) ((addr) & ~((size)-1))
9
10#define MIN_CHUNK_SIZE 8
11
12extern snrt_allocator_t l3_allocator;
13
14inline snrt_allocator_t *snrt_l1_allocator() {
15 return (snrt_allocator_t *)&(cls()->l1_allocator);
16}
17
18inline snrt_allocator_t *snrt_l3_allocator() { return &l3_allocator; }
19
20inline void *snrt_l1_next() { return (void *)snrt_l1_allocator()->next; }
21
22inline void *snrt_l3_next() { return (void *)snrt_l3_allocator()->next; }
23
31inline void *snrt_l1_alloc(size_t size) {
32 snrt_allocator_t *alloc = snrt_l1_allocator();
33
34 // TODO colluca: do we need this? What does it imply?
35 // one more instruction, TCDM consumption...
36 size = ALIGN_UP(size, MIN_CHUNK_SIZE);
37
38 // TODO colluca
39 // if (alloc->next + size > alloc->base + alloc->size) {
40 // snrt_trace(
41 // SNRT_TRACE_ALLOC,
42 // "Not enough memory to allocate: base %#x size %#x next %#x\n",
43 // alloc->base, alloc->size, alloc->next);
44 // return 0;
45 // }
46
47 void *ret = (void *)alloc->next;
48 alloc->next += size;
49 return ret;
50}
51
55inline void snrt_l1_update_next(void *next) {
56 snrt_allocator_t *alloc = snrt_l1_allocator();
57 alloc->next = (uint32_t)next;
58}
59
67inline void *snrt_l3_alloc(size_t size) {
68 snrt_allocator_t *alloc = snrt_l3_allocator();
69
70 size = ALIGN_UP(size, MIN_CHUNK_SIZE);
71
72 // TODO: L3 alloc size check
73
74 void *ret = (void *)alloc->next;
75 alloc->next += size;
76 return ret;
77}
78
79inline void snrt_alloc_init() {
80 // Only one core per cluster has to initialize the L1 allocator
81 if (snrt_is_dm_core()) {
82 // Initialize L1 allocator
83 // Note: at the moment the allocator assumes all of the TCDM is
84 // available for allocation. However, the CLS, TLS and stack already
85 // occupy a possibly significant portion.
86 snrt_l1_allocator()->base =
87 ALIGN_UP(snrt_l1_start_addr(), MIN_CHUNK_SIZE);
88 snrt_l1_allocator()->end = snrt_l1_end_addr();
89 snrt_l1_allocator()->next = snrt_l1_allocator()->base;
90 // Initialize L3 allocator
91 extern uint32_t _edram;
92 snrt_l3_allocator()->base = ALIGN_UP((uint32_t)&_edram, MIN_CHUNK_SIZE);
93 snrt_l3_allocator()->end = snrt_l3_allocator()->base;
94 snrt_l3_allocator()->next = snrt_l3_allocator()->base;
95 }
96 // Synchronize with other cores
97 snrt_cluster_hw_barrier();
98}
99
100// TODO colluca: optimize by using DMA
101inline void *snrt_memset(void *ptr, int value, size_t num) {
102 for (uint32_t i = 0; i < num; ++i)
103 *((uint8_t *)ptr + i) = (unsigned char)value;
104 return ptr;
105}
Definition alloc_decls.h:10