2 * Copyright 2014 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
10 #include <rockchip_drm.h>
21 struct rockchip_private_map_data {
26 static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
27 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
28 DRM_FORMAT_XRGB8888 };
30 static const uint32_t texture_source_formats[] = { DRM_FORMAT_R8, DRM_FORMAT_NV12,
31 DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID };
33 static int afbc_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, uint32_t format)
35 /* We've restricted ourselves to four bytes per pixel. */
36 const uint32_t pixel_size = 4;
38 const uint32_t clump_width = 4;
39 const uint32_t clump_height = 4;
43 const uint32_t block_width = 4 * clump_width;
44 const uint32_t block_height = 4 * clump_height;
46 const uint32_t block_width = 8 * clump_width;
47 const uint32_t block_height = 2 * clump_height;
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;
56 const uint32_t header_plane_size = total_blocks * header_block_size;
57 const uint32_t body_plane_size = total_blocks * body_block_size;
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;
63 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;
66 bo->strides[0] = width_in_blocks * block_width * pixel_size;
67 bo->sizes[0] = total_size;
70 bo->total_size = total_size;
72 bo->format_modifiers[0] = DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC;
77 static int rockchip_add_kms_item(struct driver *drv, const struct kms_item *item)
82 struct combination *combo;
83 struct format_metadata metadata;
85 for (i = 0; i < drv->combos.size; i++) {
86 combo = &drv->combos.data[i];
87 if (combo->format == item->format) {
88 if (item->modifier == DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC) {
89 use_flags = BO_USE_RENDERING | BO_USE_SCANOUT | BO_USE_TEXTURE;
90 metadata.modifier = item->modifier;
92 metadata.priority = 2;
94 for (j = 0; j < ARRAY_SIZE(texture_source_formats); j++) {
95 if (item->format == texture_source_formats[j])
96 use_flags &= ~BO_USE_RENDERING;
100 drv_add_combination(drv, item[i].format, &metadata, use_flags);
104 combo->use_flags |= item->use_flags;
112 static int rockchip_init(struct driver *drv)
115 uint32_t i, num_items;
116 struct kms_item *items;
117 struct format_metadata metadata;
120 metadata.priority = 1;
121 metadata.modifier = DRM_FORMAT_MOD_LINEAR;
123 ret = drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
124 &metadata, BO_USE_RENDER_MASK);
128 ret = drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
129 &metadata, BO_USE_TEXTURE_MASK);
133 drv_modify_combination(drv, DRM_FORMAT_XRGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
134 drv_modify_combination(drv, DRM_FORMAT_ARGB8888, &metadata, BO_USE_CURSOR | BO_USE_SCANOUT);
136 /* Camera ISP supports only NV12 output. */
137 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
138 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
140 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
143 drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
144 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
146 items = drv_query_kms(drv, &num_items);
147 if (!items || !num_items)
150 for (i = 0; i < num_items; i++) {
151 ret = rockchip_add_kms_item(drv, &items[i]);
162 static bool has_modifier(const uint64_t *list, uint32_t count, uint64_t modifier)
165 for (i = 0; i < count; i++)
166 if (list[i] == modifier)
172 static int rockchip_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
173 uint32_t format, const uint64_t *modifiers,
178 struct drm_rockchip_gem_create gem_create;
180 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);
184 uint32_t aligned_width = w_mbs * 16;
185 uint32_t aligned_height = DIV_ROUND_UP(h_mbs * 16 * 3, 2);
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 &&
190 has_modifier(modifiers, count, DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC)) {
191 /* If the caller has decided they can use AFBC, always
193 afbc_bo_from_format(bo, width, height, format);
195 if (!has_modifier(modifiers, count, DRM_FORMAT_MOD_LINEAR)) {
197 fprintf(stderr, "no usable modifier found\n");
203 * 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.
208 stride = drv_stride_from_format(format, width, 0);
209 if (format == DRM_FORMAT_YVU420 || format == DRM_FORMAT_YVU420_ANDROID)
210 stride = ALIGN(stride, 128);
212 stride = ALIGN(stride, 64);
214 drv_bo_from_format(bo, stride, height, format);
217 memset(&gem_create, 0, sizeof(gem_create));
218 gem_create.size = bo->total_size;
220 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_CREATE, &gem_create);
223 fprintf(stderr, "drv: DRM_IOCTL_ROCKCHIP_GEM_CREATE failed (size=%llu)\n",
228 for (plane = 0; plane < bo->num_planes; plane++)
229 bo->handles[plane].u32 = gem_create.handle;
234 static int rockchip_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
237 uint64_t modifiers[] = { DRM_FORMAT_MOD_LINEAR };
238 return rockchip_bo_create_with_modifiers(bo, width, height, format, modifiers,
239 ARRAY_SIZE(modifiers));
242 static void *rockchip_bo_map(struct bo *bo, struct map_info *data, size_t plane, uint32_t map_flags)
245 struct drm_rockchip_gem_map_off gem_map;
246 struct rockchip_private_map_data *priv;
248 /* We can only map buffers created with SW access flags, which should
249 * have no modifiers (ie, not AFBC). */
250 if (bo->format_modifiers[0] == DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC)
253 memset(&gem_map, 0, sizeof(gem_map));
254 gem_map.handle = bo->handles[0].u32;
256 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET, &gem_map);
258 fprintf(stderr, "drv: DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET failed\n");
262 void *addr = mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
265 data->length = bo->total_size;
267 if (bo->use_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);
273 addr = priv->cached_addr;
279 static int rockchip_bo_unmap(struct bo *bo, struct map_info *data)
282 struct rockchip_private_map_data *priv = data->priv;
283 data->addr = priv->gem_addr;
284 free(priv->cached_addr);
289 return munmap(data->addr, data->length);
292 static int rockchip_bo_flush(struct bo *bo, struct map_info *data)
294 struct rockchip_private_map_data *priv = data->priv;
295 if (priv && (data->map_flags & BO_MAP_WRITE))
296 memcpy(priv->gem_addr, priv->cached_addr, bo->total_size);
301 static uint32_t rockchip_resolve_format(uint32_t format, uint64_t use_flags)
304 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
305 /* Camera subsystem requires NV12. */
306 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
307 return DRM_FORMAT_NV12;
308 /*HACK: See b/28671744 */
309 return DRM_FORMAT_XBGR8888;
310 case DRM_FORMAT_FLEX_YCbCr_420_888:
311 return DRM_FORMAT_NV12;
317 struct backend backend_rockchip = {
319 .init = rockchip_init,
320 .bo_create = rockchip_bo_create,
321 .bo_create_with_modifiers = rockchip_bo_create_with_modifiers,
322 .bo_destroy = drv_gem_bo_destroy,
323 .bo_import = drv_prime_bo_import,
324 .bo_map = rockchip_bo_map,
325 .bo_unmap = rockchip_bo_unmap,
326 .bo_flush = rockchip_bo_flush,
327 .resolve_format = rockchip_resolve_format,