• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 The Chromium OS Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6 
7 #ifdef DRV_MEDIATEK
8 
9 // clang-format off
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <inttypes.h>
13 #include <poll.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <sys/mman.h>
17 #include <unistd.h>
18 #include <xf86drm.h>
19 #include <mediatek_drm.h>
20 // clang-format on
21 
22 #include "drv_helpers.h"
23 #include "drv_priv.h"
24 #include "util.h"
25 
26 #define TILE_TYPE_LINEAR 0
27 
28 // clang-format off
29 #if defined(MTK_MT8183) || \
30     defined(MTK_MT8186)
31 // clang-format on
32 #define SUPPORTS_YUV422
33 #endif
34 
35 // All platforms except MT8173 should USE_NV12_FOR_HW_VIDEO_DECODING.
36 // clang-format off
37 #if defined(MTK_MT8183) || \
38     defined(MTK_MT8186) || \
39     defined(MTK_MT8188G) || \
40     defined(MTK_MT8192) || \
41     defined(MTK_MT8195)
42 // clang-format on
43 #define USE_NV12_FOR_HW_VIDEO_DECODING
44 #else
45 #define DONT_USE_64_ALIGNMENT_FOR_VIDEO_BUFFERS
46 #endif
47 
48 // For Mali Sigurd based GPUs, the texture unit reads outside the specified texture dimensions.
49 // Therefore, certain formats require extra memory padding to its allocated surface to prevent the
50 // hardware from reading outside an allocation. For YVU420, we need additional padding for the last
51 // chroma plane.
52 #if defined(MTK_MT8186)
53 #define USE_EXTRA_PADDING_FOR_YVU420
54 #endif
55 
56 struct mediatek_private_map_data {
57 	void *cached_addr;
58 	void *gem_addr;
59 	int prime_fd;
60 };
61 
62 static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
63 						  DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
64 						  DRM_FORMAT_XRGB8888 };
65 
66 // clang-format off
67 static const uint32_t texture_source_formats[] = {
68 #ifdef SUPPORTS_YUV422
69 	DRM_FORMAT_NV21,
70 	DRM_FORMAT_YUYV,
71 #endif
72 	DRM_FORMAT_ABGR2101010,
73 	DRM_FORMAT_ABGR16161616F,
74 	DRM_FORMAT_NV12,
75 	DRM_FORMAT_YVU420,
76 	DRM_FORMAT_YVU420_ANDROID
77 };
78 
79 static const uint32_t video_yuv_formats[] = {
80 	DRM_FORMAT_NV21,
81 	DRM_FORMAT_NV12,
82 	DRM_FORMAT_YUYV,
83 	DRM_FORMAT_YVU420,
84 	DRM_FORMAT_YVU420_ANDROID
85 };
86 // clang-format on
87 
is_video_yuv_format(uint32_t format)88 static bool is_video_yuv_format(uint32_t format)
89 {
90 	size_t i;
91 	for (i = 0; i < ARRAY_SIZE(video_yuv_formats); ++i) {
92 		if (format == video_yuv_formats[i])
93 			return true;
94 	}
95 	return false;
96 }
97 
mediatek_init(struct driver * drv)98 static int mediatek_init(struct driver *drv)
99 {
100 	struct format_metadata metadata;
101 
102 	drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
103 			     &LINEAR_METADATA, BO_USE_RENDER_MASK | BO_USE_SCANOUT);
104 
105 	drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
106 			     &LINEAR_METADATA, BO_USE_TEXTURE_MASK);
107 
108 	drv_add_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA, BO_USE_SW_MASK | BO_USE_LINEAR);
109 
110 	/* YUYV format for video overlay and camera subsystem. */
111 	drv_add_combination(drv, DRM_FORMAT_YUYV, &LINEAR_METADATA,
112 			    BO_USE_HW_VIDEO_DECODER | BO_USE_SCANOUT | BO_USE_LINEAR |
113 				BO_USE_TEXTURE);
114 
115 	/* Android CTS tests require this. */
116 	drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
117 
118 	/* Support BO_USE_HW_VIDEO_DECODER for protected content minigbm allocations. */
119 	metadata.tiling = TILE_TYPE_LINEAR;
120 	metadata.priority = 1;
121 	metadata.modifier = DRM_FORMAT_MOD_LINEAR;
122 	drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata, BO_USE_HW_VIDEO_DECODER);
123 	drv_modify_combination(drv, DRM_FORMAT_YVU420_ANDROID, &metadata, BO_USE_HW_VIDEO_DECODER);
124 #ifdef USE_NV12_FOR_HW_VIDEO_DECODING
125 	// TODO(hiroh): Switch to use NV12 for video decoder on MT8173 as well.
126 	drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata, BO_USE_HW_VIDEO_DECODER);
127 #endif
128 
129 	/*
130 	 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB for input/output from
131 	 * hardware decoder/encoder.
132 	 */
133 	drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
134 			       BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER |
135 				   BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
136 				   BO_USE_GPU_DATA_BUFFER | BO_USE_SENSOR_DIRECT_DATA);
137 
138 	/* NV12 format for encoding and display. */
139 	drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
140 			       BO_USE_SCANOUT | BO_USE_HW_VIDEO_ENCODER | BO_USE_CAMERA_READ |
141 				   BO_USE_CAMERA_WRITE);
142 
143 #ifdef MTK_MT8183
144 	/* Only for MT8183 Camera subsystem */
145 	drv_modify_combination(drv, DRM_FORMAT_NV21, &metadata,
146 			       BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
147 	drv_modify_combination(drv, DRM_FORMAT_YUYV, &metadata,
148 			       BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
149 	drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata,
150 			       BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
151 	/* Private formats for private reprocessing in camera */
152 	drv_add_combination(drv, DRM_FORMAT_MTISP_SXYZW10, &metadata,
153 			    BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SW_MASK);
154 #endif
155 
156 	return drv_modify_linear_combinations(drv);
157 }
158 
mediatek_bo_create_with_modifiers(struct bo * bo,uint32_t width,uint32_t height,uint32_t format,const uint64_t * modifiers,uint32_t count)159 static int mediatek_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
160 					     uint32_t format, const uint64_t *modifiers,
161 					     uint32_t count)
162 {
163 	int ret;
164 	size_t plane;
165 	uint32_t stride;
166 	struct drm_mtk_gem_create gem_create = { 0 };
167 	/*
168 	 * We identify the ChromeOS Camera App buffers via these two USE flags. Those buffers need
169 	 * the same alignment as the video hardware encoding.
170 	 */
171 	const bool is_camera_preview =
172 	    (bo->meta.use_flags & BO_USE_SCANOUT) && (bo->meta.use_flags & BO_USE_CAMERA_WRITE);
173 
174 	if (!drv_has_modifier(modifiers, count, DRM_FORMAT_MOD_LINEAR)) {
175 		errno = EINVAL;
176 		drv_loge("no usable modifier found\n");
177 		return -EINVAL;
178 	}
179 
180 	/*
181 	 * Since the ARM L1 cache line size is 64 bytes, align to that as a
182 	 * performance optimization, except for video buffers on certain platforms,
183 	 * these should only be accessed from the GPU and VCODEC subsystems (maybe
184 	 * also MDP), so it's better to align to macroblocks.
185 	 */
186 	stride = drv_stride_from_format(format, width, 0);
187 #ifdef DONT_USE_64_ALIGNMENT_FOR_VIDEO_BUFFERS
188 	const uint32_t alignment = is_video_yuv_format(format) ? 16 : 64;
189 	stride = ALIGN(stride, alignment);
190 #else
191 	stride = ALIGN(stride, 64);
192 #endif
193 
194 	if ((bo->meta.use_flags & BO_USE_HW_VIDEO_ENCODER) || is_camera_preview) {
195 		uint32_t aligned_height = ALIGN(height, 32);
196 		uint32_t padding[DRV_MAX_PLANES] = { 0 };
197 
198 		for (plane = 0; plane < bo->meta.num_planes; ++plane) {
199 			uint32_t plane_stride = drv_stride_from_format(format, stride, plane);
200 			padding[plane] = plane_stride *
201 					 (32 / drv_vertical_subsampling_from_format(format, plane));
202 		}
203 
204 		drv_bo_from_format_and_padding(bo, stride, 1, aligned_height, format, padding);
205 	} else {
206 #ifdef SUPPORTS_YUV422
207 		/*
208 		 * JPEG Encoder Accelerator requires 16x16 alignment. We want the buffer
209 		 * from camera can be put in JEA directly so align the height to 16
210 		 * bytes.
211 		 */
212 		if (format == DRM_FORMAT_NV12)
213 			height = ALIGN(height, 16);
214 #endif
215 		drv_bo_from_format(bo, stride, 1, height, format);
216 
217 #ifdef USE_EXTRA_PADDING_FOR_YVU420
218 		/*
219 		 * Apply extra padding for YV12 if the height does not meet round up requirement and
220 		 * the image is to be sampled by gpu.
221 		 */
222 		static const uint32_t required_round_up = 4;
223 		const uint32_t height_mod = height % required_round_up;
224 		if ((format == DRM_FORMAT_YVU420 || format == DRM_FORMAT_YVU420_ANDROID) &&
225 		    (bo->meta.use_flags & BO_USE_TEXTURE) && height_mod) {
226 			const uint32_t height_padding = required_round_up - height_mod;
227 			const uint32_t u_padding =
228 			    drv_size_from_format(format, bo->meta.strides[2], height_padding, 2);
229 
230 			bo->meta.total_size += u_padding;
231 
232 			/*
233 			 * Since we are not aligning Y, we must make sure that its padding fits
234 			 * inside the rest of the space allocated for the V/U planes.
235 			 */
236 			const uint32_t y_padding =
237 			    drv_size_from_format(format, bo->meta.strides[0], height_padding, 0);
238 			const uint32_t vu_size = drv_bo_get_plane_size(bo, 2) * 2;
239 			if (y_padding > vu_size) {
240 				/* Align with mali workaround to pad all 3 planes. */
241 				bo->meta.total_size += y_padding + u_padding;
242 			}
243 		}
244 #endif
245 	}
246 
247 	gem_create.size = bo->meta.total_size;
248 
249 	ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_CREATE, &gem_create);
250 	if (ret) {
251 		drv_loge("DRM_IOCTL_MTK_GEM_CREATE failed (size=%" PRIu64 ")\n", gem_create.size);
252 		return -errno;
253 	}
254 
255 	for (plane = 0; plane < bo->meta.num_planes; plane++)
256 		bo->handles[plane].u32 = gem_create.handle;
257 
258 	return 0;
259 }
260 
mediatek_bo_create(struct bo * bo,uint32_t width,uint32_t height,uint32_t format,uint64_t use_flags)261 static int mediatek_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
262 			      uint64_t use_flags)
263 {
264 	uint64_t modifiers[] = { DRM_FORMAT_MOD_LINEAR };
265 	return mediatek_bo_create_with_modifiers(bo, width, height, format, modifiers,
266 						 ARRAY_SIZE(modifiers));
267 }
268 
mediatek_bo_map(struct bo * bo,struct vma * vma,uint32_t map_flags)269 static void *mediatek_bo_map(struct bo *bo, struct vma *vma, uint32_t map_flags)
270 {
271 	int ret, prime_fd;
272 	struct drm_mtk_gem_map_off gem_map = { 0 };
273 	struct mediatek_private_map_data *priv;
274 	void *addr = NULL;
275 
276 	gem_map.handle = bo->handles[0].u32;
277 
278 	ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_MAP_OFFSET, &gem_map);
279 	if (ret) {
280 		drv_loge("DRM_IOCTL_MTK_GEM_MAP_OFFSET failed\n");
281 		return MAP_FAILED;
282 	}
283 
284 	prime_fd = drv_bo_get_plane_fd(bo, 0);
285 	if (prime_fd < 0) {
286 		drv_loge("Failed to get a prime fd\n");
287 		return MAP_FAILED;
288 	}
289 
290 	addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
291 		    gem_map.offset);
292 	if (addr == MAP_FAILED)
293 		goto out_close_prime_fd;
294 
295 	vma->length = bo->meta.total_size;
296 
297 	priv = calloc(1, sizeof(*priv));
298 	if (!priv)
299 		goto out_unmap_addr;
300 
301 	if (bo->meta.use_flags & BO_USE_RENDERSCRIPT) {
302 		priv->cached_addr = calloc(1, bo->meta.total_size);
303 		if (!priv->cached_addr)
304 			goto out_free_priv;
305 
306 		priv->gem_addr = addr;
307 		addr = priv->cached_addr;
308 	}
309 
310 	priv->prime_fd = prime_fd;
311 	vma->priv = priv;
312 
313 	return addr;
314 
315 out_free_priv:
316 	free(priv);
317 out_unmap_addr:
318 	munmap(addr, bo->meta.total_size);
319 out_close_prime_fd:
320 	close(prime_fd);
321 	return MAP_FAILED;
322 }
323 
mediatek_bo_unmap(struct bo * bo,struct vma * vma)324 static int mediatek_bo_unmap(struct bo *bo, struct vma *vma)
325 {
326 	if (vma->priv) {
327 		struct mediatek_private_map_data *priv = vma->priv;
328 
329 		if (priv->cached_addr) {
330 			vma->addr = priv->gem_addr;
331 			free(priv->cached_addr);
332 		}
333 
334 		close(priv->prime_fd);
335 		free(priv);
336 		vma->priv = NULL;
337 	}
338 
339 	return munmap(vma->addr, vma->length);
340 }
341 
mediatek_bo_invalidate(struct bo * bo,struct mapping * mapping)342 static int mediatek_bo_invalidate(struct bo *bo, struct mapping *mapping)
343 {
344 	struct mediatek_private_map_data *priv = mapping->vma->priv;
345 
346 	if (priv) {
347 		struct pollfd fds = {
348 			.fd = priv->prime_fd,
349 		};
350 
351 		if (mapping->vma->map_flags & BO_MAP_WRITE)
352 			fds.events |= POLLOUT;
353 
354 		if (mapping->vma->map_flags & BO_MAP_READ)
355 			fds.events |= POLLIN;
356 
357 		poll(&fds, 1, -1);
358 		if (fds.revents != fds.events)
359 			drv_loge("poll prime_fd failed\n");
360 
361 		if (priv->cached_addr)
362 			memcpy(priv->cached_addr, priv->gem_addr, bo->meta.total_size);
363 	}
364 
365 	return 0;
366 }
367 
mediatek_bo_flush(struct bo * bo,struct mapping * mapping)368 static int mediatek_bo_flush(struct bo *bo, struct mapping *mapping)
369 {
370 	struct mediatek_private_map_data *priv = mapping->vma->priv;
371 	if (priv && priv->cached_addr && (mapping->vma->map_flags & BO_MAP_WRITE))
372 		memcpy(priv->gem_addr, priv->cached_addr, bo->meta.total_size);
373 
374 	return 0;
375 }
376 
mediatek_resolve_format_and_use_flags(struct driver * drv,uint32_t format,uint64_t use_flags,uint32_t * out_format,uint64_t * out_use_flags)377 static void mediatek_resolve_format_and_use_flags(struct driver *drv, uint32_t format,
378 						  uint64_t use_flags, uint32_t *out_format,
379 						  uint64_t *out_use_flags)
380 {
381 	*out_format = format;
382 	*out_use_flags = use_flags;
383 	switch (format) {
384 	case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
385 #ifdef MTK_MT8183
386 		/* Only MT8183 Camera subsystem offers private reprocessing
387 		 * capability. CAMERA_READ indicates the buffer is intended for
388 		 * reprocessing and hence given the private format for MTK. */
389 		if (use_flags & BO_USE_CAMERA_READ) {
390 			*out_format = DRM_FORMAT_MTISP_SXYZW10;
391 			break;
392 		}
393 #endif
394 		if (use_flags & BO_USE_CAMERA_WRITE) {
395 			*out_format = DRM_FORMAT_NV12;
396 			break;
397 		}
398 
399 		/* HACK: See b/28671744 */
400 		*out_format = DRM_FORMAT_XBGR8888;
401 		*out_use_flags &= ~BO_USE_HW_VIDEO_ENCODER;
402 		break;
403 	case DRM_FORMAT_FLEX_YCbCr_420_888:
404 #ifdef USE_NV12_FOR_HW_VIDEO_DECODING
405 		// TODO(hiroh): Switch to use NV12 for video decoder on MT8173 as well.
406 		if (use_flags & (BO_USE_HW_VIDEO_DECODER)) {
407 			*out_format = DRM_FORMAT_NV12;
408 			break;
409 		}
410 #endif
411 		if (use_flags &
412 		    (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_ENCODER)) {
413 			*out_format = DRM_FORMAT_NV12;
414 			break;
415 		}
416 
417 		/* HACK: See b/139714614 */
418 		*out_format = DRM_FORMAT_YVU420;
419 		*out_use_flags &= ~BO_USE_SCANOUT;
420 		break;
421 	default:
422 		break;
423 	}
424 	/* Mediatek doesn't support YUV overlays */
425 	if (is_video_yuv_format(format))
426 		*out_use_flags &= ~BO_USE_SCANOUT;
427 }
428 
429 const struct backend backend_mediatek = {
430 	.name = "mediatek",
431 	.init = mediatek_init,
432 	.bo_create = mediatek_bo_create,
433 	.bo_create_with_modifiers = mediatek_bo_create_with_modifiers,
434 	.bo_destroy = drv_gem_bo_destroy,
435 	.bo_import = drv_prime_bo_import,
436 	.bo_map = mediatek_bo_map,
437 	.bo_unmap = mediatek_bo_unmap,
438 	.bo_invalidate = mediatek_bo_invalidate,
439 	.bo_flush = mediatek_bo_flush,
440 	.resolve_format_and_use_flags = mediatek_resolve_format_and_use_flags,
441 };
442 
443 #endif
444