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.
11 #include <drm_fourcc.h>
25 /* Alignment values are based on SDM845 Gfx IP */
26 #define DEFAULT_ALIGNMENT 64
27 #define BUFFER_SIZE_ALIGN 4096
29 #define VENUS_STRIDE_ALIGN 128
30 #define VENUS_SCANLINE_ALIGN 16
31 #define NV12_LINEAR_PADDING (12 * 1024)
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
37 #define MSM_UBWC_TILING 1
39 static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
40 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
41 DRM_FORMAT_XRGB8888 };
43 static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_R8,
44 DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID };
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).
52 static uint32_t get_ubwc_meta_size(uint32_t width, uint32_t height, uint32_t tile_width,
55 uint32_t macrotile_width, macrotile_height;
57 macrotile_width = DIV_ROUND_UP(width, tile_width);
58 macrotile_height = DIV_ROUND_UP(height, tile_height);
60 // Align meta buffer width to 64 blocks
61 macrotile_width = ALIGN(macrotile_width, MACROTILE_WIDTH_ALIGN);
63 // Align meta buffer height to 16 blocks
64 macrotile_height = ALIGN(macrotile_height, MACROTILE_HEIGHT_ALIGN);
66 return ALIGN(macrotile_width * macrotile_height, PLANE_SIZE_ALIGN);
69 static void msm_calculate_layout(struct bo *bo)
71 uint32_t width, height;
73 width = bo->meta.width;
74 height = bo->meta.height;
76 /* NV12 format requires extra padding with platform
77 * specific alignments for venus driver
79 if (bo->meta.format == DRM_FORMAT_NV12) {
80 uint32_t y_stride, uv_stride, y_scanline, uv_scanline, y_plane, uv_plane, size,
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);
86 uv_scanline = ALIGN(DIV_ROUND_UP(height, 2),
87 VENUS_SCANLINE_ALIGN * (bo->meta.tiling ? 2 : 1));
88 y_plane = y_stride * y_scanline;
89 uv_plane = uv_stride * uv_scanline;
91 if (bo->meta.tiling == MSM_UBWC_TILING) {
92 y_plane = ALIGN(y_plane, PLANE_SIZE_ALIGN);
93 uv_plane = ALIGN(uv_plane, PLANE_SIZE_ALIGN);
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);
98 extra_padding = NV12_LINEAR_PADDING;
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;
105 size = y_plane + uv_plane + extra_padding;
106 bo->meta.total_size = ALIGN(size, BUFFER_SIZE_ALIGN);
107 bo->meta.sizes[1] = bo->meta.total_size - bo->meta.sizes[0];
109 uint32_t stride, alignw, alignh;
111 alignw = ALIGN(width, DEFAULT_ALIGNMENT);
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 ||
116 (bo->meta.format == DRM_FORMAT_R8 && height == 1)) {
119 alignh = ALIGN(height, DEFAULT_ALIGNMENT);
122 stride = drv_stride_from_format(bo->meta.format, alignw, 0);
124 /* Calculate size and assign stride, size, offset to each plane based on format */
125 drv_bo_from_format(bo, stride, alignh, bo->meta.format);
127 /* For all RGB UBWC formats */
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));
136 static bool is_ubwc_fmt(uint32_t format)
139 case DRM_FORMAT_XBGR8888:
140 case DRM_FORMAT_ABGR8888:
141 case DRM_FORMAT_XRGB8888:
142 case DRM_FORMAT_ARGB8888:
143 case DRM_FORMAT_NV12:
150 static void msm_add_ubwc_combinations(struct driver *drv, const uint32_t *formats,
151 uint32_t num_formats, struct format_metadata *metadata,
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);
165 * Check for buggy apps that are known to not support modifiers, to avoid surprising them
166 * with a UBWC buffer.
168 static bool should_avoid_ubwc(void)
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.
179 if (dlsym(RTLD_DEFAULT, "waffle_display_connect")) {
180 drv_log("WARNING: waffle detected, disabling UBWC\n");
184 /* The video_decode_accelerator_tests needs to read back the frames
185 * to verify they are correct. The frame verification relies on
186 * computing the MD5 of the video frame. UBWC results in a different
187 * MD5. This turns off UBWC for gtest until a proper frame
188 * comparison can be made
189 * Rely on the same mechanism that waffle is using, but this time check
190 * for a dynamic library function that is present in chrome, but missing
191 * in gtest. Cups is not loaded for video tests.
195 if (!dlsym(RTLD_DEFAULT, "cupsFilePrintf")) {
196 drv_log("WARNING: gtest detected, disabling UBWC\n");
203 static int msm_init(struct driver *drv)
205 struct format_metadata metadata;
206 uint64_t render_use_flags = BO_USE_RENDER_MASK | BO_USE_SCANOUT;
207 uint64_t texture_use_flags = BO_USE_TEXTURE_MASK | BO_USE_HW_VIDEO_DECODER;
208 uint64_t sw_flags = (BO_USE_RENDERSCRIPT | BO_USE_SW_MASK | BO_USE_LINEAR);
210 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
211 &LINEAR_METADATA, render_use_flags);
213 drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
214 &LINEAR_METADATA, texture_use_flags);
216 /* The camera stack standardizes on NV12 for YUV buffers. */
217 /* YVU420 and NV12 formats for camera, display and encoding. */
218 drv_modify_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA,
219 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT |
220 BO_USE_HW_VIDEO_ENCODER);
223 * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
224 * from camera and input/output from hardware decoder/encoder.
226 drv_modify_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA,
227 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
228 BO_USE_HW_VIDEO_ENCODER);
230 /* Android CTS tests require this. */
231 drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
233 drv_modify_linear_combinations(drv);
235 if (should_avoid_ubwc())
238 metadata.tiling = MSM_UBWC_TILING;
239 metadata.priority = 2;
240 metadata.modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
242 render_use_flags &= ~sw_flags;
243 texture_use_flags &= ~sw_flags;
245 msm_add_ubwc_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
246 &metadata, render_use_flags);
248 msm_add_ubwc_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
249 &metadata, texture_use_flags);
251 drv_modify_combination(drv, DRM_FORMAT_NV12, &metadata,
252 BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT |
253 BO_USE_HW_VIDEO_ENCODER);
258 static int msm_bo_create_for_modifier(struct bo *bo, uint32_t width, uint32_t height,
259 uint32_t format, const uint64_t modifier)
261 struct drm_msm_gem_new req = { 0 };
265 bo->meta.tiling = (modifier == DRM_FORMAT_MOD_QCOM_COMPRESSED) ? MSM_UBWC_TILING : 0;
266 msm_calculate_layout(bo);
268 req.flags = MSM_BO_WC | MSM_BO_SCANOUT;
269 req.size = bo->meta.total_size;
271 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_NEW, &req);
273 drv_log("DRM_IOCTL_MSM_GEM_NEW failed with %s\n", strerror(errno));
278 * Though we use only one plane, we need to set handle for
279 * all planes to pass kernel checks
281 for (i = 0; i < bo->meta.num_planes; i++) {
282 bo->handles[i].u32 = req.handle;
283 bo->meta.format_modifiers[i] = modifier;
289 static int msm_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
290 uint32_t format, const uint64_t *modifiers, uint32_t count)
292 static const uint64_t modifier_order[] = {
293 DRM_FORMAT_MOD_QCOM_COMPRESSED,
294 DRM_FORMAT_MOD_LINEAR,
298 drv_pick_modifier(modifiers, count, modifier_order, ARRAY_SIZE(modifier_order));
300 return msm_bo_create_for_modifier(bo, width, height, format, modifier);
303 /* msm_bo_create will create linear buffers for now */
304 static int msm_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
307 struct combination *combo = drv_get_combination(bo->drv, format, flags);
310 drv_log("invalid format = %d, flags = %" PRIx64 " combination\n", format, flags);
314 return msm_bo_create_for_modifier(bo, width, height, format, combo->metadata.modifier);
317 static void *msm_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
320 struct drm_msm_gem_info req = { 0 };
322 req.handle = bo->handles[0].u32;
323 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_INFO, &req);
325 drv_log("DRM_IOCLT_MSM_GEM_INFO failed with %s\n", strerror(errno));
328 vma->length = bo->meta.total_size;
330 return mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
334 static uint32_t msm_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
337 case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
338 /* Camera subsystem requires NV12. */
339 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
340 return DRM_FORMAT_NV12;
341 /*HACK: See b/28671744 */
342 return DRM_FORMAT_XBGR8888;
343 case DRM_FORMAT_FLEX_YCbCr_420_888:
344 return DRM_FORMAT_NV12;
350 const struct backend backend_msm = {
353 .bo_create = msm_bo_create,
354 .bo_create_with_modifiers = msm_bo_create_with_modifiers,
355 .bo_destroy = drv_gem_bo_destroy,
356 .bo_import = drv_prime_bo_import,
357 .bo_map = msm_bo_map,
358 .bo_unmap = drv_bo_munmap,
359 .resolve_format = msm_resolve_format,