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 // TODO: L3 alloc size check
71
72 void *ret = (void *)alloc->next;
73 alloc->next += size;
74 return ret;
75}
76
77inline void snrt_alloc_init() {
78 // Only one core per cluster has to initialize the L1 allocator
79 if (snrt_is_dm_core()) {
80 // Initialize L1 allocator
81 // Note: at the moment the allocator assumes all of the TCDM is
82 // available for allocation. However, the CLS, TLS and stack already
83 // occupy a possibly significant portion.
84 snrt_l1_allocator()->base =
85 ALIGN_UP(snrt_l1_start_addr(), MIN_CHUNK_SIZE);
86 snrt_l1_allocator()->end = snrt_l1_end_addr();
87 snrt_l1_allocator()->next = snrt_l1_allocator()->base;
88 // Initialize L3 allocator
89 extern uint32_t _edram;
90 snrt_l3_allocator()->base = ALIGN_UP((uint32_t)&_edram, MIN_CHUNK_SIZE);
91 snrt_l3_allocator()->end = snrt_l3_allocator()->base;
92 snrt_l3_allocator()->next = snrt_l3_allocator()->base;
93 }
94 // Synchronize with other cores
95 snrt_cluster_hw_barrier();
96}
97
98// TODO colluca: optimize by using DMA
99inline void *snrt_memset(void *ptr, int value, size_t num) {
100 for (uint32_t i = 0; i < num; ++i)
101 *((uint8_t *)ptr + i) = (unsigned char)value;
102 return ptr;
103}
Definition alloc_decls.h:10