blob: cdfc9ab73ae26d436cfbe8b4be7cb9452cb82ef3 [file] [log] [blame]
JB Tsai0c16a0f2015-03-19 14:30:31 +08001/*
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
Gurchetan Singh46faf6b2016-08-05 14:40:07 -07007#ifdef DRV_MEDIATEK
JB Tsai0c16a0f2015-03-19 14:30:31 +08008
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -08009// clang-format off
Stéphane Marchesin6ac299f2019-03-21 12:23:29 -070010#include <errno.h>
Luigi Santivetti500928f2018-08-28 10:09:20 +010011#include <fcntl.h>
Nicolas Boichatd7c83382019-08-29 21:46:29 +080012#include <inttypes.h>
Luigi Santivetti500928f2018-08-28 10:09:20 +010013#include <poll.h>
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -070014#include <stdio.h>
JB Tsai0c16a0f2015-03-19 14:30:31 +080015#include <string.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070016#include <sys/mman.h>
Luigi Santivetti500928f2018-08-28 10:09:20 +010017#include <unistd.h>
JB Tsai0c16a0f2015-03-19 14:30:31 +080018#include <xf86drm.h>
19#include <mediatek_drm.h>
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080020// clang-format on
Gurchetan Singhef920532016-08-12 16:38:25 -070021
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070022#include "drv_priv.h"
JB Tsai0c16a0f2015-03-19 14:30:31 +080023#include "helpers.h"
Gurchetan Singh179687e2016-10-28 10:07:35 -070024#include "util.h"
25
Miguel Casasdea0ccb2018-07-02 09:40:25 -040026#define TILE_TYPE_LINEAR 0
27
Gurchetan Singh469a3aa2017-08-03 18:17:34 -070028struct mediatek_private_map_data {
29 void *cached_addr;
30 void *gem_addr;
Luigi Santivetti500928f2018-08-28 10:09:20 +010031 int prime_fd;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -070032};
33
Gurchetan Singh767c5382018-05-05 00:42:12 +000034static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
Gurchetan Singh71bc6652018-09-17 17:42:05 -070035 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
36 DRM_FORMAT_XRGB8888 };
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070037
Nick Fan01c40142018-10-08 11:53:26 +080038#ifdef MTK_MT8183
Gurchetan Singhdc9b1202019-06-04 16:53:54 -070039static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV21, DRM_FORMAT_NV12,
40 DRM_FORMAT_YUYV, DRM_FORMAT_YVU420,
41 DRM_FORMAT_YVU420_ANDROID };
Nick Fan01c40142018-10-08 11:53:26 +080042#else
Gurchetan Singhdc9b1202019-06-04 16:53:54 -070043static const uint32_t texture_source_formats[] = { DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID,
44 DRM_FORMAT_NV12 };
Nick Fan01c40142018-10-08 11:53:26 +080045#endif
Gurchetan Singh179687e2016-10-28 10:07:35 -070046
47static int mediatek_init(struct driver *drv)
48{
Miguel Casasdea0ccb2018-07-02 09:40:25 -040049 struct format_metadata metadata;
50
Gurchetan Singhd3001452017-11-03 17:18:36 -070051 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
Gurchetan Singh1914f982020-03-24 13:53:51 -070052 &LINEAR_METADATA, BO_USE_RENDER_MASK | BO_USE_SCANOUT);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070053
Gurchetan Singhd3001452017-11-03 17:18:36 -070054 drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
55 &LINEAR_METADATA, BO_USE_TEXTURE_MASK);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070056
Gurchetan Singh8312ec22019-09-24 10:37:59 -070057 drv_add_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA,
58 BO_USE_SW_MASK | BO_USE_LINEAR | BO_USE_PROTECTED);
Hirokazu Honda3b8d4d02019-07-31 16:35:52 +090059 /*
60 * Chrome uses DMA-buf mmap to write to YV12 buffers, which are then accessed by the
61 * Video Encoder Accelerator (VEA). It could also support NV12 potentially in the future.
62 */
63 drv_modify_combination(drv, DRM_FORMAT_YVU420, &LINEAR_METADATA, BO_USE_HW_VIDEO_ENCODER);
64 drv_modify_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA, BO_USE_HW_VIDEO_ENCODER);
65
Gurchetan Singh71bc6652018-09-17 17:42:05 -070066 /* Android CTS tests require this. */
67 drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
68
Miguel Casasdea0ccb2018-07-02 09:40:25 -040069 /* Support BO_USE_HW_VIDEO_DECODER for protected content minigbm allocations. */
70 metadata.tiling = TILE_TYPE_LINEAR;
71 metadata.priority = 1;
72 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
73 drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata, BO_USE_HW_VIDEO_DECODER);
74 drv_modify_combination(drv, DRM_FORMAT_YVU420_ANDROID, &metadata, BO_USE_HW_VIDEO_DECODER);
Hirokazu Honda0f0ce6f2019-07-24 19:40:20 +090075 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata, BO_USE_HW_VIDEO_DECODER);
Miguel Casasdea0ccb2018-07-02 09:40:25 -040076
Nick Fan01c40142018-10-08 11:53:26 +080077#ifdef MTK_MT8183
78 /* Only for MT8183 Camera subsystem */
79 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
80 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
81 drv_modify_combination(drv, DRM_FORMAT_NV21, &metadata,
82 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
83 drv_modify_combination(drv, DRM_FORMAT_YUYV, &metadata,
84 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
85 drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata,
86 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
87 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
Gurchetan Singh39490e92019-05-28 17:49:09 -070088 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
Jasmine Chenc7aa9742019-08-14 15:28:22 +080089 /* Private formats for private reprocessing in camera */
90 drv_add_combination(drv, DRM_FORMAT_MTISP_SXYZW10, &metadata,
91 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SW_MASK);
Nick Fan01c40142018-10-08 11:53:26 +080092#endif
93
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070094 return drv_modify_linear_combinations(drv);
Gurchetan Singh179687e2016-10-28 10:07:35 -070095}
JB Tsai0c16a0f2015-03-19 14:30:31 +080096
Fritz Koenig1b9b5b92019-03-19 13:25:45 -070097static int mediatek_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
98 uint32_t format, const uint64_t *modifiers,
99 uint32_t count)
JB Tsai0c16a0f2015-03-19 14:30:31 +0800100{
JB Tsai0c16a0f2015-03-19 14:30:31 +0800101 int ret;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700102 size_t plane;
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700103 uint32_t stride;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700104 struct drm_mtk_gem_create gem_create;
JB Tsai0c16a0f2015-03-19 14:30:31 +0800105
Fritz Koenig1b9b5b92019-03-19 13:25:45 -0700106 if (!drv_has_modifier(modifiers, count, DRM_FORMAT_MOD_LINEAR)) {
107 errno = EINVAL;
108 drv_log("no usable modifier found\n");
109 return -EINVAL;
110 }
111
Gurchetan Singh6ea14ba2017-02-08 15:09:13 -0800112 /*
113 * Since the ARM L1 cache line size is 64 bytes, align to that as a
114 * performance optimization.
115 */
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700116 stride = drv_stride_from_format(format, width, 0);
117 stride = ALIGN(stride, 64);
Hirokazu Honda2a2bfc22019-10-11 15:54:50 +0900118
119 if (bo->meta.use_flags & BO_USE_HW_VIDEO_ENCODER) {
120 uint32_t aligned_height = ALIGN(height, 32);
121 uint32_t padding[DRV_MAX_PLANES] = { 0 };
122
123 for (plane = 0; plane < bo->meta.num_planes; ++plane) {
124 uint32_t plane_stride = drv_stride_from_format(format, stride, plane);
125 padding[plane] = plane_stride *
126 (32 / drv_vertical_subsampling_from_format(format, plane));
127 }
128
129 drv_bo_from_format_and_padding(bo, stride, aligned_height, format, padding);
130 } else {
Moja Hsu059ac082019-10-02 14:47:10 +0800131#ifdef MTK_MT8183
132 /*
133 * JPEG Encoder Accelerator requires 16x16 alignment. We want the buffer
134 * from camera can be put in JEA directly so align the height to 16
135 * bytes.
136 */
137 if (format == DRM_FORMAT_NV12)
138 height = ALIGN(height, 16);
139#endif
Hirokazu Honda2a2bfc22019-10-11 15:54:50 +0900140 drv_bo_from_format(bo, stride, height, format);
141 }
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500142
JB Tsai0c16a0f2015-03-19 14:30:31 +0800143 memset(&gem_create, 0, sizeof(gem_create));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700144 gem_create.size = bo->meta.total_size;
JB Tsai0c16a0f2015-03-19 14:30:31 +0800145
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700146 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_CREATE, &gem_create);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700147 if (ret) {
Nicolas Boichatd7c83382019-08-29 21:46:29 +0800148 drv_log("DRM_IOCTL_MTK_GEM_CREATE failed (size=%" PRIu64 ")\n", gem_create.size);
Stéphane Marchesin6ac299f2019-03-21 12:23:29 -0700149 return -errno;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700150 }
JB Tsai0c16a0f2015-03-19 14:30:31 +0800151
Gurchetan Singh298b7572019-09-19 09:55:18 -0700152 for (plane = 0; plane < bo->meta.num_planes; plane++)
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700153 bo->handles[plane].u32 = gem_create.handle;
JB Tsai0c16a0f2015-03-19 14:30:31 +0800154
155 return 0;
156}
157
Fritz Koenig1b9b5b92019-03-19 13:25:45 -0700158static int mediatek_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
159 uint64_t use_flags)
160{
161 uint64_t modifiers[] = { DRM_FORMAT_MOD_LINEAR };
162 return mediatek_bo_create_with_modifiers(bo, width, height, format, modifiers,
163 ARRAY_SIZE(modifiers));
164}
165
Gurchetan Singhee43c302017-11-14 18:20:27 -0800166static void *mediatek_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Gurchetan Singhef920532016-08-12 16:38:25 -0700167{
Luigi Santivetti500928f2018-08-28 10:09:20 +0100168 int ret, prime_fd;
Gurchetan Singhef920532016-08-12 16:38:25 -0700169 struct drm_mtk_gem_map_off gem_map;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700170 struct mediatek_private_map_data *priv;
Gurchetan Singhef920532016-08-12 16:38:25 -0700171
172 memset(&gem_map, 0, sizeof(gem_map));
173 gem_map.handle = bo->handles[0].u32;
174
175 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_MAP_OFFSET, &gem_map);
176 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700177 drv_log("DRM_IOCTL_MTK_GEM_MAP_OFFSET failed\n");
Gurchetan Singhef920532016-08-12 16:38:25 -0700178 return MAP_FAILED;
179 }
180
David Stevensddb56b52020-03-13 15:24:37 +0900181 prime_fd = drv_bo_get_plane_fd(bo, 0);
182 if (prime_fd < 0) {
Luigi Santivetti500928f2018-08-28 10:09:20 +0100183 drv_log("Failed to get a prime fd\n");
184 return MAP_FAILED;
185 }
186
Gurchetan Singh298b7572019-09-19 09:55:18 -0700187 void *addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700188 gem_map.offset);
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700189
Gurchetan Singh298b7572019-09-19 09:55:18 -0700190 vma->length = bo->meta.total_size;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700191
Luigi Santivetti500928f2018-08-28 10:09:20 +0100192 priv = calloc(1, sizeof(*priv));
193 priv->prime_fd = prime_fd;
194 vma->priv = priv;
195
Gurchetan Singh298b7572019-09-19 09:55:18 -0700196 if (bo->meta.use_flags & BO_USE_RENDERSCRIPT) {
197 priv->cached_addr = calloc(1, bo->meta.total_size);
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700198 priv->gem_addr = addr;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700199 addr = priv->cached_addr;
200 }
201
202 return addr;
203}
204
Gurchetan Singhee43c302017-11-14 18:20:27 -0800205static int mediatek_bo_unmap(struct bo *bo, struct vma *vma)
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700206{
Gurchetan Singhee43c302017-11-14 18:20:27 -0800207 if (vma->priv) {
208 struct mediatek_private_map_data *priv = vma->priv;
Luigi Santivettia72f4422018-09-12 16:28:21 +0100209
210 if (priv->cached_addr) {
211 vma->addr = priv->gem_addr;
212 free(priv->cached_addr);
213 }
214
Luigi Santivetti500928f2018-08-28 10:09:20 +0100215 close(priv->prime_fd);
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700216 free(priv);
Gurchetan Singhee43c302017-11-14 18:20:27 -0800217 vma->priv = NULL;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700218 }
219
Gurchetan Singhee43c302017-11-14 18:20:27 -0800220 return munmap(vma->addr, vma->length);
Gurchetan Singhef920532016-08-12 16:38:25 -0700221}
222
Gurchetan Singhef262d82017-11-28 16:56:17 -0800223static int mediatek_bo_invalidate(struct bo *bo, struct mapping *mapping)
224{
Luigi Santivetti500928f2018-08-28 10:09:20 +0100225 struct mediatek_private_map_data *priv = mapping->vma->priv;
226
227 if (priv) {
228 struct pollfd fds = {
229 .fd = priv->prime_fd,
230 };
231
232 if (mapping->vma->map_flags & BO_MAP_WRITE)
233 fds.events |= POLLOUT;
234
235 if (mapping->vma->map_flags & BO_MAP_READ)
236 fds.events |= POLLIN;
237
238 poll(&fds, 1, -1);
239 if (fds.revents != fds.events)
240 drv_log("poll prime_fd failed\n");
241
242 if (priv->cached_addr)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700243 memcpy(priv->cached_addr, priv->gem_addr, bo->meta.total_size);
Gurchetan Singhef262d82017-11-28 16:56:17 -0800244 }
245
246 return 0;
247}
248
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700249static int mediatek_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700250{
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700251 struct mediatek_private_map_data *priv = mapping->vma->priv;
Luigi Santivetti500928f2018-08-28 10:09:20 +0100252 if (priv && priv->cached_addr && (mapping->vma->map_flags & BO_MAP_WRITE))
Gurchetan Singh298b7572019-09-19 09:55:18 -0700253 memcpy(priv->gem_addr, priv->cached_addr, bo->meta.total_size);
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700254
255 return 0;
256}
257
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700258static uint32_t mediatek_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700259{
260 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800261 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Nick Fan01c40142018-10-08 11:53:26 +0800262#ifdef MTK_MT8183
Jasmine Chenc7aa9742019-08-14 15:28:22 +0800263 /* Only MT8183 Camera subsystem offers private reprocessing
264 * capability. CAMERA_READ indicates the buffer is intended for
265 * reprocessing and hence given the private format for MTK. */
266 if (use_flags & BO_USE_CAMERA_READ)
267 return DRM_FORMAT_MTISP_SXYZW10;
268 /* For non-reprocessing uses, only MT8183 Camera subsystem
269 * requires NV12. */
270 else if (use_flags & BO_USE_CAMERA_WRITE)
Nick Fan01c40142018-10-08 11:53:26 +0800271 return DRM_FORMAT_NV12;
272#endif
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700273 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800274 return DRM_FORMAT_XBGR8888;
275 case DRM_FORMAT_FLEX_YCbCr_420_888:
Nick Fan01c40142018-10-08 11:53:26 +0800276#ifdef MTK_MT8183
Alexandre Courbot6ad63822019-10-01 16:07:31 +0900277 /* MT8183 camera and decoder subsystems require NV12. */
Hirokazu Honda2a2bfc22019-10-11 15:54:50 +0900278 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
279 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER)) {
Gurchetan Singh39490e92019-05-28 17:49:09 -0700280 return DRM_FORMAT_NV12;
Hirokazu Honda2a2bfc22019-10-11 15:54:50 +0900281 }
Nick Fan01c40142018-10-08 11:53:26 +0800282#endif
Hirokazu Honda3d856022019-08-23 14:31:31 +0900283 return DRM_FORMAT_YVU420;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700284 default:
285 return format;
286 }
287}
288
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700289const struct backend backend_mediatek = {
JB Tsai0c16a0f2015-03-19 14:30:31 +0800290 .name = "mediatek",
Gurchetan Singh179687e2016-10-28 10:07:35 -0700291 .init = mediatek_init,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700292 .bo_create = mediatek_bo_create,
Fritz Koenig1b9b5b92019-03-19 13:25:45 -0700293 .bo_create_with_modifiers = mediatek_bo_create_with_modifiers,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700294 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singh71611d62017-01-03 16:49:56 -0800295 .bo_import = drv_prime_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700296 .bo_map = mediatek_bo_map,
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700297 .bo_unmap = mediatek_bo_unmap,
Gurchetan Singhef262d82017-11-28 16:56:17 -0800298 .bo_invalidate = mediatek_bo_invalidate,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700299 .bo_flush = mediatek_bo_flush,
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700300 .resolve_format = mediatek_resolve_format,
JB Tsai0c16a0f2015-03-19 14:30:31 +0800301};
302
303#endif