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