blob: 6d25e3af62b1ccdfd23d37e6092942222df666d8 [file] [log] [blame]
Stéphane Marchesin25a26062014-09-12 16:18:59 -07001/*
Daniele Castagna7a755de2016-12-16 17:32:30 -05002 * Copyright 2014 The Chromium OS Authors. All rights reserved.
Stéphane Marchesin25a26062014-09-12 16:18:59 -07003 * 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_ROCKCHIP
Stéphane Marchesin25a26062014-09-12 16:18:59 -07008
Jason Macnakdbc63f72022-06-23 18:34:55 -07009#include <drm_fourcc.h>
Zach Reizner58080df2016-04-27 11:14:41 -070010#include <errno.h>
Nicolas Boichatd7c83382019-08-29 21:46:29 +080011#include <inttypes.h>
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080012#include <rockchip_drm.h>
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -070013#include <stdio.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070014#include <string.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070015#include <sys/mman.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070016#include <xf86drm.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070017
Yiwei Zhangb7a64442021-09-30 05:13:10 +000018#include "drv_helpers.h"
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070019#include "drv_priv.h"
Zach Reizner58080df2016-04-27 11:14:41 -070020#include "util.h"
Stéphane Marchesin25a26062014-09-12 16:18:59 -070021
Jason Macnakdbc63f72022-06-23 18:34:55 -070022#define DRM_FORMAT_MOD_ROCKCHIP_AFBC \
23 DRM_FORMAT_MOD_ARM_AFBC(AFBC_FORMAT_MOD_BLOCK_SIZE_16x16 | AFBC_FORMAT_MOD_SPARSE | \
24 AFBC_FORMAT_MOD_YTR)
25
Gurchetan Singh469a3aa2017-08-03 18:17:34 -070026struct rockchip_private_map_data {
27 void *cached_addr;
28 void *gem_addr;
29};
30
Gurchetan Singhc87a6d32019-12-19 10:51:14 -080031static const uint32_t scanout_render_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
32 DRM_FORMAT_BGR888, DRM_FORMAT_RGB565,
33 DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB8888 };
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070034
Gurchetan Singhc87a6d32019-12-19 10:51:14 -080035static const uint32_t texture_only_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_YVU420,
36 DRM_FORMAT_YVU420_ANDROID };
Gurchetan Singh179687e2016-10-28 10:07:35 -070037
Jason Macnakdbc63f72022-06-23 18:34:55 -070038static int afbc_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
39 uint64_t modifier)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -070040{
41 /* We've restricted ourselves to four bytes per pixel. */
42 const uint32_t pixel_size = 4;
43
44 const uint32_t clump_width = 4;
45 const uint32_t clump_height = 4;
46
47#define AFBC_NARROW 1
48#if AFBC_NARROW == 1
49 const uint32_t block_width = 4 * clump_width;
50 const uint32_t block_height = 4 * clump_height;
51#else
52 const uint32_t block_width = 8 * clump_width;
53 const uint32_t block_height = 2 * clump_height;
54#endif
55
56 const uint32_t header_block_size = 16;
57 const uint32_t body_block_size = block_width * block_height * pixel_size;
58 const uint32_t width_in_blocks = DIV_ROUND_UP(width, block_width);
59 const uint32_t height_in_blocks = DIV_ROUND_UP(height, block_height);
60 const uint32_t total_blocks = width_in_blocks * height_in_blocks;
61
62 const uint32_t header_plane_size = total_blocks * header_block_size;
63 const uint32_t body_plane_size = total_blocks * body_block_size;
64
65 /* GPU requires 64 bytes, but EGL import code expects 1024 byte
66 * alignement for the body plane. */
67 const uint32_t body_plane_alignment = 1024;
68
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080069 const uint32_t body_plane_offset = ALIGN(header_plane_size, body_plane_alignment);
70 const uint32_t total_size = body_plane_offset + body_plane_size;
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -070071
Gurchetan Singh298b7572019-09-19 09:55:18 -070072 bo->meta.strides[0] = width_in_blocks * block_width * pixel_size;
73 bo->meta.sizes[0] = total_size;
74 bo->meta.offsets[0] = 0;
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -070075
Gurchetan Singh298b7572019-09-19 09:55:18 -070076 bo->meta.total_size = total_size;
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -070077
Jason Macnakdbc63f72022-06-23 18:34:55 -070078 bo->meta.format_modifier = modifier;
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -070079
80 return 0;
81}
82
Gurchetan Singh179687e2016-10-28 10:07:35 -070083static int rockchip_init(struct driver *drv)
84{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080085 struct format_metadata metadata;
86
87 metadata.tiling = 0;
88 metadata.priority = 1;
Kristian H. Kristensenbc8c5932017-10-24 18:36:32 -070089 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080090
Gurchetan Singhc87a6d32019-12-19 10:51:14 -080091 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
92 &metadata, BO_USE_RENDER_MASK | BO_USE_SCANOUT);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070093
Gurchetan Singhc87a6d32019-12-19 10:51:14 -080094 drv_add_combinations(drv, texture_only_formats, ARRAY_SIZE(texture_only_formats), &metadata,
95 BO_USE_TEXTURE_MASK);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080096
Hirokazu Honda3bd681c2020-06-23 17:52:20 +090097 /* NV12 format for camera, display, decoding and encoding. */
Jeffy Chen55525f52017-09-19 17:15:30 +080098 /* Camera ISP supports only NV12 output. */
99 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
Hirokazu Honda3bd681c2020-06-23 17:52:20 +0900100 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT |
101 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
Gurchetan Singhc87a6d32019-12-19 10:51:14 -0800102
103 drv_modify_linear_combinations(drv);
Jeffy Chen55525f52017-09-19 17:15:30 +0800104 /*
105 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
David Stevens49518142020-06-15 13:48:48 +0900106 * from camera and input/output from hardware decoder/encoder.
Jeffy Chen55525f52017-09-19 17:15:30 +0800107 */
Gurchetan Singhdc9b1202019-06-04 16:53:54 -0700108 drv_add_combination(drv, DRM_FORMAT_R8, &metadata,
109 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SW_MASK |
Gurchetan Singhbbba9dd2020-10-12 17:31:10 -0700110 BO_USE_LINEAR | BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
Jeffy Chen55525f52017-09-19 17:15:30 +0800111
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800112 return 0;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700113}
114
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800115static int rockchip_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
116 uint32_t format, const uint64_t *modifiers,
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700117 uint32_t count)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700118{
Zach Reizner58080df2016-04-27 11:14:41 -0700119 int ret;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700120 size_t plane;
Gurchetan Singh99644382020-10-07 15:28:11 -0700121 struct drm_rockchip_gem_create gem_create = { 0 };
Jason Macnakdbc63f72022-06-23 18:34:55 -0700122 uint64_t afbc_modifier;
123
124 if (drv_has_modifier(modifiers, count, DRM_FORMAT_MOD_ROCKCHIP_AFBC))
125 afbc_modifier = DRM_FORMAT_MOD_ROCKCHIP_AFBC;
126 else if (drv_has_modifier(modifiers, count, DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC))
127 afbc_modifier = DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC;
128 else
129 afbc_modifier = 0;
Zach Reizner58080df2016-04-27 11:14:41 -0700130
Pin-chih Lin19643412017-07-25 08:06:26 +0000131 if (format == DRM_FORMAT_NV12) {
Hirokazu Hondab892a8f2019-08-01 16:37:47 +0900132 uint32_t w_mbs = DIV_ROUND_UP(width, 16);
133 uint32_t h_mbs = DIV_ROUND_UP(height, 16);
Pin-chih Lin19643412017-07-25 08:06:26 +0000134
135 uint32_t aligned_width = w_mbs * 16;
Hirokazu Honda785a5482020-01-14 04:08:12 +0000136 uint32_t aligned_height = h_mbs * 16;
Pin-chih Lin19643412017-07-25 08:06:26 +0000137
Hirokazu Honda785a5482020-01-14 04:08:12 +0000138 drv_bo_from_format(bo, aligned_width, aligned_height, format);
Hirokazu Hondab892a8f2019-08-01 16:37:47 +0900139 /*
140 * drv_bo_from_format updates total_size. Add an extra data space for rockchip video
141 * driver to store motion vectors.
142 */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700143 bo->meta.total_size += w_mbs * h_mbs * 128;
Jason Macnakdbc63f72022-06-23 18:34:55 -0700144 } else if (width <= 2560 && afbc_modifier && bo->drv->compression) {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700145 /* If the caller has decided they can use AFBC, always
146 * pick that */
Jason Macnakdbc63f72022-06-23 18:34:55 -0700147 afbc_bo_from_format(bo, width, height, format, afbc_modifier);
Gurchetan Singh10a11802016-09-23 15:27:07 -0700148 } else {
Fritz Koenig1b9b5b92019-03-19 13:25:45 -0700149 if (!drv_has_modifier(modifiers, count, DRM_FORMAT_MOD_LINEAR)) {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700150 errno = EINVAL;
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700151 drv_log("no usable modifier found\n");
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800152 return -errno;
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700153 }
Gurchetan Singh6ea14ba2017-02-08 15:09:13 -0800154
Pin-chih Lin19643412017-07-25 08:06:26 +0000155 uint32_t stride;
Gurchetan Singh6ea14ba2017-02-08 15:09:13 -0800156 /*
Pin-chih Lin19643412017-07-25 08:06:26 +0000157 * Since the ARM L1 cache line size is 64 bytes, align to that
158 * as a performance optimization. For YV12, the Mali cmem allocator
159 * requires that chroma planes are aligned to 64-bytes, so align the
160 * luma plane to 128 bytes.
Gurchetan Singh6ea14ba2017-02-08 15:09:13 -0800161 */
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700162 stride = drv_stride_from_format(format, width, 0);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800163 if (format == DRM_FORMAT_YVU420 || format == DRM_FORMAT_YVU420_ANDROID)
Gurchetan Singh4c3aa422017-03-23 18:47:06 -0700164 stride = ALIGN(stride, 128);
165 else
166 stride = ALIGN(stride, 64);
167
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700168 drv_bo_from_format(bo, stride, height, format);
Gurchetan Singh10a11802016-09-23 15:27:07 -0700169 }
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700170
Gurchetan Singh298b7572019-09-19 09:55:18 -0700171 gem_create.size = bo->meta.total_size;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800172 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_CREATE, &gem_create);
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700173
174 if (ret) {
Nicolas Boichatd7c83382019-08-29 21:46:29 +0800175 drv_log("DRM_IOCTL_ROCKCHIP_GEM_CREATE failed (size=%" PRIu64 ")\n",
176 gem_create.size);
Stéphane Marchesin6ac299f2019-03-21 12:23:29 -0700177 return -errno;
Zach Reizner58080df2016-04-27 11:14:41 -0700178 }
179
Gurchetan Singh298b7572019-09-19 09:55:18 -0700180 for (plane = 0; plane < bo->meta.num_planes; plane++)
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700181 bo->handles[plane].u32 = gem_create.handle;
182
183 return 0;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700184}
185
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800186static int rockchip_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700187 uint64_t use_flags)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700188{
Kristian H. Kristensenbc8c5932017-10-24 18:36:32 -0700189 uint64_t modifiers[] = { DRM_FORMAT_MOD_LINEAR };
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800190 return rockchip_bo_create_with_modifiers(bo, width, height, format, modifiers,
191 ARRAY_SIZE(modifiers));
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700192}
193
Gurchetan Singhee43c302017-11-14 18:20:27 -0800194static void *rockchip_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Gurchetan Singhef920532016-08-12 16:38:25 -0700195{
196 int ret;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700197 struct rockchip_private_map_data *priv;
Gurchetan Singh99644382020-10-07 15:28:11 -0700198 struct drm_rockchip_gem_map_off gem_map = { 0 };
Yiwei Zhangafdf87d2021-09-28 04:06:06 +0000199 void *addr = NULL;
Gurchetan Singhef920532016-08-12 16:38:25 -0700200
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700201 /* We can only map buffers created with SW access flags, which should
202 * have no modifiers (ie, not AFBC). */
Jason Macnakdbc63f72022-06-23 18:34:55 -0700203 if (bo->meta.format_modifier == DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC ||
204 bo->meta.format_modifier == DRM_FORMAT_MOD_ROCKCHIP_AFBC)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700205 return MAP_FAILED;
206
Gurchetan Singhef920532016-08-12 16:38:25 -0700207 gem_map.handle = bo->handles[0].u32;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800208 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET, &gem_map);
Gurchetan Singhef920532016-08-12 16:38:25 -0700209 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700210 drv_log("DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET failed\n");
Gurchetan Singhef920532016-08-12 16:38:25 -0700211 return MAP_FAILED;
212 }
213
Yiwei Zhangafdf87d2021-09-28 04:06:06 +0000214 addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
215 gem_map.offset);
216 if (addr == MAP_FAILED)
217 return MAP_FAILED;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700218
Gurchetan Singh298b7572019-09-19 09:55:18 -0700219 vma->length = bo->meta.total_size;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700220
Gurchetan Singh298b7572019-09-19 09:55:18 -0700221 if (bo->meta.use_flags & BO_USE_RENDERSCRIPT) {
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700222 priv = calloc(1, sizeof(*priv));
Yiwei Zhangafdf87d2021-09-28 04:06:06 +0000223 if (!priv)
224 goto out_unmap_addr;
225
Gurchetan Singh298b7572019-09-19 09:55:18 -0700226 priv->cached_addr = calloc(1, bo->meta.total_size);
Yiwei Zhangafdf87d2021-09-28 04:06:06 +0000227 if (!priv->cached_addr)
228 goto out_free_priv;
229
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700230 priv->gem_addr = addr;
Gurchetan Singhee43c302017-11-14 18:20:27 -0800231 vma->priv = priv;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700232 addr = priv->cached_addr;
233 }
234
235 return addr;
Yiwei Zhangafdf87d2021-09-28 04:06:06 +0000236
237out_free_priv:
238 free(priv);
239out_unmap_addr:
240 munmap(addr, bo->meta.total_size);
241 return MAP_FAILED;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700242}
243
Gurchetan Singhee43c302017-11-14 18:20:27 -0800244static int rockchip_bo_unmap(struct bo *bo, struct vma *vma)
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700245{
Gurchetan Singhee43c302017-11-14 18:20:27 -0800246 if (vma->priv) {
247 struct rockchip_private_map_data *priv = vma->priv;
248 vma->addr = priv->gem_addr;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700249 free(priv->cached_addr);
250 free(priv);
Gurchetan Singhee43c302017-11-14 18:20:27 -0800251 vma->priv = NULL;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700252 }
253
Gurchetan Singhee43c302017-11-14 18:20:27 -0800254 return munmap(vma->addr, vma->length);
Gurchetan Singhef920532016-08-12 16:38:25 -0700255}
256
Gurchetan Singhef262d82017-11-28 16:56:17 -0800257static int rockchip_bo_invalidate(struct bo *bo, struct mapping *mapping)
258{
259 if (mapping->vma->priv) {
260 struct rockchip_private_map_data *priv = mapping->vma->priv;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700261 memcpy(priv->cached_addr, priv->gem_addr, bo->meta.total_size);
Gurchetan Singhef262d82017-11-28 16:56:17 -0800262 }
263
264 return 0;
265}
266
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700267static int rockchip_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700268{
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700269 struct rockchip_private_map_data *priv = mapping->vma->priv;
270 if (priv && (mapping->vma->map_flags & BO_MAP_WRITE))
Gurchetan Singh298b7572019-09-19 09:55:18 -0700271 memcpy(priv->gem_addr, priv->cached_addr, bo->meta.total_size);
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700272
273 return 0;
274}
275
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700276const struct backend backend_rockchip = {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700277 .name = "rockchip",
Gurchetan Singh179687e2016-10-28 10:07:35 -0700278 .init = rockchip_init,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700279 .bo_create = rockchip_bo_create,
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700280 .bo_create_with_modifiers = rockchip_bo_create_with_modifiers,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700281 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singh71611d62017-01-03 16:49:56 -0800282 .bo_import = drv_prime_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700283 .bo_map = rockchip_bo_map,
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700284 .bo_unmap = rockchip_bo_unmap,
Gurchetan Singhef262d82017-11-28 16:56:17 -0800285 .bo_invalidate = rockchip_bo_invalidate,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700286 .bo_flush = rockchip_bo_flush,
Yiwei Zhangb8ad7b82021-10-01 17:55:14 +0000287 .resolve_format_and_use_flags = drv_resolve_format_and_use_flags_helper,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700288};
289
290#endif