blob: ac23b73ae37fb619a4acf46e2f1430c20890710d [file] [log] [blame]
Rajesh Yadav7f79cb52018-01-22 18:29:06 +05301/*
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 Shah067594b2018-11-26 10:05:18 -08009#include <assert.h>
Rob Clarke48e4d72020-08-07 08:08:30 -070010#include <dlfcn.h>
Tanmay Shah067594b2018-11-26 10:05:18 -080011#include <drm_fourcc.h>
Tanmay Shah617ee712018-07-11 16:41:05 -070012#include <errno.h>
Stephen Boyd5ff0cfd2019-04-09 11:03:00 -070013#include <inttypes.h>
Tanmay Shah617ee712018-07-11 16:41:05 -070014#include <msm_drm.h>
Tanmay Shah067594b2018-11-26 10:05:18 -080015#include <stdbool.h>
Tanmay Shah617ee712018-07-11 16:41:05 -070016#include <stdio.h>
17#include <string.h>
18#include <sys/mman.h>
19#include <xf86drm.h>
20
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053021#include "drv_priv.h"
22#include "helpers.h"
23#include "util.h"
24
Tanmay Shah067594b2018-11-26 10:05:18 -080025/* Alignment values are based on SDM845 Gfx IP */
Tanmay Shah617ee712018-07-11 16:41:05 -070026#define DEFAULT_ALIGNMENT 64
Tanmay Shahc65bd8c2018-11-21 09:14:14 -080027#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 Shah067594b2018-11-26 10:05:18 -080032#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 Marchesin23e006a2018-02-28 16:37:46 -080038
Gurchetan Singhb131c9d2018-08-28 14:17:05 -070039static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
Gurchetan Singh71bc6652018-09-17 17:42:05 -070040 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
41 DRM_FORMAT_XRGB8888 };
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053042
Tanmay Shah617ee712018-07-11 16:41:05 -070043static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_R8,
44 DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID };
Alexandre Courbot1805a9b2018-05-21 19:05:10 +090045
Tanmay Shah067594b2018-11-26 10:05:18 -080046/*
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 */
52static 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
69static void msm_calculate_layout(struct bo *bo)
Tanmay Shahc65bd8c2018-11-21 09:14:14 -080070{
71 uint32_t width, height;
72
Gurchetan Singh298b7572019-09-19 09:55:18 -070073 width = bo->meta.width;
74 height = bo->meta.height;
Tanmay Shahc65bd8c2018-11-21 09:14:14 -080075
76 /* NV12 format requires extra padding with platform
77 * specific alignments for venus driver
78 */
Gurchetan Singh298b7572019-09-19 09:55:18 -070079 if (bo->meta.format == DRM_FORMAT_NV12) {
Tanmay Shah067594b2018-11-26 10:05:18 -080080 uint32_t y_stride, uv_stride, y_scanline, uv_scanline, y_plane, uv_plane, size,
81 extra_padding;
82
Tanmay Shahc65bd8c2018-11-21 09:14:14 -080083 y_stride = ALIGN(width, VENUS_STRIDE_ALIGN);
84 uv_stride = ALIGN(width, VENUS_STRIDE_ALIGN);
85 y_scanline = ALIGN(height, VENUS_SCANLINE_ALIGN * 2);
86 uv_scanline = ALIGN(DIV_ROUND_UP(height, 2), VENUS_SCANLINE_ALIGN);
87 y_plane = y_stride * y_scanline;
88 uv_plane = uv_stride * uv_scanline;
89
Gurchetan Singh298b7572019-09-19 09:55:18 -070090 if (bo->meta.tiling == MSM_UBWC_TILING) {
Tanmay Shah067594b2018-11-26 10:05:18 -080091 y_plane += get_ubwc_meta_size(width, height, 32, 8);
92 uv_plane += get_ubwc_meta_size(width >> 1, height >> 1, 16, 8);
93 extra_padding = NV12_UBWC_PADDING(y_stride);
94 } else {
95 extra_padding = NV12_LINEAR_PADDING;
96 }
97
Gurchetan Singh298b7572019-09-19 09:55:18 -070098 bo->meta.strides[0] = y_stride;
99 bo->meta.sizes[0] = y_plane;
100 bo->meta.offsets[1] = y_plane;
101 bo->meta.strides[1] = uv_stride;
Tanmay Shah067594b2018-11-26 10:05:18 -0800102 size = y_plane + uv_plane + extra_padding;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700103 bo->meta.total_size = ALIGN(size, BUFFER_SIZE_ALIGN);
104 bo->meta.sizes[1] = bo->meta.total_size - bo->meta.sizes[0];
Tanmay Shahc65bd8c2018-11-21 09:14:14 -0800105 } else {
106 uint32_t stride, alignw, alignh;
107
108 alignw = ALIGN(width, DEFAULT_ALIGNMENT);
Jeffrey Kardatzkea6a9efc2020-02-21 15:35:01 -0800109 /* HAL_PIXEL_FORMAT_YV12 requires that the buffer's height not be aligned.
110 DRM_FORMAT_R8 of height one is used for JPEG camera output, so don't
111 height align that. */
112 if (bo->meta.format == DRM_FORMAT_YVU420_ANDROID ||
Gurchetan Singh8d884742020-03-24 13:48:54 -0700113 (bo->meta.format == DRM_FORMAT_R8 && height == 1)) {
Tanmay Shahc65bd8c2018-11-21 09:14:14 -0800114 alignh = height;
115 } else {
116 alignh = ALIGN(height, DEFAULT_ALIGNMENT);
117 }
118
Gurchetan Singh298b7572019-09-19 09:55:18 -0700119 stride = drv_stride_from_format(bo->meta.format, alignw, 0);
Tanmay Shahc65bd8c2018-11-21 09:14:14 -0800120
121 /* Calculate size and assign stride, size, offset to each plane based on format */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700122 drv_bo_from_format(bo, stride, alignh, bo->meta.format);
Tanmay Shah067594b2018-11-26 10:05:18 -0800123
124 /* For all RGB UBWC formats */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700125 if (bo->meta.tiling == MSM_UBWC_TILING) {
126 bo->meta.sizes[0] += get_ubwc_meta_size(width, height, 16, 4);
127 bo->meta.total_size = bo->meta.sizes[0];
128 assert(IS_ALIGNED(bo->meta.total_size, BUFFER_SIZE_ALIGN));
Tanmay Shah067594b2018-11-26 10:05:18 -0800129 }
130 }
131}
132
133static bool is_ubwc_fmt(uint32_t format)
134{
135 switch (format) {
136 case DRM_FORMAT_XBGR8888:
137 case DRM_FORMAT_ABGR8888:
Fritz Koenige5c3fdf2019-12-10 16:30:34 -0800138 case DRM_FORMAT_XRGB8888:
139 case DRM_FORMAT_ARGB8888:
Tanmay Shah067594b2018-11-26 10:05:18 -0800140 case DRM_FORMAT_NV12:
141 return 1;
142 default:
143 return 0;
144 }
145}
146
147static void msm_add_ubwc_combinations(struct driver *drv, const uint32_t *formats,
148 uint32_t num_formats, struct format_metadata *metadata,
149 uint64_t use_flags)
150{
151 for (uint32_t i = 0; i < num_formats; i++) {
152 if (is_ubwc_fmt(formats[i])) {
153 struct combination combo = { .format = formats[i],
154 .metadata = *metadata,
155 .use_flags = use_flags };
156 drv_array_append(drv->combos, &combo);
157 }
Tanmay Shahc65bd8c2018-11-21 09:14:14 -0800158 }
159}
160
Rob Clarke48e4d72020-08-07 08:08:30 -0700161/**
162 * Check for buggy apps that are known to not support modifiers, to avoid surprising them
163 * with a UBWC buffer.
164 */
165static bool should_avoid_ubwc(void)
166{
167#ifndef __ANDROID__
168 /* waffle is buggy and, requests a renderable buffer (which on qcom platforms, we
169 * want to use UBWC), and then passes it to the kernel discarding the modifier.
170 * So mesa ends up correctly rendering to as tiled+compressed, but kernel tries
171 * to display as linear. Other platforms do not see this issue, simply because
172 * they only use compressed (ex, AFBC) with the BO_USE_SCANOUT flag.
173 *
174 * See b/163137550
175 */
176 if (dlsym(RTLD_DEFAULT, "waffle_display_connect")) {
177 drv_log("WARNING: waffle detected, disabling UBWC\n");
178 return true;
179 }
180#endif
181 return false;
182}
183
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530184static int msm_init(struct driver *drv)
185{
Tanmay Shah067594b2018-11-26 10:05:18 -0800186 struct format_metadata metadata;
Jeffrey Kardatzkeafb2c562020-03-02 12:25:55 -0800187 uint64_t render_use_flags = BO_USE_RENDER_MASK | BO_USE_SCANOUT;
Tanmay Shah067594b2018-11-26 10:05:18 -0800188 uint64_t texture_use_flags = BO_USE_TEXTURE_MASK | BO_USE_HW_VIDEO_DECODER;
Kristian H. Kristensen2de95ec2020-09-08 03:28:23 +0000189 uint64_t sw_flags = (BO_USE_RENDERSCRIPT | BO_USE_SW_MASK |
Tanmay Shah067594b2018-11-26 10:05:18 -0800190 BO_USE_LINEAR | BO_USE_PROTECTED);
191
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530192 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
Tanmay Shah067594b2018-11-26 10:05:18 -0800193 &LINEAR_METADATA, render_use_flags);
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530194
Tanmay Shah617ee712018-07-11 16:41:05 -0700195 drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
Tanmay Shah067594b2018-11-26 10:05:18 -0800196 &LINEAR_METADATA, texture_use_flags);
Alexandre Courbot1805a9b2018-05-21 19:05:10 +0900197
Ricky Liangf11f1df2020-02-13 10:42:46 +0800198 /* The camera stack standardizes on NV12 for YUV buffers. */
Hirokazu Honda3bd681c2020-06-23 17:52:20 +0900199 /* YVU420 and NV12 formats for camera, display and encoding. */
Ricky Liangf11f1df2020-02-13 10:42:46 +0800200 drv_modify_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA,
Hirokazu Honda3bd681c2020-06-23 17:52:20 +0900201 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT |
202 BO_USE_HW_VIDEO_ENCODER);
203
Ricky Liangf11f1df2020-02-13 10:42:46 +0800204 /*
205 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
David Stevens49518142020-06-15 13:48:48 +0900206 * from camera and input/output from hardware decoder/encoder.
Ricky Liangf11f1df2020-02-13 10:42:46 +0800207 */
Douglas Anderson0c03c9b2020-07-21 08:06:46 -0700208 drv_modify_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA,
David Stevens49518142020-06-15 13:48:48 +0900209 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
210 BO_USE_HW_VIDEO_ENCODER);
Ricky Liangf11f1df2020-02-13 10:42:46 +0800211
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700212 /* Android CTS tests require this. */
213 drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
214
Tanmay Shah067594b2018-11-26 10:05:18 -0800215 drv_modify_linear_combinations(drv);
216
Rob Clarke48e4d72020-08-07 08:08:30 -0700217 if (should_avoid_ubwc())
218 return 0;
219
Tanmay Shah067594b2018-11-26 10:05:18 -0800220 metadata.tiling = MSM_UBWC_TILING;
221 metadata.priority = 2;
222 metadata.modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
223
224 render_use_flags &= ~sw_flags;
225 texture_use_flags &= ~sw_flags;
226
Kristian H. Kristensen8f782d82020-08-26 00:26:24 +0000227 msm_add_ubwc_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
228 &metadata, render_use_flags);
Tanmay Shah067594b2018-11-26 10:05:18 -0800229
Kristian H. Kristensen8f782d82020-08-26 00:26:24 +0000230 msm_add_ubwc_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
231 &metadata, texture_use_flags);
Tanmay Shah067594b2018-11-26 10:05:18 -0800232
233 return 0;
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530234}
235
Tanmay Shah067594b2018-11-26 10:05:18 -0800236static int msm_bo_create_for_modifier(struct bo *bo, uint32_t width, uint32_t height,
237 uint32_t format, const uint64_t modifier)
Stéphane Marchesin23e006a2018-02-28 16:37:46 -0800238{
Tanmay Shah617ee712018-07-11 16:41:05 -0700239 struct drm_msm_gem_new req;
Tanmay Shah617ee712018-07-11 16:41:05 -0700240 int ret;
241 size_t i;
242
Gurchetan Singh298b7572019-09-19 09:55:18 -0700243 bo->meta.tiling = (modifier == DRM_FORMAT_MOD_QCOM_COMPRESSED) ? MSM_UBWC_TILING : 0;
Tanmay Shah067594b2018-11-26 10:05:18 -0800244
245 msm_calculate_layout(bo);
Tanmay Shah617ee712018-07-11 16:41:05 -0700246
247 memset(&req, 0, sizeof(req));
248 req.flags = MSM_BO_WC | MSM_BO_SCANOUT;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700249 req.size = bo->meta.total_size;
Tanmay Shah617ee712018-07-11 16:41:05 -0700250
251 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_NEW, &req);
252 if (ret) {
253 drv_log("DRM_IOCTL_MSM_GEM_NEW failed with %s\n", strerror(errno));
Stéphane Marchesin6ac299f2019-03-21 12:23:29 -0700254 return -errno;
Tanmay Shah617ee712018-07-11 16:41:05 -0700255 }
256
257 /*
258 * Though we use only one plane, we need to set handle for
259 * all planes to pass kernel checks
260 */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700261 for (i = 0; i < bo->meta.num_planes; i++) {
Tanmay Shah617ee712018-07-11 16:41:05 -0700262 bo->handles[i].u32 = req.handle;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700263 bo->meta.format_modifiers[i] = modifier;
Tanmay Shah617ee712018-07-11 16:41:05 -0700264 }
265
266 return 0;
267}
268
Tanmay Shah067594b2018-11-26 10:05:18 -0800269static int msm_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
270 uint32_t format, const uint64_t *modifiers, uint32_t count)
271{
272 static const uint64_t modifier_order[] = {
273 DRM_FORMAT_MOD_QCOM_COMPRESSED,
274 DRM_FORMAT_MOD_LINEAR,
275 };
276
277 uint64_t modifier =
278 drv_pick_modifier(modifiers, count, modifier_order, ARRAY_SIZE(modifier_order));
279
280 return msm_bo_create_for_modifier(bo, width, height, format, modifier);
281}
282
283/* msm_bo_create will create linear buffers for now */
284static int msm_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
285 uint64_t flags)
286{
287 struct combination *combo = drv_get_combination(bo->drv, format, flags);
288
289 if (!combo) {
Stephen Boyd5ff0cfd2019-04-09 11:03:00 -0700290 drv_log("invalid format = %d, flags = %" PRIx64 " combination\n", format, flags);
Tanmay Shah067594b2018-11-26 10:05:18 -0800291 return -EINVAL;
292 }
293
294 return msm_bo_create_for_modifier(bo, width, height, format, combo->metadata.modifier);
295}
296
Tanmay Shah617ee712018-07-11 16:41:05 -0700297static void *msm_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
298{
299 int ret;
300 struct drm_msm_gem_info req;
301
302 memset(&req, 0, sizeof(req));
303 req.handle = bo->handles[0].u32;
304
305 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_INFO, &req);
306 if (ret) {
307 drv_log("DRM_IOCLT_MSM_GEM_INFO failed with %s\n", strerror(errno));
308 return MAP_FAILED;
309 }
Gurchetan Singh298b7572019-09-19 09:55:18 -0700310 vma->length = bo->meta.total_size;
Tanmay Shah617ee712018-07-11 16:41:05 -0700311
Gurchetan Singh298b7572019-09-19 09:55:18 -0700312 return mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
Tanmay Shah617ee712018-07-11 16:41:05 -0700313 req.offset);
Stéphane Marchesin23e006a2018-02-28 16:37:46 -0800314}
315
Jeffrey Kardatzkee3cd0d32020-02-25 10:33:37 -0800316static uint32_t msm_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
317{
318 switch (format) {
Douglas Anderson64b80ce2020-05-19 10:12:14 -0700319 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
320 /* Camera subsystem requires NV12. */
321 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
322 return DRM_FORMAT_NV12;
323 /*HACK: See b/28671744 */
324 return DRM_FORMAT_XBGR8888;
Jeffrey Kardatzkee3cd0d32020-02-25 10:33:37 -0800325 case DRM_FORMAT_FLEX_YCbCr_420_888:
326 return DRM_FORMAT_NV12;
327 default:
328 return format;
329 }
330}
331
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530332const struct backend backend_msm = {
333 .name = "msm",
334 .init = msm_init,
Stéphane Marchesin23e006a2018-02-28 16:37:46 -0800335 .bo_create = msm_bo_create,
Tanmay Shah067594b2018-11-26 10:05:18 -0800336 .bo_create_with_modifiers = msm_bo_create_with_modifiers,
Tanmay Shah617ee712018-07-11 16:41:05 -0700337 .bo_destroy = drv_gem_bo_destroy,
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530338 .bo_import = drv_prime_bo_import,
Tanmay Shah617ee712018-07-11 16:41:05 -0700339 .bo_map = msm_bo_map,
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530340 .bo_unmap = drv_bo_munmap,
Jeffrey Kardatzkee3cd0d32020-02-25 10:33:37 -0800341 .resolve_format = msm_resolve_format,
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530342};
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530343#endif /* DRV_MSM */