blob: 30d9fbe4453295f801643ad3170b54f9aef8fd2d [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
Zach Reizner58080df2016-04-27 11:14:41 -07009#include <errno.h>
Nicolas Boichatd7c83382019-08-29 21:46:29 +080010#include <inttypes.h>
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080011#include <rockchip_drm.h>
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -070012#include <stdio.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070013#include <string.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070014#include <sys/mman.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070015#include <xf86drm.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070016
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070017#include "drv_priv.h"
Dominik Behre13ac282015-01-13 00:59:21 -080018#include "helpers.h"
Zach Reizner58080df2016-04-27 11:14:41 -070019#include "util.h"
Stéphane Marchesin25a26062014-09-12 16:18:59 -070020
Gurchetan Singh469a3aa2017-08-03 18:17:34 -070021struct rockchip_private_map_data {
22 void *cached_addr;
23 void *gem_addr;
24};
25
Gurchetan Singhc87a6d32019-12-19 10:51:14 -080026static const uint32_t scanout_render_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
27 DRM_FORMAT_BGR888, DRM_FORMAT_RGB565,
28 DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB8888 };
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070029
Gurchetan Singhc87a6d32019-12-19 10:51:14 -080030static const uint32_t texture_only_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_YVU420,
31 DRM_FORMAT_YVU420_ANDROID };
Gurchetan Singh179687e2016-10-28 10:07:35 -070032
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080033static int afbc_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, uint32_t format)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -070034{
35 /* We've restricted ourselves to four bytes per pixel. */
36 const uint32_t pixel_size = 4;
37
38 const uint32_t clump_width = 4;
39 const uint32_t clump_height = 4;
40
41#define AFBC_NARROW 1
42#if AFBC_NARROW == 1
43 const uint32_t block_width = 4 * clump_width;
44 const uint32_t block_height = 4 * clump_height;
45#else
46 const uint32_t block_width = 8 * clump_width;
47 const uint32_t block_height = 2 * clump_height;
48#endif
49
50 const uint32_t header_block_size = 16;
51 const uint32_t body_block_size = block_width * block_height * pixel_size;
52 const uint32_t width_in_blocks = DIV_ROUND_UP(width, block_width);
53 const uint32_t height_in_blocks = DIV_ROUND_UP(height, block_height);
54 const uint32_t total_blocks = width_in_blocks * height_in_blocks;
55
56 const uint32_t header_plane_size = total_blocks * header_block_size;
57 const uint32_t body_plane_size = total_blocks * body_block_size;
58
59 /* GPU requires 64 bytes, but EGL import code expects 1024 byte
60 * alignement for the body plane. */
61 const uint32_t body_plane_alignment = 1024;
62
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080063 const uint32_t body_plane_offset = ALIGN(header_plane_size, body_plane_alignment);
64 const uint32_t total_size = body_plane_offset + body_plane_size;
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -070065
Gurchetan Singh298b7572019-09-19 09:55:18 -070066 bo->meta.strides[0] = width_in_blocks * block_width * pixel_size;
67 bo->meta.sizes[0] = total_size;
68 bo->meta.offsets[0] = 0;
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -070069
Gurchetan Singh298b7572019-09-19 09:55:18 -070070 bo->meta.total_size = total_size;
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -070071
Gurchetan Singh298b7572019-09-19 09:55:18 -070072 bo->meta.format_modifiers[0] = DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC;
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -070073
74 return 0;
75}
76
Gurchetan Singh179687e2016-10-28 10:07:35 -070077static int rockchip_init(struct driver *drv)
78{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080079 struct format_metadata metadata;
80
81 metadata.tiling = 0;
82 metadata.priority = 1;
Kristian H. Kristensenbc8c5932017-10-24 18:36:32 -070083 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080084
Gurchetan Singhc87a6d32019-12-19 10:51:14 -080085 drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats),
86 &metadata, BO_USE_RENDER_MASK | BO_USE_SCANOUT);
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070087
Gurchetan Singhc87a6d32019-12-19 10:51:14 -080088 drv_add_combinations(drv, texture_only_formats, ARRAY_SIZE(texture_only_formats), &metadata,
89 BO_USE_TEXTURE_MASK);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080090
Hirokazu Honda3bd681c2020-06-23 17:52:20 +090091 /* NV12 format for camera, display, decoding and encoding. */
Jeffy Chen55525f52017-09-19 17:15:30 +080092 /* Camera ISP supports only NV12 output. */
93 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
Hirokazu Honda3bd681c2020-06-23 17:52:20 +090094 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT |
95 BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
Gurchetan Singhc87a6d32019-12-19 10:51:14 -080096
97 drv_modify_linear_combinations(drv);
Jeffy Chen55525f52017-09-19 17:15:30 +080098 /*
99 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
David Stevens49518142020-06-15 13:48:48 +0900100 * from camera and input/output from hardware decoder/encoder.
Jeffy Chen55525f52017-09-19 17:15:30 +0800101 */
Gurchetan Singhdc9b1202019-06-04 16:53:54 -0700102 drv_add_combination(drv, DRM_FORMAT_R8, &metadata,
103 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SW_MASK |
Gurchetan Singhbbba9dd2020-10-12 17:31:10 -0700104 BO_USE_LINEAR | BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER);
Jeffy Chen55525f52017-09-19 17:15:30 +0800105
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800106 return 0;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700107}
108
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800109static int rockchip_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
110 uint32_t format, const uint64_t *modifiers,
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700111 uint32_t count)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700112{
Zach Reizner58080df2016-04-27 11:14:41 -0700113 int ret;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700114 size_t plane;
Gurchetan Singh99644382020-10-07 15:28:11 -0700115 struct drm_rockchip_gem_create gem_create = { 0 };
Zach Reizner58080df2016-04-27 11:14:41 -0700116
Pin-chih Lin19643412017-07-25 08:06:26 +0000117 if (format == DRM_FORMAT_NV12) {
Hirokazu Hondab892a8f2019-08-01 16:37:47 +0900118 uint32_t w_mbs = DIV_ROUND_UP(width, 16);
119 uint32_t h_mbs = DIV_ROUND_UP(height, 16);
Pin-chih Lin19643412017-07-25 08:06:26 +0000120
121 uint32_t aligned_width = w_mbs * 16;
Hirokazu Honda785a5482020-01-14 04:08:12 +0000122 uint32_t aligned_height = h_mbs * 16;
Pin-chih Lin19643412017-07-25 08:06:26 +0000123
Hirokazu Honda785a5482020-01-14 04:08:12 +0000124 drv_bo_from_format(bo, aligned_width, aligned_height, format);
Hirokazu Hondab892a8f2019-08-01 16:37:47 +0900125 /*
126 * drv_bo_from_format updates total_size. Add an extra data space for rockchip video
127 * driver to store motion vectors.
128 */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700129 bo->meta.total_size += w_mbs * h_mbs * 128;
Pin-chih Lin19643412017-07-25 08:06:26 +0000130 } else if (width <= 2560 &&
Pilar Molina Lopez28cf2f12020-11-12 18:19:42 -0500131 drv_has_modifier(modifiers, count, DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC) &&
132 bo->drv->compression) {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700133 /* If the caller has decided they can use AFBC, always
134 * pick that */
135 afbc_bo_from_format(bo, width, height, format);
Gurchetan Singh10a11802016-09-23 15:27:07 -0700136 } else {
Fritz Koenig1b9b5b92019-03-19 13:25:45 -0700137 if (!drv_has_modifier(modifiers, count, DRM_FORMAT_MOD_LINEAR)) {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700138 errno = EINVAL;
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700139 drv_log("no usable modifier found\n");
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700140 return -1;
141 }
Gurchetan Singh6ea14ba2017-02-08 15:09:13 -0800142
Pin-chih Lin19643412017-07-25 08:06:26 +0000143 uint32_t stride;
Gurchetan Singh6ea14ba2017-02-08 15:09:13 -0800144 /*
Pin-chih Lin19643412017-07-25 08:06:26 +0000145 * Since the ARM L1 cache line size is 64 bytes, align to that
146 * as a performance optimization. For YV12, the Mali cmem allocator
147 * requires that chroma planes are aligned to 64-bytes, so align the
148 * luma plane to 128 bytes.
Gurchetan Singh6ea14ba2017-02-08 15:09:13 -0800149 */
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700150 stride = drv_stride_from_format(format, width, 0);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800151 if (format == DRM_FORMAT_YVU420 || format == DRM_FORMAT_YVU420_ANDROID)
Gurchetan Singh4c3aa422017-03-23 18:47:06 -0700152 stride = ALIGN(stride, 128);
153 else
154 stride = ALIGN(stride, 64);
155
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700156 drv_bo_from_format(bo, stride, height, format);
Gurchetan Singh10a11802016-09-23 15:27:07 -0700157 }
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700158
Gurchetan Singh298b7572019-09-19 09:55:18 -0700159 gem_create.size = bo->meta.total_size;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800160 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_CREATE, &gem_create);
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700161
162 if (ret) {
Nicolas Boichatd7c83382019-08-29 21:46:29 +0800163 drv_log("DRM_IOCTL_ROCKCHIP_GEM_CREATE failed (size=%" PRIu64 ")\n",
164 gem_create.size);
Stéphane Marchesin6ac299f2019-03-21 12:23:29 -0700165 return -errno;
Zach Reizner58080df2016-04-27 11:14:41 -0700166 }
167
Gurchetan Singh298b7572019-09-19 09:55:18 -0700168 for (plane = 0; plane < bo->meta.num_planes; plane++)
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700169 bo->handles[plane].u32 = gem_create.handle;
170
171 return 0;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700172}
173
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800174static int rockchip_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700175 uint64_t use_flags)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700176{
Kristian H. Kristensenbc8c5932017-10-24 18:36:32 -0700177 uint64_t modifiers[] = { DRM_FORMAT_MOD_LINEAR };
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800178 return rockchip_bo_create_with_modifiers(bo, width, height, format, modifiers,
179 ARRAY_SIZE(modifiers));
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700180}
181
Gurchetan Singhee43c302017-11-14 18:20:27 -0800182static void *rockchip_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
Gurchetan Singhef920532016-08-12 16:38:25 -0700183{
184 int ret;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700185 struct rockchip_private_map_data *priv;
Gurchetan Singh99644382020-10-07 15:28:11 -0700186 struct drm_rockchip_gem_map_off gem_map = { 0 };
Gurchetan Singhef920532016-08-12 16:38:25 -0700187
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700188 /* We can only map buffers created with SW access flags, which should
189 * have no modifiers (ie, not AFBC). */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700190 if (bo->meta.format_modifiers[0] == DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700191 return MAP_FAILED;
192
Gurchetan Singhef920532016-08-12 16:38:25 -0700193 gem_map.handle = bo->handles[0].u32;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800194 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET, &gem_map);
Gurchetan Singhef920532016-08-12 16:38:25 -0700195 if (ret) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700196 drv_log("DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET failed\n");
Gurchetan Singhef920532016-08-12 16:38:25 -0700197 return MAP_FAILED;
198 }
199
Gurchetan Singh298b7572019-09-19 09:55:18 -0700200 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 -0700201 gem_map.offset);
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700202
Gurchetan Singh298b7572019-09-19 09:55:18 -0700203 vma->length = bo->meta.total_size;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700204
Gurchetan Singh298b7572019-09-19 09:55:18 -0700205 if (bo->meta.use_flags & BO_USE_RENDERSCRIPT) {
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700206 priv = calloc(1, sizeof(*priv));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700207 priv->cached_addr = calloc(1, bo->meta.total_size);
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700208 priv->gem_addr = addr;
Gurchetan Singhee43c302017-11-14 18:20:27 -0800209 vma->priv = priv;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700210 addr = priv->cached_addr;
211 }
212
213 return addr;
214}
215
Gurchetan Singhee43c302017-11-14 18:20:27 -0800216static int rockchip_bo_unmap(struct bo *bo, struct vma *vma)
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700217{
Gurchetan Singhee43c302017-11-14 18:20:27 -0800218 if (vma->priv) {
219 struct rockchip_private_map_data *priv = vma->priv;
220 vma->addr = priv->gem_addr;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700221 free(priv->cached_addr);
222 free(priv);
Gurchetan Singhee43c302017-11-14 18:20:27 -0800223 vma->priv = NULL;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700224 }
225
Gurchetan Singhee43c302017-11-14 18:20:27 -0800226 return munmap(vma->addr, vma->length);
Gurchetan Singhef920532016-08-12 16:38:25 -0700227}
228
Gurchetan Singhef262d82017-11-28 16:56:17 -0800229static int rockchip_bo_invalidate(struct bo *bo, struct mapping *mapping)
230{
231 if (mapping->vma->priv) {
232 struct rockchip_private_map_data *priv = mapping->vma->priv;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700233 memcpy(priv->cached_addr, priv->gem_addr, bo->meta.total_size);
Gurchetan Singhef262d82017-11-28 16:56:17 -0800234 }
235
236 return 0;
237}
238
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700239static int rockchip_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700240{
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700241 struct rockchip_private_map_data *priv = mapping->vma->priv;
242 if (priv && (mapping->vma->map_flags & BO_MAP_WRITE))
Gurchetan Singh298b7572019-09-19 09:55:18 -0700243 memcpy(priv->gem_addr, priv->cached_addr, bo->meta.total_size);
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700244
245 return 0;
246}
247
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700248static uint32_t rockchip_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700249{
250 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800251 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Jeffy Chen55525f52017-09-19 17:15:30 +0800252 /* Camera subsystem requires NV12. */
Gurchetan Singha1892b22017-09-28 16:40:52 -0700253 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
Jeffy Chen55525f52017-09-19 17:15:30 +0800254 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700255 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800256 return DRM_FORMAT_XBGR8888;
257 case DRM_FORMAT_FLEX_YCbCr_420_888:
258 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700259 default:
260 return format;
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700261 }
262}
263
Gurchetan Singh3e9d3832017-10-31 10:36:25 -0700264const struct backend backend_rockchip = {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700265 .name = "rockchip",
Gurchetan Singh179687e2016-10-28 10:07:35 -0700266 .init = rockchip_init,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700267 .bo_create = rockchip_bo_create,
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700268 .bo_create_with_modifiers = rockchip_bo_create_with_modifiers,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700269 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singh71611d62017-01-03 16:49:56 -0800270 .bo_import = drv_prime_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700271 .bo_map = rockchip_bo_map,
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700272 .bo_unmap = rockchip_bo_unmap,
Gurchetan Singhef262d82017-11-28 16:56:17 -0800273 .bo_invalidate = rockchip_bo_invalidate,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700274 .bo_flush = rockchip_bo_flush,
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700275 .resolve_format = rockchip_resolve_format,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700276};
277
278#endif