xref: /aosp_15_r20/system/chre/platform/tinysys/memory.cc (revision 84e339476a462649f82315436d70fd732297a399)
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   aligned_free(pointer);
49 }
50 
nanoappBinaryDramFree(void * pointer)51 void nanoappBinaryDramFree(void *pointer) {
52   aligned_dram_free(pointer);
53 }
54 
memoryAllocDram(size_t size)55 void *memoryAllocDram(size_t size) {
56   return pvPortDramMalloc(size);
57 }
58 
memoryFreeDram(void * pointer)59 void memoryFreeDram(void *pointer) {
60   vPortDramFree(pointer);
61 }
62 
palSystemApiMemoryAlloc(size_t size)63 void *palSystemApiMemoryAlloc(size_t size) {
64   return memoryAlloc(size);
65 }
66 
palSystemApiMemoryFree(void * pointer)67 void palSystemApiMemoryFree(void *pointer) {
68   memoryFree(pointer);
69 }
70 
nanoappBinaryAlloc(size_t size,size_t alignment)71 void *nanoappBinaryAlloc(size_t size, size_t alignment) {
72   return aligned_malloc(size, alignment);
73 }
74 
nanoappBinaryDramAlloc(size_t size,size_t alignment)75 void *nanoappBinaryDramAlloc(size_t size, size_t alignment) {
76   // aligned_dram_malloc() requires the alignment being multiple of
77   // CACHE_LINE_SIZE (128 bytes), we will align to page size (4k)
78   return aligned_dram_malloc(size, alignment);
79 }
80 
memoryAlloc(size_t size)81 void *memoryAlloc(size_t size) {
82   void *address = pvPortMalloc(size);
83   if (address == nullptr && size > 0) {
84     // Try dram if allocation from sram fails.
85     // DramVoteClient tracks the duration of the allocations falling back to
86     // dram. The idea is that only transient allocations are allowed to fall
87     // back to dram. Any long-lived allocation should be done explicitly via
88     // corresponding memory allocation APIs.
89     DramVoteClientSingleton::get()->incrementDramVoteCount();
90     address = pvPortDramMalloc(size);
91 
92     // DRAM allocation failed too.
93     if (address == nullptr) {
94       DramVoteClientSingleton::get()->decrementDramVoteCount();
95     }
96   }
97   return address;
98 }
99 
memoryFree(void * pointer)100 void memoryFree(void *pointer) {
101   if (isInDram(pointer)) {
102     vPortDramFree(pointer);
103     DramVoteClientSingleton::get()->decrementDramVoteCount();
104   } else {
105     vPortFree(pointer);
106   }
107 }
108 }  // namespace chre
109