1 /*
2  * Copyright (C) 2017 ARM Limited. All rights reserved.
3  *
4  * Copyright (C) 2008 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #ifndef MALI_GRALLOC_BUFFER_H_
19 #define MALI_GRALLOC_BUFFER_H_
20 
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <sys/mman.h>
25 
26 #include "mali_gralloc_private_interface_types.h"
27 
28 /* NOTE:
29  * If your framebuffer device driver is integrated with dma_buf, you will have to
30  * change this IOCTL definition to reflect your integration with the framebuffer
31  * device.
32  * Expected return value is a structure filled with a file descriptor
33  * backing your framebuffer device memory.
34  */
35 struct fb_dmabuf_export
36 {
37 	__u32 fd;
38 	__u32 flags;
39 };
40 #define FBIOGET_DMABUF _IOR('F', 0x21, struct fb_dmabuf_export)
41 
42 /* the max string size of GRALLOC_HARDWARE_GPU0 & GRALLOC_HARDWARE_FB0
43  * 8 is big enough for "gpu0" & "fb0" currently
44  */
45 #define MALI_GRALLOC_HARDWARE_MAX_STR_LEN 8
46 #define NUM_FB_BUFFERS 2
47 
48 /* Define number of shared file descriptors */
49 #define GRALLOC_ARM_NUM_FDS 2
50 
51 #define NUM_INTS_IN_PRIVATE_HANDLE ((sizeof(struct private_handle_t) - sizeof(native_handle)) / sizeof(int) - GRALLOC_ARM_NUM_FDS)
52 
53 #define SZ_4K 0x00001000
54 #define SZ_2M 0x00200000
55 
56 struct private_handle_t;
57 
58 #ifndef __cplusplus
59 /* C99 with pedantic don't allow anonymous unions which is used in below struct
60  * Disable pedantic for C for this struct only.
61  */
62 #pragma GCC diagnostic push
63 #pragma GCC diagnostic ignored "-Wpedantic"
64 #endif
65 
66 #ifdef __cplusplus
67 struct private_handle_t : public native_handle
68 {
69 #else
70 struct private_handle_t
71 {
72 	struct native_handle nativeHandle;
73 #endif
74 
75 #ifdef __cplusplus
76 	/* Never intended to be used from C code */
77 	enum
78 	{
79 		PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
80 		PRIV_FLAGS_USES_ION_COMPOUND_HEAP = 0x00000002,
81 		PRIV_FLAGS_USES_ION = 0x00000004,
82 		PRIV_FLAGS_USES_ION_DMA_HEAP = 0x00000008
83 	};
84 
85 	enum
86 	{
87 		LOCK_STATE_WRITE = 1 << 31,
88 		LOCK_STATE_MAPPED = 1 << 30,
89 		LOCK_STATE_READ_MASK = 0x3FFFFFFF
90 	};
91 #endif
92 
93 	/*
94 	 * Shared file descriptor for dma_buf sharing. This must be the first element in the
95 	 * structure so that binder knows where it is and can properly share it between
96 	 * processes.
97 	 * DO NOT MOVE THIS ELEMENT!
98 	 */
99 	int share_fd;
100 	int share_attr_fd;
101 
102 	// ints
103 	int magic;
104 	int req_format;
105 	uint64_t internal_format;
106 	int byte_stride;
107 	int flags;
108 	int size;
109 	int width;
110 	int height;
111 	int format;
112 	int internalWidth;
113 	int internalHeight;
114 	int stride;
115 
116 	/*
117 	 * Multi-layer buffers.
118 	 *
119 	 * Gralloc 1.0 supports multiple image layers within the same
120 	 * buffer allocation, where GRALLOC1_CAPABILITY_LAYERED_BUFFERS is enabled.
121 	 * 'layer_count' defines the number of layers that have been allocated.
122 	 * All layers are the same size (in bytes) and 'size' defines the
123 	 * number of bytes in the whole allocation.
124 	 * Size of each layer = 'size' / 'layer_count'.
125 	 * Offset to nth layer = n * ('size' / 'layer_count'),
126 	 * where n=0 for the first layer.
127 	 */
128 	uint32_t layer_count;
129 	union
130 	{
131 		void *base;
132 		uint64_t padding;
133 	};
134 	int      usage;
135 	uint64_t consumer_usage;
136 	uint64_t producer_usage;
137 	uint64_t backing_store_id;
138 	int backing_store_size;
139 	int writeOwner;
140 	int allocating_pid;
141 	int remote_pid;
142 	int ref_count;
143 	// locally mapped shared attribute area
144 	union
145 	{
146 		void *attr_base;
147 		uint64_t padding3;
148 	};
149 
150 	mali_gralloc_yuv_info yuv_info;
151 
152 	// Following members is for framebuffer only
153 	int fd;
154 	union
155 	{
156 		off_t offset;
157 		uint64_t padding4;
158 	};
159 
160 	/*
161 	 * min_pgsz denotes minimum phys_page size used by this buffer.
162 	 * if buffer memory is physical contiguous set min_pgsz to buff->size
163 	 * if not sure buff's real phys_page size, you can use SZ_4K for safe.
164 	 */
165 	int min_pgsz;
166 
167 	// for request width and height
168 	uint32_t req_width;
169 	uint32_t req_height;
170 	uint64_t padding_1;
171 	uint64_t padding_2;
172 
173 #ifdef __cplusplus
174 	/*
175 	 * We track the number of integers in the structure. There are 16 unconditional
176 	 * integers (magic - pid, yuv_info, fd and offset). Note that the fd element is
177 	 * considered an int not an fd because it is not intended to be used outside the
178 	 * surface flinger process. The GRALLOC_ARM_NUM_INTS variable is used to track the
179 	 * number of integers that are conditionally included. Similar considerations apply
180 	 * to the number of fds.
181 	 */
182 	static const int sNumFds = GRALLOC_ARM_NUM_FDS;
183 	static const int sMagic = 0x3141592;
184 
private_handle_tprivate_handle_t185 	private_handle_t(int _flags, int _size, void *_base, uint64_t _consumer_usage, uint64_t _producer_usage,
186 	                 int fb_file, off_t fb_offset)
187 	    : share_fd(-1)
188 	    , share_attr_fd(-1)
189 	    , magic(sMagic)
190 	    , flags(_flags)
191 	    , size(_size)
192 	    , width(0)
193 	    , height(0)
194 	    , stride(0)
195 	    , layer_count(0)
196 	    , base(_base)
197 	    , consumer_usage(_consumer_usage)
198 	    , producer_usage(_producer_usage)
199 	    , backing_store_id(0x0)
200 	    , backing_store_size(0)
201 	    , writeOwner(0)
202 	    , allocating_pid(getpid())
203 	    , remote_pid(-1)
204 	    , ref_count(1)
205 	    , attr_base(MAP_FAILED)
206 	    , yuv_info(MALI_YUV_NO_INFO)
207 	    , fd(fb_file)
208 	    , offset(fb_offset)
209 	{
210 		version = sizeof(native_handle);
211 		numFds = sNumFds;
212 		numInts = NUM_INTS_IN_PRIVATE_HANDLE;
213 	}
214 
private_handle_tprivate_handle_t215 	private_handle_t(int _flags, int _size, int _min_pgsz, uint64_t _consumer_usage, uint64_t _producer_usage,
216 	                 int _shared_fd, int _req_format, uint64_t _internal_format, int _byte_stride, int _width,
217 	                 int _height, int _stride, int _internalWidth, int _internalHeight, int _backing_store_size,
218 			 uint64_t _layer_count)
219 	    : share_fd(_shared_fd)
220 	    , share_attr_fd(-1)
221 	    , magic(sMagic)
222 	    , req_format(_req_format)
223 	    , internal_format(_internal_format)
224 	    , byte_stride(_byte_stride)
225 	    , flags(_flags)
226 	    , size(_size)
227 	    , width(_width)
228 	    , height(_height)
229 	    , internalWidth(_internalWidth)
230 	    , internalHeight(_internalHeight)
231 	    , stride(_stride)
232 	    , layer_count(_layer_count)
233 	    , base(NULL)
234 	    , consumer_usage(_consumer_usage)
235 	    , producer_usage(_producer_usage)
236 	    , backing_store_id(0x0)
237 	    , backing_store_size(_backing_store_size)
238 	    , writeOwner(0)
239 	    , allocating_pid(getpid())
240 	    , remote_pid(-1)
241 	    , ref_count(1)
242 	    , attr_base(MAP_FAILED)
243 	    , yuv_info(MALI_YUV_NO_INFO)
244 	    , fd(-1)
245 	    , offset(0)
246 	    , min_pgsz(_min_pgsz)
247 	{
248 		version = sizeof(native_handle);
249 		numFds = sNumFds;
250 		numInts = NUM_INTS_IN_PRIVATE_HANDLE;
251 		format  = _req_format;
252 	}
253 
~private_handle_tprivate_handle_t254 	~private_handle_t()
255 	{
256 		magic = 0;
257 	}
258 
usesPhysicallyContiguousMemoryprivate_handle_t259 	bool usesPhysicallyContiguousMemory()
260 	{
261 		return (flags & PRIV_FLAGS_FRAMEBUFFER) ? true : false;
262 	}
263 
validateprivate_handle_t264 	static int validate(const native_handle *h)
265 	{
266 		const private_handle_t *hnd = (const private_handle_t *)h;
267 
268 		if (!h || h->version != sizeof(native_handle) || h->numInts != NUM_INTS_IN_PRIVATE_HANDLE ||
269 		    h->numFds != sNumFds || hnd->magic != sMagic)
270 		{
271 			return -EINVAL;
272 		}
273 
274 		return 0;
275 	}
276 
dynamicCastprivate_handle_t277 	static private_handle_t *dynamicCast(const native_handle *in)
278 	{
279 		if (validate(in) == 0)
280 		{
281 			return (private_handle_t *)in;
282 		}
283 
284 		return NULL;
285 	}
286 #endif
287 };
288 #ifndef __cplusplus
289 /* Restore previous diagnostic for pedantic */
290 #pragma GCC diagnostic pop
291 #endif
292 
293 #endif /* MALI_GRALLOC_BUFFER_H_ */
294