OSDN Git Service

cros_gralloc: Fix logging statement format specifier
[android-x86/external-minigbm.git] / msm.c
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
9 #include <assert.h>
10 #include <drm_fourcc.h>
11 #include <errno.h>
12 #include <inttypes.h>
13 #include <msm_drm.h>
14 #include <stdbool.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <sys/mman.h>
18 #include <xf86drm.h>
19
20 #include "drv_priv.h"
21 #include "helpers.h"
22 #include "util.h"
23
24 /* Alignment values are based on SDM845 Gfx IP */
25 #define DEFAULT_ALIGNMENT 64
26 #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)
31 #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
37
38 static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
39                                                   DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
40                                                   DRM_FORMAT_XRGB8888 };
41
42 static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_R8,
43                                                    DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID };
44
45 /*
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  */
51 static 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
68 static void msm_calculate_layout(struct bo *bo)
69 {
70         uint32_t width, height;
71
72         width = bo->meta.width;
73         height = bo->meta.height;
74
75         /* NV12 format requires extra padding with platform
76          * specific alignments for venus driver
77          */
78         if (bo->meta.format == DRM_FORMAT_NV12) {
79                 uint32_t y_stride, uv_stride, y_scanline, uv_scanline, y_plane, uv_plane, size,
80                     extra_padding;
81
82                 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
89                 if (bo->meta.tiling == MSM_UBWC_TILING) {
90                         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
97                 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;
101                 size = y_plane + uv_plane + extra_padding;
102                 bo->meta.total_size = ALIGN(size, BUFFER_SIZE_ALIGN);
103                 bo->meta.sizes[1] = bo->meta.total_size - bo->meta.sizes[0];
104         } else {
105                 uint32_t stride, alignw, alignh;
106
107                 alignw = ALIGN(width, DEFAULT_ALIGNMENT);
108                 /* HAL_PIXEL_FORMAT_YV12 requires that the buffer's height not be aligned.
109                         DRM_FORMAT_R8 of height one is used for JPEG camera output, so don't
110                         height align that. */
111                 if (bo->meta.format == DRM_FORMAT_YVU420_ANDROID ||
112                     (bo->meta.format == DRM_FORMAT_R8 && height == 1)) {
113                         alignh = height;
114                 } else {
115                         alignh = ALIGN(height, DEFAULT_ALIGNMENT);
116                 }
117
118                 stride = drv_stride_from_format(bo->meta.format, alignw, 0);
119
120                 /* Calculate size and assign stride, size, offset to each plane based on format */
121                 drv_bo_from_format(bo, stride, alignh, bo->meta.format);
122
123                 /* For all RGB UBWC formats */
124                 if (bo->meta.tiling == MSM_UBWC_TILING) {
125                         bo->meta.sizes[0] += get_ubwc_meta_size(width, height, 16, 4);
126                         bo->meta.total_size = bo->meta.sizes[0];
127                         assert(IS_ALIGNED(bo->meta.total_size, BUFFER_SIZE_ALIGN));
128                 }
129         }
130 }
131
132 static bool is_ubwc_fmt(uint32_t format)
133 {
134         switch (format) {
135         case DRM_FORMAT_XBGR8888:
136         case DRM_FORMAT_ABGR8888:
137         case DRM_FORMAT_XRGB8888:
138         case DRM_FORMAT_ARGB8888:
139         case DRM_FORMAT_NV12:
140                 return 1;
141         default:
142                 return 0;
143         }
144 }
145
146 static void msm_add_ubwc_combinations(struct driver *drv, const uint32_t *formats,
147                                       uint32_t num_formats, struct format_metadata *metadata,
148                                       uint64_t use_flags)
149 {
150         for (uint32_t i = 0; i < num_formats; i++) {
151                 if (is_ubwc_fmt(formats[i])) {
152                         struct combination combo = { .format = formats[i],
153                                                      .metadata = *metadata,
154                                                      .use_flags = use_flags };
155                         drv_array_append(drv->combos, &combo);
156                 }
157         }
158 }
159
160 static int msm_init(struct driver *drv)
161 {
162         struct format_metadata metadata;
163         uint64_t render_use_flags = BO_USE_RENDER_MASK | BO_USE_SCANOUT;
164         uint64_t texture_use_flags = BO_USE_TEXTURE_MASK | BO_USE_HW_VIDEO_DECODER;
165         uint64_t sw_flags = (BO_USE_RENDERSCRIPT | BO_USE_SW_WRITE_OFTEN | BO_USE_SW_READ_OFTEN |
166                              BO_USE_LINEAR | BO_USE_PROTECTED);
167
168         drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
169                              &LINEAR_METADATA, render_use_flags);
170
171         drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
172                              &LINEAR_METADATA, texture_use_flags);
173
174         /* The camera stack standardizes on NV12 for YUV buffers. */
175         /* YVU420 and NV12 formats for camera, display and encoding. */
176         drv_modify_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA,
177                                BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT |
178                                    BO_USE_HW_VIDEO_ENCODER);
179
180         /*
181          * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
182          * from camera and input/output from hardware decoder/encoder.
183          */
184         drv_modify_combination(drv, DRM_FORMAT_R8, &metadata,
185                                BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
186                                    BO_USE_HW_VIDEO_ENCODER);
187
188         /* Android CTS tests require this. */
189         drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
190
191         drv_modify_linear_combinations(drv);
192
193         metadata.tiling = MSM_UBWC_TILING;
194         metadata.priority = 2;
195         metadata.modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
196
197         render_use_flags &= ~sw_flags;
198         texture_use_flags &= ~sw_flags;
199
200         msm_add_ubwc_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
201                                   &metadata, render_use_flags);
202
203         msm_add_ubwc_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
204                                   &metadata, texture_use_flags);
205
206         return 0;
207 }
208
209 static int msm_bo_create_for_modifier(struct bo *bo, uint32_t width, uint32_t height,
210                                       uint32_t format, const uint64_t modifier)
211 {
212         struct drm_msm_gem_new req;
213         int ret;
214         size_t i;
215
216         bo->meta.tiling = (modifier == DRM_FORMAT_MOD_QCOM_COMPRESSED) ? MSM_UBWC_TILING : 0;
217
218         msm_calculate_layout(bo);
219
220         memset(&req, 0, sizeof(req));
221         req.flags = MSM_BO_WC | MSM_BO_SCANOUT;
222         req.size = bo->meta.total_size;
223
224         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_NEW, &req);
225         if (ret) {
226                 drv_log("DRM_IOCTL_MSM_GEM_NEW failed with %s\n", strerror(errno));
227                 return -errno;
228         }
229
230         /*
231          * Though we use only one plane, we need to set handle for
232          * all planes to pass kernel checks
233          */
234         for (i = 0; i < bo->meta.num_planes; i++) {
235                 bo->handles[i].u32 = req.handle;
236                 bo->meta.format_modifiers[i] = modifier;
237         }
238
239         return 0;
240 }
241
242 static int msm_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
243                                         uint32_t format, const uint64_t *modifiers, uint32_t count)
244 {
245         static const uint64_t modifier_order[] = {
246                 DRM_FORMAT_MOD_QCOM_COMPRESSED,
247                 DRM_FORMAT_MOD_LINEAR,
248         };
249
250         uint64_t modifier =
251             drv_pick_modifier(modifiers, count, modifier_order, ARRAY_SIZE(modifier_order));
252
253         return msm_bo_create_for_modifier(bo, width, height, format, modifier);
254 }
255
256 /* msm_bo_create will create linear buffers for now */
257 static int msm_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
258                          uint64_t flags)
259 {
260         struct combination *combo = drv_get_combination(bo->drv, format, flags);
261
262         if (!combo) {
263                 drv_log("invalid format = %d, flags = %" PRIx64 " combination\n", format, flags);
264                 return -EINVAL;
265         }
266
267         return msm_bo_create_for_modifier(bo, width, height, format, combo->metadata.modifier);
268 }
269
270 static void *msm_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
271 {
272         int ret;
273         struct drm_msm_gem_info req;
274
275         memset(&req, 0, sizeof(req));
276         req.handle = bo->handles[0].u32;
277
278         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_INFO, &req);
279         if (ret) {
280                 drv_log("DRM_IOCLT_MSM_GEM_INFO failed with %s\n", strerror(errno));
281                 return MAP_FAILED;
282         }
283         vma->length = bo->meta.total_size;
284
285         return mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
286                     req.offset);
287 }
288
289 static uint32_t msm_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
290 {
291         switch (format) {
292         case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
293                 /* Camera subsystem requires NV12. */
294                 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
295                         return DRM_FORMAT_NV12;
296                 /*HACK: See b/28671744 */
297                 return DRM_FORMAT_XBGR8888;
298         case DRM_FORMAT_FLEX_YCbCr_420_888:
299                 return DRM_FORMAT_NV12;
300         default:
301                 return format;
302         }
303 }
304
305 const struct backend backend_msm = {
306         .name = "msm",
307         .init = msm_init,
308         .bo_create = msm_bo_create,
309         .bo_create_with_modifiers = msm_bo_create_with_modifiers,
310         .bo_destroy = drv_gem_bo_destroy,
311         .bo_import = drv_prime_bo_import,
312         .bo_map = msm_bo_map,
313         .bo_unmap = drv_bo_munmap,
314         .resolve_format = msm_resolve_format,
315 };
316 #endif /* DRV_MSM */