blob: bf1c09a6b5ab12caa2c7ef05dafb8a36642e5ce0 [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 Hondafd8b8ab2020-06-16 15:28:56 +090059
Gurchetan Singh71bc6652018-09-17 17:42:05 -070060 /* Android CTS tests require this. */
61 drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
62
Miguel Casasdea0ccb2018-07-02 09:40:25 -040063 /* Support BO_USE_HW_VIDEO_DECODER for protected content minigbm allocations. */
64 metadata.tiling = TILE_TYPE_LINEAR;
65 metadata.priority = 1;
66 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
67 drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata, BO_USE_HW_VIDEO_DECODER);
68 drv_modify_combination(drv, DRM_FORMAT_YVU420_ANDROID, &metadata, BO_USE_HW_VIDEO_DECODER);
Hirokazu Honda0f0ce6f2019-07-24 19:40:20 +090069 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata, BO_USE_HW_VIDEO_DECODER);
Miguel Casasdea0ccb2018-07-02 09:40:25 -040070
David Stevens49518142020-06-15 13:48:48 +090071 /*
72 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB for input/output from
73 * hardware decoder/encoder.
74 */
75 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
76 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
77
Nick Fan01c40142018-10-08 11:53:26 +080078#ifdef MTK_MT8183
Hirokazu Honda3bd681c2020-06-23 17:52:20 +090079 /* NV12 format for encoding and display. */
80 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
81 BO_USE_SCANOUT | BO_USE_HW_VIDEO_ENCODER);
82
Nick Fan01c40142018-10-08 11:53:26 +080083 /* Only for MT8183 Camera subsystem */
84 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
85 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
86 drv_modify_combination(drv, DRM_FORMAT_NV21, &metadata,
87 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
88 drv_modify_combination(drv, DRM_FORMAT_YUYV, &metadata,
89 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
90 drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata,
91 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
92 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
Gurchetan Singh39490e92019-05-28 17:49:09 -070093 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
Jasmine Chenc7aa9742019-08-14 15:28:22 +080094 /* 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);
Nick Fan01c40142018-10-08 11:53:26 +080097#endif
98
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070099 return drv_modify_linear_combinations(drv);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700100}
JB Tsai0c16a0f2015-03-19 14:30:31 +0800101
Fritz Koenig1b9b5b92019-03-19 13:25:45 -0700102static 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)
JB Tsai0c16a0f2015-03-19 14:30:31 +0800105{
JB Tsai0c16a0f2015-03-19 14:30:31 +0800106 int ret;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700107 size_t plane;
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700108 uint32_t stride;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700109 struct drm_mtk_gem_create gem_create;
JB Tsai0c16a0f2015-03-19 14:30:31 +0800110
Fritz Koenig1b9b5b92019-03-19 13:25:45 -0700111 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
Gurchetan Singh6ea14ba2017-02-08 15:09:13 -0800117 /*
118 * Since the ARM L1 cache line size is 64 bytes, align to that as a
119 * performance optimization.
120 */
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700121 stride = drv_stride_from_format(format, width, 0);
122 stride = ALIGN(stride, 64);
Hirokazu Honda2a2bfc22019-10-11 15:54:50 +0900123
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 {
Moja Hsu059ac082019-10-02 14:47:10 +0800136#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
Hirokazu Honda2a2bfc22019-10-11 15:54:50 +0900145 drv_bo_from_format(bo, stride, height, format);
146 }
Yuly Novikov96c7a3b2015-12-08 22:48:29 -0500147
JB Tsai0c16a0f2015-03-19 14:30:31 +0800148 memset(&gem_create, 0, sizeof(gem_create));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700149 gem_create.size = bo->meta.total_size;
JB Tsai0c16a0f2015-03-19 14:30:31 +0800150
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700151 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_CREATE, &gem_create);
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700152 if (ret) {
Nicolas Boichatd7c83382019-08-29 21:46:29 +0800153 drv_log("DRM_IOCTL_MTK_GEM_CREATE failed (size=%" PRIu64 ")\n", gem_create.size);
Stéphane Marchesin6ac299f2019-03-21 12:23:29 -0700154 return -errno;
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -0700155 }
JB Tsai0c16a0f2015-03-19 14:30:31 +0800156
Gurchetan Singh298b7572019-09-19 09:55:18 -0700157 for (plane = 0; plane < bo->meta.num_planes; plane++)
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700158 bo->handles[plane].u32 = gem_create.handle;
JB Tsai0c16a0f2015-03-19 14:30:31 +0800159
160 return 0;
161}
162
Fritz Koenig1b9b5b92019-03-19 13:25:45 -0700163static int mediatek_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
164 uint64_t use_flags)
165{
166 uint64_t modifiers[] = { DRM_FORMAT_MOD_LINEAR };
167 return mediatek_bo_create_with_modifiers(bo, width, height, format, modifiers,
168 ARRAY_SIZE(modifiers));
169}
170
Gurchetan Singhee43c302017-11-14 18:20:27 -0800171static void *mediatek_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Gurchetan Singhef920532016-08-12 16:38:25 -0700172{
Luigi Santivetti500928f2018-08-28 10:09:20 +0100173 int ret, prime_fd;
Gurchetan Singhef920532016-08-12 16:38:25 -0700174 struct drm_mtk_gem_map_off gem_map;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700175 struct mediatek_private_map_data *priv;
Gurchetan Singhef920532016-08-12 16:38:25 -0700176
177 memset(&gem_map, 0, sizeof(gem_map));
178 gem_map.handle = bo->handles[0].u32;
179
180 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_MAP_OFFSET, &gem_map);
181 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700182 drv_log("DRM_IOCTL_MTK_GEM_MAP_OFFSET failed\n");
Gurchetan Singhef920532016-08-12 16:38:25 -0700183 return MAP_FAILED;
184 }
185
David Stevensddb56b52020-03-13 15:24:37 +0900186 prime_fd = drv_bo_get_plane_fd(bo, 0);
187 if (prime_fd < 0) {
Luigi Santivetti500928f2018-08-28 10:09:20 +0100188 drv_log("Failed to get a prime fd\n");
189 return MAP_FAILED;
190 }
191
Gurchetan Singh298b7572019-09-19 09:55:18 -0700192 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 -0700193 gem_map.offset);
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700194
Gurchetan Singh298b7572019-09-19 09:55:18 -0700195 vma->length = bo->meta.total_size;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700196
Luigi Santivetti500928f2018-08-28 10:09:20 +0100197 priv = calloc(1, sizeof(*priv));
198 priv->prime_fd = prime_fd;
199 vma->priv = priv;
200
Gurchetan Singh298b7572019-09-19 09:55:18 -0700201 if (bo->meta.use_flags & BO_USE_RENDERSCRIPT) {
202 priv->cached_addr = calloc(1, bo->meta.total_size);
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700203 priv->gem_addr = addr;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700204 addr = priv->cached_addr;
205 }
206
207 return addr;
208}
209
Gurchetan Singhee43c302017-11-14 18:20:27 -0800210static int mediatek_bo_unmap(struct bo *bo, struct vma *vma)
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700211{
Gurchetan Singhee43c302017-11-14 18:20:27 -0800212 if (vma->priv) {
213 struct mediatek_private_map_data *priv = vma->priv;
Luigi Santivettia72f4422018-09-12 16:28:21 +0100214
215 if (priv->cached_addr) {
216 vma->addr = priv->gem_addr;
217 free(priv->cached_addr);
218 }
219
Luigi Santivetti500928f2018-08-28 10:09:20 +0100220 close(priv->prime_fd);
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700221 free(priv);
Gurchetan Singhee43c302017-11-14 18:20:27 -0800222 vma->priv = NULL;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700223 }
224
Gurchetan Singhee43c302017-11-14 18:20:27 -0800225 return munmap(vma->addr, vma->length);
Gurchetan Singhef920532016-08-12 16:38:25 -0700226}
227
Gurchetan Singhef262d82017-11-28 16:56:17 -0800228static int mediatek_bo_invalidate(struct bo *bo, struct mapping *mapping)
229{
Luigi Santivetti500928f2018-08-28 10:09:20 +0100230 struct mediatek_private_map_data *priv = mapping->vma->priv;
231
232 if (priv) {
233 struct pollfd fds = {
234 .fd = priv->prime_fd,
235 };
236
237 if (mapping->vma->map_flags & BO_MAP_WRITE)
238 fds.events |= POLLOUT;
239
240 if (mapping->vma->map_flags & BO_MAP_READ)
241 fds.events |= POLLIN;
242
243 poll(&fds, 1, -1);
244 if (fds.revents != fds.events)
245 drv_log("poll prime_fd failed\n");
246
247 if (priv->cached_addr)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700248 memcpy(priv->cached_addr, priv->gem_addr, bo->meta.total_size);
Gurchetan Singhef262d82017-11-28 16:56:17 -0800249 }
250
251 return 0;
252}
253
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700254static int mediatek_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700255{
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700256 struct mediatek_private_map_data *priv = mapping->vma->priv;
Luigi Santivetti500928f2018-08-28 10:09:20 +0100257 if (priv && priv->cached_addr && (mapping->vma->map_flags & BO_MAP_WRITE))
Gurchetan Singh298b7572019-09-19 09:55:18 -0700258 memcpy(priv->gem_addr, priv->cached_addr, bo->meta.total_size);
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700259
260 return 0;
261}
262
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700263static uint32_t mediatek_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700264{
265 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800266 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Nick Fan01c40142018-10-08 11:53:26 +0800267#ifdef MTK_MT8183
Jasmine Chenc7aa9742019-08-14 15:28:22 +0800268 /* Only MT8183 Camera subsystem offers private reprocessing
269 * capability. CAMERA_READ indicates the buffer is intended for
270 * reprocessing and hence given the private format for MTK. */
271 if (use_flags & BO_USE_CAMERA_READ)
272 return DRM_FORMAT_MTISP_SXYZW10;
273 /* For non-reprocessing uses, only MT8183 Camera subsystem
274 * requires NV12. */
275 else if (use_flags & BO_USE_CAMERA_WRITE)
Nick Fan01c40142018-10-08 11:53:26 +0800276 return DRM_FORMAT_NV12;
277#endif
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700278 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800279 return DRM_FORMAT_XBGR8888;
280 case DRM_FORMAT_FLEX_YCbCr_420_888:
Nick Fan01c40142018-10-08 11:53:26 +0800281#ifdef MTK_MT8183
Alexandre Courbot6ad63822019-10-01 16:07:31 +0900282 /* MT8183 camera and decoder subsystems require NV12. */
Hirokazu Honda2a2bfc22019-10-11 15:54:50 +0900283 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE |
284 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER)) {
Gurchetan Singh39490e92019-05-28 17:49:09 -0700285 return DRM_FORMAT_NV12;
Hirokazu Honda2a2bfc22019-10-11 15:54:50 +0900286 }
Nick Fan01c40142018-10-08 11:53:26 +0800287#endif
Hirokazu Honda3d856022019-08-23 14:31:31 +0900288 return DRM_FORMAT_YVU420;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700289 default:
290 return format;
291 }
292}
293
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700294const struct backend backend_mediatek = {
JB Tsai0c16a0f2015-03-19 14:30:31 +0800295 .name = "mediatek",
Gurchetan Singh179687e2016-10-28 10:07:35 -0700296 .init = mediatek_init,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700297 .bo_create = mediatek_bo_create,
Fritz Koenig1b9b5b92019-03-19 13:25:45 -0700298 .bo_create_with_modifiers = mediatek_bo_create_with_modifiers,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700299 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singh71611d62017-01-03 16:49:56 -0800300 .bo_import = drv_prime_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700301 .bo_map = mediatek_bo_map,
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700302 .bo_unmap = mediatek_bo_unmap,
Gurchetan Singhef262d82017-11-28 16:56:17 -0800303 .bo_invalidate = mediatek_bo_invalidate,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700304 .bo_flush = mediatek_bo_flush,
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700305 .resolve_format = mediatek_resolve_format,
JB Tsai0c16a0f2015-03-19 14:30:31 +0800306};
307
308#endif