blob: 140ec935249117a315929a96984c4f779c955356 [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>
Ilja H. Friedelf9d2ab72015-04-09 14:08:36 -070010#include <stdio.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070011#include <string.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070012#include <sys/mman.h>
Stéphane Marchesin25a26062014-09-12 16:18:59 -070013#include <xf86drm.h>
14#include <rockchip_drm.h>
15
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070016#include "drv_priv.h"
Dominik Behre13ac282015-01-13 00:59:21 -080017#include "helpers.h"
Zach Reizner58080df2016-04-27 11:14:41 -070018#include "util.h"
Stéphane Marchesin25a26062014-09-12 16:18:59 -070019
Gurchetan Singhb016ffb2017-01-19 10:43:44 -080020static struct supported_combination combos[12] = {
Gurchetan Singh179687e2016-10-28 10:07:35 -070021 {DRM_FORMAT_ABGR8888, DRM_FORMAT_MOD_NONE,
Gurchetan Singh458976f2016-11-23 17:32:33 -080022 BO_USE_RENDERING | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN |
23 BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY},
Gurchetan Singh179687e2016-10-28 10:07:35 -070024 {DRM_FORMAT_ARGB8888, DRM_FORMAT_MOD_NONE,
Gurchetan Singh458976f2016-11-23 17:32:33 -080025 BO_USE_CURSOR | BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN},
Gurchetan Singh179687e2016-10-28 10:07:35 -070026 {DRM_FORMAT_ARGB8888, DRM_FORMAT_MOD_NONE,
Gurchetan Singh458976f2016-11-23 17:32:33 -080027 BO_USE_RENDERING | BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY},
Gurchetan Singh179687e2016-10-28 10:07:35 -070028 {DRM_FORMAT_NV12, DRM_FORMAT_MOD_NONE,
Gurchetan Singh458976f2016-11-23 17:32:33 -080029 BO_USE_RENDERING | BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY},
Gurchetan Singh179687e2016-10-28 10:07:35 -070030 {DRM_FORMAT_NV12, DRM_FORMAT_MOD_NONE,
Gurchetan Singh458976f2016-11-23 17:32:33 -080031 BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN},
Gurchetan Singh179687e2016-10-28 10:07:35 -070032 {DRM_FORMAT_RGB565, DRM_FORMAT_MOD_NONE,
Gurchetan Singh458976f2016-11-23 17:32:33 -080033 BO_USE_RENDERING | BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY},
Gurchetan Singh179687e2016-10-28 10:07:35 -070034 {DRM_FORMAT_XBGR8888, DRM_FORMAT_MOD_NONE,
Gurchetan Singh458976f2016-11-23 17:32:33 -080035 BO_USE_RENDERING | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN |
36 BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY},
Gurchetan Singh179687e2016-10-28 10:07:35 -070037 {DRM_FORMAT_XBGR8888, DRM_FORMAT_MOD_NONE,
Gurchetan Singh458976f2016-11-23 17:32:33 -080038 BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN},
Gurchetan Singh179687e2016-10-28 10:07:35 -070039 {DRM_FORMAT_XRGB8888, DRM_FORMAT_MOD_NONE,
Gurchetan Singh458976f2016-11-23 17:32:33 -080040 BO_USE_CURSOR | BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN},
Gurchetan Singh179687e2016-10-28 10:07:35 -070041 {DRM_FORMAT_XRGB8888, DRM_FORMAT_MOD_NONE,
Gurchetan Singh458976f2016-11-23 17:32:33 -080042 BO_USE_RENDERING | BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY},
Gurchetan Singh179687e2016-10-28 10:07:35 -070043 {DRM_FORMAT_YVU420, DRM_FORMAT_MOD_NONE,
Gurchetan Singh458976f2016-11-23 17:32:33 -080044 BO_USE_RENDERING | BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY},
Gurchetan Singhb016ffb2017-01-19 10:43:44 -080045 {DRM_FORMAT_YVU420, DRM_FORMAT_MOD_NONE,
46 BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN},
Gurchetan Singh179687e2016-10-28 10:07:35 -070047};
48
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -070049static int afbc_bo_from_format(struct bo *bo, uint32_t width, uint32_t height,
50 uint32_t format)
51{
52 /* We've restricted ourselves to four bytes per pixel. */
53 const uint32_t pixel_size = 4;
54
55 const uint32_t clump_width = 4;
56 const uint32_t clump_height = 4;
57
58#define AFBC_NARROW 1
59#if AFBC_NARROW == 1
60 const uint32_t block_width = 4 * clump_width;
61 const uint32_t block_height = 4 * clump_height;
62#else
63 const uint32_t block_width = 8 * clump_width;
64 const uint32_t block_height = 2 * clump_height;
65#endif
66
67 const uint32_t header_block_size = 16;
68 const uint32_t body_block_size = block_width * block_height * pixel_size;
69 const uint32_t width_in_blocks = DIV_ROUND_UP(width, block_width);
70 const uint32_t height_in_blocks = DIV_ROUND_UP(height, block_height);
71 const uint32_t total_blocks = width_in_blocks * height_in_blocks;
72
73 const uint32_t header_plane_size = total_blocks * header_block_size;
74 const uint32_t body_plane_size = total_blocks * body_block_size;
75
76 /* GPU requires 64 bytes, but EGL import code expects 1024 byte
77 * alignement for the body plane. */
78 const uint32_t body_plane_alignment = 1024;
79
80 const uint32_t body_plane_offset =
81 ALIGN(header_plane_size, body_plane_alignment);
82 const uint32_t total_size =
83 body_plane_offset + body_plane_size;
84
85 bo->strides[0] = width_in_blocks * block_width * pixel_size;
86 bo->sizes[0] = total_size;
87 bo->offsets[0] = 0;
88
89 bo->total_size = total_size;
90
91 bo->format_modifiers[0] = DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC;
92
93 return 0;
94}
95
Gurchetan Singh179687e2016-10-28 10:07:35 -070096static int rockchip_init(struct driver *drv)
97{
98 drv_insert_combinations(drv, combos, ARRAY_SIZE(combos));
99 return drv_add_kms_flags(drv);
100}
101
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700102static bool has_modifier(const uint64_t *list, uint32_t count, uint64_t modifier)
103{
104 uint32_t i;
105
106 for (i = 0; i < count; i++)
107 if (list[i] == modifier)
108 return true;
109
110 return false;
111}
112
113static int rockchip_bo_create_with_modifiers(struct bo *bo,
114 uint32_t width, uint32_t height,
115 uint32_t format,
116 const uint64_t *modifiers,
117 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 Singhf64487b2016-07-14 19:54:44 -0700121 struct drm_rockchip_gem_create gem_create;
Zach Reizner58080df2016-04-27 11:14:41 -0700122
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800123 if (format == DRM_FORMAT_NV12) {
Gurchetan Singh10a11802016-09-23 15:27:07 -0700124 uint32_t w_mbs = DIV_ROUND_UP(ALIGN(width, 16), 16);
Gurchetan Singhd3fbe5b2017-01-23 07:58:59 -0800125 uint32_t h_mbs = DIV_ROUND_UP(ALIGN(height, 16), 16);
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700126
Gurchetan Singh10a11802016-09-23 15:27:07 -0700127 uint32_t aligned_width = w_mbs * 16;
128 uint32_t aligned_height = DIV_ROUND_UP(h_mbs * 16 * 3, 2);
129
130 drv_bo_from_format(bo, aligned_width, height, format);
131 bo->total_size = bo->strides[0] * aligned_height
132 + w_mbs * h_mbs * 128;
Kristian H. Kristensend1ae0ff2017-02-06 22:16:55 -0800133 } else if (width <= 2560 &&
134 has_modifier(modifiers, count,
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700135 DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC)) {
136 /* If the caller has decided they can use AFBC, always
137 * pick that */
138 afbc_bo_from_format(bo, width, height, format);
Gurchetan Singh10a11802016-09-23 15:27:07 -0700139 } else {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700140 if (!has_modifier(modifiers, count, DRM_FORMAT_MOD_NONE)) {
141 errno = EINVAL;
142 fprintf(stderr, "no usable modifier found\n");
143 return -1;
144 }
Gurchetan Singh6ea14ba2017-02-08 15:09:13 -0800145
146 /*
147 * Since the ARM L1 cache line size is 64 bytes, align to that
148 * as a performance optimization.
149 */
150 uint32_t bytes_per_pixel = drv_stride_from_format(format, 1, 0);
151 width = ALIGN(width, DIV_ROUND_UP(64, bytes_per_pixel));
Gurchetan Singh10a11802016-09-23 15:27:07 -0700152 drv_bo_from_format(bo, width, height, format);
153 }
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700154
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700155 memset(&gem_create, 0, sizeof(gem_create));
Gurchetan Singha40ca9e2016-08-29 19:51:45 -0700156 gem_create.size = bo->total_size;
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700157
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700158 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_CREATE,
Gurchetan Singh10a11802016-09-23 15:27:07 -0700159 &gem_create);
Gurchetan Singhf64487b2016-07-14 19:54:44 -0700160
161 if (ret) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700162 fprintf(stderr, "drv: DRM_IOCTL_ROCKCHIP_GEM_CREATE failed "
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700163 "(size=%llu)\n", gem_create.size);
164 return ret;
Zach Reizner58080df2016-04-27 11:14:41 -0700165 }
166
Gurchetan Singh42cc6d62016-08-29 18:19:19 -0700167 for (plane = 0; plane < bo->num_planes; plane++)
168 bo->handles[plane].u32 = gem_create.handle;
169
170 return 0;
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700171}
172
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700173static int rockchip_bo_create(struct bo *bo, uint32_t width, uint32_t height,
174 uint32_t format, uint32_t flags)
175{
176 uint64_t modifiers[] = { DRM_FORMAT_MOD_NONE };
177
178 return rockchip_bo_create_with_modifiers(bo, width, height, format,
179 modifiers, ARRAY_SIZE(modifiers));
180}
181
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700182static void *rockchip_bo_map(struct bo *bo, struct map_info *data, size_t plane)
Gurchetan Singhef920532016-08-12 16:38:25 -0700183{
184 int ret;
185 struct drm_rockchip_gem_map_off gem_map;
186
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700187 /* We can only map buffers created with SW access flags, which should
188 * have no modifiers (ie, not AFBC). */
189 if (bo->format_modifiers[0] == DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC)
190 return MAP_FAILED;
191
Gurchetan Singhef920532016-08-12 16:38:25 -0700192 memset(&gem_map, 0, sizeof(gem_map));
193 gem_map.handle = bo->handles[0].u32;
194
195 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET,
196 &gem_map);
197 if (ret) {
198 fprintf(stderr,
199 "drv: DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET failed\n");
200 return MAP_FAILED;
201 }
202
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700203 data->length = bo->total_size;
204
Gurchetan Singha40ca9e2016-08-29 19:51:45 -0700205 return mmap(0, bo->total_size, PROT_READ | PROT_WRITE, MAP_SHARED,
Gurchetan Singhef920532016-08-12 16:38:25 -0700206 bo->drv->fd, gem_map.offset);
207}
208
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800209static uint32_t rockchip_resolve_format(uint32_t format)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700210{
211 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800212 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700213 /*HACK: See b/28671744 */
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800214 return DRM_FORMAT_XBGR8888;
215 case DRM_FORMAT_FLEX_YCbCr_420_888:
216 return DRM_FORMAT_NV12;
Gurchetan Singhd6fb5772016-08-29 19:13:51 -0700217 default:
218 return format;
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700219 }
220}
221
Gurchetan Singh179687e2016-10-28 10:07:35 -0700222struct backend backend_rockchip =
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700223{
224 .name = "rockchip",
Gurchetan Singh179687e2016-10-28 10:07:35 -0700225 .init = rockchip_init,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700226 .bo_create = rockchip_bo_create,
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700227 .bo_create_with_modifiers = rockchip_bo_create_with_modifiers,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700228 .bo_destroy = drv_gem_bo_destroy,
Gurchetan Singh71611d62017-01-03 16:49:56 -0800229 .bo_import = drv_prime_bo_import,
Gurchetan Singhd7c84fd2016-08-16 18:18:24 -0700230 .bo_map = rockchip_bo_map,
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700231 .resolve_format = rockchip_resolve_format,
Stéphane Marchesin25a26062014-09-12 16:18:59 -0700232};
233
234#endif