1 /*
2 * Copyright (c) 2011-2018, 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 char property[PROPERTY_VALUE_MAX];
104 property_get(USE_SYSTEM_HEAP_FOR_SENSORS, property, "1");
105 if (!(strncmp(property, "0", PROPERTY_VALUE_MAX))) {
106 use_system_heap_for_sensors_ = false;
107 }
108
109 if (!ion_allocator_->Init()) {
110 return false;
111 }
112
113 return true;
114 }
115
~Allocator()116 Allocator::~Allocator() {
117 if (ion_allocator_) {
118 delete ion_allocator_;
119 }
120 }
121
AllocateMem(AllocData * alloc_data,uint64_t usage,int format)122 int Allocator::AllocateMem(AllocData *alloc_data, uint64_t usage, int format) {
123 int ret;
124 alloc_data->uncached = UseUncached(format, usage);
125
126 // After this point we should have the right heap set, there is no fallback
127 GetIonHeapInfo(usage, &alloc_data->heap_id, &alloc_data->alloc_type, &alloc_data->flags);
128
129 ret = ion_allocator_->AllocBuffer(alloc_data);
130 if (ret >= 0) {
131 alloc_data->alloc_type |= private_handle_t::PRIV_FLAGS_USES_ION;
132 } else {
133 ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x", __FUNCTION__,
134 alloc_data->heap_id, alloc_data->flags);
135 }
136
137 return ret;
138 }
139
MapBuffer(void ** base,unsigned int size,unsigned int offset,int fd)140 int Allocator::MapBuffer(void **base, unsigned int size, unsigned int offset, int fd) {
141 if (ion_allocator_) {
142 return ion_allocator_->MapBuffer(base, size, offset, fd);
143 }
144
145 return -EINVAL;
146 }
147
ImportBuffer(int fd)148 int Allocator::ImportBuffer(int fd) {
149 if (ion_allocator_) {
150 return ion_allocator_->ImportBuffer(fd);
151 }
152 return -EINVAL;
153 }
154
FreeBuffer(void * base,unsigned int size,unsigned int offset,int fd,int handle)155 int Allocator::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd, int handle) {
156 if (ion_allocator_) {
157 return ion_allocator_->FreeBuffer(base, size, offset, fd, handle);
158 }
159
160 return -EINVAL;
161 }
162
CleanBuffer(void * base,unsigned int size,unsigned int offset,int handle,int op,int fd)163 int Allocator::CleanBuffer(void *base, unsigned int size, unsigned int offset, int handle, int op,
164 int fd) {
165 if (ion_allocator_) {
166 return ion_allocator_->CleanBuffer(base, size, offset, handle, op, fd);
167 }
168
169 return -EINVAL;
170 }
171
CheckForBufferSharing(uint32_t num_descriptors,const vector<shared_ptr<BufferDescriptor>> & descriptors,ssize_t * max_index)172 bool Allocator::CheckForBufferSharing(uint32_t num_descriptors,
173 const vector<shared_ptr<BufferDescriptor>> &descriptors,
174 ssize_t *max_index) {
175 unsigned int cur_heap_id = 0, prev_heap_id = 0;
176 unsigned int cur_alloc_type = 0, prev_alloc_type = 0;
177 unsigned int cur_ion_flags = 0, prev_ion_flags = 0;
178 bool cur_uncached = false, prev_uncached = false;
179 unsigned int alignedw, alignedh;
180 unsigned int max_size = 0;
181
182 *max_index = -1;
183 for (uint32_t i = 0; i < num_descriptors; i++) {
184 // Check Cached vs non-cached and all the ION flags
185 cur_uncached = UseUncached(descriptors[i]->GetFormat(), descriptors[i]->GetUsage());
186 GetIonHeapInfo(descriptors[i]->GetUsage(), &cur_heap_id, &cur_alloc_type, &cur_ion_flags);
187
188 if (i > 0 && (cur_heap_id != prev_heap_id || cur_alloc_type != prev_alloc_type ||
189 cur_ion_flags != prev_ion_flags)) {
190 return false;
191 }
192
193 // For same format type, find the descriptor with bigger size
194 GetAlignedWidthAndHeight(GetBufferInfo(*descriptors[i]), &alignedw, &alignedh);
195 unsigned int size = GetSize(GetBufferInfo(*descriptors[i]), alignedw, alignedh);
196 if (max_size < size) {
197 *max_index = INT(i);
198 max_size = size;
199 }
200
201 prev_heap_id = cur_heap_id;
202 prev_uncached = cur_uncached;
203 prev_ion_flags = cur_ion_flags;
204 prev_alloc_type = cur_alloc_type;
205 }
206
207 return true;
208 }
209
GetIonHeapInfo(uint64_t usage,unsigned int * ion_heap_id,unsigned int * alloc_type,unsigned int * ion_flags)210 void Allocator::GetIonHeapInfo(uint64_t usage, unsigned int *ion_heap_id, unsigned int *alloc_type,
211 unsigned int *ion_flags) {
212 unsigned int heap_id = 0;
213 unsigned int type = 0;
214 uint32_t flags = 0;
215 if (usage & GRALLOC_USAGE_PROTECTED) {
216 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
217 heap_id = ION_HEAP(SD_HEAP_ID);
218 /*
219 * There is currently no flag in ION for Secure Display
220 * VM. Please add it to the define once available.
221 */
222 flags |= UINT(ION_SD_FLAGS);
223 } else if (usage & BufferUsage::CAMERA_OUTPUT) {
224 heap_id = ION_HEAP(SD_HEAP_ID);
225 if (usage & GRALLOC_USAGE_PRIVATE_CDSP) {
226 flags |= UINT(ION_SECURE | ION_FLAG_CP_CDSP);
227 }
228 if (usage & BufferUsage::COMPOSER_OVERLAY) {
229 flags |= UINT(ION_SC_PREVIEW_FLAGS);
230 } else {
231 flags |= UINT(ION_SC_FLAGS);
232 }
233 } else if (usage & GRALLOC_USAGE_PRIVATE_CDSP) {
234 heap_id = ION_HEAP(ION_SECURE_CARVEOUT_HEAP_ID);
235 flags |= UINT(ION_SECURE | ION_FLAG_CP_CDSP);
236 } else {
237 heap_id = ION_HEAP(CP_HEAP_ID);
238 flags |= UINT(ION_CP_FLAGS);
239 }
240 }
241
242 if (usage & BufferUsage::SENSOR_DIRECT_DATA) {
243 if (use_system_heap_for_sensors_) {
244 ALOGI("gralloc::sns_direct_data with system_heap");
245 heap_id |= ION_HEAP(ION_SYSTEM_HEAP_ID);
246 } else {
247 ALOGI("gralloc::sns_direct_data with adsp_heap");
248 heap_id |= ION_HEAP(ION_ADSP_HEAP_ID);
249 }
250 }
251
252 if (flags & UINT(ION_SECURE)) {
253 type |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER;
254 }
255
256 // if no ion heap flags are set, default to system heap
257 if (!heap_id) {
258 heap_id = ION_HEAP(ION_SYSTEM_HEAP_ID);
259 }
260
261 *alloc_type = type;
262 *ion_flags = flags;
263 *ion_heap_id = heap_id;
264
265 return;
266 }
267 } // namespace gralloc
268