blob: dbc5b7032a679e81bc6506d7146dbce760456ee3 [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>
10#include <drm_fourcc.h>
Tanmay Shah617ee712018-07-11 16:41:05 -070011#include <errno.h>
Stephen Boyd5ff0cfd2019-04-09 11:03:00 -070012#include <inttypes.h>
Tanmay Shah617ee712018-07-11 16:41:05 -070013#include <msm_drm.h>
Tanmay Shah067594b2018-11-26 10:05:18 -080014#include <stdbool.h>
Tanmay Shah617ee712018-07-11 16:41:05 -070015#include <stdio.h>
16#include <string.h>
17#include <sys/mman.h>
18#include <xf86drm.h>
19
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053020#include "drv_priv.h"
21#include "helpers.h"
22#include "util.h"
23
Tanmay Shah067594b2018-11-26 10:05:18 -080024/* Alignment values are based on SDM845 Gfx IP */
Tanmay Shah617ee712018-07-11 16:41:05 -070025#define DEFAULT_ALIGNMENT 64
Tanmay Shahc65bd8c2018-11-21 09:14:14 -080026#define BUFFER_SIZE_ALIGN 4096
27
28#define VENUS_STRIDE_ALIGN 128
29#define VENUS_SCANLINE_ALIGN 16
30#define NV12_LINEAR_PADDING (12 * 1024)
Tanmay Shah067594b2018-11-26 10:05:18 -080031#define NV12_UBWC_PADDING(y_stride) (MAX(16 * 1024, y_stride * 48))
32#define MACROTILE_WIDTH_ALIGN 64
33#define MACROTILE_HEIGHT_ALIGN 16
34#define PLANE_SIZE_ALIGN 4096
35
36#define MSM_UBWC_TILING 1
Stéphane Marchesin23e006a2018-02-28 16:37:46 -080037
Gurchetan Singhb131c9d2018-08-28 14:17:05 -070038static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
Gurchetan Singh71bc6652018-09-17 17:42:05 -070039 DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
40 DRM_FORMAT_XRGB8888 };
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053041
Tanmay Shah617ee712018-07-11 16:41:05 -070042static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_R8,
43 DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID };
Alexandre Courbot1805a9b2018-05-21 19:05:10 +090044
Tanmay Shah067594b2018-11-26 10:05:18 -080045/*
46 * Each macrotile consists of m x n (mostly 4 x 4) tiles.
47 * Pixel data pitch/stride is aligned with macrotile width.
48 * Pixel data height is aligned with macrotile height.
49 * Entire pixel data buffer is aligned with 4k(bytes).
50 */
51static uint32_t get_ubwc_meta_size(uint32_t width, uint32_t height, uint32_t tile_width,
52 uint32_t tile_height)
53{
54 uint32_t macrotile_width, macrotile_height;
55
56 macrotile_width = DIV_ROUND_UP(width, tile_width);
57 macrotile_height = DIV_ROUND_UP(height, tile_height);
58
59 // Align meta buffer width to 64 blocks
60 macrotile_width = ALIGN(macrotile_width, MACROTILE_WIDTH_ALIGN);
61
62 // Align meta buffer height to 16 blocks
63 macrotile_height = ALIGN(macrotile_height, MACROTILE_HEIGHT_ALIGN);
64
65 return ALIGN(macrotile_width * macrotile_height, PLANE_SIZE_ALIGN);
66}
67
68static void msm_calculate_layout(struct bo *bo)
Tanmay Shahc65bd8c2018-11-21 09:14:14 -080069{
70 uint32_t width, height;
71
Gurchetan Singh298b7572019-09-19 09:55:18 -070072 width = bo->meta.width;
73 height = bo->meta.height;
Tanmay Shahc65bd8c2018-11-21 09:14:14 -080074
75 /* NV12 format requires extra padding with platform
76 * specific alignments for venus driver
77 */
Gurchetan Singh298b7572019-09-19 09:55:18 -070078 if (bo->meta.format == DRM_FORMAT_NV12) {
Tanmay Shah067594b2018-11-26 10:05:18 -080079 uint32_t y_stride, uv_stride, y_scanline, uv_scanline, y_plane, uv_plane, size,
80 extra_padding;
81
Tanmay Shahc65bd8c2018-11-21 09:14:14 -080082 y_stride = ALIGN(width, VENUS_STRIDE_ALIGN);
83 uv_stride = ALIGN(width, VENUS_STRIDE_ALIGN);
84 y_scanline = ALIGN(height, VENUS_SCANLINE_ALIGN * 2);
85 uv_scanline = ALIGN(DIV_ROUND_UP(height, 2), VENUS_SCANLINE_ALIGN);
86 y_plane = y_stride * y_scanline;
87 uv_plane = uv_stride * uv_scanline;
88
Gurchetan Singh298b7572019-09-19 09:55:18 -070089 if (bo->meta.tiling == MSM_UBWC_TILING) {
Tanmay Shah067594b2018-11-26 10:05:18 -080090 y_plane += get_ubwc_meta_size(width, height, 32, 8);
91 uv_plane += get_ubwc_meta_size(width >> 1, height >> 1, 16, 8);
92 extra_padding = NV12_UBWC_PADDING(y_stride);
93 } else {
94 extra_padding = NV12_LINEAR_PADDING;
95 }
96
Gurchetan Singh298b7572019-09-19 09:55:18 -070097 bo->meta.strides[0] = y_stride;
98 bo->meta.sizes[0] = y_plane;
99 bo->meta.offsets[1] = y_plane;
100 bo->meta.strides[1] = uv_stride;
Tanmay Shah067594b2018-11-26 10:05:18 -0800101 size = y_plane + uv_plane + extra_padding;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700102 bo->meta.total_size = ALIGN(size, BUFFER_SIZE_ALIGN);
103 bo->meta.sizes[1] = bo->meta.total_size - bo->meta.sizes[0];
Tanmay Shahc65bd8c2018-11-21 09:14:14 -0800104 } else {
105 uint32_t stride, alignw, alignh;
106
107 alignw = ALIGN(width, DEFAULT_ALIGNMENT);
Tanmay Shahc65bd8c2018-11-21 09:14:14 -0800108 /* HAL_PIXEL_FORMAT_YV12 requires that the buffer's height not be aligned. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700109 if (bo->meta.format == DRM_FORMAT_YVU420_ANDROID) {
Tanmay Shahc65bd8c2018-11-21 09:14:14 -0800110 alignh = height;
111 } else {
112 alignh = ALIGN(height, DEFAULT_ALIGNMENT);
113 }
114
Gurchetan Singh298b7572019-09-19 09:55:18 -0700115 stride = drv_stride_from_format(bo->meta.format, alignw, 0);
Tanmay Shahc65bd8c2018-11-21 09:14:14 -0800116
117 /* Calculate size and assign stride, size, offset to each plane based on format */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700118 drv_bo_from_format(bo, stride, alignh, bo->meta.format);
Tanmay Shah067594b2018-11-26 10:05:18 -0800119
120 /* For all RGB UBWC formats */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700121 if (bo->meta.tiling == MSM_UBWC_TILING) {
122 bo->meta.sizes[0] += get_ubwc_meta_size(width, height, 16, 4);
123 bo->meta.total_size = bo->meta.sizes[0];
124 assert(IS_ALIGNED(bo->meta.total_size, BUFFER_SIZE_ALIGN));
Tanmay Shah067594b2018-11-26 10:05:18 -0800125 }
126 }
127}
128
129static bool is_ubwc_fmt(uint32_t format)
130{
131 switch (format) {
132 case DRM_FORMAT_XBGR8888:
133 case DRM_FORMAT_ABGR8888:
Fritz Koenige5c3fdf2019-12-10 16:30:34 -0800134 case DRM_FORMAT_XRGB8888:
135 case DRM_FORMAT_ARGB8888:
Tanmay Shah067594b2018-11-26 10:05:18 -0800136 case DRM_FORMAT_NV12:
137 return 1;
138 default:
139 return 0;
140 }
141}
142
143static void msm_add_ubwc_combinations(struct driver *drv, const uint32_t *formats,
144 uint32_t num_formats, struct format_metadata *metadata,
145 uint64_t use_flags)
146{
147 for (uint32_t i = 0; i < num_formats; i++) {
148 if (is_ubwc_fmt(formats[i])) {
149 struct combination combo = { .format = formats[i],
150 .metadata = *metadata,
151 .use_flags = use_flags };
152 drv_array_append(drv->combos, &combo);
153 }
Tanmay Shahc65bd8c2018-11-21 09:14:14 -0800154 }
155}
156
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530157static int msm_init(struct driver *drv)
158{
Tanmay Shah067594b2018-11-26 10:05:18 -0800159 struct format_metadata metadata;
160 uint64_t render_use_flags = BO_USE_RENDER_MASK;
161 uint64_t texture_use_flags = BO_USE_TEXTURE_MASK | BO_USE_HW_VIDEO_DECODER;
162 uint64_t sw_flags = (BO_USE_RENDERSCRIPT | BO_USE_SW_WRITE_OFTEN | BO_USE_SW_READ_OFTEN |
163 BO_USE_LINEAR | BO_USE_PROTECTED);
164
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530165 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
Tanmay Shah067594b2018-11-26 10:05:18 -0800166 &LINEAR_METADATA, render_use_flags);
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530167
Tanmay Shah617ee712018-07-11 16:41:05 -0700168 drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
Tanmay Shah067594b2018-11-26 10:05:18 -0800169 &LINEAR_METADATA, texture_use_flags);
Alexandre Courbot1805a9b2018-05-21 19:05:10 +0900170
Hirokazu Honda3b8d4d02019-07-31 16:35:52 +0900171 /*
172 * Chrome uses DMA-buf mmap to write to YV12 buffers, which are then accessed by the
173 * Video Encoder Accelerator (VEA). It could also support NV12 potentially in the future.
174 */
175 drv_modify_combination(drv, DRM_FORMAT_YVU420, &LINEAR_METADATA, BO_USE_HW_VIDEO_ENCODER);
176 drv_modify_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA, BO_USE_HW_VIDEO_ENCODER);
177
Gurchetan Singh71bc6652018-09-17 17:42:05 -0700178 /* Android CTS tests require this. */
179 drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
180
Tanmay Shah067594b2018-11-26 10:05:18 -0800181 drv_modify_linear_combinations(drv);
182
183 metadata.tiling = MSM_UBWC_TILING;
184 metadata.priority = 2;
185 metadata.modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
186
187 render_use_flags &= ~sw_flags;
188 texture_use_flags &= ~sw_flags;
189
190 msm_add_ubwc_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
Fritz Koenige5c3fdf2019-12-10 16:30:34 -0800191 &metadata, render_use_flags | BO_USE_SCANOUT);
Tanmay Shah067594b2018-11-26 10:05:18 -0800192
193 msm_add_ubwc_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
Fritz Koenige5c3fdf2019-12-10 16:30:34 -0800194 &metadata, texture_use_flags | BO_USE_SCANOUT);
Tanmay Shah067594b2018-11-26 10:05:18 -0800195
196 return 0;
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530197}
198
Tanmay Shah067594b2018-11-26 10:05:18 -0800199static int msm_bo_create_for_modifier(struct bo *bo, uint32_t width, uint32_t height,
200 uint32_t format, const uint64_t modifier)
Stéphane Marchesin23e006a2018-02-28 16:37:46 -0800201{
Tanmay Shah617ee712018-07-11 16:41:05 -0700202 struct drm_msm_gem_new req;
Tanmay Shah617ee712018-07-11 16:41:05 -0700203 int ret;
204 size_t i;
205
Gurchetan Singh298b7572019-09-19 09:55:18 -0700206 bo->meta.tiling = (modifier == DRM_FORMAT_MOD_QCOM_COMPRESSED) ? MSM_UBWC_TILING : 0;
Tanmay Shah067594b2018-11-26 10:05:18 -0800207
208 msm_calculate_layout(bo);
Tanmay Shah617ee712018-07-11 16:41:05 -0700209
210 memset(&req, 0, sizeof(req));
211 req.flags = MSM_BO_WC | MSM_BO_SCANOUT;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700212 req.size = bo->meta.total_size;
Tanmay Shah617ee712018-07-11 16:41:05 -0700213
214 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_NEW, &req);
215 if (ret) {
216 drv_log("DRM_IOCTL_MSM_GEM_NEW failed with %s\n", strerror(errno));
Stéphane Marchesin6ac299f2019-03-21 12:23:29 -0700217 return -errno;
Tanmay Shah617ee712018-07-11 16:41:05 -0700218 }
219
220 /*
221 * Though we use only one plane, we need to set handle for
222 * all planes to pass kernel checks
223 */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700224 for (i = 0; i < bo->meta.num_planes; i++) {
Tanmay Shah617ee712018-07-11 16:41:05 -0700225 bo->handles[i].u32 = req.handle;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700226 bo->meta.format_modifiers[i] = modifier;
Tanmay Shah617ee712018-07-11 16:41:05 -0700227 }
228
229 return 0;
230}
231
Tanmay Shah067594b2018-11-26 10:05:18 -0800232static int msm_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
233 uint32_t format, const uint64_t *modifiers, uint32_t count)
234{
235 static const uint64_t modifier_order[] = {
236 DRM_FORMAT_MOD_QCOM_COMPRESSED,
237 DRM_FORMAT_MOD_LINEAR,
238 };
239
240 uint64_t modifier =
241 drv_pick_modifier(modifiers, count, modifier_order, ARRAY_SIZE(modifier_order));
242
243 return msm_bo_create_for_modifier(bo, width, height, format, modifier);
244}
245
246/* msm_bo_create will create linear buffers for now */
247static int msm_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
248 uint64_t flags)
249{
250 struct combination *combo = drv_get_combination(bo->drv, format, flags);
251
252 if (!combo) {
Stephen Boyd5ff0cfd2019-04-09 11:03:00 -0700253 drv_log("invalid format = %d, flags = %" PRIx64 " combination\n", format, flags);
Tanmay Shah067594b2018-11-26 10:05:18 -0800254 return -EINVAL;
255 }
256
257 return msm_bo_create_for_modifier(bo, width, height, format, combo->metadata.modifier);
258}
259
Tanmay Shah617ee712018-07-11 16:41:05 -0700260static void *msm_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
261{
262 int ret;
263 struct drm_msm_gem_info req;
264
265 memset(&req, 0, sizeof(req));
266 req.handle = bo->handles[0].u32;
267
268 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_INFO, &req);
269 if (ret) {
270 drv_log("DRM_IOCLT_MSM_GEM_INFO failed with %s\n", strerror(errno));
271 return MAP_FAILED;
272 }
Gurchetan Singh298b7572019-09-19 09:55:18 -0700273 vma->length = bo->meta.total_size;
Tanmay Shah617ee712018-07-11 16:41:05 -0700274
Gurchetan Singh298b7572019-09-19 09:55:18 -0700275 return mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
Tanmay Shah617ee712018-07-11 16:41:05 -0700276 req.offset);
Stéphane Marchesin23e006a2018-02-28 16:37:46 -0800277}
278
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530279const struct backend backend_msm = {
280 .name = "msm",
281 .init = msm_init,
Stéphane Marchesin23e006a2018-02-28 16:37:46 -0800282 .bo_create = msm_bo_create,
Tanmay Shah067594b2018-11-26 10:05:18 -0800283 .bo_create_with_modifiers = msm_bo_create_with_modifiers,
Tanmay Shah617ee712018-07-11 16:41:05 -0700284 .bo_destroy = drv_gem_bo_destroy,
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530285 .bo_import = drv_prime_bo_import,
Tanmay Shah617ee712018-07-11 16:41:05 -0700286 .bo_map = msm_bo_map,
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530287 .bo_unmap = drv_bo_munmap,
288};
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530289#endif /* DRV_MSM */