JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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. |
| 5 | */ |
| 6 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 7 | #ifdef DRV_MEDIATEK |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 8 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 9 | // clang-format off |
Stéphane Marchesin | 6ac299f | 2019-03-21 12:23:29 -0700 | [diff] [blame] | 10 | #include <errno.h> |
Luigi Santivetti | 500928f | 2018-08-28 10:09:20 +0100 | [diff] [blame] | 11 | #include <fcntl.h> |
Nicolas Boichat | d7c8338 | 2019-08-29 21:46:29 +0800 | [diff] [blame] | 12 | #include <inttypes.h> |
Luigi Santivetti | 500928f | 2018-08-28 10:09:20 +0100 | [diff] [blame] | 13 | #include <poll.h> |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 14 | #include <stdio.h> |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 15 | #include <string.h> |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 16 | #include <sys/mman.h> |
Luigi Santivetti | 500928f | 2018-08-28 10:09:20 +0100 | [diff] [blame] | 17 | #include <unistd.h> |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 18 | #include <xf86drm.h> |
| 19 | #include <mediatek_drm.h> |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 20 | // clang-format on |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 21 | |
Yiwei Zhang | b7a6444 | 2021-09-30 05:13:10 +0000 | [diff] [blame] | 22 | #include "drv_helpers.h" |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 23 | #include "drv_priv.h" |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 24 | #include "util.h" |
| 25 | |
Miguel Casas | dea0ccb | 2018-07-02 09:40:25 -0400 | [diff] [blame] | 26 | #define TILE_TYPE_LINEAR 0 |
| 27 | |
Hsin-Yi Wang | 8fe9f19 | 2022-01-18 17:51:02 +0800 | [diff] [blame] | 28 | #if defined(MTK_MT8183) || defined(MTK_MT8186) |
Yiwei Zhang | 185a139 | 2022-09-08 19:21:19 +0000 | [diff] [blame] | 29 | #define SUPPORTS_YUV422 |
Hsin-Yi Wang | 8fe9f19 | 2022-01-18 17:51:02 +0800 | [diff] [blame] | 30 | #endif |
| 31 | |
| 32 | // All platforms except MT8173 should USE_NV12_FOR_HW_VIDEO_DECODING. |
| 33 | #if defined(MTK_MT8183) || defined(MTK_MT8186) || defined(MTK_MT8192) || defined(MTK_MT8195) |
| 34 | #define USE_NV12_FOR_HW_VIDEO_DECODING |
Miguel Casas | 35fd7d2 | 2022-02-18 10:52:28 -0800 | [diff] [blame] | 35 | #else |
| 36 | #define DONT_USE_64_ALIGNMENT_FOR_VIDEO_BUFFERS |
Hsin-Yi Wang | 8fe9f19 | 2022-01-18 17:51:02 +0800 | [diff] [blame] | 37 | #endif |
| 38 | |
Yiwei Zhang | 5c93742 | 2022-08-10 18:23:30 +0000 | [diff] [blame] | 39 | // For Mali Sigurd based GPUs, the texture unit reads outside the specified texture dimensions. |
| 40 | // Therefore, certain formats require extra memory padding to its allocated surface to prevent the |
| 41 | // hardware from reading outside an allocation. For YVU420, we need additional padding for the last |
| 42 | // chroma plane. |
| 43 | #if defined(MTK_MT8186) |
| 44 | #define USE_EXTRA_PADDING_FOR_YVU420 |
| 45 | #endif |
| 46 | |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 47 | struct mediatek_private_map_data { |
| 48 | void *cached_addr; |
| 49 | void *gem_addr; |
Luigi Santivetti | 500928f | 2018-08-28 10:09:20 +0100 | [diff] [blame] | 50 | int prime_fd; |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Gurchetan Singh | 767c538 | 2018-05-05 00:42:12 +0000 | [diff] [blame] | 53 | static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888, |
Gurchetan Singh | 71bc665 | 2018-09-17 17:42:05 -0700 | [diff] [blame] | 54 | DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888, |
| 55 | DRM_FORMAT_XRGB8888 }; |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 56 | |
Hsin-Yi Wang | 8fe9f19 | 2022-01-18 17:51:02 +0800 | [diff] [blame] | 57 | // clang-format off |
Yiwei Zhang | f4c1725 | 2021-10-30 08:29:48 +0000 | [diff] [blame] | 58 | static const uint32_t texture_source_formats[] = { |
Yiwei Zhang | 185a139 | 2022-09-08 19:21:19 +0000 | [diff] [blame] | 59 | #ifdef SUPPORTS_YUV422 |
Hsin-Yi Wang | 8fe9f19 | 2022-01-18 17:51:02 +0800 | [diff] [blame] | 60 | DRM_FORMAT_NV21, |
| 61 | DRM_FORMAT_YUYV, |
Yiwei Zhang | 185a139 | 2022-09-08 19:21:19 +0000 | [diff] [blame] | 62 | #endif |
Hsin-Yi Wang | 8fe9f19 | 2022-01-18 17:51:02 +0800 | [diff] [blame] | 63 | DRM_FORMAT_ABGR2101010, |
| 64 | DRM_FORMAT_ABGR16161616F, |
Hsin-Yi Wang | 8fe9f19 | 2022-01-18 17:51:02 +0800 | [diff] [blame] | 65 | DRM_FORMAT_NV12, |
| 66 | DRM_FORMAT_YVU420, |
| 67 | DRM_FORMAT_YVU420_ANDROID |
| 68 | }; |
Miguel Casas | 35fd7d2 | 2022-02-18 10:52:28 -0800 | [diff] [blame] | 69 | |
Miguel Casas | 35fd7d2 | 2022-02-18 10:52:28 -0800 | [diff] [blame] | 70 | static const uint32_t video_yuv_formats[] = { |
| 71 | DRM_FORMAT_NV21, |
| 72 | DRM_FORMAT_NV12, |
Justin Green | c9f6462 | 2022-08-22 16:46:16 -0400 | [diff] [blame] | 73 | DRM_FORMAT_YUYV, |
Miguel Casas | 35fd7d2 | 2022-02-18 10:52:28 -0800 | [diff] [blame] | 74 | DRM_FORMAT_YVU420, |
| 75 | DRM_FORMAT_YVU420_ANDROID |
| 76 | }; |
Hsin-Yi Wang | 8fe9f19 | 2022-01-18 17:51:02 +0800 | [diff] [blame] | 77 | // clang-format on |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 78 | |
Miguel Casas | 35fd7d2 | 2022-02-18 10:52:28 -0800 | [diff] [blame] | 79 | static bool is_video_yuv_format(uint32_t format) |
| 80 | { |
| 81 | size_t i; |
| 82 | for (i = 0; i < ARRAY_SIZE(video_yuv_formats); ++i) { |
| 83 | if (format == video_yuv_formats[i]) |
| 84 | return true; |
| 85 | } |
| 86 | return false; |
| 87 | } |
Miguel Casas | 35fd7d2 | 2022-02-18 10:52:28 -0800 | [diff] [blame] | 88 | |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 89 | static int mediatek_init(struct driver *drv) |
| 90 | { |
Miguel Casas | dea0ccb | 2018-07-02 09:40:25 -0400 | [diff] [blame] | 91 | struct format_metadata metadata; |
| 92 | |
Gurchetan Singh | d300145 | 2017-11-03 17:18:36 -0700 | [diff] [blame] | 93 | drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats), |
Gurchetan Singh | 1914f98 | 2020-03-24 13:53:51 -0700 | [diff] [blame] | 94 | &LINEAR_METADATA, BO_USE_RENDER_MASK | BO_USE_SCANOUT); |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 95 | |
Gurchetan Singh | d300145 | 2017-11-03 17:18:36 -0700 | [diff] [blame] | 96 | drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats), |
| 97 | &LINEAR_METADATA, BO_USE_TEXTURE_MASK); |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 98 | |
Gurchetan Singh | bbba9dd | 2020-10-12 17:31:10 -0700 | [diff] [blame] | 99 | drv_add_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA, BO_USE_SW_MASK | BO_USE_LINEAR); |
Hirokazu Honda | fd8b8ab | 2020-06-16 15:28:56 +0900 | [diff] [blame] | 100 | |
Justin Green | c9f6462 | 2022-08-22 16:46:16 -0400 | [diff] [blame] | 101 | /* YUYV format for video overlay and camera subsystem. */ |
| 102 | drv_add_combination(drv, DRM_FORMAT_YUYV, &LINEAR_METADATA, |
Justin Green | 1805193 | 2022-08-30 13:04:29 -0400 | [diff] [blame] | 103 | BO_USE_HW_VIDEO_DECODER | BO_USE_SCANOUT | BO_USE_LINEAR | |
| 104 | BO_USE_TEXTURE); |
Justin Green | c9f6462 | 2022-08-22 16:46:16 -0400 | [diff] [blame] | 105 | |
Gurchetan Singh | 71bc665 | 2018-09-17 17:42:05 -0700 | [diff] [blame] | 106 | /* Android CTS tests require this. */ |
| 107 | drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK); |
| 108 | |
Miguel Casas | dea0ccb | 2018-07-02 09:40:25 -0400 | [diff] [blame] | 109 | /* Support BO_USE_HW_VIDEO_DECODER for protected content minigbm allocations. */ |
| 110 | metadata.tiling = TILE_TYPE_LINEAR; |
| 111 | metadata.priority = 1; |
| 112 | metadata.modifier = DRM_FORMAT_MOD_LINEAR; |
| 113 | drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata, BO_USE_HW_VIDEO_DECODER); |
| 114 | drv_modify_combination(drv, DRM_FORMAT_YVU420_ANDROID, &metadata, BO_USE_HW_VIDEO_DECODER); |
Hsin-Yi Wang | 8fe9f19 | 2022-01-18 17:51:02 +0800 | [diff] [blame] | 115 | #ifdef USE_NV12_FOR_HW_VIDEO_DECODING |
Wei Lee | 2f02cfb | 2020-08-05 17:24:45 +0800 | [diff] [blame] | 116 | // TODO(hiroh): Switch to use NV12 for video decoder on MT8173 as well. |
Hirokazu Honda | 0f0ce6f | 2019-07-24 19:40:20 +0900 | [diff] [blame] | 117 | drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata, BO_USE_HW_VIDEO_DECODER); |
Wei Lee | 2f02cfb | 2020-08-05 17:24:45 +0800 | [diff] [blame] | 118 | #endif |
Miguel Casas | dea0ccb | 2018-07-02 09:40:25 -0400 | [diff] [blame] | 119 | |
David Stevens | 4951814 | 2020-06-15 13:48:48 +0900 | [diff] [blame] | 120 | /* |
| 121 | * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB for input/output from |
| 122 | * hardware decoder/encoder. |
| 123 | */ |
| 124 | drv_modify_combination(drv, DRM_FORMAT_R8, &metadata, |
Wei Lee | e03625b | 2020-07-21 11:27:14 +0800 | [diff] [blame] | 125 | BO_USE_HW_VIDEO_DECODER | BO_USE_HW_VIDEO_ENCODER | |
Yiwei Zhang | bbe1fd3 | 2022-07-20 20:44:22 +0000 | [diff] [blame] | 126 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | |
Jason Macnak | 80f664c | 2022-07-19 16:44:22 -0700 | [diff] [blame] | 127 | BO_USE_GPU_DATA_BUFFER | BO_USE_SENSOR_DIRECT_DATA); |
David Stevens | 4951814 | 2020-06-15 13:48:48 +0900 | [diff] [blame] | 128 | |
Hirokazu Honda | 3bd681c | 2020-06-23 17:52:20 +0900 | [diff] [blame] | 129 | /* NV12 format for encoding and display. */ |
| 130 | drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata, |
Wei Lee | e03625b | 2020-07-21 11:27:14 +0800 | [diff] [blame] | 131 | BO_USE_SCANOUT | BO_USE_HW_VIDEO_ENCODER | BO_USE_CAMERA_READ | |
| 132 | BO_USE_CAMERA_WRITE); |
Hirokazu Honda | 3bd681c | 2020-06-23 17:52:20 +0900 | [diff] [blame] | 133 | |
Wei Lee | e03625b | 2020-07-21 11:27:14 +0800 | [diff] [blame] | 134 | #ifdef MTK_MT8183 |
Nick Fan | 01c4014 | 2018-10-08 11:53:26 +0800 | [diff] [blame] | 135 | /* Only for MT8183 Camera subsystem */ |
Nick Fan | 01c4014 | 2018-10-08 11:53:26 +0800 | [diff] [blame] | 136 | drv_modify_combination(drv, DRM_FORMAT_NV21, &metadata, |
| 137 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE); |
| 138 | drv_modify_combination(drv, DRM_FORMAT_YUYV, &metadata, |
| 139 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE); |
| 140 | drv_modify_combination(drv, DRM_FORMAT_YVU420, &metadata, |
| 141 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE); |
Jasmine Chen | c7aa974 | 2019-08-14 15:28:22 +0800 | [diff] [blame] | 142 | /* Private formats for private reprocessing in camera */ |
| 143 | drv_add_combination(drv, DRM_FORMAT_MTISP_SXYZW10, &metadata, |
| 144 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SW_MASK); |
Nick Fan | 01c4014 | 2018-10-08 11:53:26 +0800 | [diff] [blame] | 145 | #endif |
| 146 | |
Gurchetan Singh | 8ac0c9a | 2017-05-15 09:34:22 -0700 | [diff] [blame] | 147 | return drv_modify_linear_combinations(drv); |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 148 | } |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 149 | |
Fritz Koenig | 1b9b5b9 | 2019-03-19 13:25:45 -0700 | [diff] [blame] | 150 | static int mediatek_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height, |
| 151 | uint32_t format, const uint64_t *modifiers, |
| 152 | uint32_t count) |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 153 | { |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 154 | int ret; |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 155 | size_t plane; |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 156 | uint32_t stride; |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 157 | struct drm_mtk_gem_create gem_create = { 0 }; |
Hsin-Yi Wang | 2581c47 | 2022-04-06 17:34:33 +0800 | [diff] [blame] | 158 | /* |
| 159 | * We identify the ChromeOS Camera App buffers via these two USE flags. Those buffers need |
| 160 | * the same alignment as the video hardware encoding. |
| 161 | */ |
| 162 | const bool is_camera_preview = |
| 163 | (bo->meta.use_flags & BO_USE_SCANOUT) && (bo->meta.use_flags & BO_USE_CAMERA_WRITE); |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 164 | |
Fritz Koenig | 1b9b5b9 | 2019-03-19 13:25:45 -0700 | [diff] [blame] | 165 | if (!drv_has_modifier(modifiers, count, DRM_FORMAT_MOD_LINEAR)) { |
| 166 | errno = EINVAL; |
Yiwei Zhang | 0495473 | 2022-07-13 23:34:33 +0000 | [diff] [blame] | 167 | drv_loge("no usable modifier found\n"); |
Fritz Koenig | 1b9b5b9 | 2019-03-19 13:25:45 -0700 | [diff] [blame] | 168 | return -EINVAL; |
| 169 | } |
| 170 | |
Yiwei Zhang | 6fb145b | 2022-10-07 16:20:38 +0000 | [diff] [blame] | 171 | /* |
| 172 | * Since the ARM L1 cache line size is 64 bytes, align to that as a |
| 173 | * performance optimization, except for video buffers on certain platforms, |
| 174 | * these should only be accessed from the GPU and VCODEC subsystems (maybe |
| 175 | * also MDP), so it's better to align to macroblocks. |
| 176 | */ |
Gurchetan Singh | 6423ecb | 2017-03-29 08:23:40 -0700 | [diff] [blame] | 177 | stride = drv_stride_from_format(format, width, 0); |
Yiwei Zhang | 6fb145b | 2022-10-07 16:20:38 +0000 | [diff] [blame] | 178 | #ifdef DONT_USE_64_ALIGNMENT_FOR_VIDEO_BUFFERS |
| 179 | const uint32_t alignment = is_video_yuv_format(format) ? 16 : 64; |
| 180 | stride = ALIGN(stride, alignment); |
| 181 | #else |
| 182 | stride = ALIGN(stride, 64); |
| 183 | #endif |
Hirokazu Honda | 2a2bfc2 | 2019-10-11 15:54:50 +0900 | [diff] [blame] | 184 | |
Hsin-Yi Wang | 2581c47 | 2022-04-06 17:34:33 +0800 | [diff] [blame] | 185 | if ((bo->meta.use_flags & BO_USE_HW_VIDEO_ENCODER) || is_camera_preview) { |
Hirokazu Honda | 2a2bfc2 | 2019-10-11 15:54:50 +0900 | [diff] [blame] | 186 | uint32_t aligned_height = ALIGN(height, 32); |
| 187 | uint32_t padding[DRV_MAX_PLANES] = { 0 }; |
| 188 | |
| 189 | for (plane = 0; plane < bo->meta.num_planes; ++plane) { |
| 190 | uint32_t plane_stride = drv_stride_from_format(format, stride, plane); |
| 191 | padding[plane] = plane_stride * |
| 192 | (32 / drv_vertical_subsampling_from_format(format, plane)); |
| 193 | } |
| 194 | |
| 195 | drv_bo_from_format_and_padding(bo, stride, aligned_height, format, padding); |
| 196 | } else { |
Yiwei Zhang | 185a139 | 2022-09-08 19:21:19 +0000 | [diff] [blame] | 197 | #ifdef SUPPORTS_YUV422 |
Moja Hsu | 059ac08 | 2019-10-02 14:47:10 +0800 | [diff] [blame] | 198 | /* |
| 199 | * JPEG Encoder Accelerator requires 16x16 alignment. We want the buffer |
| 200 | * from camera can be put in JEA directly so align the height to 16 |
| 201 | * bytes. |
| 202 | */ |
| 203 | if (format == DRM_FORMAT_NV12) |
| 204 | height = ALIGN(height, 16); |
| 205 | #endif |
Hirokazu Honda | 2a2bfc2 | 2019-10-11 15:54:50 +0900 | [diff] [blame] | 206 | drv_bo_from_format(bo, stride, height, format); |
Yiwei Zhang | 5c93742 | 2022-08-10 18:23:30 +0000 | [diff] [blame] | 207 | |
| 208 | #ifdef USE_EXTRA_PADDING_FOR_YVU420 |
Yiwei Zhang | b2dde01 | 2022-09-27 04:12:54 +0000 | [diff] [blame] | 209 | /* |
| 210 | * Apply extra padding for YV12 if the height does not meet round up requirement and |
| 211 | * the image is to be sampled by gpu. |
| 212 | */ |
| 213 | static const uint32_t required_round_up = 4; |
| 214 | const uint32_t height_mod = height % required_round_up; |
Yiwei Zhang | 5c93742 | 2022-08-10 18:23:30 +0000 | [diff] [blame] | 215 | if ((format == DRM_FORMAT_YVU420 || format == DRM_FORMAT_YVU420_ANDROID) && |
Yiwei Zhang | b2dde01 | 2022-09-27 04:12:54 +0000 | [diff] [blame] | 216 | (bo->meta.use_flags & BO_USE_TEXTURE) && height_mod) { |
| 217 | const uint32_t height_padding = required_round_up - height_mod; |
| 218 | const uint32_t u_padding = |
| 219 | drv_size_from_format(format, bo->meta.strides[2], height_padding, 2); |
| 220 | |
| 221 | bo->meta.total_size += u_padding; |
| 222 | |
| 223 | /* |
| 224 | * Since we are not aligning Y, we must make sure that its padding fits |
| 225 | * inside the rest of the space allocated for the V/U planes. |
| 226 | */ |
| 227 | const uint32_t y_padding = |
| 228 | drv_size_from_format(format, bo->meta.strides[0], height_padding, 0); |
| 229 | const uint32_t vu_size = drv_bo_get_plane_size(bo, 2) * 2; |
| 230 | if (y_padding > vu_size) { |
| 231 | /* Align with mali workaround to pad all 3 planes. */ |
| 232 | bo->meta.total_size += y_padding + u_padding; |
| 233 | } |
Yiwei Zhang | 5c93742 | 2022-08-10 18:23:30 +0000 | [diff] [blame] | 234 | } |
| 235 | #endif |
Hirokazu Honda | 2a2bfc2 | 2019-10-11 15:54:50 +0900 | [diff] [blame] | 236 | } |
Yuly Novikov | 96c7a3b | 2015-12-08 22:48:29 -0500 | [diff] [blame] | 237 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 238 | gem_create.size = bo->meta.total_size; |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 239 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 240 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_CREATE, &gem_create); |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 241 | if (ret) { |
Yiwei Zhang | 0495473 | 2022-07-13 23:34:33 +0000 | [diff] [blame] | 242 | drv_loge("DRM_IOCTL_MTK_GEM_CREATE failed (size=%" PRIu64 ")\n", gem_create.size); |
Stéphane Marchesin | 6ac299f | 2019-03-21 12:23:29 -0700 | [diff] [blame] | 243 | return -errno; |
Ilja H. Friedel | f9d2ab7 | 2015-04-09 14:08:36 -0700 | [diff] [blame] | 244 | } |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 245 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 246 | for (plane = 0; plane < bo->meta.num_planes; plane++) |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 247 | bo->handles[plane].u32 = gem_create.handle; |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
Fritz Koenig | 1b9b5b9 | 2019-03-19 13:25:45 -0700 | [diff] [blame] | 252 | static int mediatek_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format, |
| 253 | uint64_t use_flags) |
| 254 | { |
| 255 | uint64_t modifiers[] = { DRM_FORMAT_MOD_LINEAR }; |
| 256 | return mediatek_bo_create_with_modifiers(bo, width, height, format, modifiers, |
| 257 | ARRAY_SIZE(modifiers)); |
| 258 | } |
| 259 | |
Gurchetan Singh | ee43c30 | 2017-11-14 18:20:27 -0800 | [diff] [blame] | 260 | static void *mediatek_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] | 261 | { |
Luigi Santivetti | 500928f | 2018-08-28 10:09:20 +0100 | [diff] [blame] | 262 | int ret, prime_fd; |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 263 | struct drm_mtk_gem_map_off gem_map = { 0 }; |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 264 | struct mediatek_private_map_data *priv; |
Yiwei Zhang | afdf87d | 2021-09-28 04:06:06 +0000 | [diff] [blame] | 265 | void *addr = NULL; |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 266 | |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 267 | gem_map.handle = bo->handles[0].u32; |
| 268 | |
| 269 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MTK_GEM_MAP_OFFSET, &gem_map); |
| 270 | if (ret) { |
Yiwei Zhang | 0495473 | 2022-07-13 23:34:33 +0000 | [diff] [blame] | 271 | drv_loge("DRM_IOCTL_MTK_GEM_MAP_OFFSET failed\n"); |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 272 | return MAP_FAILED; |
| 273 | } |
| 274 | |
David Stevens | ddb56b5 | 2020-03-13 15:24:37 +0900 | [diff] [blame] | 275 | prime_fd = drv_bo_get_plane_fd(bo, 0); |
| 276 | if (prime_fd < 0) { |
Yiwei Zhang | 0495473 | 2022-07-13 23:34:33 +0000 | [diff] [blame] | 277 | drv_loge("Failed to get a prime fd\n"); |
Luigi Santivetti | 500928f | 2018-08-28 10:09:20 +0100 | [diff] [blame] | 278 | return MAP_FAILED; |
| 279 | } |
| 280 | |
Yiwei Zhang | afdf87d | 2021-09-28 04:06:06 +0000 | [diff] [blame] | 281 | addr = mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd, |
| 282 | gem_map.offset); |
| 283 | if (addr == MAP_FAILED) |
| 284 | goto out_close_prime_fd; |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 285 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 286 | vma->length = bo->meta.total_size; |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 287 | |
Luigi Santivetti | 500928f | 2018-08-28 10:09:20 +0100 | [diff] [blame] | 288 | priv = calloc(1, sizeof(*priv)); |
Yiwei Zhang | afdf87d | 2021-09-28 04:06:06 +0000 | [diff] [blame] | 289 | if (!priv) |
| 290 | goto out_unmap_addr; |
Luigi Santivetti | 500928f | 2018-08-28 10:09:20 +0100 | [diff] [blame] | 291 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 292 | if (bo->meta.use_flags & BO_USE_RENDERSCRIPT) { |
| 293 | priv->cached_addr = calloc(1, bo->meta.total_size); |
Yiwei Zhang | afdf87d | 2021-09-28 04:06:06 +0000 | [diff] [blame] | 294 | if (!priv->cached_addr) |
| 295 | goto out_free_priv; |
| 296 | |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 297 | priv->gem_addr = addr; |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 298 | addr = priv->cached_addr; |
| 299 | } |
| 300 | |
Yiwei Zhang | afdf87d | 2021-09-28 04:06:06 +0000 | [diff] [blame] | 301 | priv->prime_fd = prime_fd; |
| 302 | vma->priv = priv; |
| 303 | |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 304 | return addr; |
Yiwei Zhang | afdf87d | 2021-09-28 04:06:06 +0000 | [diff] [blame] | 305 | |
| 306 | out_free_priv: |
| 307 | free(priv); |
| 308 | out_unmap_addr: |
| 309 | munmap(addr, bo->meta.total_size); |
| 310 | out_close_prime_fd: |
| 311 | close(prime_fd); |
| 312 | return MAP_FAILED; |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Gurchetan Singh | ee43c30 | 2017-11-14 18:20:27 -0800 | [diff] [blame] | 315 | static int mediatek_bo_unmap(struct bo *bo, struct vma *vma) |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 316 | { |
Gurchetan Singh | ee43c30 | 2017-11-14 18:20:27 -0800 | [diff] [blame] | 317 | if (vma->priv) { |
| 318 | struct mediatek_private_map_data *priv = vma->priv; |
Luigi Santivetti | a72f442 | 2018-09-12 16:28:21 +0100 | [diff] [blame] | 319 | |
| 320 | if (priv->cached_addr) { |
| 321 | vma->addr = priv->gem_addr; |
| 322 | free(priv->cached_addr); |
| 323 | } |
| 324 | |
Luigi Santivetti | 500928f | 2018-08-28 10:09:20 +0100 | [diff] [blame] | 325 | close(priv->prime_fd); |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 326 | free(priv); |
Gurchetan Singh | ee43c30 | 2017-11-14 18:20:27 -0800 | [diff] [blame] | 327 | vma->priv = NULL; |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Gurchetan Singh | ee43c30 | 2017-11-14 18:20:27 -0800 | [diff] [blame] | 330 | return munmap(vma->addr, vma->length); |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Gurchetan Singh | ef262d8 | 2017-11-28 16:56:17 -0800 | [diff] [blame] | 333 | static int mediatek_bo_invalidate(struct bo *bo, struct mapping *mapping) |
| 334 | { |
Luigi Santivetti | 500928f | 2018-08-28 10:09:20 +0100 | [diff] [blame] | 335 | struct mediatek_private_map_data *priv = mapping->vma->priv; |
| 336 | |
| 337 | if (priv) { |
| 338 | struct pollfd fds = { |
| 339 | .fd = priv->prime_fd, |
| 340 | }; |
| 341 | |
| 342 | if (mapping->vma->map_flags & BO_MAP_WRITE) |
| 343 | fds.events |= POLLOUT; |
| 344 | |
| 345 | if (mapping->vma->map_flags & BO_MAP_READ) |
| 346 | fds.events |= POLLIN; |
| 347 | |
| 348 | poll(&fds, 1, -1); |
| 349 | if (fds.revents != fds.events) |
Yiwei Zhang | 0495473 | 2022-07-13 23:34:33 +0000 | [diff] [blame] | 350 | drv_loge("poll prime_fd failed\n"); |
Luigi Santivetti | 500928f | 2018-08-28 10:09:20 +0100 | [diff] [blame] | 351 | |
| 352 | if (priv->cached_addr) |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 353 | memcpy(priv->cached_addr, priv->gem_addr, bo->meta.total_size); |
Gurchetan Singh | ef262d8 | 2017-11-28 16:56:17 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
Gurchetan Singh | 47e629b | 2017-11-02 14:07:18 -0700 | [diff] [blame] | 359 | static int mediatek_bo_flush(struct bo *bo, struct mapping *mapping) |
Gurchetan Singh | 8e02e05 | 2017-09-14 14:18:43 -0700 | [diff] [blame] | 360 | { |
Gurchetan Singh | 47e629b | 2017-11-02 14:07:18 -0700 | [diff] [blame] | 361 | struct mediatek_private_map_data *priv = mapping->vma->priv; |
Luigi Santivetti | 500928f | 2018-08-28 10:09:20 +0100 | [diff] [blame] | 362 | if (priv && priv->cached_addr && (mapping->vma->map_flags & BO_MAP_WRITE)) |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 363 | memcpy(priv->gem_addr, priv->cached_addr, bo->meta.total_size); |
Gurchetan Singh | 8e02e05 | 2017-09-14 14:18:43 -0700 | [diff] [blame] | 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 368 | static void mediatek_resolve_format_and_use_flags(struct driver *drv, uint32_t format, |
| 369 | uint64_t use_flags, uint32_t *out_format, |
| 370 | uint64_t *out_use_flags) |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 371 | { |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 372 | *out_format = format; |
| 373 | *out_use_flags = use_flags; |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 374 | switch (format) { |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 375 | case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED: |
Nick Fan | 01c4014 | 2018-10-08 11:53:26 +0800 | [diff] [blame] | 376 | #ifdef MTK_MT8183 |
Jasmine Chen | c7aa974 | 2019-08-14 15:28:22 +0800 | [diff] [blame] | 377 | /* Only MT8183 Camera subsystem offers private reprocessing |
| 378 | * capability. CAMERA_READ indicates the buffer is intended for |
| 379 | * reprocessing and hence given the private format for MTK. */ |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 380 | if (use_flags & BO_USE_CAMERA_READ) { |
| 381 | *out_format = DRM_FORMAT_MTISP_SXYZW10; |
| 382 | break; |
| 383 | } |
Nick Fan | 01c4014 | 2018-10-08 11:53:26 +0800 | [diff] [blame] | 384 | #endif |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 385 | if (use_flags & BO_USE_CAMERA_WRITE) { |
| 386 | *out_format = DRM_FORMAT_NV12; |
| 387 | break; |
| 388 | } |
Wei Lee | e03625b | 2020-07-21 11:27:14 +0800 | [diff] [blame] | 389 | |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 390 | /* HACK: See b/28671744 */ |
| 391 | *out_format = DRM_FORMAT_XBGR8888; |
Yiwei Zhang | 3a171db | 2021-10-01 22:12:05 +0000 | [diff] [blame] | 392 | *out_use_flags &= ~BO_USE_HW_VIDEO_ENCODER; |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 393 | break; |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 394 | case DRM_FORMAT_FLEX_YCbCr_420_888: |
Hsin-Yi Wang | 8fe9f19 | 2022-01-18 17:51:02 +0800 | [diff] [blame] | 395 | #ifdef USE_NV12_FOR_HW_VIDEO_DECODING |
Wei Lee | 2f02cfb | 2020-08-05 17:24:45 +0800 | [diff] [blame] | 396 | // TODO(hiroh): Switch to use NV12 for video decoder on MT8173 as well. |
| 397 | if (use_flags & (BO_USE_HW_VIDEO_DECODER)) { |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 398 | *out_format = DRM_FORMAT_NV12; |
| 399 | break; |
Wei Lee | 2f02cfb | 2020-08-05 17:24:45 +0800 | [diff] [blame] | 400 | } |
| 401 | #endif |
| 402 | if (use_flags & |
| 403 | (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_ENCODER)) { |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 404 | *out_format = DRM_FORMAT_NV12; |
| 405 | break; |
Hirokazu Honda | 2a2bfc2 | 2019-10-11 15:54:50 +0900 | [diff] [blame] | 406 | } |
Yiwei Zhang | b3caf22 | 2021-10-01 21:55:58 +0000 | [diff] [blame] | 407 | |
| 408 | /* HACK: See b/139714614 */ |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 409 | *out_format = DRM_FORMAT_YVU420; |
Yiwei Zhang | b3caf22 | 2021-10-01 21:55:58 +0000 | [diff] [blame] | 410 | *out_use_flags &= ~BO_USE_SCANOUT; |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 411 | break; |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 412 | default: |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 413 | break; |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 414 | } |
Hsin-Yi Wang | d4df3d5 | 2022-03-28 13:29:06 +0800 | [diff] [blame] | 415 | /* Mediatek doesn't support YUV overlays */ |
| 416 | if (is_video_yuv_format(format)) |
| 417 | *out_use_flags &= ~BO_USE_SCANOUT; |
Gurchetan Singh | 42cc6d6 | 2016-08-29 18:19:19 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Gurchetan Singh | 3e9d383 | 2017-10-31 10:36:25 -0700 | [diff] [blame] | 420 | const struct backend backend_mediatek = { |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 421 | .name = "mediatek", |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 422 | .init = mediatek_init, |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame] | 423 | .bo_create = mediatek_bo_create, |
Fritz Koenig | 1b9b5b9 | 2019-03-19 13:25:45 -0700 | [diff] [blame] | 424 | .bo_create_with_modifiers = mediatek_bo_create_with_modifiers, |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 425 | .bo_destroy = drv_gem_bo_destroy, |
Gurchetan Singh | 71611d6 | 2017-01-03 16:49:56 -0800 | [diff] [blame] | 426 | .bo_import = drv_prime_bo_import, |
Gurchetan Singh | d7c84fd | 2016-08-16 18:18:24 -0700 | [diff] [blame] | 427 | .bo_map = mediatek_bo_map, |
Gurchetan Singh | 469a3aa | 2017-08-03 18:17:34 -0700 | [diff] [blame] | 428 | .bo_unmap = mediatek_bo_unmap, |
Gurchetan Singh | ef262d8 | 2017-11-28 16:56:17 -0800 | [diff] [blame] | 429 | .bo_invalidate = mediatek_bo_invalidate, |
Gurchetan Singh | 8e02e05 | 2017-09-14 14:18:43 -0700 | [diff] [blame] | 430 | .bo_flush = mediatek_bo_flush, |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 431 | .resolve_format_and_use_flags = mediatek_resolve_format_and_use_flags, |
JB Tsai | 0c16a0f | 2015-03-19 14:30:31 +0800 | [diff] [blame] | 432 | }; |
| 433 | |
| 434 | #endif |