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> |
Nicolas Boichat | d7c8338 | 2019-08-29 21:46:29 +0800 | [diff] [blame] | 10 | #include <inttypes.h> |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 11 | #include <rockchip_drm.h> |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 12 | #include <stdio.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 13 | #include <string.h> |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 14 | #include <sys/mman.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 15 | #include <xf86drm.h> |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 16 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 17 | #include "drv_priv.h" |
Dominik Behr | e13ac28 | 2015-01-13 00:59:21 -0800 | [diff] [blame] | 18 | #include "helpers.h" |
Zach Reizner | 58080df | 2016-04-27 11:14:41 -0700 | [diff] [blame] | 19 | #include "util.h" |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 20 | |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 21 | struct rockchip_private_map_data { |
| 22 | void *cached_addr; |
| 23 | void *gem_addr; |
| 24 | }; |
| 25 | |
Gurchetan Singh | c87a6d3 | 2019-12-19 10:51:14 -0800 | [diff] [blame] | 26 | static const uint32_t scanout_render_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888, |
| 27 | DRM_FORMAT_BGR888, DRM_FORMAT_RGB565, |
| 28 | DRM_FORMAT_XBGR8888, DRM_FORMAT_XRGB8888 }; |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 29 | |
Gurchetan Singh | c87a6d3 | 2019-12-19 10:51:14 -0800 | [diff] [blame] | 30 | static const uint32_t texture_only_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_YVU420, |
| 31 | DRM_FORMAT_YVU420_ANDROID }; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 32 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 33 | static int afbc_bo_from_format(struct bo *bo, uint32_t width, uint32_t height, uint32_t format) |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 34 | { |
| 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 Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 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; |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 65 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 66 | bo->meta.strides[0] = width_in_blocks * block_width * pixel_size; |
| 67 | bo->meta.sizes[0] = total_size; |
| 68 | bo->meta.offsets[0] = 0; |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 69 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 70 | bo->meta.total_size = total_size; |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 71 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 72 | bo->meta.format_modifiers[0] = DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC; |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 77 | static int rockchip_init(struct driver *drv) |
| 78 | { |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 79 | struct format_metadata metadata; |
| 80 | |
| 81 | metadata.tiling = 0; |
| 82 | metadata.priority = 1; |
Kristian H. Kristensen | bc8c593 | 2017-10-24 18:36:32 -0700 | [diff] [blame] | 83 | metadata.modifier = DRM_FORMAT_MOD_LINEAR; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 84 | |
Gurchetan Singh | c87a6d3 | 2019-12-19 10:51:14 -0800 | [diff] [blame] | 85 | drv_add_combinations(drv, scanout_render_formats, ARRAY_SIZE(scanout_render_formats), |
| 86 | &metadata, BO_USE_RENDER_MASK | BO_USE_SCANOUT); |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 87 | |
Gurchetan Singh | c87a6d3 | 2019-12-19 10:51:14 -0800 | [diff] [blame] | 88 | drv_add_combinations(drv, texture_only_formats, ARRAY_SIZE(texture_only_formats), &metadata, |
| 89 | BO_USE_TEXTURE_MASK); |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 90 | |
Hirokazu Honda | 3bd681c | 2020-06-23 17:52:20 +0900 | [diff] [blame] | 91 | /* NV12 format for camera, display, decoding and encoding. */ |
Jeffy Chen | 55525f5 | 2017-09-19 17:15:30 +0800 | [diff] [blame] | 92 | /* Camera ISP supports only NV12 output. */ |
| 93 | drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata, |
Hirokazu Honda | 3bd681c | 2020-06-23 17:52:20 +0900 | [diff] [blame] | 94 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT | |
| 95 | BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER); |
Gurchetan Singh | c87a6d3 | 2019-12-19 10:51:14 -0800 | [diff] [blame] | 96 | |
| 97 | drv_modify_linear_combinations(drv); |
Jeffy Chen | 55525f5 | 2017-09-19 17:15:30 +0800 | [diff] [blame] | 98 | /* |
| 99 | * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots |
David Stevens | 4951814 | 2020-06-15 13:48:48 +0900 | [diff] [blame] | 100 | * from camera and input/output from hardware decoder/encoder. |
Jeffy Chen | 55525f5 | 2017-09-19 17:15:30 +0800 | [diff] [blame] | 101 | */ |
Gurchetan Singh | dc9b120 | 2019-06-04 16:53:54 -0700 | [diff] [blame] | 102 | drv_add_combination(drv, DRM_FORMAT_R8, &metadata, |
| 103 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SW_MASK | |
Gurchetan Singh | bbba9dd | 2020-10-12 17:31:10 -0700 | [diff] [blame] | 104 | BO_USE_LINEAR | BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER); |
Jeffy Chen | 55525f5 | 2017-09-19 17:15:30 +0800 | [diff] [blame] | 105 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 106 | return 0; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 109 | static int rockchip_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height, |
| 110 | uint32_t format, const uint64_t *modifiers, |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 111 | uint32_t count) |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 112 | { |
Zach Reizner | 58080df | 2016-04-27 11:14:41 -0700 | [diff] [blame] | 113 | int ret; |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 114 | size_t plane; |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 115 | struct drm_rockchip_gem_create gem_create = { 0 }; |
Zach Reizner | 58080df | 2016-04-27 11:14:41 -0700 | [diff] [blame] | 116 | |
Pin-chih Lin | 1964341 | 2017-07-25 08:06:26 +0000 | [diff] [blame] | 117 | if (format == DRM_FORMAT_NV12) { |
Hirokazu Honda | b892a8f | 2019-08-01 16:37:47 +0900 | [diff] [blame] | 118 | uint32_t w_mbs = DIV_ROUND_UP(width, 16); |
| 119 | uint32_t h_mbs = DIV_ROUND_UP(height, 16); |
Pin-chih Lin | 1964341 | 2017-07-25 08:06:26 +0000 | [diff] [blame] | 120 | |
| 121 | uint32_t aligned_width = w_mbs * 16; |
Hirokazu Honda | 785a548 | 2020-01-14 04:08:12 +0000 | [diff] [blame] | 122 | uint32_t aligned_height = h_mbs * 16; |
Pin-chih Lin | 1964341 | 2017-07-25 08:06:26 +0000 | [diff] [blame] | 123 | |
Hirokazu Honda | 785a548 | 2020-01-14 04:08:12 +0000 | [diff] [blame] | 124 | drv_bo_from_format(bo, aligned_width, aligned_height, format); |
Hirokazu Honda | b892a8f | 2019-08-01 16:37:47 +0900 | [diff] [blame] | 125 | /* |
| 126 | * drv_bo_from_format updates total_size. Add an extra data space for rockchip video |
| 127 | * driver to store motion vectors. |
| 128 | */ |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 129 | bo->meta.total_size += w_mbs * h_mbs * 128; |
Pin-chih Lin | 1964341 | 2017-07-25 08:06:26 +0000 | [diff] [blame] | 130 | } else if (width <= 2560 && |
Pilar Molina Lopez | 28cf2f1 | 2020-11-12 18:19:42 -0500 | [diff] [blame] | 131 | drv_has_modifier(modifiers, count, DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC) && |
| 132 | bo->drv->compression) { |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 133 | /* If the caller has decided they can use AFBC, always |
| 134 | * pick that */ |
| 135 | afbc_bo_from_format(bo, width, height, format); |
Gurchetan Singh | 10a1180 | 2016-09-23 15:27:07 -0700 | [diff] [blame] | 136 | } else { |
Fritz Koenig | 1b9b5b9 | 2019-03-19 13:25:45 -0700 | [diff] [blame] | 137 | if (!drv_has_modifier(modifiers, count, DRM_FORMAT_MOD_LINEAR)) { |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 138 | errno = EINVAL; |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 139 | drv_log("no usable modifier found\n"); |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 140 | return -1; |
| 141 | } |
Gurchetan Singh | 6ea14ba | 2017-02-08 15:09:13 -0800 | [diff] [blame] | 142 | |
Pin-chih Lin | 1964341 | 2017-07-25 08:06:26 +0000 | [diff] [blame] | 143 | uint32_t stride; |
Gurchetan Singh | 6ea14ba | 2017-02-08 15:09:13 -0800 | [diff] [blame] | 144 | /* |
Pin-chih Lin | 1964341 | 2017-07-25 08:06:26 +0000 | [diff] [blame] | 145 | * Since the ARM L1 cache line size is 64 bytes, align to that |
| 146 | * as a performance optimization. For YV12, the Mali cmem allocator |
| 147 | * requires that chroma planes are aligned to 64-bytes, so align the |
| 148 | * luma plane to 128 bytes. |
Gurchetan Singh | 6ea14ba | 2017-02-08 15:09:13 -0800 | [diff] [blame] | 149 | */ |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 150 | stride = drv_stride_from_format(format, width, 0); |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 151 | if (format == DRM_FORMAT_YVU420 || format == DRM_FORMAT_YVU420_ANDROID) |
Gurchetan Singh | 4c3aa42 | 2017-03-23 18:47:06 -0700 | [diff] [blame] | 152 | stride = ALIGN(stride, 128); |
| 153 | else |
| 154 | stride = ALIGN(stride, 64); |
| 155 | |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 156 | drv_bo_from_format(bo, stride, height, format); |
Gurchetan Singh | 10a1180 | 2016-09-23 15:27:07 -0700 | [diff] [blame] | 157 | } |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 158 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 159 | gem_create.size = bo->meta.total_size; |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 160 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_CREATE, &gem_create); |
Gurchetan Singh | f64487b | 2016-07-14 19:54:44 -0700 | [diff] [blame] | 161 | |
| 162 | if (ret) { |
Nicolas Boichat | d7c8338 | 2019-08-29 21:46:29 +0800 | [diff] [blame] | 163 | drv_log("DRM_IOCTL_ROCKCHIP_GEM_CREATE failed (size=%" PRIu64 ")\n", |
| 164 | gem_create.size); |
Stéphane Marchesin | 6ac299f | 2019-03-21 12:23:29 -0700 | [diff] [blame] | 165 | return -errno; |
Zach Reizner | 58080df | 2016-04-27 11:14:41 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 168 | for (plane = 0; plane < bo->meta.num_planes; plane++) |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 169 | bo->handles[plane].u32 = gem_create.handle; |
| 170 | |
| 171 | return 0; |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 174 | static int rockchip_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format, |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 175 | uint64_t use_flags) |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 176 | { |
Kristian H. Kristensen | bc8c593 | 2017-10-24 18:36:32 -0700 | [diff] [blame] | 177 | uint64_t modifiers[] = { DRM_FORMAT_MOD_LINEAR }; |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 178 | return rockchip_bo_create_with_modifiers(bo, width, height, format, modifiers, |
| 179 | ARRAY_SIZE(modifiers)); |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Gurchetan Singh | ee43c30 | 2017-11-14 18:20:27 -0800 | [diff] [blame] | 182 | static void *rockchip_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags) |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 183 | { |
| 184 | int ret; |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 185 | struct rockchip_private_map_data *priv; |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 186 | struct drm_rockchip_gem_map_off gem_map = { 0 }; |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 187 | |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 188 | /* We can only map buffers created with SW access flags, which should |
| 189 | * have no modifiers (ie, not AFBC). */ |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 190 | if (bo->meta.format_modifiers[0] == DRM_FORMAT_MOD_CHROMEOS_ROCKCHIP_AFBC) |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 191 | return MAP_FAILED; |
| 192 | |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 193 | gem_map.handle = bo->handles[0].u32; |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 194 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET, &gem_map); |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 195 | if (ret) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 196 | drv_log("DRM_IOCTL_ROCKCHIP_GEM_MAP_OFFSET failed\n"); |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 197 | return MAP_FAILED; |
| 198 | } |
| 199 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 200 | void *addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd, |
Gurchetan Singh | cfb8876 | 2017-09-28 17:14:50 -0700 | [diff] [blame] | 201 | gem_map.offset); |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 202 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 203 | vma->length = bo->meta.total_size; |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 204 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 205 | if (bo->meta.use_flags & BO_USE_RENDERSCRIPT) { |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 206 | priv = calloc(1, sizeof(*priv)); |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 207 | priv->cached_addr = calloc(1, bo->meta.total_size); |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 208 | priv->gem_addr = addr; |
Gurchetan Singh | ee43c30 | 2017-11-14 18:20:27 -0800 | [diff] [blame] | 209 | vma->priv = priv; |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 210 | addr = priv->cached_addr; |
| 211 | } |
| 212 | |
| 213 | return addr; |
| 214 | } |
| 215 | |
Gurchetan Singh | ee43c30 | 2017-11-14 18:20:27 -0800 | [diff] [blame] | 216 | static int rockchip_bo_unmap(struct bo *bo, struct vma *vma) |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 217 | { |
Gurchetan Singh | ee43c30 | 2017-11-14 18:20:27 -0800 | [diff] [blame] | 218 | if (vma->priv) { |
| 219 | struct rockchip_private_map_data *priv = vma->priv; |
| 220 | vma->addr = priv->gem_addr; |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 221 | free(priv->cached_addr); |
| 222 | free(priv); |
Gurchetan Singh | ee43c30 | 2017-11-14 18:20:27 -0800 | [diff] [blame] | 223 | vma->priv = NULL; |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Gurchetan Singh | ee43c30 | 2017-11-14 18:20:27 -0800 | [diff] [blame] | 226 | return munmap(vma->addr, vma->length); |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Gurchetan Singh | ef262d8 | 2017-11-28 16:56:17 -0800 | [diff] [blame] | 229 | static int rockchip_bo_invalidate(struct bo *bo, struct mapping *mapping) |
| 230 | { |
| 231 | if (mapping->vma->priv) { |
| 232 | struct rockchip_private_map_data *priv = mapping->vma->priv; |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 233 | memcpy(priv->cached_addr, priv->gem_addr, bo->meta.total_size); |
Gurchetan Singh | ef262d8 | 2017-11-28 16:56:17 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
Gurchetan Singh | 47e629b | 2017-11-02 14:07:18 -0700 | [diff] [blame] | 239 | static int rockchip_bo_flush(struct bo *bo, struct mapping *mapping) |
Gurchetan Singh | 8e02e05 | 2017-09-14 14:18:43 -0700 | [diff] [blame] | 240 | { |
Gurchetan Singh | 47e629b | 2017-11-02 14:07:18 -0700 | [diff] [blame] | 241 | struct rockchip_private_map_data *priv = mapping->vma->priv; |
| 242 | if (priv && (mapping->vma->map_flags & BO_MAP_WRITE)) |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 243 | memcpy(priv->gem_addr, priv->cached_addr, bo->meta.total_size); |
Gurchetan Singh | 8e02e05 | 2017-09-14 14:18:43 -0700 | [diff] [blame] | 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
Gurchetan Singh | 0d44d48 | 2019-06-04 19:39:51 -0700 | [diff] [blame] | 248 | static uint32_t rockchip_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags) |
Gurchetan Singh | bfba8c2 | 2016-08-16 17:57:10 -0700 | [diff] [blame] | 249 | { |
| 250 | switch (format) { |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 251 | case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED: |
Jeffy Chen | 55525f5 | 2017-09-19 17:15:30 +0800 | [diff] [blame] | 252 | /* Camera subsystem requires NV12. */ |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 253 | if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)) |
Jeffy Chen | 55525f5 | 2017-09-19 17:15:30 +0800 | [diff] [blame] | 254 | return DRM_FORMAT_NV12; |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 255 | /*HACK: See b/28671744 */ |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 256 | return DRM_FORMAT_XBGR8888; |
| 257 | case DRM_FORMAT_FLEX_YCbCr_420_888: |
| 258 | return DRM_FORMAT_NV12; |
Gurchetan Singh | d6fb577 | 2016-08-29 19:13:51 -0700 | [diff] [blame] | 259 | default: |
| 260 | return format; |
Gurchetan Singh | bfba8c2 | 2016-08-16 17:57:10 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Gurchetan Singh | 3e9d383 | 2017-10-31 10:36:25 -0700 | [diff] [blame] | 264 | const struct backend backend_rockchip = { |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 265 | .name = "rockchip", |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 266 | .init = rockchip_init, |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame] | 267 | .bo_create = rockchip_bo_create, |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 268 | .bo_create_with_modifiers = rockchip_bo_create_with_modifiers, |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 269 | .bo_destroy = drv_gem_bo_destroy, |
Gurchetan Singh | 71611d6 | 2017-01-03 16:49:56 -0800 | [diff] [blame] | 270 | .bo_import = drv_prime_bo_import, |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame] | 271 | .bo_map = rockchip_bo_map, |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 272 | .bo_unmap = rockchip_bo_unmap, |
Gurchetan Singh | ef262d8 | 2017-11-28 16:56:17 -0800 | [diff] [blame] | 273 | .bo_invalidate = rockchip_bo_invalidate, |
Gurchetan Singh | 8e02e05 | 2017-09-14 14:18:43 -0700 | [diff] [blame] | 274 | .bo_flush = rockchip_bo_flush, |
Gurchetan Singh | bfba8c2 | 2016-08-16 17:57:10 -0700 | [diff] [blame] | 275 | .resolve_format = rockchip_resolve_format, |
Stéphane Marchesin | 25a2606 | 2014-09-12 16:18:59 -0700 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | #endif |