1 /*
2 * Copyright (c) 2011-2020, The Linux Foundation. All rights reserved.
3
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <log/log.h>
31 #include <cutils/properties.h>
32 #include <algorithm>
33 #include <vector>
34
35 #include "gr_allocator.h"
36 #include "gr_utils.h"
37 #include "gralloc_priv.h"
38
39 #include "qd_utils.h"
40
41 #ifndef ION_FLAG_CP_PIXEL
42 #define ION_FLAG_CP_PIXEL 0
43 #endif
44
45 #ifndef ION_FLAG_ALLOW_NON_CONTIG
46 #define ION_FLAG_ALLOW_NON_CONTIG 0
47 #endif
48
49 #ifndef ION_FLAG_CP_CAMERA_PREVIEW
50 #define ION_FLAG_CP_CAMERA_PREVIEW 0
51 #endif
52
53 #ifndef ION_SECURE
54 #define ION_SECURE ION_FLAG_SECURE
55 #endif
56
57 #ifndef ION_FLAG_CP_CDSP
58 #define ION_FLAG_CP_CDSP 0
59 #endif
60
61 // b/130367754 added support for EXT_DSP flag in ION allocator
62 // This ION flag allows to use EXT_DSR(RO)+SECCAM(RW)->HLOS(RWX) security rule.
63 // Set EXT_DSP flag when we use secure camera datapath.
64 // Later we probably have a way to set this flag from upper layers (like HAL).
65 #ifndef ION_FLAG_CP_DSP_EXT
66 #define ION_FLAG_CP_DSP_EXT 0
67 #endif
68
69 #ifdef SLAVE_SIDE_CP
70 #define CP_HEAP_ID ION_CP_MM_HEAP_ID
71 #define SD_HEAP_ID CP_HEAP_ID
72 #define ION_CP_FLAGS (ION_SECURE | ION_FLAG_ALLOW_NON_CONTIG)
73 #define ION_SD_FLAGS ION_SECURE
74 #define ION_SC_FLAGS ION_SECURE
75 #define ION_SC_PREVIEW_FLAGS ION_SECURE
76 #else // MASTER_SIDE_CP
77 #ifdef HYPERVISOR
78 #define CP_HEAP_ID ION_SECURE_DISPLAY_HEAP_ID
79 #else
80 #define CP_HEAP_ID ION_SECURE_HEAP_ID
81 #endif
82 #define SD_HEAP_ID ION_SECURE_DISPLAY_HEAP_ID
83 #define ION_CP_FLAGS (ION_SECURE | ION_FLAG_CP_PIXEL)
84 #define ION_SD_FLAGS (ION_SECURE | ION_FLAG_CP_SEC_DISPLAY)
85 #define ION_SC_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA | ION_FLAG_CP_DSP_EXT)
86 #define ION_SC_PREVIEW_FLAGS (ION_SECURE | ION_FLAG_CP_CAMERA_PREVIEW)
87 #endif
88
89 using std::shared_ptr;
90 using std::vector;
91
92 namespace gralloc {
93
GetBufferInfo(const BufferDescriptor & descriptor)94 static BufferInfo GetBufferInfo(const BufferDescriptor &descriptor) {
95 return BufferInfo(descriptor.GetWidth(), descriptor.GetHeight(), descriptor.GetFormat(),
96 descriptor.GetUsage());
97 }
98
Allocator()99 Allocator::Allocator() : ion_allocator_(nullptr) {}
100
Init()101 bool Allocator::Init() {
102 ion_allocator_ = new IonAlloc();
103
104 if (!ion_allocator_->Init()) {
105 return false;
106 }
107
108 return true;
109 }
110
~Allocator()111 Allocator::~Allocator() {
112 if (ion_allocator_) {
113 delete ion_allocator_;
114 }
115 }
116
SetProperties(gralloc::GrallocProperties props)117 void Allocator::SetProperties(gralloc::GrallocProperties props) {
118 use_system_heap_for_sensors_ = props.use_system_heap_for_sensors;
119 }
120
AllocateMem(AllocData * alloc_data,uint64_t usage,int format)121 int Allocator::AllocateMem(AllocData *alloc_data, uint64_t usage, int format) {
122 int ret;
123 alloc_data->uncached = UseUncached(format, usage);
124
125 // After this point we should have the right heap set, there is no fallback
126 GetIonHeapInfo(usage, &alloc_data->heap_id, &alloc_data->alloc_type, &alloc_data->flags);
127
128 ret = ion_allocator_->AllocBuffer(alloc_data);
129 if (ret >= 0) {
130 alloc_data->alloc_type |= private_handle_t::PRIV_FLAGS_USES_ION;
131 } else {
132 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x", __FUNCTION__,
133 alloc_data->heap_id, alloc_data->flags);
134 }
135
136 return ret;
137 }
138
MapBuffer(void ** base,unsigned int size,unsigned int offset,int fd)139 int Allocator::MapBuffer(void **base, unsigned int size, unsigned int offset, int fd) {
140 if (ion_allocator_) {
141 return ion_allocator_->MapBuffer(base, size, offset, fd);
142 }
143
144 return -EINVAL;
145 }
146
ImportBuffer(int fd)147 int Allocator::ImportBuffer(int fd) {
148 if (ion_allocator_) {
149 return ion_allocator_->ImportBuffer(fd);
150 }
151 return -EINVAL;
152 }
153
FreeBuffer(void * base,unsigned int size,unsigned int offset,int fd,int handle)154 int Allocator::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd, int handle) {
155 if (ion_allocator_) {
156 return ion_allocator_->FreeBuffer(base, size, offset, fd, handle);
157 }
158
159 return -EINVAL;
160 }
161
CleanBuffer(void * base,unsigned int size,unsigned int offset,int handle,int op,int fd)162 int Allocator::CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op,
163 int fd) {
164 if (ion_allocator_) {
165 return ion_allocator_->CleanBuffer(base, size, offset, handle, op, fd);
166 }
167
168 return -EINVAL;
169 }
170
CheckForBufferSharing(uint32_t num_descriptors,const vector<shared_ptr<BufferDescriptor>> & descriptors,ssize_t * max_index)171 bool Allocator::CheckForBufferSharing(uint32_t num_descriptors,
172 const vector<shared_ptr<BufferDescriptor>> &descriptors,
173 ssize_t *max_index) {
174 unsigned int cur_heap_id = 0, prev_heap_id = 0;
175 unsigned int cur_alloc_type = 0, prev_alloc_type = 0;
176 unsigned int cur_ion_flags = 0, prev_ion_flags = 0;
177 bool cur_uncached = false, prev_uncached = false;
178 unsigned int alignedw, alignedh;
179 unsigned int max_size = 0;
180
181 *max_index = -1;
182 for (uint32_t i = 0; i < num_descriptors; i++) {
183 // Check Cached vs non-cached and all the ION flags
184 cur_uncached = UseUncached(descriptors[i]->GetFormat(), descriptors[i]->GetUsage());
185 GetIonHeapInfo(descriptors[i]->GetUsage(), &cur_heap_id, &cur_alloc_type, &cur_ion_flags);
186
187 if (i > 0 && (cur_heap_id != prev_heap_id || cur_alloc_type != prev_alloc_type ||
188 cur_ion_flags != prev_ion_flags)) {
189 return false;
190 }
191
192 // For same format type, find the descriptor with bigger size
193 GetAlignedWidthAndHeight(GetBufferInfo(*descriptors[i]), &alignedw, &alignedh);
194 unsigned int size = GetSize(GetBufferInfo(*descriptors[i]), alignedw, alignedh);
195 if (max_size < size) {
196 *max_index = INT(i);
197 max_size = size;
198 }
199
200 prev_heap_id = cur_heap_id;
201 prev_uncached = cur_uncached;
202 prev_ion_flags = cur_ion_flags;
203 prev_alloc_type = cur_alloc_type;
204 }
205
206 return true;
207 }
208
GetIonHeapInfo(uint64_t usage,unsigned int * ion_heap_id,unsigned int * alloc_type,unsigned int * ion_flags)209 void Allocator::GetIonHeapInfo(uint64_t usage, unsigned int *ion_heap_id, unsigned int *alloc_type,
210 unsigned int *ion_flags) {
211 unsigned int heap_id = 0;
212 unsigned int type = 0;
213 uint32_t flags = 0;
214 if (usage & GRALLOC_USAGE_PROTECTED) {
215 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
216 heap_id = ION_HEAP(SD_HEAP_ID);
217 /*
218 * There is currently no flag in ION for Secure Display
219 * VM. Please add it to the define once available.
220 */
221 flags |= UINT(ION_SD_FLAGS);
222 } else if (usage & BufferUsage::CAMERA_OUTPUT) {
223 heap_id = ION_HEAP(SD_HEAP_ID);
224 if (usage & GRALLOC_USAGE_PRIVATE_CDSP) {
225 flags |= UINT(ION_SECURE | ION_FLAG_CP_CDSP);
226 }
227 if (usage & BufferUsage::COMPOSER_OVERLAY) {
228 flags |= UINT(ION_SC_PREVIEW_FLAGS);
229 } else {
230 flags |= UINT(ION_SC_FLAGS);
231 }
232 } else if (usage & GRALLOC_USAGE_PRIVATE_CDSP) {
233 heap_id = ION_HEAP(ION_SECURE_CARVEOUT_HEAP_ID);
234 flags |= UINT(ION_SECURE | ION_FLAG_CP_CDSP);
235 } else {
236 heap_id = ION_HEAP(CP_HEAP_ID);
237 flags |= UINT(ION_CP_FLAGS);
238 }
239 }
240
241 if (usage & BufferUsage::SENSOR_DIRECT_DATA) {
242 if (use_system_heap_for_sensors_) {
243 ALOGI("gralloc::sns_direct_data with system_heap");
244 heap_id |= ION_HEAP(ION_SYSTEM_HEAP_ID);
245 } else {
246 ALOGI("gralloc::sns_direct_data with adsp_heap");
247 heap_id |= ION_HEAP(ION_ADSP_HEAP_ID);
248 }
249 }
250
251 if (flags & UINT(ION_SECURE)) {
252 type |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
253 }
254
255 // if no ion heap flags are set, default to system heap
256 if (!heap_id) {
257 heap_id = ION_HEAP(ION_SYSTEM_HEAP_ID);
258 }
259
260 *alloc_type = type;
261 *ion_flags = flags;
262 *ion_heap_id = heap_id;
263
264 return;
265 }
266 } // namespace gralloc
267