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 | |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 21 | #include "drv_priv.h" |
| 22 | #include "helpers.h" |
| 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, |
| 41 | DRM_FORMAT_XRGB8888 }; |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 42 | |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 43 | static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_R8, |
| 44 | DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID }; |
Alexandre Courbot | 1805a9b | 2018-05-21 19:05:10 +0900 | [diff] [blame] | 45 | |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 46 | /* |
| 47 | * Each macrotile consists of m x n (mostly 4 x 4) tiles. |
| 48 | * Pixel data pitch/stride is aligned with macrotile width. |
| 49 | * Pixel data height is aligned with macrotile height. |
| 50 | * Entire pixel data buffer is aligned with 4k(bytes). |
| 51 | */ |
| 52 | static uint32_t get_ubwc_meta_size(uint32_t width, uint32_t height, uint32_t tile_width, |
| 53 | uint32_t tile_height) |
| 54 | { |
| 55 | uint32_t macrotile_width, macrotile_height; |
| 56 | |
| 57 | macrotile_width = DIV_ROUND_UP(width, tile_width); |
| 58 | macrotile_height = DIV_ROUND_UP(height, tile_height); |
| 59 | |
| 60 | // Align meta buffer width to 64 blocks |
| 61 | macrotile_width = ALIGN(macrotile_width, MACROTILE_WIDTH_ALIGN); |
| 62 | |
| 63 | // Align meta buffer height to 16 blocks |
| 64 | macrotile_height = ALIGN(macrotile_height, MACROTILE_HEIGHT_ALIGN); |
| 65 | |
| 66 | return ALIGN(macrotile_width * macrotile_height, PLANE_SIZE_ALIGN); |
| 67 | } |
| 68 | |
| 69 | static void msm_calculate_layout(struct bo *bo) |
Tanmay Shah | c65bd8c | 2018-11-21 09:14:14 -0800 | [diff] [blame] | 70 | { |
| 71 | uint32_t width, height; |
| 72 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 73 | width = bo->meta.width; |
| 74 | height = bo->meta.height; |
Tanmay Shah | c65bd8c | 2018-11-21 09:14:14 -0800 | [diff] [blame] | 75 | |
| 76 | /* NV12 format requires extra padding with platform |
| 77 | * specific alignments for venus driver |
| 78 | */ |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 79 | if (bo->meta.format == DRM_FORMAT_NV12) { |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 80 | uint32_t y_stride, uv_stride, y_scanline, uv_scanline, y_plane, uv_plane, size, |
| 81 | extra_padding; |
| 82 | |
Tanmay Shah | c65bd8c | 2018-11-21 09:14:14 -0800 | [diff] [blame] | 83 | y_stride = ALIGN(width, VENUS_STRIDE_ALIGN); |
| 84 | uv_stride = ALIGN(width, VENUS_STRIDE_ALIGN); |
| 85 | y_scanline = ALIGN(height, VENUS_SCANLINE_ALIGN * 2); |
Fritz Koenig | b03e9c8 | 2020-09-09 15:22:46 -0700 | [diff] [blame^] | 86 | uv_scanline = ALIGN(DIV_ROUND_UP(height, 2), |
| 87 | VENUS_SCANLINE_ALIGN * (bo->meta.tiling ? 2 : 1)); |
Tanmay Shah | c65bd8c | 2018-11-21 09:14:14 -0800 | [diff] [blame] | 88 | y_plane = y_stride * y_scanline; |
| 89 | uv_plane = uv_stride * uv_scanline; |
| 90 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 91 | if (bo->meta.tiling == MSM_UBWC_TILING) { |
Fritz Koenig | b03e9c8 | 2020-09-09 15:22:46 -0700 | [diff] [blame^] | 92 | y_plane = ALIGN(y_plane, PLANE_SIZE_ALIGN); |
| 93 | uv_plane = ALIGN(uv_plane, PLANE_SIZE_ALIGN); |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 94 | y_plane += get_ubwc_meta_size(width, height, 32, 8); |
| 95 | uv_plane += get_ubwc_meta_size(width >> 1, height >> 1, 16, 8); |
| 96 | extra_padding = NV12_UBWC_PADDING(y_stride); |
| 97 | } else { |
| 98 | extra_padding = NV12_LINEAR_PADDING; |
| 99 | } |
| 100 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 101 | bo->meta.strides[0] = y_stride; |
| 102 | bo->meta.sizes[0] = y_plane; |
| 103 | bo->meta.offsets[1] = y_plane; |
| 104 | bo->meta.strides[1] = uv_stride; |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 105 | size = y_plane + uv_plane + extra_padding; |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 106 | bo->meta.total_size = ALIGN(size, BUFFER_SIZE_ALIGN); |
| 107 | bo->meta.sizes[1] = bo->meta.total_size - bo->meta.sizes[0]; |
Tanmay Shah | c65bd8c | 2018-11-21 09:14:14 -0800 | [diff] [blame] | 108 | } else { |
| 109 | uint32_t stride, alignw, alignh; |
| 110 | |
| 111 | alignw = ALIGN(width, DEFAULT_ALIGNMENT); |
Jeffrey Kardatzke | a6a9efc | 2020-02-21 15:35:01 -0800 | [diff] [blame] | 112 | /* HAL_PIXEL_FORMAT_YV12 requires that the buffer's height not be aligned. |
| 113 | DRM_FORMAT_R8 of height one is used for JPEG camera output, so don't |
| 114 | height align that. */ |
| 115 | if (bo->meta.format == DRM_FORMAT_YVU420_ANDROID || |
Gurchetan Singh | 8d88474 | 2020-03-24 13:48:54 -0700 | [diff] [blame] | 116 | (bo->meta.format == DRM_FORMAT_R8 && height == 1)) { |
Tanmay Shah | c65bd8c | 2018-11-21 09:14:14 -0800 | [diff] [blame] | 117 | alignh = height; |
| 118 | } else { |
| 119 | alignh = ALIGN(height, DEFAULT_ALIGNMENT); |
| 120 | } |
| 121 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 122 | stride = drv_stride_from_format(bo->meta.format, alignw, 0); |
Tanmay Shah | c65bd8c | 2018-11-21 09:14:14 -0800 | [diff] [blame] | 123 | |
| 124 | /* 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] | 125 | drv_bo_from_format(bo, stride, alignh, bo->meta.format); |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 126 | |
| 127 | /* For all RGB UBWC formats */ |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 128 | if (bo->meta.tiling == MSM_UBWC_TILING) { |
| 129 | bo->meta.sizes[0] += get_ubwc_meta_size(width, height, 16, 4); |
| 130 | bo->meta.total_size = bo->meta.sizes[0]; |
| 131 | assert(IS_ALIGNED(bo->meta.total_size, BUFFER_SIZE_ALIGN)); |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | static bool is_ubwc_fmt(uint32_t format) |
| 137 | { |
| 138 | switch (format) { |
| 139 | case DRM_FORMAT_XBGR8888: |
| 140 | case DRM_FORMAT_ABGR8888: |
Fritz Koenig | e5c3fdf | 2019-12-10 16:30:34 -0800 | [diff] [blame] | 141 | case DRM_FORMAT_XRGB8888: |
| 142 | case DRM_FORMAT_ARGB8888: |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 143 | case DRM_FORMAT_NV12: |
| 144 | return 1; |
| 145 | default: |
| 146 | return 0; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | static void msm_add_ubwc_combinations(struct driver *drv, const uint32_t *formats, |
| 151 | uint32_t num_formats, struct format_metadata *metadata, |
| 152 | uint64_t use_flags) |
| 153 | { |
| 154 | for (uint32_t i = 0; i < num_formats; i++) { |
| 155 | if (is_ubwc_fmt(formats[i])) { |
| 156 | struct combination combo = { .format = formats[i], |
| 157 | .metadata = *metadata, |
| 158 | .use_flags = use_flags }; |
| 159 | drv_array_append(drv->combos, &combo); |
| 160 | } |
Tanmay Shah | c65bd8c | 2018-11-21 09:14:14 -0800 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
Rob Clark | e48e4d7 | 2020-08-07 08:08:30 -0700 | [diff] [blame] | 164 | /** |
| 165 | * Check for buggy apps that are known to not support modifiers, to avoid surprising them |
| 166 | * with a UBWC buffer. |
| 167 | */ |
| 168 | static bool should_avoid_ubwc(void) |
| 169 | { |
| 170 | #ifndef __ANDROID__ |
| 171 | /* waffle is buggy and, requests a renderable buffer (which on qcom platforms, we |
| 172 | * want to use UBWC), and then passes it to the kernel discarding the modifier. |
| 173 | * So mesa ends up correctly rendering to as tiled+compressed, but kernel tries |
| 174 | * to display as linear. Other platforms do not see this issue, simply because |
| 175 | * they only use compressed (ex, AFBC) with the BO_USE_SCANOUT flag. |
| 176 | * |
| 177 | * See b/163137550 |
| 178 | */ |
| 179 | if (dlsym(RTLD_DEFAULT, "waffle_display_connect")) { |
| 180 | drv_log("WARNING: waffle detected, disabling UBWC\n"); |
| 181 | return true; |
| 182 | } |
| 183 | #endif |
| 184 | return false; |
| 185 | } |
| 186 | |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 187 | static int msm_init(struct driver *drv) |
| 188 | { |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 189 | struct format_metadata metadata; |
Jeffrey Kardatzke | afb2c56 | 2020-03-02 12:25:55 -0800 | [diff] [blame] | 190 | uint64_t render_use_flags = BO_USE_RENDER_MASK | BO_USE_SCANOUT; |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 191 | uint64_t texture_use_flags = BO_USE_TEXTURE_MASK | BO_USE_HW_VIDEO_DECODER; |
Kristian H. Kristensen | 2de95ec | 2020-09-08 03:28:23 +0000 | [diff] [blame] | 192 | uint64_t sw_flags = (BO_USE_RENDERSCRIPT | BO_USE_SW_MASK | |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 193 | BO_USE_LINEAR | BO_USE_PROTECTED); |
| 194 | |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 195 | drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats), |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 196 | &LINEAR_METADATA, render_use_flags); |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 197 | |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 198 | drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats), |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 199 | &LINEAR_METADATA, texture_use_flags); |
Alexandre Courbot | 1805a9b | 2018-05-21 19:05:10 +0900 | [diff] [blame] | 200 | |
Ricky Liang | f11f1df | 2020-02-13 10:42:46 +0800 | [diff] [blame] | 201 | /* The camera stack standardizes on NV12 for YUV buffers. */ |
Hirokazu Honda | 3bd681c | 2020-06-23 17:52:20 +0900 | [diff] [blame] | 202 | /* YVU420 and NV12 formats for camera, display and encoding. */ |
Ricky Liang | f11f1df | 2020-02-13 10:42:46 +0800 | [diff] [blame] | 203 | drv_modify_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA, |
Hirokazu Honda | 3bd681c | 2020-06-23 17:52:20 +0900 | [diff] [blame] | 204 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT | |
| 205 | BO_USE_HW_VIDEO_ENCODER); |
| 206 | |
Ricky Liang | f11f1df | 2020-02-13 10:42:46 +0800 | [diff] [blame] | 207 | /* |
| 208 | * 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] | 209 | * from camera and input/output from hardware decoder/encoder. |
Ricky Liang | f11f1df | 2020-02-13 10:42:46 +0800 | [diff] [blame] | 210 | */ |
Douglas Anderson | 0c03c9b | 2020-07-21 08:06:46 -0700 | [diff] [blame] | 211 | drv_modify_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA, |
David Stevens | 4951814 | 2020-06-15 13:48:48 +0900 | [diff] [blame] | 212 | BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER | |
| 213 | BO_USE_HW_VIDEO_ENCODER); |
Ricky Liang | f11f1df | 2020-02-13 10:42:46 +0800 | [diff] [blame] | 214 | |
Gurchetan Singh | 71bc665 | 2018-09-17 17:42:05 -0700 | [diff] [blame] | 215 | /* Android CTS tests require this. */ |
| 216 | drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK); |
| 217 | |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 218 | drv_modify_linear_combinations(drv); |
| 219 | |
Rob Clark | e48e4d7 | 2020-08-07 08:08:30 -0700 | [diff] [blame] | 220 | if (should_avoid_ubwc()) |
| 221 | return 0; |
| 222 | |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 223 | metadata.tiling = MSM_UBWC_TILING; |
| 224 | metadata.priority = 2; |
| 225 | metadata.modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED; |
| 226 | |
| 227 | render_use_flags &= ~sw_flags; |
| 228 | texture_use_flags &= ~sw_flags; |
| 229 | |
Kristian H. Kristensen | 8f782d8 | 2020-08-26 00:26:24 +0000 | [diff] [blame] | 230 | msm_add_ubwc_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats), |
| 231 | &metadata, render_use_flags); |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 232 | |
Kristian H. Kristensen | 8f782d8 | 2020-08-26 00:26:24 +0000 | [diff] [blame] | 233 | msm_add_ubwc_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats), |
| 234 | &metadata, texture_use_flags); |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 235 | |
| 236 | return 0; |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 237 | } |
| 238 | |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 239 | static int msm_bo_create_for_modifier(struct bo *bo, uint32_t width, uint32_t height, |
| 240 | uint32_t format, const uint64_t modifier) |
Stéphane Marchesin | 23e006a | 2018-02-28 16:37:46 -0800 | [diff] [blame] | 241 | { |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 242 | struct drm_msm_gem_new req; |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 243 | int ret; |
| 244 | size_t i; |
| 245 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 246 | 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] | 247 | |
| 248 | msm_calculate_layout(bo); |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 249 | |
| 250 | memset(&req, 0, sizeof(req)); |
| 251 | req.flags = MSM_BO_WC | MSM_BO_SCANOUT; |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 252 | req.size = bo->meta.total_size; |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 253 | |
| 254 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_NEW, &req); |
| 255 | if (ret) { |
| 256 | 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] | 257 | return -errno; |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Though we use only one plane, we need to set handle for |
| 262 | * all planes to pass kernel checks |
| 263 | */ |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 264 | for (i = 0; i < bo->meta.num_planes; i++) { |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 265 | bo->handles[i].u32 = req.handle; |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 266 | bo->meta.format_modifiers[i] = modifier; |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 272 | static int msm_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height, |
| 273 | uint32_t format, const uint64_t *modifiers, uint32_t count) |
| 274 | { |
| 275 | static const uint64_t modifier_order[] = { |
| 276 | DRM_FORMAT_MOD_QCOM_COMPRESSED, |
| 277 | DRM_FORMAT_MOD_LINEAR, |
| 278 | }; |
| 279 | |
| 280 | uint64_t modifier = |
| 281 | drv_pick_modifier(modifiers, count, modifier_order, ARRAY_SIZE(modifier_order)); |
| 282 | |
| 283 | return msm_bo_create_for_modifier(bo, width, height, format, modifier); |
| 284 | } |
| 285 | |
| 286 | /* msm_bo_create will create linear buffers for now */ |
| 287 | static int msm_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format, |
| 288 | uint64_t flags) |
| 289 | { |
| 290 | struct combination *combo = drv_get_combination(bo->drv, format, flags); |
| 291 | |
| 292 | if (!combo) { |
Stephen Boyd | 5ff0cfd | 2019-04-09 11:03:00 -0700 | [diff] [blame] | 293 | drv_log("invalid format = %d, flags = %" PRIx64 " combination\n", format, flags); |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 294 | return -EINVAL; |
| 295 | } |
| 296 | |
| 297 | return msm_bo_create_for_modifier(bo, width, height, format, combo->metadata.modifier); |
| 298 | } |
| 299 | |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 300 | static void *msm_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags) |
| 301 | { |
| 302 | int ret; |
| 303 | struct drm_msm_gem_info req; |
| 304 | |
| 305 | memset(&req, 0, sizeof(req)); |
| 306 | req.handle = bo->handles[0].u32; |
| 307 | |
| 308 | ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_INFO, &req); |
| 309 | if (ret) { |
| 310 | drv_log("DRM_IOCLT_MSM_GEM_INFO failed with %s\n", strerror(errno)); |
| 311 | return MAP_FAILED; |
| 312 | } |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 313 | vma->length = bo->meta.total_size; |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 314 | |
Gurchetan Singh | 298b757 | 2019-09-19 09:55:18 -0700 | [diff] [blame] | 315 | 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] | 316 | req.offset); |
Stéphane Marchesin | 23e006a | 2018-02-28 16:37:46 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Jeffrey Kardatzke | e3cd0d3 | 2020-02-25 10:33:37 -0800 | [diff] [blame] | 319 | static uint32_t msm_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags) |
| 320 | { |
| 321 | switch (format) { |
Douglas Anderson | 64b80ce | 2020-05-19 10:12:14 -0700 | [diff] [blame] | 322 | case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED: |
| 323 | /* Camera subsystem requires NV12. */ |
| 324 | if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE)) |
| 325 | return DRM_FORMAT_NV12; |
| 326 | /*HACK: See b/28671744 */ |
| 327 | return DRM_FORMAT_XBGR8888; |
Jeffrey Kardatzke | e3cd0d3 | 2020-02-25 10:33:37 -0800 | [diff] [blame] | 328 | case DRM_FORMAT_FLEX_YCbCr_420_888: |
| 329 | return DRM_FORMAT_NV12; |
| 330 | default: |
| 331 | return format; |
| 332 | } |
| 333 | } |
| 334 | |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 335 | const struct backend backend_msm = { |
| 336 | .name = "msm", |
| 337 | .init = msm_init, |
Stéphane Marchesin | 23e006a | 2018-02-28 16:37:46 -0800 | [diff] [blame] | 338 | .bo_create = msm_bo_create, |
Tanmay Shah | 067594b | 2018-11-26 10:05:18 -0800 | [diff] [blame] | 339 | .bo_create_with_modifiers = msm_bo_create_with_modifiers, |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 340 | .bo_destroy = drv_gem_bo_destroy, |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 341 | .bo_import = drv_prime_bo_import, |
Tanmay Shah | 617ee71 | 2018-07-11 16:41:05 -0700 | [diff] [blame] | 342 | .bo_map = msm_bo_map, |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 343 | .bo_unmap = drv_bo_munmap, |
Jeffrey Kardatzke | e3cd0d3 | 2020-02-25 10:33:37 -0800 | [diff] [blame] | 344 | .resolve_format = msm_resolve_format, |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 345 | }; |
Rajesh Yadav | 7f79cb5 | 2018-01-22 18:29:06 +0530 | [diff] [blame] | 346 | #endif /* DRV_MSM */ |