blob: 50ea4ef76402b21e2d510d5aed694cc9407b87f1 [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 Singh8ac0c9a2017-05-15 09:34:22 -070021static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
22 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
23 DRM_FORMAT_XRGB8888 };
24
25static const uint32_t texture_source_formats[] = { DRM_FORMAT_R8, DRM_FORMAT_NV12,
26 DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID };
Gurchetan Singh179687e2016-10-28 10:07:35 -070027
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080028static 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 -070029{
30 /* We've restricted ourselves to four bytes per pixel. */
31 const uint32_t pixel_size = 4;
32
33 const uint32_t clump_width = 4;
34 const uint32_t clump_height = 4;
35
36#define AFBC_NARROW 1
37#if AFBC_NARROW == 1
38 const uint32_t block_width = 4 * clump_width;
39 const uint32_t block_height = 4 * clump_height;
40#else
41 const uint32_t block_width = 8 * clump_width;
42 const uint32_t block_height = 2 * clump_height;
43#endif
44
45 const uint32_t header_block_size = 16;
46 const uint32_t body_block_size = block_width * block_height * pixel_size;
47 const uint32_t width_in_blocks = DIV_ROUND_UP(width, block_width);
48 const uint32_t height_in_blocks = DIV_ROUND_UP(height, block_height);
49 const uint32_t total_blocks = width_in_blocks * height_in_blocks;
50
51 const uint32_t header_plane_size = total_blocks * header_block_size;
52 const uint32_t body_plane_size = total_blocks * body_block_size;
53
54 /* GPU requires 64 bytes, but EGL import code expects 1024 byte
55 * alignement for the body plane. */
56 const uint32_t body_plane_alignment = 1024;
57
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080058 const uint32_t body_plane_offset = ALIGN(header_plane_size, body_plane_alignment);
59 const uint32_t total_size = body_plane_offset + body_plane_size;
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -070060
61 bo->strides[0] = width_in_blocks * block_width * pixel_size;
62 bo->sizes[0] = total_size;
63 bo->offsets[0] = 0;
64
65 bo->total_size = total_size;
66
67 bo->format_modifiers[0] = DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC;
68
69 return 0;
70}
71
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080072static int rockchip_add_kms_item(struct driver *drv, const struct kms_item *item)
73{
74 int ret;
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070075 uint32_t i, j;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080076 uint64_t flags;
77 struct combination *combo;
78 struct format_metadata metadata;
79
80 for (i = 0; i < drv->backend->combos.size; i++) {
81 combo = &drv->backend->combos.data[i];
82 if (combo->format == item->format) {
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080083 if (item->modifier == DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC) {
84 flags = BO_USE_RENDERING | BO_USE_SCANOUT | BO_USE_TEXTURE;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080085 metadata.modifier = item->modifier;
86 metadata.tiling = 0;
87 metadata.priority = 2;
88
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -070089 for (j = 0; j < ARRAY_SIZE(texture_source_formats); j++) {
90 if (item->format == texture_source_formats[j])
91 flags &= ~BO_USE_RENDERING;
92 }
93
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080094 ret = drv_add_combination(drv, item[i].format, &metadata, flags);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -080095 if (ret)
96 return ret;
97 } else {
98 combo->usage |= item->usage;
99 }
100 }
101 }
102
103 return 0;
104}
105
Gurchetan Singh179687e2016-10-28 10:07:35 -0700106static int rockchip_init(struct driver *drv)
107{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800108 int ret;
109 uint32_t i, num_items;
110 struct kms_item *items;
111 struct format_metadata metadata;
112
113 metadata.tiling = 0;
114 metadata.priority = 1;
115 metadata.modifier = DRM_FORMAT_MOD_NONE;
116
Gurchetan Singh8ac0c9a2017-05-15 09:34:22 -0700117 ret = drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
118 &metadata, BO_USE_RENDER_MASK);
119 if (ret)
120 return ret;
121
122 ret = drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
123 &metadata, BO_USE_TEXTURE_MASK);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800124 if (ret)
125 return ret;
126
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800127 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
128 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800129
130 items = drv_query_kms(drv, &num_items);
131 if (!items || !num_items)
132 return 0;
133
134 for (i = 0; i < num_items; i++) {
135 ret = rockchip_add_kms_item(drv, &items[i]);
136 if (ret) {
137 free(items);
138 return ret;
139 }
140 }
141
142 free(items);
143 return 0;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700144}
145
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700146static bool has_modifier(const uint64_t *list, uint32_t count, uint64_t modifier)
147{
148 uint32_t i;
149
150 for (i = 0; i < count; i++)
151 if (list[i] == modifier)
152 return true;
153
154 return false;
155}
156
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800157static int rockchip_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
158 uint32_t format, const uint64_t *modifiers,
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700159 uint32_t count)
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700160{
Zach Reizner58080df2016-04-27 11:14:41 -0700161 int ret;
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700162 size_t plane;
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700163 struct drm_rockchip_gem_create gem_create;
Zach Reizner58080df2016-04-27 11:14:41 -0700164
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800165 if (format == DRM_FORMAT_NV12) {
Gurchetan Singh10a11802016-09-23 15:27:07 -0700166 uint32_t w_mbs = DIV_ROUND_UP(ALIGN(width, 16), 16);
Gurchetan Singhd3fbe5b2017-01-23 07:58:59 -0800167 uint32_t h_mbs = DIV_ROUND_UP(ALIGN(height, 16), 16);
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700168
Gurchetan Singh10a11802016-09-23 15:27:07 -0700169 uint32_t aligned_width = w_mbs * 16;
170 uint32_t aligned_height = DIV_ROUND_UP(h_mbs * 16 * 3, 2);
171
172 drv_bo_from_format(bo, aligned_width, height, format);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800173 bo->total_size = bo->strides[0] * aligned_height + w_mbs * h_mbs * 128;
Kristian H. Kristensend1ae0ff2017-02-06 22:16:55 -0800174 } else if (width <= 2560 &&
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800175 has_modifier(modifiers, count, DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC)) {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700176 /* If the caller has decided they can use AFBC, always
177 * pick that */
178 afbc_bo_from_format(bo, width, height, format);
Gurchetan Singh10a11802016-09-23 15:27:07 -0700179 } else {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700180 if (!has_modifier(modifiers, count, DRM_FORMAT_MOD_NONE)) {
181 errno = EINVAL;
182 fprintf(stderr, "no usable modifier found\n");
183 return -1;
184 }
Gurchetan Singh6ea14ba2017-02-08 15:09:13 -0800185
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700186 uint32_t stride;
Gurchetan Singh6ea14ba2017-02-08 15:09:13 -0800187 /*
188 * Since the ARM L1 cache line size is 64 bytes, align to that
Gurchetan Singh4c3aa422017-03-23 18:47:06 -0700189 * as a performance optimization. For YV12, the Mali cmem allocator
190 * requires that chroma planes are aligned to 64-bytes, so align the
191 * luma plane to 128 bytes.
Gurchetan Singh6ea14ba2017-02-08 15:09:13 -0800192 */
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700193 stride = drv_stride_from_format(format, width, 0);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800194 if (format == DRM_FORMAT_YVU420 || format == DRM_FORMAT_YVU420_ANDROID)
Gurchetan Singh4c3aa422017-03-23 18:47:06 -0700195 stride = ALIGN(stride, 128);
196 else
197 stride = ALIGN(stride, 64);
198
Gurchetan Singh6423ecb2017-03-29 08:23:40 -0700199 drv_bo_from_format(bo, stride, height, format);
Gurchetan Singh10a11802016-09-23 15:27:07 -0700200 }
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700201
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700202 memset(&gem_create, 0, sizeof(gem_create));
Gurchetan Singha40ca9e2016-08-29 19:51:45 -0700203 gem_create.size = bo->total_size;
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700204
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800205 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_CREATE, &gem_create);
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700206
207 if (ret) {
Gurchetan Singhcb1471b2017-05-15 14:33:16 -0700208 fprintf(stderr, "drv: DRM_IOCTL_ROCKCHIP_GEM_CREATE failed (size=%llu)\n",
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800209 gem_create.size);
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700210 return ret;
Zach Reizner58080df2016-04-27 11:14:41 -0700211 }
212
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700213 for (plane = 0; plane < bo->num_planes; plane++)
214 bo->handles[plane].u32 = gem_create.handle;
215
216 return 0;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700217}
218
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800219static int rockchip_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
220 uint32_t flags)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700221{
222 uint64_t modifiers[] = { DRM_FORMAT_MOD_NONE };
223
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800224 return rockchip_bo_create_with_modifiers(bo, width, height, format, modifiers,
225 ARRAY_SIZE(modifiers));
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700226}
227
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700228static void *rockchip_bo_map(struct bo *bo, struct map_info *data, size_t plane)
Gurchetan Singhef920532016-08-12 16:38:25 -0700229{
230 int ret;
231 struct drm_rockchip_gem_map_off gem_map;
232
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700233 /* We can only map buffers created with SW access flags, which should
234 * have no modifiers (ie, not AFBC). */
235 if (bo->format_modifiers[0] == DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC)
236 return MAP_FAILED;
237
Gurchetan Singhef920532016-08-12 16:38:25 -0700238 memset(&gem_map, 0, sizeof(gem_map));
239 gem_map.handle = bo->handles[0].u32;
240
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800241 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET, &gem_map);
Gurchetan Singhef920532016-08-12 16:38:25 -0700242 if (ret) {
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800243 fprintf(stderr, "drv: DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET failed\n");
Gurchetan Singhef920532016-08-12 16:38:25 -0700244 return MAP_FAILED;
245 }
246
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700247 data->length = bo->total_size;
248
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800249 return mmap(0, bo->total_size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->drv->fd,
250 gem_map.offset);
Gurchetan Singhef920532016-08-12 16:38:25 -0700251}
252
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800253static uint32_t rockchip_resolve_format(uint32_t format)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700254{
255 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800256 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700257 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800258 return DRM_FORMAT_XBGR8888;
259 case DRM_FORMAT_FLEX_YCbCr_420_888:
260 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700261 default:
262 return format;
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700263 }
264}
265
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800266struct backend backend_rockchip = {
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700267 .name = "rockchip",
Gurchetan Singh179687e2016-10-28 10:07:35 -0700268 .init = rockchip_init,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700269 .bo_create = rockchip_bo_create,
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700270 .bo_create_with_modifiers = rockchip_bo_create_with_modifiers,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700271 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singh71611d62017-01-03 16:49:56 -0800272 .bo_import = drv_prime_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700273 .bo_map = rockchip_bo_map,
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700274 .resolve_format = rockchip_resolve_format,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700275};
276
277#endif