• 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_priv.h"
23 #include "helpers.h"
24 #include "util.h"
25 
26 #define TILE_TYPE_LINEAR 0
27 
28 struct mediatek_private_map_data {
29 	void *cached_addr;
30 	void *gem_addr;
31 	int prime_fd;
32 };
33 
34 static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
35 						  DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
36 						  DRM_FORMAT_XRGB8888 };
37 
38 #ifdef MTK_MT8183
39 static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV21, DRM_FORMAT_NV12,
40 						   DRM_FORMAT_YUYV, DRM_FORMAT_YVU420,
41 						   DRM_FORMAT_YVU420_ANDROID };
42 #else
43 static const uint32_t texture_source_formats[] = { DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID,
44 						   DRM_FORMAT_NV12 };
45 #endif
46 
mediatek_init(struct driver * drv)47 static int mediatek_init(struct driver *drv)
48 {
49 	struct format_metadata metadata;
50 
51 	drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
52 			     &LINEAR_METADATA, BO_USE_RENDER_MASK | BO_USE_SCANOUT);
53 
54 	drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
55 			     &LINEAR_METADATA, BO_USE_TEXTURE_MASK);
56 
57 	drv_add_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA, BO_USE_SW_MASK | BO_USE_LINEAR);
58 
59 	/* Android CTS tests require this. */
60 	drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
61 
62 	/* Support BO_USE_HW_VIDEO_DECODER for protected content minigbm allocations. */
63 	metadata.tiling = TILE_TYPE_LINEAR;
64 	metadata.priority = 1;
65 	metadata.modifier = DRM_FORMAT_MOD_LINEAR;
66 	drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata, BO_USE_HW_VIDEO_DECODER);
67 	drv_modify_combination(drv, DRM_FORMAT_YVU420_ANDROID, &metadata, BO_USE_HW_VIDEO_DECODER);
68 #if defined(MTK_MT8183) || defined(MTK_MT8192) || defined(MTK_MT8195)
69 	// TODO(hiroh): Switch to use NV12 for video decoder on MT8173 as well.
70 	drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata, BO_USE_HW_VIDEO_DECODER);
71 #endif
72 
73 	/*
74 	 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB for input/output from
75 	 * hardware decoder/encoder.
76 	 */
77 	drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
78 			       BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER |
79 				   BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
80 
81 	/* NV12 format for encoding and display. */
82 	drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
83 			       BO_USE_SCANOUT | BO_USE_HW_VIDEO_ENCODER | BO_USE_CAMERA_READ |
84 				   BO_USE_CAMERA_WRITE);
85 
86 #ifdef MTK_MT8183
87 	/* Only for MT8183 Camera subsystem */
88 	drv_modify_combination(drv, DRM_FORMAT_NV21, &metadata,
89 			       BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
90 	drv_modify_combination(drv, DRM_FORMAT_YUYV, &metadata,
91 			       BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
92 	drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata,
93 			       BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
94 	/* Private formats for private reprocessing in camera */
95 	drv_add_combination(drv, DRM_FORMAT_MTISP_SXYZW10, &metadata,
96 			    BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SW_MASK);
97 #endif
98 
99 	return drv_modify_linear_combinations(drv);
100 }
101 
mediatek_bo_create_with_modifiers(struct bo * bo,uint32_t width,uint32_t height,uint32_t format,const uint64_t * modifiers,uint32_t count)102 static int mediatek_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
103 					     uint32_t format, const uint64_t *modifiers,
104 					     uint32_t count)
105 {
106 	int ret;
107 	size_t plane;
108 	uint32_t stride;
109 	struct drm_mtk_gem_create gem_create = { 0 };
110 
111 	if (!drv_has_modifier(modifiers, count, DRM_FORMAT_MOD_LINEAR)) {
112 		errno = EINVAL;
113 		drv_log("no usable modifier found\n");
114 		return -EINVAL;
115 	}
116 
117 	/*
118 	 * Since the ARM L1 cache line size is 64 bytes, align to that as a
119 	 * performance optimization.
120 	 */
121 	stride = drv_stride_from_format(format, width, 0);
122 	stride = ALIGN(stride, 64);
123 
124 	if (bo->meta.use_flags & BO_USE_HW_VIDEO_ENCODER) {
125 		uint32_t aligned_height = ALIGN(height, 32);
126 		uint32_t padding[DRV_MAX_PLANES] = { 0 };
127 
128 		for (plane = 0; plane < bo->meta.num_planes; ++plane) {
129 			uint32_t plane_stride = drv_stride_from_format(format, stride, plane);
130 			padding[plane] = plane_stride *
131 					 (32 / drv_vertical_subsampling_from_format(format, plane));
132 		}
133 
134 		drv_bo_from_format_and_padding(bo, stride, aligned_height, format, padding);
135 	} else {
136 #ifdef MTK_MT8183
137 		/*
138 		 * JPEG Encoder Accelerator requires 16x16 alignment. We want the buffer
139 		 * from camera can be put in JEA directly so align the height to 16
140 		 * bytes.
141 		 */
142 		if (format == DRM_FORMAT_NV12)
143 			height = ALIGN(height, 16);
144 #endif
145 		drv_bo_from_format(bo, stride, height, format);
146 	}
147 
148 	gem_create.size = bo->meta.total_size;
149 
150 	ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_CREATE, &gem_create);
151 	if (ret) {
152 		drv_log("DRM_IOCTL_MTK_GEM_CREATE failed (size=%" PRIu64 ")\n", gem_create.size);
153 		return -errno;
154 	}
155 
156 	for (plane = 0; plane < bo->meta.num_planes; plane++)
157 		bo->handles[plane].u32 = gem_create.handle;
158 
159 	return 0;
160 }
161 
mediatek_bo_create(struct bo * bo,uint32_t width,uint32_t height,uint32_t format,uint64_t use_flags)162 static int mediatek_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
163 			      uint64_t use_flags)
164 {
165 	uint64_t modifiers[] = { DRM_FORMAT_MOD_LINEAR };
166 	return mediatek_bo_create_with_modifiers(bo, width, height, format, modifiers,
167 						 ARRAY_SIZE(modifiers));
168 }
169 
mediatek_bo_map(struct bo * bo,struct vma * vma,size_t plane,uint32_t map_flags)170 static void *mediatek_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
171 {
172 	int ret, prime_fd;
173 	struct drm_mtk_gem_map_off gem_map = { 0 };
174 	struct mediatek_private_map_data *priv;
175 
176 	gem_map.handle = bo->handles[0].u32;
177 
178 	ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_MAP_OFFSET, &gem_map);
179 	if (ret) {
180 		drv_log("DRM_IOCTL_MTK_GEM_MAP_OFFSET failed\n");
181 		return MAP_FAILED;
182 	}
183 
184 	prime_fd = drv_bo_get_plane_fd(bo, 0);
185 	if (prime_fd < 0) {
186 		drv_log("Failed to get a prime fd\n");
187 		return MAP_FAILED;
188 	}
189 
190 	void *addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
191 			  gem_map.offset);
192 
193 	vma->length = bo->meta.total_size;
194 
195 	priv = calloc(1, sizeof(*priv));
196 	priv->prime_fd = prime_fd;
197 	vma->priv = priv;
198 
199 	if (bo->meta.use_flags & BO_USE_RENDERSCRIPT) {
200 		priv->cached_addr = calloc(1, bo->meta.total_size);
201 		priv->gem_addr = addr;
202 		addr = priv->cached_addr;
203 	}
204 
205 	return addr;
206 }
207 
mediatek_bo_unmap(struct bo * bo,struct vma * vma)208 static int mediatek_bo_unmap(struct bo *bo, struct vma *vma)
209 {
210 	if (vma->priv) {
211 		struct mediatek_private_map_data *priv = vma->priv;
212 
213 		if (priv->cached_addr) {
214 			vma->addr = priv->gem_addr;
215 			free(priv->cached_addr);
216 		}
217 
218 		close(priv->prime_fd);
219 		free(priv);
220 		vma->priv = NULL;
221 	}
222 
223 	return munmap(vma->addr, vma->length);
224 }
225 
mediatek_bo_invalidate(struct bo * bo,struct mapping * mapping)226 static int mediatek_bo_invalidate(struct bo *bo, struct mapping *mapping)
227 {
228 	struct mediatek_private_map_data *priv = mapping->vma->priv;
229 
230 	if (priv) {
231 		struct pollfd fds = {
232 			.fd = priv->prime_fd,
233 		};
234 
235 		if (mapping->vma->map_flags & BO_MAP_WRITE)
236 			fds.events |= POLLOUT;
237 
238 		if (mapping->vma->map_flags & BO_MAP_READ)
239 			fds.events |= POLLIN;
240 
241 		poll(&fds, 1, -1);
242 		if (fds.revents != fds.events)
243 			drv_log("poll prime_fd failed\n");
244 
245 		if (priv->cached_addr)
246 			memcpy(priv->cached_addr, priv->gem_addr, bo->meta.total_size);
247 	}
248 
249 	return 0;
250 }
251 
mediatek_bo_flush(struct bo * bo,struct mapping * mapping)252 static int mediatek_bo_flush(struct bo *bo, struct mapping *mapping)
253 {
254 	struct mediatek_private_map_data *priv = mapping->vma->priv;
255 	if (priv && priv->cached_addr && (mapping->vma->map_flags & BO_MAP_WRITE))
256 		memcpy(priv->gem_addr, priv->cached_addr, bo->meta.total_size);
257 
258 	return 0;
259 }
260 
mediatek_resolve_format(struct driver * drv,uint32_t format,uint64_t use_flags)261 static uint32_t mediatek_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
262 {
263 	switch (format) {
264 	case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
265 #ifdef MTK_MT8183
266 		/* Only MT8183 Camera subsystem offers private reprocessing
267 		 * capability. CAMERA_READ indicates the buffer is intended for
268 		 * reprocessing and hence given the private format for MTK. */
269 		if (use_flags & BO_USE_CAMERA_READ)
270 			return DRM_FORMAT_MTISP_SXYZW10;
271 #endif
272 		if (use_flags & BO_USE_CAMERA_WRITE)
273 			return DRM_FORMAT_NV12;
274 
275 		/*HACK: See b/28671744 */
276 		return DRM_FORMAT_XBGR8888;
277 	case DRM_FORMAT_FLEX_YCbCr_420_888:
278 #if defined(MTK_MT8183) || defined(MTK_MT8192) || defined(MTK_MT8195)
279 		// TODO(hiroh): Switch to use NV12 for video decoder on MT8173 as well.
280 		if (use_flags & (BO_USE_HW_VIDEO_DECODER)) {
281 			return DRM_FORMAT_NV12;
282 		}
283 #endif
284 		if (use_flags &
285 		    (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_ENCODER)) {
286 			return DRM_FORMAT_NV12;
287 		}
288 		return DRM_FORMAT_YVU420;
289 	default:
290 		return format;
291 	}
292 }
293 
294 const struct backend backend_mediatek = {
295 	.name = "mediatek",
296 	.init = mediatek_init,
297 	.bo_create = mediatek_bo_create,
298 	.bo_create_with_modifiers = mediatek_bo_create_with_modifiers,
299 	.bo_destroy = drv_gem_bo_destroy,
300 	.bo_import = drv_prime_bo_import,
301 	.bo_map = mediatek_bo_map,
302 	.bo_unmap = mediatek_bo_unmap,
303 	.bo_invalidate = mediatek_bo_invalidate,
304 	.bo_flush = mediatek_bo_flush,
305 	.resolve_format = mediatek_resolve_format,
306 };
307 
308 #endif
309