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.
10 #include <drm_fourcc.h>
23 /* Alignment values are based on SDM845 Gfx IP */
24 #define DEFAULT_ALIGNMENT 64
25 #define BUFFER_SIZE_ALIGN 4096
27 #define VENUS_STRIDE_ALIGN 128
28 #define VENUS_SCANLINE_ALIGN 16
29 #define NV12_LINEAR_PADDING (12 * 1024)
30 #define NV12_UBWC_PADDING(y_stride) (MAX(16 * 1024, y_stride * 48))
31 #define MACROTILE_WIDTH_ALIGN 64
32 #define MACROTILE_HEIGHT_ALIGN 16
33 #define PLANE_SIZE_ALIGN 4096
35 #define MSM_UBWC_TILING 1
37 static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
38 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
39 DRM_FORMAT_XRGB8888 };
41 static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_R8,
42 DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID };
45 * Each macrotile consists of m x n (mostly 4 x 4) tiles.
46 * Pixel data pitch/stride is aligned with macrotile width.
47 * Pixel data height is aligned with macrotile height.
48 * Entire pixel data buffer is aligned with 4k(bytes).
50 static uint32_t get_ubwc_meta_size(uint32_t width, uint32_t height, uint32_t tile_width,
53 uint32_t macrotile_width, macrotile_height;
55 macrotile_width = DIV_ROUND_UP(width, tile_width);
56 macrotile_height = DIV_ROUND_UP(height, tile_height);
58 // Align meta buffer width to 64 blocks
59 macrotile_width = ALIGN(macrotile_width, MACROTILE_WIDTH_ALIGN);
61 // Align meta buffer height to 16 blocks
62 macrotile_height = ALIGN(macrotile_height, MACROTILE_HEIGHT_ALIGN);
64 return ALIGN(macrotile_width * macrotile_height, PLANE_SIZE_ALIGN);
67 static void msm_calculate_layout(struct bo *bo)
69 uint32_t width, height;
74 /* NV12 format requires extra padding with platform
75 * specific alignments for venus driver
77 if (bo->format == DRM_FORMAT_NV12) {
78 uint32_t y_stride, uv_stride, y_scanline, uv_scanline, y_plane, uv_plane, size,
81 y_stride = ALIGN(width, VENUS_STRIDE_ALIGN);
82 uv_stride = ALIGN(width, VENUS_STRIDE_ALIGN);
83 y_scanline = ALIGN(height, VENUS_SCANLINE_ALIGN * 2);
84 uv_scanline = ALIGN(DIV_ROUND_UP(height, 2), VENUS_SCANLINE_ALIGN);
85 y_plane = y_stride * y_scanline;
86 uv_plane = uv_stride * uv_scanline;
88 if (bo->tiling == MSM_UBWC_TILING) {
89 y_plane += get_ubwc_meta_size(width, height, 32, 8);
90 uv_plane += get_ubwc_meta_size(width >> 1, height >> 1, 16, 8);
91 extra_padding = NV12_UBWC_PADDING(y_stride);
93 extra_padding = NV12_LINEAR_PADDING;
96 bo->strides[0] = y_stride;
97 bo->sizes[0] = y_plane;
98 bo->offsets[1] = y_plane;
99 bo->strides[1] = uv_stride;
100 size = y_plane + uv_plane + extra_padding;
101 bo->total_size = ALIGN(size, BUFFER_SIZE_ALIGN);
102 bo->sizes[1] = bo->total_size - bo->sizes[0];
104 uint32_t stride, alignw, alignh;
106 alignw = ALIGN(width, DEFAULT_ALIGNMENT);
107 /* HAL_PIXEL_FORMAT_YV12 requires that the buffer's height not be aligned. */
108 if (bo->format == DRM_FORMAT_YVU420_ANDROID) {
111 alignh = ALIGN(height, DEFAULT_ALIGNMENT);
114 stride = drv_stride_from_format(bo->format, alignw, 0);
116 /* Calculate size and assign stride, size, offset to each plane based on format */
117 drv_bo_from_format(bo, stride, alignh, bo->format);
119 /* For all RGB UBWC formats */
120 if (bo->tiling == MSM_UBWC_TILING) {
121 bo->sizes[0] += get_ubwc_meta_size(width, height, 16, 4);
122 bo->total_size = bo->sizes[0];
123 assert(IS_ALIGNED(bo->total_size, BUFFER_SIZE_ALIGN));
128 static bool is_ubwc_fmt(uint32_t format)
131 case DRM_FORMAT_XBGR8888:
132 case DRM_FORMAT_ABGR8888:
133 case DRM_FORMAT_NV12:
140 static void msm_add_ubwc_combinations(struct driver *drv, const uint32_t *formats,
141 uint32_t num_formats, struct format_metadata *metadata,
144 for (uint32_t i = 0; i < num_formats; i++) {
145 if (is_ubwc_fmt(formats[i])) {
146 struct combination combo = { .format = formats[i],
147 .metadata = *metadata,
148 .use_flags = use_flags };
149 drv_array_append(drv->combos, &combo);
154 static int msm_init(struct driver *drv)
156 struct format_metadata metadata;
157 uint64_t render_use_flags = BO_USE_RENDER_MASK;
158 uint64_t texture_use_flags = BO_USE_TEXTURE_MASK | BO_USE_HW_VIDEO_DECODER;
159 uint64_t sw_flags = (BO_USE_RENDERSCRIPT | BO_USE_SW_WRITE_OFTEN | BO_USE_SW_READ_OFTEN |
160 BO_USE_LINEAR | BO_USE_PROTECTED);
162 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
163 &LINEAR_METADATA, render_use_flags);
165 drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
166 &LINEAR_METADATA, texture_use_flags);
168 /* Android CTS tests require this. */
169 drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
171 drv_modify_linear_combinations(drv);
173 metadata.tiling = MSM_UBWC_TILING;
174 metadata.priority = 2;
175 metadata.modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
177 render_use_flags &= ~sw_flags;
178 texture_use_flags &= ~sw_flags;
180 msm_add_ubwc_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
181 &metadata, render_use_flags);
183 msm_add_ubwc_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
184 &metadata, texture_use_flags);
189 static int msm_bo_create_for_modifier(struct bo *bo, uint32_t width, uint32_t height,
190 uint32_t format, const uint64_t modifier)
192 struct drm_msm_gem_new req;
196 bo->tiling = (modifier == DRM_FORMAT_MOD_QCOM_COMPRESSED) ? MSM_UBWC_TILING : 0;
198 msm_calculate_layout(bo);
200 memset(&req, 0, sizeof(req));
201 req.flags = MSM_BO_WC | MSM_BO_SCANOUT;
202 req.size = bo->total_size;
204 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_NEW, &req);
206 drv_log("DRM_IOCTL_MSM_GEM_NEW failed with %s\n", strerror(errno));
211 * Though we use only one plane, we need to set handle for
212 * all planes to pass kernel checks
214 for (i = 0; i < bo->num_planes; i++) {
215 bo->handles[i].u32 = req.handle;
216 bo->format_modifiers[i] = modifier;
222 static int msm_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
223 uint32_t format, const uint64_t *modifiers, uint32_t count)
225 static const uint64_t modifier_order[] = {
226 DRM_FORMAT_MOD_QCOM_COMPRESSED,
227 DRM_FORMAT_MOD_LINEAR,
231 drv_pick_modifier(modifiers, count, modifier_order, ARRAY_SIZE(modifier_order));
233 return msm_bo_create_for_modifier(bo, width, height, format, modifier);
236 /* msm_bo_create will create linear buffers for now */
237 static int msm_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
240 struct combination *combo = drv_get_combination(bo->drv, format, flags);
243 drv_log("invalid format = %d, flags = %llx combination\n", format, flags);
247 return msm_bo_create_for_modifier(bo, width, height, format, combo->metadata.modifier);
250 static void *msm_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
253 struct drm_msm_gem_info req;
255 memset(&req, 0, sizeof(req));
256 req.handle = bo->handles[0].u32;
258 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_INFO, &req);
260 drv_log("DRM_IOCLT_MSM_GEM_INFO failed with %s\n", strerror(errno));
263 vma->length = bo->total_size;
265 return mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
269 const struct backend backend_msm = {
272 .bo_create = msm_bo_create,
273 .bo_create_with_modifiers = msm_bo_create_with_modifiers,
274 .bo_destroy = drv_gem_bo_destroy,
275 .bo_import = drv_prime_bo_import,
276 .bo_map = msm_bo_map,
277 .bo_unmap = drv_bo_munmap,