1 /*
2  * Copyright (C) 2010 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 
19 #ifndef GRALLOC_PRIV_H_
20 #define GRALLOC_PRIV_H_
21 
22 #include <stdint.h>
23 #include <pthread.h>
24 #include <errno.h>
25 #include <linux/fb.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 
29 #include <hardware/gralloc.h>
30 #include <cutils/native_handle.h>
31 #include "alloc_device.h"
32 #include <utils/Log.h>
33 
34 #ifdef MALI_600
35 #define GRALLOC_ARM_UMP_MODULE 0
36 #define GRALLOC_ARM_DMA_BUF_MODULE 1
37 #else
38 
39 /* NOTE:
40  * If your framebuffer device driver is integrated with UMP, you will have to
41  * change this IOCTL definition to reflect your integration with the framebuffer
42  * device.
43  * Expected return value is a UMP secure id backing your framebuffer device memory.
44  */
45 
46 /*#define IOCTL_GET_FB_UMP_SECURE_ID    _IOR('F', 311, unsigned int)*/
47 #define GRALLOC_ARM_UMP_MODULE 0
48 #define GRALLOC_ARM_DMA_BUF_MODULE 1
49 
50 /* NOTE:
51  * If your framebuffer device driver is integrated with dma_buf, you will have to
52  * change this IOCTL definition to reflect your integration with the framebuffer
53  * device.
54  * Expected return value is a structure filled with a file descriptor
55  * backing your framebuffer device memory.
56  */
57 #if GRALLOC_ARM_DMA_BUF_MODULE
58 struct fb_dmabuf_export
59 {
60 	__u32 fd;
61 	__u32 flags;
62 };
63 
64 /* Un-comment this line to use dma_buf framebuffer */
65 /*#define FBIOGET_DMABUF    _IOR('F', 0x21, struct fb_dmabuf_export)*/
66 
67 #if PLATFORM_SDK_VERSION >= 21
68 typedef int ion_user_handle_t;
69 #define ION_INVALID_HANDLE 0
70 #else
71 
72 typedef struct ion_handle *ion_user_handle_t;
73 
74 #define ION_INVALID_HANDLE NULL
75 #endif /* new libion */
76 
77 #endif /* GRALLOC_ARM_DMA_BUF_MODULE */
78 
79 
80 #endif
81 
82 /* the max string size of GRALLOC_HARDWARE_GPU0 & GRALLOC_HARDWARE_FB0
83  * 8 is big enough for "gpu0" & "fb0" currently
84  */
85 #define MALI_GRALLOC_HARDWARE_MAX_STR_LEN 8
86 #define NUM_FB_BUFFERS 2
87 
88 #if GRALLOC_ARM_UMP_MODULE
89 #include <ump/ump.h>
90 #endif
91 
92 #define MALI_IGNORE(x) (void)x
93 typedef enum
94 {
95 	MALI_YUV_NO_INFO,
96 	MALI_YUV_BT601_NARROW,
97 	MALI_YUV_BT601_WIDE,
98 	MALI_YUV_BT709_NARROW,
99 	MALI_YUV_BT709_WIDE,
100 } mali_gralloc_yuv_info;
101 
102 struct private_handle_t;
103 
104 struct private_module_t
105 {
106 	gralloc_module_t base;
107 
108 	private_handle_t *framebuffer;
109 	uint32_t flags;
110 	uint32_t numBuffers;
111 	uint32_t bufferMask;
112 	pthread_mutex_t lock;
113 	buffer_handle_t currentBuffer;
114 	int ion_client;
115 	int system_heap_id;
116 	int cma_heap_id;
117 	bool gralloc_legacy_ion;
118 
119 	struct fb_var_screeninfo info;
120 	struct fb_fix_screeninfo finfo;
121 	float xdpi;
122 	float ydpi;
123 	float fps;
124 
125 	enum
126 	{
127 		// flag to indicate we'll post this buffer
128 		PRIV_USAGE_LOCKED_FOR_POST = 0x80000000
129 	};
130 
131 	/* default constructor */
132 	private_module_t();
133 };
134 
135 #ifdef __cplusplus
136 struct private_handle_t : public native_handle
137 {
138 #else
139 struct private_handle_t
140 {
141 	struct native_handle nativeHandle;
142 #endif
143 
144 	enum
145 	{
146 		PRIV_FLAGS_FRAMEBUFFER = 0x00000001,
147 		PRIV_FLAGS_USES_UMP    = 0x00000002,
148 		PRIV_FLAGS_USES_ION    = 0x00000004,
149 	};
150 
151 	enum
152 	{
153 		LOCK_STATE_WRITE     =   1 << 31,
154 		LOCK_STATE_MAPPED    =   1 << 30,
155 		LOCK_STATE_UNREGISTERED  =   1 << 29,
156 		LOCK_STATE_READ_MASK =   0x3FFFFFFF
157 	};
158 
159 	// ints
160 #if GRALLOC_ARM_DMA_BUF_MODULE
161 	/*shared file descriptor for dma_buf sharing*/
162 	int     share_fd;
163 #endif
164 	int     magic;
165 	int     flags;
166 	int     usage;
167 	int     size;
168 	int     width;
169 	int     height;
170 	union {
171 		int     format;
172 		int	req_format; /* same name as gralloc960 */
173 		/*unused but referenced in drm_hwc */
174 		int	internal_format;
175 	};
176 	int     stride;
177 	union
178 	{
179 		void   *base;
180 		uint64_t padding;
181 	};
182 	int     lockState;
183 	int     writeOwner;
184 	int     pid;
185 
186 	mali_gralloc_yuv_info yuv_info;
187 
188 	// Following members are for UMP memory only
189 #if GRALLOC_ARM_UMP_MODULE
190 	int     ump_id;
191 	int     ump_mem_handle;
192 #endif
193 
194 	// Following members is for framebuffer only
195 	int     fd; //Shallow copy, DO NOT duplicate
196 	int     offset;
197 	union
198 	{
199 		void *fb_paddr;
200 		uint64_t fb_paddr_padding;
201 	};
202 	int	byte_stride;
203 #if GRALLOC_ARM_DMA_BUF_MODULE
204 	ion_user_handle_t ion_hnd;
205 #endif
206 #if GRALLOC_ARM_DMA_BUF_MODULE
207 #define GRALLOC_ARM_NUM_FDS 1
208 #else
209 #define GRALLOC_ARM_NUM_FDS 0
210 #endif
211 
212 #ifdef __cplusplus
213 	static const int sNumFds = GRALLOC_ARM_NUM_FDS;
214 	static const int sMagic = 0x3141592;
215 
216 #if GRALLOC_ARM_UMP_MODULE
private_handle_tprivate_handle_t217 	private_handle_t(int flags, int usage, int size, void *base, int lock_state, ump_secure_id secure_id, ump_handle handle):
218 #if GRALLOC_ARM_DMA_BUF_MODULE
219 		share_fd(-1),
220 #endif
221 		magic(sMagic),
222 		flags(flags),
223 		usage(usage),
224 		size(size),
225 		width(0),
226 		height(0),
227 		format(0),
228 		stride(0),
229 		base(base),
230 		lockState(lock_state),
231 		writeOwner(0),
232 		pid(getpid()),
233 		yuv_info(MALI_YUV_NO_INFO),
234 		ump_id((int)secure_id),
235 		ump_mem_handle((int)handle),
236 		fd(0),
237 		offset(0),
238 		fb_paddr(NULL)
239 #if GRALLOC_ARM_DMA_BUF_MODULE
240 		,
241 		ion_hnd(ION_INVALID_HANDLE)
242 #endif
243 
244 	{
245 		version = sizeof(native_handle);
246 		numFds = sNumFds;
247 		numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
248 	}
249 #endif
250 
251 #if GRALLOC_ARM_DMA_BUF_MODULE
private_handle_tprivate_handle_t252 	private_handle_t(int flags, int usage, int size, void *base, int lock_state):
253 		share_fd(-1),
254 		magic(sMagic),
255 		flags(flags),
256 		usage(usage),
257 		size(size),
258 		width(0),
259 		height(0),
260 		format(0),
261 		stride(0),
262 		base(base),
263 		lockState(lock_state),
264 		writeOwner(0),
265 		pid(getpid()),
266 		yuv_info(MALI_YUV_NO_INFO),
267 #if GRALLOC_ARM_UMP_MODULE
268 		ump_id((int)UMP_INVALID_SECURE_ID),
269 		ump_mem_handle((int)UMP_INVALID_MEMORY_HANDLE),
270 #endif
271 		fd(0),
272 		offset(0),
273 		fb_paddr(NULL),
274 		ion_hnd(ION_INVALID_HANDLE)
275 
276 	{
277 		version = sizeof(native_handle);
278 		numFds = sNumFds;
279 		numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
280 	}
281 
282 #endif
283 
private_handle_tprivate_handle_t284 	private_handle_t(int flags, int usage, int size, void *base, int lock_state, int fb_file, int fb_offset, void *fb_paddr):
285 #if GRALLOC_ARM_DMA_BUF_MODULE
286 		share_fd(-1),
287 #endif
288 		magic(sMagic),
289 		flags(flags),
290 		usage(usage),
291 		size(size),
292 		width(0),
293 		height(0),
294 		format(0),
295 		stride(0),
296 		base(base),
297 		lockState(lock_state),
298 		writeOwner(0),
299 		pid(getpid()),
300 		yuv_info(MALI_YUV_NO_INFO),
301 #if GRALLOC_ARM_UMP_MODULE
302 		ump_id((int)UMP_INVALID_SECURE_ID),
303 		ump_mem_handle((int)UMP_INVALID_MEMORY_HANDLE),
304 #endif
305 		fd(fb_file),
306 		offset(fb_offset),
307 		fb_paddr(fb_paddr)
308 #if GRALLOC_ARM_DMA_BUF_MODULE
309 		,
310 		ion_hnd(ION_INVALID_HANDLE)
311 #endif
312 
313 	{
314 		version = sizeof(native_handle);
315 		numFds = sNumFds;
316 		numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
317 	}
318 
~private_handle_tprivate_handle_t319 	~private_handle_t()
320 	{
321 		magic = 0;
322 	}
323 
usesPhysicallyContiguousMemoryprivate_handle_t324 	bool usesPhysicallyContiguousMemory()
325 	{
326 		return (flags & PRIV_FLAGS_FRAMEBUFFER) ? true : false;
327 	}
328 
validateprivate_handle_t329 	static int validate(const native_handle *h)
330 	{
331 		const private_handle_t *hnd = (const private_handle_t *)h;
332 
333 		if (!hnd || hnd->version != sizeof(native_handle) || hnd->magic != sMagic)
334 		{
335 			return -EINVAL;
336 		}
337 
338 		int numFds = sNumFds;
339 		int numInts = (sizeof(private_handle_t) - sizeof(native_handle)) / sizeof(int) - sNumFds;
340 
341 #if GRALLOC_ARM_DMA_BUF_MODULE
342 		if (hnd->share_fd < 0)
343 		{
344 			numFds--;
345 			numInts++;
346 		}
347 #endif
348 
349 		if (hnd->numFds != numFds || hnd->numInts != numInts)
350 		{
351 			return -EINVAL;
352 		}
353 
354 		return 0;
355 	}
356 
dynamicCastprivate_handle_t357 	static private_handle_t *dynamicCast(const native_handle *in)
358 	{
359 		if (validate(in) == 0)
360 		{
361 			return (private_handle_t *) in;
362 		}
363 
364 		return NULL;
365 	}
366 #endif
367 };
368 
369 #endif /* GRALLOC_PRIV_H_ */
370