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 #if !defined(ANDROID) || (ANDROID_API_LEVEL >= 31 && defined(HAS_DMABUF_SYSTEM_HEAP))
14 #include <linux/dma-heap.h>
15 #endif
16 #include <poll.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <sys/ioctl.h>
20 #include <sys/mman.h>
21 #include <unistd.h>
22 #include <xf86drm.h>
23 #include <mediatek_drm.h>
24 // clang-format on
25
26 #include "drv_helpers.h"
27 #include "drv_priv.h"
28 #include "util.h"
29
30 #define TILE_TYPE_LINEAR 0
31
32 // clang-format off
33 #if defined(MTK_MT8183) || \
34 defined(MTK_MT8186)
35 // clang-format on
36 #define SUPPORT_YUV422
37 #endif
38
39 // All platforms except MT8173 should USE_NV12_FOR_HW_VIDEO_DECODING
40 // and SUPPORT_FP16_AND_10BIT_ABGR
41 // clang-format off
42 #if defined(MTK_MT8183) || \
43 defined(MTK_MT8186) || \
44 defined(MTK_MT8188G) || \
45 defined(MTK_MT8192) || \
46 defined(MTK_MT8195) || \
47 defined(MTK_MT8196)
48 // clang-format on
49 #define USE_NV12_FOR_HW_VIDEO_DECODING
50 #define SUPPORT_FP16_AND_10BIT_ABGR
51 #else
52 #define DONT_USE_64_ALIGNMENT_FOR_VIDEO_BUFFERS
53 #endif
54
55 // Devices newer than MT8186 support AR30 overlays and 10-bit video.
56 // clang-format off
57 #if !defined(MTK_MT8173) && \
58 !defined(MTK_MT8183) && \
59 !defined(MTK_MT8186) && \
60 !defined(MTK_MT8192)
61 // clang-format on
62 #define SUPPORT_P010
63 #define SUPPORT_AR30_OVERLAYS
64 #endif
65
66 // For Mali Sigurd based GPUs, the texture unit reads outside the specified texture dimensions.
67 // Therefore, certain formats require extra memory padding to its allocated surface to prevent the
68 // hardware from reading outside an allocation. For YVU420, we need additional padding for the last
69 // chroma plane.
70 #if defined(MTK_MT8186)
71 #define USE_EXTRA_PADDING_FOR_YVU420
72 #endif
73
74 struct mediatek_private_drv_data {
75 int dma_heap_fd;
76 };
77
78 struct mediatek_private_map_data {
79 void *cached_addr;
80 void *gem_addr;
81 int prime_fd;
82 };
83
84 static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
85 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
86 DRM_FORMAT_XRGB8888 };
87
88 // clang-format off
89 static const uint32_t texture_source_formats[] = {
90 #ifdef SUPPORT_YUV422
91 DRM_FORMAT_NV21,
92 DRM_FORMAT_YUYV,
93 #endif
94 #ifdef SUPPORT_P010
95 DRM_FORMAT_P010,
96 #endif
97 #ifdef SUPPORT_FP16_AND_10BIT_ABGR
98 DRM_FORMAT_ABGR2101010,
99 DRM_FORMAT_ABGR16161616F,
100 #endif
101 DRM_FORMAT_NV12,
102 DRM_FORMAT_YVU420,
103 DRM_FORMAT_YVU420_ANDROID
104 };
105
106 static const uint32_t video_yuv_formats[] = {
107 DRM_FORMAT_NV21,
108 DRM_FORMAT_NV12,
109 #ifdef SUPPORT_P010
110 DRM_FORMAT_P010,
111 #endif
112 DRM_FORMAT_YUYV,
113 DRM_FORMAT_YVU420,
114 DRM_FORMAT_YVU420_ANDROID
115 };
116 // clang-format on
117
is_video_yuv_format(uint32_t format)118 static bool is_video_yuv_format(uint32_t format)
119 {
120 size_t i;
121 for (i = 0; i < ARRAY_SIZE(video_yuv_formats); ++i) {
122 if (format == video_yuv_formats[i])
123 return true;
124 }
125 return false;
126 }
127
mediatek_init(struct driver * drv)128 static int mediatek_init(struct driver *drv)
129 {
130 struct format_metadata metadata;
131 struct mediatek_private_drv_data *priv;
132
133 priv = calloc(1, sizeof(*priv));
134 if (!priv) {
135 drv_loge("Failed calloc private data, errno=%d\n", -errno);
136 return -errno;
137 }
138
139 priv->dma_heap_fd = -1;
140 drv->priv = priv;
141
142 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
143 &LINEAR_METADATA,
144 BO_USE_RENDER_MASK | BO_USE_SCANOUT | BO_USE_PROTECTED);
145
146 drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
147 &LINEAR_METADATA, BO_USE_TEXTURE_MASK | BO_USE_PROTECTED);
148
149 drv_add_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA,
150 BO_USE_SW_MASK | BO_USE_LINEAR | BO_USE_PROTECTED);
151
152 #ifdef SUPPORT_AR30_OVERLAYS
153 drv_add_combination(drv, DRM_FORMAT_ARGB2101010, &LINEAR_METADATA,
154 BO_USE_TEXTURE | BO_USE_SCANOUT | BO_USE_PROTECTED | BO_USE_LINEAR);
155 #endif
156
157 /* YUYV format for video overlay and camera subsystem. */
158 drv_add_combination(drv, DRM_FORMAT_YUYV, &LINEAR_METADATA,
159 BO_USE_HW_VIDEO_DECODER | BO_USE_SCANOUT | BO_USE_LINEAR |
160 BO_USE_TEXTURE | BO_USE_PROTECTED);
161
162 /* Android CTS tests require this. */
163 drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
164
165 /* Support BO_USE_HW_VIDEO_DECODER for protected content minigbm allocations. */
166 metadata.tiling = TILE_TYPE_LINEAR;
167 metadata.priority = 1;
168 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
169 drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata,
170 BO_USE_HW_VIDEO_DECODER | BO_USE_PROTECTED);
171 #ifdef MTK_MT8173
172 /*
173 * b/292507490: The MT8173 decoder can output YUV420 only. Some CTS tests feed the
174 * decoded buffer to the hardware encoder and the tests allocate the buffer with
175 * DRM_FORMAT_FLEX_YCbCr_420_888 with the mask of BO_USE_HW_VIDEO_ENCODER |
176 * BO_USE_HW_VIDEO_DECODER. Therefore, we have to allocate YUV420 in the case.
177 */
178 drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata, BO_USE_HW_VIDEO_ENCODER);
179 #endif
180 drv_modify_combination(drv, DRM_FORMAT_YVU420_ANDROID, &metadata,
181 BO_USE_HW_VIDEO_DECODER | BO_USE_PROTECTED);
182 #ifdef USE_NV12_FOR_HW_VIDEO_DECODING
183 // TODO(hiroh): Switch to use NV12 for video decoder on MT8173 as well.
184 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
185 BO_USE_HW_VIDEO_DECODER | BO_USE_PROTECTED);
186 #endif
187 drv_modify_combination(drv, DRM_FORMAT_P010, &metadata,
188 BO_USE_HW_VIDEO_DECODER | BO_USE_PROTECTED);
189
190 /*
191 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB for input/output from
192 * hardware decoder/encoder.
193 */
194 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
195 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER |
196 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
197 BO_USE_GPU_DATA_BUFFER | BO_USE_SENSOR_DIRECT_DATA);
198
199 /* NV12 format for encoding and display. */
200 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
201 BO_USE_SCANOUT | BO_USE_HW_VIDEO_ENCODER | BO_USE_CAMERA_READ |
202 BO_USE_CAMERA_WRITE);
203
204 /*
205 * Android also frequently requests YV12 formats for some camera implementations
206 * (including the external provider implmenetation).
207 */
208 drv_modify_combination(drv, DRM_FORMAT_YVU420_ANDROID, &metadata,
209 BO_USE_CAMERA_WRITE);
210
211 #ifdef MTK_MT8183
212 /* Only for MT8183 Camera subsystem */
213 drv_modify_combination(drv, DRM_FORMAT_NV21, &metadata,
214 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
215 drv_modify_combination(drv, DRM_FORMAT_YUYV, &metadata,
216 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
217 drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata,
218 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
219 /* Private formats for private reprocessing in camera */
220 drv_add_combination(drv, DRM_FORMAT_MTISP_SXYZW10, &metadata,
221 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SW_MASK);
222 #endif
223
224 return drv_modify_linear_combinations(drv);
225 }
226
mediatek_close(struct driver * drv)227 static void mediatek_close(struct driver *drv)
228 {
229 struct mediatek_private_drv_data *priv = (struct mediatek_private_drv_data *)drv->priv;
230
231 if (priv->dma_heap_fd >= 0)
232 close(priv->dma_heap_fd);
233
234 free(priv);
235 drv->priv = NULL;
236 }
237
mediatek_bo_create_with_modifiers(struct bo * bo,uint32_t width,uint32_t height,uint32_t format,const uint64_t * modifiers,uint32_t count)238 static int mediatek_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
239 uint32_t format, const uint64_t *modifiers,
240 uint32_t count)
241 {
242 int ret;
243 size_t plane;
244 uint32_t stride;
245 struct drm_mtk_gem_create gem_create = { 0 };
246
247 const bool is_camera_write = bo->meta.use_flags & BO_USE_CAMERA_WRITE;
248 const bool is_hw_video_encoder = bo->meta.use_flags & BO_USE_HW_VIDEO_ENCODER;
249 const bool is_linear = bo->meta.use_flags & BO_USE_LINEAR;
250 const bool is_protected = bo->meta.use_flags & BO_USE_PROTECTED;
251 const bool is_scanout = bo->meta.use_flags & BO_USE_SCANOUT;
252 /*
253 * We identify the ChromeOS Camera App buffers via these two USE flags. Those buffers need
254 * the same alignment as the video hardware encoding.
255 */
256 const bool is_camera_preview = is_scanout && is_camera_write;
257 #ifdef MTK_MT8173
258 const bool is_mt8173_video_decoder = bo->meta.use_flags & BO_USE_HW_VIDEO_DECODER;
259 #else
260 const bool is_mt8173_video_decoder = false;
261 #endif
262 /*
263 * Android sends blobs for encoding in the shape of a single-row pixel buffer. Use R8 +
264 * single row as a proxy for Android HAL_PIXEL_FORMAT_BLOB until a drm equivalent is
265 * defined.
266 */
267 const bool is_format_blob = format == DRM_FORMAT_R8 && height == 1;
268
269 if (!drv_has_modifier(modifiers, count, DRM_FORMAT_MOD_LINEAR)) {
270 errno = EINVAL;
271 drv_loge("no usable modifier found\n");
272 return -EINVAL;
273 }
274
275 /*
276 * Since the ARM L1 cache line size is 64 bytes, align to that as a
277 * performance optimization, except for video buffers on certain platforms,
278 * these should only be accessed from the GPU and VCODEC subsystems (maybe
279 * also MDP), so it's better to align to macroblocks.
280 */
281 stride = drv_stride_from_format(format, width, 0);
282 #ifdef DONT_USE_64_ALIGNMENT_FOR_VIDEO_BUFFERS
283 const uint32_t alignment = is_video_yuv_format(format) ? 16 : 64;
284 stride = ALIGN(stride, alignment);
285 #else
286 stride = ALIGN(stride, 64);
287 #endif
288
289 /*
290 * The mediatek video decoder requires to align width and height by 64. But this is
291 * the requirement for mediatek tiled format (e.g. MT21 and MM21). The buffers are
292 * not allocated by minigbm. So we don't have to care about it. The tiled buffer is
293 * converted to NV12 or YV12, which is allocated by minigbm. V4L2 MDP doesn't
294 * require any special alignment for them.
295 * On the other hand, the mediatek video encoder reuqires a padding on each plane.
296 * When both video decoder and encoder use flag is masked (in some CTS test), we
297 * align with the encoder alignment.
298 * However, V4L2VideoDecodeAccelerator used on MT8173 fails handling the buffer with
299 * padding, although V4L2VideoDecoder used on MT8183 and later can do. We workaround
300 * this problem to allocate a buffer without padding on MT8173. This works because
301 * MT8173 decoder's output NV12 is converted to YV12 buffer that is allocated with
302 * video encoder usage mask only and thus have padding in Android.
303 * See go/mediatek-video-buffer-alignment-note for detail.
304 */
305 if ((is_hw_video_encoder && !is_mt8173_video_decoder && !is_format_blob) ||
306 is_camera_preview) {
307 uint32_t aligned_height = ALIGN(height, 32);
308 uint32_t padding[DRV_MAX_PLANES] = { 0 };
309
310 for (plane = 0; plane < bo->meta.num_planes; ++plane) {
311 uint32_t plane_stride = drv_stride_from_format(format, stride, plane);
312 padding[plane] = plane_stride *
313 (32 / drv_vertical_subsampling_from_format(format, plane));
314 }
315
316 drv_bo_from_format_and_padding(bo, stride, 1, aligned_height, format, padding);
317 } else {
318 #ifdef USE_EXTRA_PADDING_FOR_YVU420
319 /*
320 * Apply extra padding for YV12 if the height does not meet round up requirement and
321 * the image is to be sampled by gpu.
322 */
323 static const uint32_t required_round_up = 4;
324 const uint32_t height_mod = height % required_round_up;
325 const bool is_texture = bo->meta.use_flags & BO_USE_TEXTURE;
326 /*
327 * YVU420 and YVU420_ANDROID treatments have been aligned in mediatek backend. Check
328 * both since gbm frontend still maps linear YVU420 to YVU420_ANDROID for other hw
329 * backends.
330 */
331 const bool is_format_yv12 =
332 format == DRM_FORMAT_YVU420 || format == DRM_FORMAT_YVU420_ANDROID;
333 #endif
334 #ifdef SUPPORT_YUV422
335 /*
336 * JPEG Encoder Accelerator requires 16x16 alignment. We want the buffer
337 * from camera can be put in JEA directly so align the height to 16
338 * bytes.
339 */
340 if (format == DRM_FORMAT_NV12)
341 height = ALIGN(height, 16);
342 #endif
343 drv_bo_from_format(bo, stride, 1, height, format);
344
345 #ifdef USE_EXTRA_PADDING_FOR_YVU420
346 if (is_format_yv12 && is_texture && height_mod) {
347 const uint32_t height_padding = required_round_up - height_mod;
348 const uint32_t y_padding =
349 drv_size_from_format(format, bo->meta.strides[0], height_padding, 0);
350 const uint32_t u_padding =
351 drv_size_from_format(format, bo->meta.strides[2], height_padding, 2);
352 const uint32_t vu_size = drv_bo_get_plane_size(bo, 2) * 2;
353
354 bo->meta.total_size += u_padding;
355
356 /*
357 * Since we are not aligning Y, we must make sure that its padding fits
358 * inside the rest of the space allocated for the V/U planes.
359 */
360 if (y_padding > vu_size) {
361 /* Align with mali workaround to pad all 3 planes. */
362 bo->meta.total_size += y_padding + u_padding;
363 }
364 }
365 #endif
366 }
367
368 /* For protected data buffer needs to allocate from DMA_HEAP directly */
369 if (is_protected) {
370 #if !defined(ANDROID) || (ANDROID_API_LEVEL >= 31 && defined(HAS_DMABUF_SYSTEM_HEAP))
371 int ret;
372 struct mediatek_private_drv_data *priv = (struct mediatek_private_drv_data *)bo->drv->priv;
373 struct dma_heap_allocation_data heap_data = {
374 .len = bo->meta.total_size,
375 .fd_flags = O_RDWR | O_CLOEXEC,
376 };
377
378 if (format == DRM_FORMAT_P010) {
379 /*
380 * Adjust the size so we don't waste tons of space. This was allocated
381 * with 16 bpp, but we only need 10 bpp. We can safely divide by 8 because
382 * we are aligned at a multiple higher than that.
383 */
384 bo->meta.strides[0] = bo->meta.strides[0] * 10 / 16;
385 bo->meta.strides[1] = bo->meta.strides[1] * 10 / 16;
386 bo->meta.sizes[0] = bo->meta.sizes[0] * 10 / 16;
387 bo->meta.sizes[1] = bo->meta.sizes[1] * 10 / 16;
388 bo->meta.offsets[1] = bo->meta.sizes[0];
389 bo->meta.total_size = bo->meta.total_size * 10 / 16;
390 }
391
392 if (priv->dma_heap_fd < 0) {
393 priv->dma_heap_fd = open("/dev/dma_heap/restricted_mtk_cma", O_RDWR | O_CLOEXEC);
394 if (priv->dma_heap_fd < 0) {
395 drv_loge("Failed opening secure CMA heap errno=%d\n", -errno);
396 return -errno;
397 }
398 }
399
400 ret = ioctl(priv->dma_heap_fd, DMA_HEAP_IOCTL_ALLOC, &heap_data);
401 if (ret < 0) {
402 drv_loge("Failed allocating CMA buffer ret=%d\n", ret);
403 return ret;
404 }
405
406 /* Create GEM handle for secure CMA and close FD here */
407 ret = drmPrimeFDToHandle(bo->drv->fd, heap_data.fd, &bo->handle.u32);
408 close(heap_data.fd);
409 if (ret) {
410 drv_loge("Failed drmPrimeFDToHandle(fd:%d) ret=%d\n", heap_data.fd, ret);
411 return ret;
412 }
413 #else
414 drv_loge("Protected allocation not supported\n");
415 return -1;
416 #endif
417 return 0;
418 }
419
420 /*
421 * For linear scanout buffers, the read/write pattern is usually linear i.e. each address is
422 * accessed sequentially, and there are fewer chances that an address will be repeatedly
423 * accessed.
424 * This behavior leads to less TLB dependency and cache misses i.e. no need to translate the
425 * same virtual address to a physical address multiple times.
426 *
427 * With that premise, it's safe to allow the DMA framework to fulfill such allocation
428 * requests with non-continuous smaller chunks of memory (e.g., 4KiB single pages) which
429 * are generally easier to allocate compared to large continuous chunks of memory, improving
430 * memory allocation efficiency and reduce the risk of allocation failures, especially when
431 * available memory budget is low or on memory-constrained devices.
432 */
433 if (is_linear && is_scanout)
434 gem_create.flags |= DRM_MTK_GEM_CREATE_FLAG_ALLOC_SINGLE_PAGES;
435
436 gem_create.size = bo->meta.total_size;
437
438 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_CREATE, &gem_create);
439 if (ret) {
440 drv_loge("DRM_IOCTL_MTK_GEM_CREATE failed (size=%" PRIu64 ")\n", gem_create.size);
441 return -errno;
442 }
443
444 bo->handle.u32 = gem_create.handle;
445
446 return 0;
447 }
448
mediatek_bo_create(struct bo * bo,uint32_t width,uint32_t height,uint32_t format,uint64_t use_flags)449 static int mediatek_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
450 uint64_t use_flags)
451 {
452 uint64_t modifiers[] = { DRM_FORMAT_MOD_LINEAR };
453 return mediatek_bo_create_with_modifiers(bo, width, height, format, modifiers,
454 ARRAY_SIZE(modifiers));
455 }
456
mediatek_bo_map(struct bo * bo,struct vma * vma,uint32_t map_flags)457 static void *mediatek_bo_map(struct bo *bo, struct vma *vma, uint32_t map_flags)
458 {
459 int ret, prime_fd;
460 struct drm_mtk_gem_map_off gem_map = { 0 };
461 struct mediatek_private_map_data *priv;
462 void *addr = NULL;
463
464 gem_map.handle = bo->handle.u32;
465
466 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_MAP_OFFSET, &gem_map);
467 if (ret) {
468 drv_loge("DRM_IOCTL_MTK_GEM_MAP_OFFSET failed\n");
469 return MAP_FAILED;
470 }
471
472 prime_fd = drv_bo_get_plane_fd(bo, 0);
473 if (prime_fd < 0) {
474 drv_loge("Failed to get a prime fd\n");
475 return MAP_FAILED;
476 }
477
478 addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
479 gem_map.offset);
480 if (addr == MAP_FAILED)
481 goto out_close_prime_fd;
482
483 vma->length = bo->meta.total_size;
484
485 priv = calloc(1, sizeof(*priv));
486 if (!priv)
487 goto out_unmap_addr;
488
489 if (bo->meta.use_flags & BO_USE_RENDERSCRIPT) {
490 priv->cached_addr = calloc(1, bo->meta.total_size);
491 if (!priv->cached_addr)
492 goto out_free_priv;
493
494 priv->gem_addr = addr;
495 addr = priv->cached_addr;
496 }
497
498 priv->prime_fd = prime_fd;
499 vma->priv = priv;
500
501 return addr;
502
503 out_free_priv:
504 free(priv);
505 out_unmap_addr:
506 munmap(addr, bo->meta.total_size);
507 out_close_prime_fd:
508 close(prime_fd);
509 return MAP_FAILED;
510 }
511
mediatek_bo_unmap(struct bo * bo,struct vma * vma)512 static int mediatek_bo_unmap(struct bo *bo, struct vma *vma)
513 {
514 if (vma->priv) {
515 struct mediatek_private_map_data *priv = vma->priv;
516
517 if (priv->cached_addr) {
518 vma->addr = priv->gem_addr;
519 free(priv->cached_addr);
520 }
521
522 close(priv->prime_fd);
523 free(priv);
524 vma->priv = NULL;
525 }
526
527 return munmap(vma->addr, vma->length);
528 }
529
mediatek_bo_invalidate(struct bo * bo,struct mapping * mapping)530 static int mediatek_bo_invalidate(struct bo *bo, struct mapping *mapping)
531 {
532 struct mediatek_private_map_data *priv = mapping->vma->priv;
533
534 if (priv) {
535 struct pollfd fds = {
536 .fd = priv->prime_fd,
537 };
538
539 if (mapping->vma->map_flags & BO_MAP_WRITE)
540 fds.events |= POLLOUT;
541
542 if (mapping->vma->map_flags & BO_MAP_READ)
543 fds.events |= POLLIN;
544
545 poll(&fds, 1, -1);
546 if (fds.revents != fds.events)
547 drv_loge("poll prime_fd failed\n");
548
549 if (priv->cached_addr)
550 memcpy(priv->cached_addr, priv->gem_addr, bo->meta.total_size);
551 }
552
553 return 0;
554 }
555
mediatek_bo_flush(struct bo * bo,struct mapping * mapping)556 static int mediatek_bo_flush(struct bo *bo, struct mapping *mapping)
557 {
558 struct mediatek_private_map_data *priv = mapping->vma->priv;
559 if (priv && priv->cached_addr && (mapping->vma->map_flags & BO_MAP_WRITE))
560 memcpy(priv->gem_addr, priv->cached_addr, bo->meta.total_size);
561
562 return 0;
563 }
564
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)565 static void mediatek_resolve_format_and_use_flags(struct driver *drv, uint32_t format,
566 uint64_t use_flags, uint32_t *out_format,
567 uint64_t *out_use_flags)
568 {
569 *out_format = format;
570 *out_use_flags = use_flags;
571 switch (format) {
572 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
573 #ifdef MTK_MT8183
574 /* Only MT8183 Camera subsystem offers private reprocessing
575 * capability. CAMERA_READ indicates the buffer is intended for
576 * reprocessing and hence given the private format for MTK. */
577 if (use_flags & BO_USE_CAMERA_READ) {
578 *out_format = DRM_FORMAT_MTISP_SXYZW10;
579 break;
580 }
581 #endif
582 if (use_flags & BO_USE_CAMERA_WRITE) {
583 *out_format = DRM_FORMAT_NV12;
584 break;
585 }
586
587 /* HACK: See b/28671744 */
588 *out_format = DRM_FORMAT_XBGR8888;
589 *out_use_flags &= ~BO_USE_HW_VIDEO_ENCODER;
590 break;
591 case DRM_FORMAT_FLEX_YCbCr_420_888:
592 #ifdef USE_NV12_FOR_HW_VIDEO_DECODING
593 // TODO(hiroh): Switch to use NV12 for video decoder on MT8173 as well.
594 if (use_flags & (BO_USE_HW_VIDEO_DECODER)) {
595 *out_format = DRM_FORMAT_NV12;
596 break;
597 }
598 #endif
599 /*
600 * b/292507490: The MT8173 decoder can output YUV420 only. Some CTS tests feed the
601 * decoded buffer to the hardware encoder and the tests allocate the buffer with
602 * DRM_FORMAT_FLEX_YCbCr_420_888 with the mask of BO_USE_HW_VIDEO_ENCODER |
603 * BO_USE_HW_VIDEO_DECODER. Therefore, we have to allocate YUV420 in the case.
604 */
605 if (use_flags &
606 (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_ENCODER)) {
607 #ifndef MTK_MT8173
608 *out_format = DRM_FORMAT_NV12;
609 break;
610 #else
611 if (!(use_flags & BO_USE_HW_VIDEO_DECODER)) {
612 *out_format = DRM_FORMAT_NV12;
613 break;
614 }
615 #endif
616 }
617 /* HACK: See b/139714614 */
618 *out_format = DRM_FORMAT_YVU420;
619 *out_use_flags &= ~BO_USE_SCANOUT;
620 break;
621 default:
622 break;
623 }
624 /* Mediatek doesn't support YUV overlays */
625 if (is_video_yuv_format(format))
626 *out_use_flags &= ~BO_USE_SCANOUT;
627 }
628
629 const struct backend backend_mediatek = {
630 .name = "mediatek",
631 .init = mediatek_init,
632 .close = mediatek_close,
633 .bo_create = mediatek_bo_create,
634 .bo_create_with_modifiers = mediatek_bo_create_with_modifiers,
635 .bo_destroy = drv_gem_bo_destroy,
636 .bo_import = drv_prime_bo_import,
637 .bo_map = mediatek_bo_map,
638 .bo_unmap = mediatek_bo_unmap,
639 .bo_invalidate = mediatek_bo_invalidate,
640 .bo_flush = mediatek_bo_flush,
641 .resolve_format_and_use_flags = mediatek_resolve_format_and_use_flags,
642 };
643
644 #endif
645