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