Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 1 | /* |
Daniele Castagna | 7a755de | 2016-12-16 17:32:30 -0500 | [diff] [blame] | 2 | * Copyright 2014 The Chromium OS Authors. All rights reserved. |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 7 | #ifdef DRV_ROCKCHIP |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 8 | |
Zach Reizner | 58080df | 2016-04-27 11:14:41 -0700 | [diff] [blame] | 9 | #include <errno.h> |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 10 | #include <stdio.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 11 | #include <string.h> |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 12 | #include <sys/mman.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 13 | #include <xf86drm.h> |
| 14 | #include <rockchip_drm.h> |
| 15 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 16 | #include "drv_priv.h" |
Dominik Behr | e13ac28 | 2015-01-13 00:59:21 -0800 | [diff] [blame] | 17 | #include "helpers.h" |
Zach Reizner | 58080df | 2016-04-27 11:14:41 -0700 | [diff] [blame] | 18 | #include "util.h" |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 19 | |
Gurchetan Singh | b016ffb | 2017-01-19 10:43:44 -0800 | [diff] [blame] | 20 | static struct supported_combination combos[12] = { |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 21 | {DRM_FORMAT_ABGR8888, DRM_FORMAT_MOD_NONE, |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 22 | 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 Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 24 | {DRM_FORMAT_ARGB8888, DRM_FORMAT_MOD_NONE, |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 25 | BO_USE_CURSOR | BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN}, |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 26 | {DRM_FORMAT_ARGB8888, DRM_FORMAT_MOD_NONE, |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 27 | BO_USE_RENDERING | BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY}, |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 28 | {DRM_FORMAT_NV12, DRM_FORMAT_MOD_NONE, |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 29 | BO_USE_RENDERING | BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY}, |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 30 | {DRM_FORMAT_NV12, DRM_FORMAT_MOD_NONE, |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 31 | BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN}, |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 32 | {DRM_FORMAT_RGB565, DRM_FORMAT_MOD_NONE, |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 33 | BO_USE_RENDERING | BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY}, |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 34 | {DRM_FORMAT_XBGR8888, DRM_FORMAT_MOD_NONE, |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 35 | 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 Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 37 | {DRM_FORMAT_XBGR8888, DRM_FORMAT_MOD_NONE, |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 38 | BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN}, |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 39 | {DRM_FORMAT_XRGB8888, DRM_FORMAT_MOD_NONE, |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 40 | BO_USE_CURSOR | BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN}, |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 41 | {DRM_FORMAT_XRGB8888, DRM_FORMAT_MOD_NONE, |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 42 | BO_USE_RENDERING | BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY}, |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 43 | {DRM_FORMAT_YVU420, DRM_FORMAT_MOD_NONE, |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 44 | BO_USE_RENDERING | BO_USE_SW_READ_RARELY | BO_USE_SW_WRITE_RARELY}, |
Gurchetan Singh | b016ffb | 2017-01-19 10:43:44 -0800 | [diff] [blame] | 45 | {DRM_FORMAT_YVU420, DRM_FORMAT_MOD_NONE, |
| 46 | BO_USE_LINEAR | BO_USE_SW_READ_OFTEN | BO_USE_SW_WRITE_OFTEN}, |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 49 | static 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 Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 96 | static 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. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 102 | static 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 | |
| 113 | static 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 Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 118 | { |
Zach Reizner | 58080df | 2016-04-27 11:14:41 -0700 | [diff] [blame] | 119 | int ret; |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 120 | size_t plane; |
Gurchetan Singh | f64487b | 2016-07-14 19:54:44 -0700 | [diff] [blame] | 121 | struct drm_rockchip_gem_create gem_create; |
Zach Reizner | 58080df | 2016-04-27 11:14:41 -0700 | [diff] [blame] | 122 | |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 123 | if (format == DRM_FORMAT_NV12) { |
Gurchetan Singh | 10a1180 | 2016-09-23 15:27:07 -0700 | [diff] [blame] | 124 | uint32_t w_mbs = DIV_ROUND_UP(ALIGN(width, 16), 16); |
Gurchetan Singh | d3fbe5b | 2017-01-23 07:58:59 -0800 | [diff] [blame] | 125 | uint32_t h_mbs = DIV_ROUND_UP(ALIGN(height, 16), 16); |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 126 | |
Gurchetan Singh | 10a1180 | 2016-09-23 15:27:07 -0700 | [diff] [blame] | 127 | 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. Kristensen | d1ae0ff | 2017-02-06 22:16:55 -0800 | [diff] [blame] | 133 | } else if (width <= 2560 && |
| 134 | has_modifier(modifiers, count, |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 135 | 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 Singh | 10a1180 | 2016-09-23 15:27:07 -0700 | [diff] [blame] | 139 | } else { |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 140 | 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 Singh | 6ea14ba | 2017-02-08 15:09:13 -0800 | [diff] [blame] | 145 | |
| 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 Singh | 10a1180 | 2016-09-23 15:27:07 -0700 | [diff] [blame] | 152 | drv_bo_from_format(bo, width, height, format); |
| 153 | } |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 154 | |
Gurchetan Singh | f64487b | 2016-07-14 19:54:44 -0700 | [diff] [blame] | 155 | memset(&gem_create, 0, sizeof(gem_create)); |
Gurchetan Singh | a40ca9e | 2016-08-29 19:51:45 -0700 | [diff] [blame] | 156 | gem_create.size = bo->total_size; |
Gurchetan Singh | f64487b | 2016-07-14 19:54:44 -0700 | [diff] [blame] | 157 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 158 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_CREATE, |
Gurchetan Singh | 10a1180 | 2016-09-23 15:27:07 -0700 | [diff] [blame] | 159 | &gem_create); |
Gurchetan Singh | f64487b | 2016-07-14 19:54:44 -0700 | [diff] [blame] | 160 | |
| 161 | if (ret) { |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 162 | fprintf(stderr, "drv: DRM_IOCTL_ROCKCHIP_GEM_CREATE failed " |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 163 | "(size=%llu)\n", gem_create.size); |
| 164 | return ret; |
Zach Reizner | 58080df | 2016-04-27 11:14:41 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 167 | for (plane = 0; plane < bo->num_planes; plane++) |
| 168 | bo->handles[plane].u32 = gem_create.handle; |
| 169 | |
| 170 | return 0; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 173 | static 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 Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 182 | static void *rockchip_bo_map(struct bo *bo, struct map_info *data, size_t plane) |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 183 | { |
| 184 | int ret; |
| 185 | struct drm_rockchip_gem_map_off gem_map; |
| 186 | |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 187 | /* 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 Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 192 | 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 Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 203 | data->length = bo->total_size; |
| 204 | |
Gurchetan Singh | a40ca9e | 2016-08-29 19:51:45 -0700 | [diff] [blame] | 205 | return mmap(0, bo->total_size, PROT_READ | PROT_WRITE, MAP_SHARED, |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 206 | bo->drv->fd, gem_map.offset); |
| 207 | } |
| 208 | |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 209 | static uint32_t rockchip_resolve_format(uint32_t format) |
Gurchetan Singh | bfba8c2 | 2016-08-16 17:57:10 -0700 | [diff] [blame] | 210 | { |
| 211 | switch (format) { |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 212 | case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED: |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 213 | /*HACK: See b/28671744 */ |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 214 | return DRM_FORMAT_XBGR8888; |
| 215 | case DRM_FORMAT_FLEX_YCbCr_420_888: |
| 216 | return DRM_FORMAT_NV12; |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 217 | default: |
| 218 | return format; |
Gurchetan Singh | bfba8c2 | 2016-08-16 17:57:10 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 222 | struct backend backend_rockchip = |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 223 | { |
| 224 | .name = "rockchip", |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 225 | .init = rockchip_init, |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame] | 226 | .bo_create = rockchip_bo_create, |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 227 | .bo_create_with_modifiers = rockchip_bo_create_with_modifiers, |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 228 | .bo_destroy = drv_gem_bo_destroy, |
Gurchetan Singh | 71611d6 | 2017-01-03 16:49:56 -0800 | [diff] [blame] | 229 | .bo_import = drv_prime_bo_import, |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame] | 230 | .bo_map = rockchip_bo_map, |
Gurchetan Singh | bfba8c2 | 2016-08-16 17:57:10 -0700 | [diff] [blame] | 231 | .resolve_format = rockchip_resolve_format, |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | #endif |