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 #include "sensorhub/heap.h"
34
35 #ifdef __cplusplus
36 } // extern "C"
37 #endif
38
39 namespace chre {
40
41 // On tinysys voting/devoting dram are done automatically by the platform APIs
42 // so issueDramVote() should be a no-op.
issueDramVote(bool)43 void DramVoteClient::issueDramVote(bool /*enabled*/) {}
44
45 // no-op since the dma access is controlled by the kernel automatically
forceDramAccess()46 void forceDramAccess() {}
47
nanoappBinaryFree(void * pointer)48 void nanoappBinaryFree(void *pointer) {
49 aligned_free(pointer);
50 }
51
nanoappBinaryDramFree(void * pointer)52 void nanoappBinaryDramFree(void *pointer) {
53 aligned_dram_free(pointer);
54 }
55
memoryAllocDram(size_t size)56 void *memoryAllocDram(size_t size) {
57 return pvPortDramMalloc(size);
58 }
59
memoryFreeDram(void * pointer)60 void memoryFreeDram(void *pointer) {
61 vPortDramFree(pointer);
62 }
63
palSystemApiMemoryAlloc(size_t size)64 void *palSystemApiMemoryAlloc(size_t size) {
65 return memoryAlloc(size);
66 }
67
palSystemApiMemoryFree(void * pointer)68 void palSystemApiMemoryFree(void *pointer) {
69 memoryFree(pointer);
70 }
71
nanoappBinaryAlloc(size_t size,size_t alignment)72 void *nanoappBinaryAlloc(size_t size, size_t alignment) {
73 return aligned_malloc(size, alignment);
74 }
75
nanoappBinaryDramAlloc(size_t size,size_t alignment)76 void *nanoappBinaryDramAlloc(size_t size, size_t alignment) {
77 // aligned_dram_malloc() requires the alignment being multiple of
78 // CACHE_LINE_SIZE (128 bytes), we will align to page size (4k)
79 return aligned_dram_malloc(size, alignment);
80 }
81
memoryAlloc(size_t size)82 void *memoryAlloc(size_t size) {
83 void *address = heap_alloc(size);
84 if (address == nullptr && size > 0) {
85 // Try dram if allocation from sram fails.
86 // DramVoteClient tracks the duration of the allocations falling back to
87 // dram. The idea is that only transient allocations are allowed to fall
88 // back to dram. Any long-lived allocation should be done explicitly via
89 // corresponding memory allocation APIs.
90 DramVoteClientSingleton::get()->incrementDramVoteCount();
91 address = pvPortDramMalloc(size);
92
93 // DRAM allocation failed too.
94 if (address == nullptr) {
95 DramVoteClientSingleton::get()->decrementDramVoteCount();
96 }
97 }
98 return address;
99 }
100
memoryFree(void * pointer)101 void memoryFree(void *pointer) {
102 if (isInDram(pointer)) {
103 vPortDramFree(pointer);
104 DramVoteClientSingleton::get()->decrementDramVoteCount();
105 } else {
106 heap_free(pointer);
107 }
108 }
109 } // namespace chre
110