blob: 816d7043075f9c67586f77456b98365cf4209558 [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>
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080010#include <rockchip_drm.h>
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080011#include <stdbool.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 Singh8ac0c9a2017-05-15 09:34:22 -070026static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
27 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
28 DRM_FORMAT_XRGB8888 };
29
30static const uint32_t texture_source_formats[] = { DRM_FORMAT_R8, DRM_FORMAT_NV12,
31 DRM_FORMAT_YVU420, 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
66 bo->strides[0] = width_in_blocks * block_width * pixel_size;
67 bo->sizes[0] = total_size;
68 bo->offsets[0] = 0;
69
70 bo->total_size = total_size;
71
72 bo->format_modifiers[0] = DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC;
73
74 return 0;
75}
76
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080077static int rockchip_add_kms_item(struct driver *drv, const struct kms_item *item)
78{
79 int ret;
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070080 uint32_t i, j;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080081 uint64_t flags;
82 struct combination *combo;
83 struct format_metadata metadata;
84
Ege Mihmanli96b7d462017-09-19 20:13:26 -070085 for (i = 0; i < drv->combos.size; i++) {
86 combo = &drv->combos.data[i];
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080087 if (combo->format == item->format) {
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080088 if (item->modifier == DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC) {
89 flags = BO_USE_RENDERING | BO_USE_SCANOUT | BO_USE_TEXTURE;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080090 metadata.modifier = item->modifier;
91 metadata.tiling = 0;
92 metadata.priority = 2;
93
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070094 for (j = 0; j < ARRAY_SIZE(texture_source_formats); j++) {
95 if (item->format == texture_source_formats[j])
96 flags &= ~BO_USE_RENDERING;
97 }
98
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080099 ret = drv_add_combination(drv, item[i].format, &metadata, flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800100 if (ret)
101 return ret;
102 } else {
103 combo->usage |= item->usage;
104 }
105 }
106 }
107
108 return 0;
109}
110
Gurchetan Singh179687e2016-10-28 10:07:35 -0700111static int rockchip_init(struct driver *drv)
112{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800113 int ret;
114 uint32_t i, num_items;
115 struct kms_item *items;
116 struct format_metadata metadata;
117
118 metadata.tiling = 0;
119 metadata.priority = 1;
120 metadata.modifier = DRM_FORMAT_MOD_NONE;
121
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700122 ret = drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
123 &metadata, BO_USE_RENDER_MASK);
124 if (ret)
125 return ret;
126
127 ret = drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
128 &metadata, BO_USE_TEXTURE_MASK);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800129 if (ret)
130 return ret;
131
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800132 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
133 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800134
Jeffy Chen55525f52017-09-19 17:15:30 +0800135 /* Camera ISP supports only NV12 output. */
136 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
137 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
138 /*
139 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
140 * from camera.
141 */
142 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
143 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
144
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800145 items = drv_query_kms(drv, &num_items);
146 if (!items || !num_items)
147 return 0;
148
149 for (i = 0; i < num_items; i++) {
150 ret = rockchip_add_kms_item(drv, &items[i]);
151 if (ret) {
152 free(items);
153 return ret;
154 }
155 }
156
157 free(items);
158 return 0;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700159}
160
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700161static bool has_modifier(const uint64_t *list, uint32_t count, uint64_t modifier)
162{
163 uint32_t i;
164
165 for (i = 0; i < count; i++)
166 if (list[i] == modifier)
167 return true;
168
169 return false;
170}
171
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800172static int rockchip_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
173 uint32_t format, const uint64_t *modifiers,
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700174 uint32_t count)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700175{
Zach Reizner58080df2016-04-27 11:14:41 -0700176 int ret;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700177 size_t plane;
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700178 struct drm_rockchip_gem_create gem_create;
Zach Reizner58080df2016-04-27 11:14:41 -0700179
Pin-chih Lin19643412017-07-25 08:06:26 +0000180 if (format == DRM_FORMAT_NV12) {
181 uint32_t w_mbs = DIV_ROUND_UP(ALIGN(width, 16), 16);
182 uint32_t h_mbs = DIV_ROUND_UP(ALIGN(height, 16), 16);
183
184 uint32_t aligned_width = w_mbs * 16;
185 uint32_t aligned_height = DIV_ROUND_UP(h_mbs * 16 * 3, 2);
186
187 drv_bo_from_format(bo, aligned_width, height, format);
188 bo->total_size = bo->strides[0] * aligned_height + w_mbs * h_mbs * 128;
189 } else if (width <= 2560 &&
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800190 has_modifier(modifiers, count, DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC)) {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700191 /* If the caller has decided they can use AFBC, always
192 * pick that */
193 afbc_bo_from_format(bo, width, height, format);
Gurchetan Singh10a11802016-09-23 15:27:07 -0700194 } else {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700195 if (!has_modifier(modifiers, count, DRM_FORMAT_MOD_NONE)) {
196 errno = EINVAL;
197 fprintf(stderr, "no usable modifier found\n");
198 return -1;
199 }
Gurchetan Singh6ea14ba2017-02-08 15:09:13 -0800200
Pin-chih Lin19643412017-07-25 08:06:26 +0000201 uint32_t stride;
Gurchetan Singh6ea14ba2017-02-08 15:09:13 -0800202 /*
Pin-chih Lin19643412017-07-25 08:06:26 +0000203 * Since the ARM L1 cache line size is 64 bytes, align to that
204 * as a performance optimization. For YV12, the Mali cmem allocator
205 * requires that chroma planes are aligned to 64-bytes, so align the
206 * luma plane to 128 bytes.
Gurchetan Singh6ea14ba2017-02-08 15:09:13 -0800207 */
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700208 stride = drv_stride_from_format(format, width, 0);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800209 if (format == DRM_FORMAT_YVU420 || format == DRM_FORMAT_YVU420_ANDROID)
Gurchetan Singh4c3aa422017-03-23 18:47:06 -0700210 stride = ALIGN(stride, 128);
211 else
212 stride = ALIGN(stride, 64);
213
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700214 drv_bo_from_format(bo, stride, height, format);
Gurchetan Singh10a11802016-09-23 15:27:07 -0700215 }
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700216
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700217 memset(&gem_create, 0, sizeof(gem_create));
Gurchetan Singha40ca9e2016-08-29 19:51:45 -0700218 gem_create.size = bo->total_size;
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700219
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800220 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_CREATE, &gem_create);
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700221
222 if (ret) {
Gurchetan Singhcb1471b2017-05-15 14:33:16 -0700223 fprintf(stderr, "drv: DRM_IOCTL_ROCKCHIP_GEM_CREATE failed (size=%llu)\n",
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800224 gem_create.size);
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700225 return ret;
Zach Reizner58080df2016-04-27 11:14:41 -0700226 }
227
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700228 for (plane = 0; plane < bo->num_planes; plane++)
229 bo->handles[plane].u32 = gem_create.handle;
230
231 return 0;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700232}
233
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800234static int rockchip_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
235 uint32_t flags)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700236{
237 uint64_t modifiers[] = { DRM_FORMAT_MOD_NONE };
238
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800239 return rockchip_bo_create_with_modifiers(bo, width, height, format, modifiers,
240 ARRAY_SIZE(modifiers));
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700241}
242
Joe Kniss65705852017-06-29 15:02:46 -0700243static void *rockchip_bo_map(struct bo *bo, struct map_info *data, size_t plane, int prot)
Gurchetan Singhef920532016-08-12 16:38:25 -0700244{
245 int ret;
246 struct drm_rockchip_gem_map_off gem_map;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700247 struct rockchip_private_map_data *priv;
Gurchetan Singhef920532016-08-12 16:38:25 -0700248
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700249 /* We can only map buffers created with SW access flags, which should
250 * have no modifiers (ie, not AFBC). */
251 if (bo->format_modifiers[0] == DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC)
252 return MAP_FAILED;
253
Gurchetan Singhef920532016-08-12 16:38:25 -0700254 memset(&gem_map, 0, sizeof(gem_map));
255 gem_map.handle = bo->handles[0].u32;
256
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800257 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET, &gem_map);
Gurchetan Singhef920532016-08-12 16:38:25 -0700258 if (ret) {
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800259 fprintf(stderr, "drv: DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET failed\n");
Gurchetan Singhef920532016-08-12 16:38:25 -0700260 return MAP_FAILED;
261 }
262
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700263 void *addr = mmap(0, bo->total_size, prot, MAP_SHARED, bo->drv->fd, gem_map.offset);
264
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700265 data->length = bo->total_size;
266
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700267 if (bo->flags & BO_USE_RENDERSCRIPT) {
268 priv = calloc(1, sizeof(*priv));
269 priv->cached_addr = calloc(1, bo->total_size);
270 priv->gem_addr = addr;
271 memcpy(priv->cached_addr, priv->gem_addr, bo->total_size);
272 data->priv = priv;
273 addr = priv->cached_addr;
274 }
275
276 return addr;
277}
278
279static int rockchip_bo_unmap(struct bo *bo, struct map_info *data)
280{
281 if (data->priv) {
282 struct rockchip_private_map_data *priv = data->priv;
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700283 data->addr = priv->gem_addr;
284 free(priv->cached_addr);
285 free(priv);
286 data->priv = NULL;
287 }
288
289 return munmap(data->addr, data->length);
Gurchetan Singhef920532016-08-12 16:38:25 -0700290}
291
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700292static int rockchip_bo_flush(struct bo *bo, struct map_info *data)
293{
294 struct rockchip_private_map_data *priv = data->priv;
295
296 if (priv)
297 memcpy(priv->gem_addr, priv->cached_addr, bo->total_size);
298
299 return 0;
300}
301
Tomasz Figace1ae022017-07-05 18:15:06 +0900302static uint32_t rockchip_resolve_format(uint32_t format, uint64_t usage)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700303{
304 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800305 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Jeffy Chen55525f52017-09-19 17:15:30 +0800306 /* Camera subsystem requires NV12. */
307 if (usage & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
308 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700309 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800310 return DRM_FORMAT_XBGR8888;
311 case DRM_FORMAT_FLEX_YCbCr_420_888:
312 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700313 default:
314 return format;
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700315 }
316}
317
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800318struct backend backend_rockchip = {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700319 .name = "rockchip",
Gurchetan Singh179687e2016-10-28 10:07:35 -0700320 .init = rockchip_init,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700321 .bo_create = rockchip_bo_create,
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700322 .bo_create_with_modifiers = rockchip_bo_create_with_modifiers,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700323 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singh71611d62017-01-03 16:49:56 -0800324 .bo_import = drv_prime_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700325 .bo_map = rockchip_bo_map,
Gurchetan Singh469a3aa2017-08-03 18:17:34 -0700326 .bo_unmap = rockchip_bo_unmap,
Gurchetan Singh8e02e052017-09-14 14:18:43 -0700327 .bo_flush = rockchip_bo_flush,
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700328 .resolve_format = rockchip_resolve_format,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700329};
330
331#endif