1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <cstddef>
18
19 #include "chre/platform/memory.h"
20 #include "chre/platform/shared/dram_vote_client.h"
21 #include "chre/platform/shared/memory.h"
22 #include "mt_alloc.h"
23 #include "mt_dma.h"
24 #include "portable.h"
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 #include "encoding.h"
31 #include "mt_heap.h"
32 #include "resource_req.h"
33
34 #ifdef __cplusplus
35 } // extern "C"
36 #endif
37
38 namespace chre {
39
40 // On tinysys voting/devoting dram are done automatically by the platform APIs
41 // so issueDramVote() should be a no-op.
issueDramVote(bool)42 void DramVoteClient::issueDramVote(bool /*enabled*/) {}
43
44 // no-op since the dma access is controlled by the kernel automatically
forceDramAccess()45 void forceDramAccess() {}
46
nanoappBinaryFree(void * pointer)47 void nanoappBinaryFree(void *pointer) {
48 #ifdef NANOAPP_ALWAYS_IN_DRAM
49 aligned_dram_free(pointer);
50 #else
51 aligned_free(pointer);
52 #endif
53 }
54
nanoappBinaryDramFree(void * pointer)55 void nanoappBinaryDramFree(void *pointer) {
56 aligned_dram_free(pointer);
57 }
58
memoryAllocDram(size_t size)59 void *memoryAllocDram(size_t size) {
60 return pvPortDramMalloc(size);
61 }
62
memoryFreeDram(void * pointer)63 void memoryFreeDram(void *pointer) {
64 vPortDramFree(pointer);
65 }
66
palSystemApiMemoryAlloc(size_t size)67 void *palSystemApiMemoryAlloc(size_t size) {
68 return memoryAlloc(size);
69 }
70
palSystemApiMemoryFree(void * pointer)71 void palSystemApiMemoryFree(void *pointer) {
72 memoryFree(pointer);
73 }
74
nanoappBinaryAlloc(size_t size,size_t alignment)75 void *nanoappBinaryAlloc(size_t size, size_t alignment) {
76 #ifdef NANOAPP_ALWAYS_IN_DRAM
77 return aligned_dram_malloc(size, alignment);
78 #endif
79 return aligned_malloc(size, alignment);
80 }
81
nanoappBinaryDramAlloc(size_t size,size_t alignment)82 void *nanoappBinaryDramAlloc(size_t size, size_t alignment) {
83 // aligned_dram_malloc() requires the alignment being multiple of
84 // CACHE_LINE_SIZE (128 bytes), we will align to page size (4k)
85 return aligned_dram_malloc(size, alignment);
86 }
87
memoryAlloc(size_t size)88 void *memoryAlloc(size_t size) {
89 void *address = pvPortMalloc(size);
90 if (address == nullptr && size > 0) {
91 // Try dram if allocation from sram fails.
92 // DramVoteClient tracks the duration of the allocations falling back to
93 // dram. The idea is that only transient allocations are allowed to fall
94 // back to dram. Any long-lived allocation should be done explicitly via
95 // corresponding memory allocation APIs.
96 DramVoteClientSingleton::get()->incrementDramVoteCount();
97 address = pvPortDramMalloc(size);
98
99 // DRAM allocation failed too.
100 if (address == nullptr) {
101 DramVoteClientSingleton::get()->decrementDramVoteCount();
102 }
103 }
104 return address;
105 }
106
memoryFree(void * pointer)107 void memoryFree(void *pointer) {
108 if (isInDram(pointer)) {
109 vPortDramFree(pointer);
110 DramVoteClientSingleton::get()->decrementDramVoteCount();
111 } else {
112 vPortFree(pointer);
113 }
114 }
115 } // namespace chre
116