Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
| 7 | #ifdef DRV_MSM |
| 8 | |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 9 | #include <assert.h> |
Rob Clark | e48e4d7 | 2020-08-07 08:08:30 -0700 | [diff] [blame] | 10 | #include <dlfcn.h> |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 11 | #include <drm_fourcc.h> |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 12 | #include <errno.h> |
Stephen Boyd | 5ff0cfd | 2019-04-09 11:03:00 -0700 | [diff] [blame] | 13 | #include <inttypes.h> |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 14 | #include <msm_drm.h> |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 15 | #include <stdbool.h> |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | #include <string.h> |
| 18 | #include <sys/mman.h> |
| 19 | #include <xf86drm.h> |
| 20 | |
Yiwei Zhang | b7a6444 | 2021-09-30 05:13:10 +0000 | [diff] [blame] | 21 | #include "drv_helpers.h" |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 22 | #include "drv_priv.h" |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 23 | #include "util.h" |
| 24 | |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 25 | /* Alignment values are based on SDM845 Gfx IP */ |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 26 | #define DEFAULT_ALIGNMENT 64 |
Tanmay Shah | c65bd8c | 2018-11-21 09:14:14 -0800 | [diff] [blame] | 27 | #define BUFFER_SIZE_ALIGN 4096 |
| 28 | |
| 29 | #define VENUS_STRIDE_ALIGN 128 |
| 30 | #define VENUS_SCANLINE_ALIGN 16 |
| 31 | #define NV12_LINEAR_PADDING (12 * 1024) |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 32 | #define NV12_UBWC_PADDING(y_stride) (MAX(16 * 1024, y_stride * 48)) |
| 33 | #define MACROTILE_WIDTH_ALIGN 64 |
| 34 | #define MACROTILE_HEIGHT_ALIGN 16 |
| 35 | #define PLANE_SIZE_ALIGN 4096 |
| 36 | |
| 37 | #define MSM_UBWC_TILING 1 |
Stéphane Marchesin | 23e006a | 2018-02-28 16:37:46 -0800 | [diff] [blame] | 38 | |
Gurchetan Singh | b131c9d | 2018-08-28 14:17:05 -0700 | [diff] [blame] | 39 | 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] | 40 | DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888, |
Rob Clark | 06cf1f1 | 2022-01-24 16:10:59 -0800 | [diff] [blame] | 41 | DRM_FORMAT_XRGB8888, DRM_FORMAT_ABGR2101010, |
| 42 | DRM_FORMAT_ABGR16161616F }; |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 43 | |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 44 | static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_R8, |
Fritz Koenig | 2471aa2 | 2021-08-17 10:32:12 -0700 | [diff] [blame] | 45 | DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID, |
| 46 | DRM_FORMAT_P010 }; |
Alexandre Courbot | 1805a9b | 2018-05-21 19:05:10 +0900 | [diff] [blame] | 47 | |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 48 | /* |
| 49 | * Each macrotile consists of m x n (mostly 4 x 4) tiles. |
| 50 | * Pixel data pitch/stride is aligned with macrotile width. |
| 51 | * Pixel data height is aligned with macrotile height. |
| 52 | * Entire pixel data buffer is aligned with 4k(bytes). |
| 53 | */ |
| 54 | static uint32_t get_ubwc_meta_size(uint32_t width, uint32_t height, uint32_t tile_width, |
| 55 | uint32_t tile_height) |
| 56 | { |
| 57 | uint32_t macrotile_width, macrotile_height; |
| 58 | |
| 59 | macrotile_width = DIV_ROUND_UP(width, tile_width); |
| 60 | macrotile_height = DIV_ROUND_UP(height, tile_height); |
| 61 | |
| 62 | // Align meta buffer width to 64 blocks |
| 63 | macrotile_width = ALIGN(macrotile_width, MACROTILE_WIDTH_ALIGN); |
| 64 | |
| 65 | // Align meta buffer height to 16 blocks |
| 66 | macrotile_height = ALIGN(macrotile_height, MACROTILE_HEIGHT_ALIGN); |
| 67 | |
| 68 | return ALIGN(macrotile_width * macrotile_height, PLANE_SIZE_ALIGN); |
| 69 | } |
| 70 | |
Rob Clark | 7278758 | 2020-12-01 12:13:13 -0800 | [diff] [blame] | 71 | static unsigned get_pitch_alignment(struct bo *bo) |
| 72 | { |
| 73 | switch (bo->meta.format) { |
| 74 | case DRM_FORMAT_NV12: |
| 75 | return VENUS_STRIDE_ALIGN; |
| 76 | case DRM_FORMAT_YVU420: |
| 77 | case DRM_FORMAT_YVU420_ANDROID: |
| 78 | /* TODO other YUV formats? */ |
| 79 | /* Something (in the video stack?) assumes the U/V planes can use |
| 80 | * half the pitch as the Y plane.. to componsate, double the |
| 81 | * alignment: |
| 82 | */ |
| 83 | return 2 * DEFAULT_ALIGNMENT; |
| 84 | default: |
| 85 | return DEFAULT_ALIGNMENT; |
| 86 | } |
| 87 | } |
| 88 | |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 89 | static void msm_calculate_layout(struct bo *bo) |
Tanmay Shah | c65bd8c | 2018-11-21 09:14:14 -0800 | [diff] [blame] | 90 | { |
| 91 | uint32_t width, height; |
| 92 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 93 | width = bo->meta.width; |
| 94 | height = bo->meta.height; |
Tanmay Shah | c65bd8c | 2018-11-21 09:14:14 -0800 | [diff] [blame] | 95 | |
| 96 | /* NV12 format requires extra padding with platform |
| 97 | * specific alignments for venus driver |
| 98 | */ |
Fritz Koenig | 2471aa2 | 2021-08-17 10:32:12 -0700 | [diff] [blame] | 99 | if (bo->meta.format == DRM_FORMAT_NV12 || bo->meta.format == DRM_FORMAT_P010) { |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 100 | uint32_t y_stride, uv_stride, y_scanline, uv_scanline, y_plane, uv_plane, size, |
| 101 | extra_padding; |
| 102 | |
Fritz Koenig | 2471aa2 | 2021-08-17 10:32:12 -0700 | [diff] [blame] | 103 | // P010 has the same layout as NV12. The difference is that each |
| 104 | // pixel in P010 takes 2 bytes, while in NV12 each pixel takes 1 byte. |
| 105 | if (bo->meta.format == DRM_FORMAT_P010) |
| 106 | width *= 2; |
| 107 | |
Tanmay Shah | c65bd8c | 2018-11-21 09:14:14 -0800 | [diff] [blame] | 108 | y_stride = ALIGN(width, VENUS_STRIDE_ALIGN); |
| 109 | uv_stride = ALIGN(width, VENUS_STRIDE_ALIGN); |
| 110 | y_scanline = ALIGN(height, VENUS_SCANLINE_ALIGN * 2); |
Fritz Koenig | b03e9c8 | 2020-09-09 15:22:46 -0700 | [diff] [blame] | 111 | uv_scanline = ALIGN(DIV_ROUND_UP(height, 2), |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 112 | VENUS_SCANLINE_ALIGN * (bo->meta.tiling ? 2 : 1)); |
Tanmay Shah | c65bd8c | 2018-11-21 09:14:14 -0800 | [diff] [blame] | 113 | y_plane = y_stride * y_scanline; |
| 114 | uv_plane = uv_stride * uv_scanline; |
| 115 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 116 | if (bo->meta.tiling == MSM_UBWC_TILING) { |
Fritz Koenig | b03e9c8 | 2020-09-09 15:22:46 -0700 | [diff] [blame] | 117 | y_plane = ALIGN(y_plane, PLANE_SIZE_ALIGN); |
| 118 | uv_plane = ALIGN(uv_plane, PLANE_SIZE_ALIGN); |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 119 | y_plane += get_ubwc_meta_size(width, height, 32, 8); |
| 120 | uv_plane += get_ubwc_meta_size(width >> 1, height >> 1, 16, 8); |
| 121 | extra_padding = NV12_UBWC_PADDING(y_stride); |
| 122 | } else { |
| 123 | extra_padding = NV12_LINEAR_PADDING; |
| 124 | } |
| 125 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 126 | bo->meta.strides[0] = y_stride; |
| 127 | bo->meta.sizes[0] = y_plane; |
| 128 | bo->meta.offsets[1] = y_plane; |
| 129 | bo->meta.strides[1] = uv_stride; |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 130 | size = y_plane + uv_plane + extra_padding; |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 131 | bo->meta.total_size = ALIGN(size, BUFFER_SIZE_ALIGN); |
| 132 | bo->meta.sizes[1] = bo->meta.total_size - bo->meta.sizes[0]; |
Tanmay Shah | c65bd8c | 2018-11-21 09:14:14 -0800 | [diff] [blame] | 133 | } else { |
| 134 | uint32_t stride, alignw, alignh; |
| 135 | |
Rob Clark | 7278758 | 2020-12-01 12:13:13 -0800 | [diff] [blame] | 136 | alignw = ALIGN(width, get_pitch_alignment(bo)); |
Jeffrey Kardatzke | a6a9efc | 2020-02-21 15:35:01 -0800 | [diff] [blame] | 137 | /* HAL_PIXEL_FORMAT_YV12 requires that the buffer's height not be aligned. |
| 138 | DRM_FORMAT_R8 of height one is used for JPEG camera output, so don't |
| 139 | height align that. */ |
| 140 | if (bo->meta.format == DRM_FORMAT_YVU420_ANDROID || |
Gurchetan Singh | 8d88474 | 2020-03-24 13:48:54 -0700 | [diff] [blame] | 141 | (bo->meta.format == DRM_FORMAT_R8 && height == 1)) { |
Tanmay Shah | c65bd8c | 2018-11-21 09:14:14 -0800 | [diff] [blame] | 142 | alignh = height; |
| 143 | } else { |
| 144 | alignh = ALIGN(height, DEFAULT_ALIGNMENT); |
| 145 | } |
| 146 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 147 | stride = drv_stride_from_format(bo->meta.format, alignw, 0); |
Tanmay Shah | c65bd8c | 2018-11-21 09:14:14 -0800 | [diff] [blame] | 148 | |
| 149 | /* Calculate size and assign stride, size, offset to each plane based on format */ |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 150 | drv_bo_from_format(bo, stride, alignh, bo->meta.format); |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 151 | |
| 152 | /* For all RGB UBWC formats */ |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 153 | if (bo->meta.tiling == MSM_UBWC_TILING) { |
| 154 | bo->meta.sizes[0] += get_ubwc_meta_size(width, height, 16, 4); |
| 155 | bo->meta.total_size = bo->meta.sizes[0]; |
| 156 | assert(IS_ALIGNED(bo->meta.total_size, BUFFER_SIZE_ALIGN)); |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | static bool is_ubwc_fmt(uint32_t format) |
| 162 | { |
| 163 | switch (format) { |
| 164 | case DRM_FORMAT_XBGR8888: |
| 165 | case DRM_FORMAT_ABGR8888: |
Fritz Koenig | e5c3fdf | 2019-12-10 16:30:34 -0800 | [diff] [blame] | 166 | case DRM_FORMAT_XRGB8888: |
| 167 | case DRM_FORMAT_ARGB8888: |
John Stultz | 0a4229e | 2021-11-06 03:12:43 +0000 | [diff] [blame] | 168 | #ifndef QCOM_DISABLE_COMPRESSED_NV12 |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 169 | case DRM_FORMAT_NV12: |
John Stultz | 0a4229e | 2021-11-06 03:12:43 +0000 | [diff] [blame] | 170 | #endif |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 171 | return 1; |
| 172 | default: |
| 173 | return 0; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | static void msm_add_ubwc_combinations(struct driver *drv, const uint32_t *formats, |
| 178 | uint32_t num_formats, struct format_metadata *metadata, |
| 179 | uint64_t use_flags) |
| 180 | { |
| 181 | for (uint32_t i = 0; i < num_formats; i++) { |
| 182 | if (is_ubwc_fmt(formats[i])) { |
| 183 | struct combination combo = { .format = formats[i], |
| 184 | .metadata = *metadata, |
| 185 | .use_flags = use_flags }; |
| 186 | drv_array_append(drv->combos, &combo); |
| 187 | } |
Tanmay Shah | c65bd8c | 2018-11-21 09:14:14 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Rob Clark | e48e4d7 | 2020-08-07 08:08:30 -0700 | [diff] [blame] | 191 | /** |
| 192 | * Check for buggy apps that are known to not support modifiers, to avoid surprising them |
| 193 | * with a UBWC buffer. |
| 194 | */ |
| 195 | static bool should_avoid_ubwc(void) |
| 196 | { |
| 197 | #ifndef __ANDROID__ |
| 198 | /* waffle is buggy and, requests a renderable buffer (which on qcom platforms, we |
| 199 | * want to use UBWC), and then passes it to the kernel discarding the modifier. |
| 200 | * So mesa ends up correctly rendering to as tiled+compressed, but kernel tries |
| 201 | * to display as linear. Other platforms do not see this issue, simply because |
| 202 | * they only use compressed (ex, AFBC) with the BO_USE_SCANOUT flag. |
| 203 | * |
| 204 | * See b/163137550 |
| 205 | */ |
| 206 | if (dlsym(RTLD_DEFAULT, "waffle_display_connect")) { |
| 207 | drv_log("WARNING: waffle detected, disabling UBWC\n"); |
| 208 | return true; |
| 209 | } |
Yiwei Zhang | 7a1a7db | 2022-04-14 06:28:14 +0000 | [diff] [blame] | 210 | |
| 211 | /* Sommelier relies on implicit modifier, which does not pass host modifier to |
| 212 | * zwp_linux_buffer_params_v1_add. Graphics will be broken if UBWC is enabled. |
| 213 | * Sommelier shall be fixed to mirror what arc wayland_service does, and then |
| 214 | * we can re-enable UBWC here. |
| 215 | * |
| 216 | * Inherit the trick from crrev/c/2523246 previously used for gtest. The side |
| 217 | * effect is all VM guests on msm will revert back to use linear modifier. |
| 218 | * |
| 219 | * See b/229147702 |
| 220 | */ |
| 221 | if (!dlsym(RTLD_DEFAULT, "cupsFilePrintf")) { |
| 222 | drv_log("WARNING: virtualization detected, disabling UBWC\n"); |
| 223 | return true; |
| 224 | } |
Rob Clark | e48e4d7 | 2020-08-07 08:08:30 -0700 | [diff] [blame] | 225 | #endif |
| 226 | return false; |
| 227 | } |
| 228 | |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 229 | static int msm_init(struct driver *drv) |
| 230 | { |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 231 | struct format_metadata metadata; |
Jeffrey Kardatzke | afb2c56 | 2020-03-02 12:25:55 -0800 | [diff] [blame] | 232 | uint64_t render_use_flags = BO_USE_RENDER_MASK | BO_USE_SCANOUT; |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 233 | uint64_t texture_use_flags = BO_USE_TEXTURE_MASK | BO_USE_HW_VIDEO_DECODER; |
Rob Clark | 6f83a44 | 2020-10-05 12:10:51 -0700 | [diff] [blame] | 234 | /* |
| 235 | * NOTE: we actually could use tiled in the BO_USE_FRONT_RENDERING case, |
| 236 | * if we had a modifier for tiled-but-not-compressed. But we *cannot* use |
| 237 | * compressed in this case because the UBWC flags/meta data can be out of |
| 238 | * sync with pixel data while the GPU is writing a frame out to memory. |
| 239 | */ |
Gurchetan Singh | 146ee02 | 2021-04-02 16:34:05 -0700 | [diff] [blame] | 240 | uint64_t sw_flags = |
| 241 | (BO_USE_RENDERSCRIPT | BO_USE_SW_MASK | BO_USE_LINEAR | BO_USE_FRONT_RENDERING); |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 242 | |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 243 | drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats), |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 244 | &LINEAR_METADATA, render_use_flags); |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 245 | |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 246 | drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats), |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 247 | &LINEAR_METADATA, texture_use_flags); |
Alexandre Courbot | 1805a9b | 2018-05-21 19:05:10 +0900 | [diff] [blame] | 248 | |
Ricky Liang | f11f1df | 2020-02-13 10:42:46 +0800 | [diff] [blame] | 249 | /* The camera stack standardizes on NV12 for YUV buffers. */ |
Hirokazu Honda | 3bd681c | 2020-06-23 17:52:20 +0900 | [diff] [blame] | 250 | /* YVU420 and NV12 formats for camera, display and encoding. */ |
Ricky Liang | f11f1df | 2020-02-13 10:42:46 +0800 | [diff] [blame] | 251 | drv_modify_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA, |
Hirokazu Honda | 3bd681c | 2020-06-23 17:52:20 +0900 | [diff] [blame] | 252 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT | |
| 253 | BO_USE_HW_VIDEO_ENCODER); |
| 254 | |
Ricky Liang | f11f1df | 2020-02-13 10:42:46 +0800 | [diff] [blame] | 255 | /* |
| 256 | * 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] | 257 | * from camera and input/output from hardware decoder/encoder. |
Ricky Liang | f11f1df | 2020-02-13 10:42:46 +0800 | [diff] [blame] | 258 | */ |
Douglas Anderson | 0c03c9b | 2020-07-21 08:06:46 -0700 | [diff] [blame] | 259 | drv_modify_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA, |
David Stevens | 4951814 | 2020-06-15 13:48:48 +0900 | [diff] [blame] | 260 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER | |
| 261 | BO_USE_HW_VIDEO_ENCODER); |
Ricky Liang | f11f1df | 2020-02-13 10:42:46 +0800 | [diff] [blame] | 262 | |
John Stultz | d9381b6 | 2021-07-28 06:23:03 +0000 | [diff] [blame] | 263 | /* |
| 264 | * Android also frequently requests YV12 formats for some camera implementations |
Yiwei Zhang | 26e16cb | 2021-09-30 06:51:53 +0000 | [diff] [blame] | 265 | * (including the external provider implmenetation). |
John Stultz | d9381b6 | 2021-07-28 06:23:03 +0000 | [diff] [blame] | 266 | */ |
| 267 | drv_modify_combination(drv, DRM_FORMAT_YVU420_ANDROID, &LINEAR_METADATA, |
Yiwei Zhang | 26e16cb | 2021-09-30 06:51:53 +0000 | [diff] [blame] | 268 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE); |
John Stultz | d9381b6 | 2021-07-28 06:23:03 +0000 | [diff] [blame] | 269 | |
Gurchetan Singh | 71bc665 | 2018-09-17 17:42:05 -0700 | [diff] [blame] | 270 | /* Android CTS tests require this. */ |
| 271 | drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK); |
| 272 | |
Fritz Koenig | 2471aa2 | 2021-08-17 10:32:12 -0700 | [diff] [blame] | 273 | #ifdef SC_7280 |
| 274 | drv_modify_combination(drv, DRM_FORMAT_P010, &LINEAR_METADATA, |
| 275 | BO_USE_SCANOUT | BO_USE_HW_VIDEO_ENCODER); |
| 276 | #endif |
| 277 | |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 278 | drv_modify_linear_combinations(drv); |
| 279 | |
Pilar Molina Lopez | 28cf2f1 | 2020-11-12 18:19:42 -0500 | [diff] [blame] | 280 | if (should_avoid_ubwc() || !drv->compression) |
Rob Clark | e48e4d7 | 2020-08-07 08:08:30 -0700 | [diff] [blame] | 281 | return 0; |
| 282 | |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 283 | metadata.tiling = MSM_UBWC_TILING; |
| 284 | metadata.priority = 2; |
| 285 | metadata.modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED; |
| 286 | |
| 287 | render_use_flags &= ~sw_flags; |
| 288 | texture_use_flags &= ~sw_flags; |
| 289 | |
Kristian H. Kristensen | 8f782d8 | 2020-08-26 00:26:24 +0000 | [diff] [blame] | 290 | msm_add_ubwc_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats), |
| 291 | &metadata, render_use_flags); |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 292 | |
Kristian H. Kristensen | 8f782d8 | 2020-08-26 00:26:24 +0000 | [diff] [blame] | 293 | msm_add_ubwc_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats), |
| 294 | &metadata, texture_use_flags); |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 295 | |
Fritz Koenig | b8226cb | 2020-08-07 14:56:18 -0700 | [diff] [blame] | 296 | drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata, |
Gurchetan Singh | 45ca449 | 2021-04-28 17:12:52 -0700 | [diff] [blame] | 297 | BO_USE_SCANOUT | BO_USE_HW_VIDEO_ENCODER); |
Fritz Koenig | b8226cb | 2020-08-07 14:56:18 -0700 | [diff] [blame] | 298 | |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 299 | return 0; |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 300 | } |
| 301 | |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 302 | static int msm_bo_create_for_modifier(struct bo *bo, uint32_t width, uint32_t height, |
| 303 | uint32_t format, const uint64_t modifier) |
Stéphane Marchesin | 23e006a | 2018-02-28 16:37:46 -0800 | [diff] [blame] | 304 | { |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 305 | struct drm_msm_gem_new req = { 0 }; |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 306 | int ret; |
| 307 | size_t i; |
| 308 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 309 | bo->meta.tiling = (modifier == DRM_FORMAT_MOD_QCOM_COMPRESSED) ? MSM_UBWC_TILING : 0; |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 310 | msm_calculate_layout(bo); |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 311 | |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 312 | req.flags = MSM_BO_WC | MSM_BO_SCANOUT; |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 313 | req.size = bo->meta.total_size; |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 314 | |
| 315 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_NEW, &req); |
| 316 | if (ret) { |
| 317 | drv_log("DRM_IOCTL_MSM_GEM_NEW failed with %s\n", strerror(errno)); |
Stéphane Marchesin | 6ac299f | 2019-03-21 12:23:29 -0700 | [diff] [blame] | 318 | return -errno; |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Though we use only one plane, we need to set handle for |
| 323 | * all planes to pass kernel checks |
| 324 | */ |
Gurchetan Singh | 52155b4 | 2021-01-27 17:55:17 -0800 | [diff] [blame] | 325 | for (i = 0; i < bo->meta.num_planes; i++) |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 326 | bo->handles[i].u32 = req.handle; |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 327 | |
Gurchetan Singh | 52155b4 | 2021-01-27 17:55:17 -0800 | [diff] [blame] | 328 | bo->meta.format_modifier = modifier; |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 329 | return 0; |
| 330 | } |
| 331 | |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 332 | static int msm_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height, |
| 333 | uint32_t format, const uint64_t *modifiers, uint32_t count) |
| 334 | { |
| 335 | static const uint64_t modifier_order[] = { |
| 336 | DRM_FORMAT_MOD_QCOM_COMPRESSED, |
| 337 | DRM_FORMAT_MOD_LINEAR, |
| 338 | }; |
| 339 | |
| 340 | uint64_t modifier = |
| 341 | drv_pick_modifier(modifiers, count, modifier_order, ARRAY_SIZE(modifier_order)); |
| 342 | |
Pilar Molina Lopez | 28cf2f1 | 2020-11-12 18:19:42 -0500 | [diff] [blame] | 343 | if (!bo->drv->compression && modifier == DRM_FORMAT_MOD_QCOM_COMPRESSED) |
| 344 | modifier = DRM_FORMAT_MOD_LINEAR; |
| 345 | |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 346 | return msm_bo_create_for_modifier(bo, width, height, format, modifier); |
| 347 | } |
| 348 | |
| 349 | /* msm_bo_create will create linear buffers for now */ |
| 350 | static int msm_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format, |
| 351 | uint64_t flags) |
| 352 | { |
| 353 | struct combination *combo = drv_get_combination(bo->drv, format, flags); |
| 354 | |
| 355 | if (!combo) { |
Stephen Boyd | 5ff0cfd | 2019-04-09 11:03:00 -0700 | [diff] [blame] | 356 | drv_log("invalid format = %d, flags = %" PRIx64 " combination\n", format, flags); |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 357 | return -EINVAL; |
| 358 | } |
| 359 | |
| 360 | return msm_bo_create_for_modifier(bo, width, height, format, combo->metadata.modifier); |
| 361 | } |
| 362 | |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 363 | static void *msm_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags) |
| 364 | { |
| 365 | int ret; |
Gurchetan Singh | 9964438 | 2020-10-07 15:28:11 -0700 | [diff] [blame] | 366 | struct drm_msm_gem_info req = { 0 }; |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 367 | |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 368 | req.handle = bo->handles[0].u32; |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 369 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_INFO, &req); |
| 370 | if (ret) { |
| 371 | drv_log("DRM_IOCLT_MSM_GEM_INFO failed with %s\n", strerror(errno)); |
| 372 | return MAP_FAILED; |
| 373 | } |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 374 | vma->length = bo->meta.total_size; |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 375 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 376 | return mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd, |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 377 | req.offset); |
Stéphane Marchesin | 23e006a | 2018-02-28 16:37:46 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 380 | const struct backend backend_msm = { |
| 381 | .name = "msm", |
| 382 | .init = msm_init, |
Stéphane Marchesin | 23e006a | 2018-02-28 16:37:46 -0800 | [diff] [blame] | 383 | .bo_create = msm_bo_create, |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 384 | .bo_create_with_modifiers = msm_bo_create_with_modifiers, |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 385 | .bo_destroy = drv_gem_bo_destroy, |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 386 | .bo_import = drv_prime_bo_import, |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 387 | .bo_map = msm_bo_map, |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 388 | .bo_unmap = drv_bo_munmap, |
Yiwei Zhang | b8ad7b8 | 2021-10-01 17:55:14 +0000 | [diff] [blame] | 389 | .resolve_format_and_use_flags = drv_resolve_format_and_use_flags_helper, |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 390 | }; |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 391 | #endif /* DRV_MSM */ |