OSDN Git Service

msm: Disable UBWC briefly while we land the ARC++ modifier support
[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 <dlfcn.h>
11 #include <drm_fourcc.h>
12 #include <errno.h>
13 #include <inttypes.h>
14 #include <msm_drm.h>
15 #include <stdbool.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <sys/mman.h>
19 #include <xf86drm.h>
20
21 #include "drv_priv.h"
22 #include "helpers.h"
23 #include "util.h"
24
25 /* Alignment values are based on SDM845 Gfx IP */
26 #define DEFAULT_ALIGNMENT 64
27 #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)
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
36
37 #define MSM_UBWC_TILING 1
38
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 };
42
43 static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_R8,
44                                                    DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID };
45
46 /*
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  */
52 static 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
69 static void msm_calculate_layout(struct bo *bo)
70 {
71         uint32_t width, height;
72
73         width = bo->meta.width;
74         height = bo->meta.height;
75
76         /* NV12 format requires extra padding with platform
77          * specific alignments for venus driver
78          */
79         if (bo->meta.format == DRM_FORMAT_NV12) {
80                 uint32_t y_stride, uv_stride, y_scanline, uv_scanline, y_plane, uv_plane, size,
81                     extra_padding;
82
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), VENUS_SCANLINE_ALIGN);
87                 y_plane = y_stride * y_scanline;
88                 uv_plane = uv_stride * uv_scanline;
89
90                 if (bo->meta.tiling == MSM_UBWC_TILING) {
91                         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
98                 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;
102                 size = y_plane + uv_plane + extra_padding;
103                 bo->meta.total_size = ALIGN(size, BUFFER_SIZE_ALIGN);
104                 bo->meta.sizes[1] = bo->meta.total_size - bo->meta.sizes[0];
105         } else {
106                 uint32_t stride, alignw, alignh;
107
108                 alignw = ALIGN(width, DEFAULT_ALIGNMENT);
109                 /* 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 ||
113                     (bo->meta.format == DRM_FORMAT_R8 && height == 1)) {
114                         alignh = height;
115                 } else {
116                         alignh = ALIGN(height, DEFAULT_ALIGNMENT);
117                 }
118
119                 stride = drv_stride_from_format(bo->meta.format, alignw, 0);
120
121                 /* Calculate size and assign stride, size, offset to each plane based on format */
122                 drv_bo_from_format(bo, stride, alignh, bo->meta.format);
123
124                 /* For all RGB UBWC formats */
125                 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));
129                 }
130         }
131 }
132
133 static bool is_ubwc_fmt(uint32_t format)
134 {
135         switch (format) {
136         case DRM_FORMAT_XBGR8888:
137         case DRM_FORMAT_ABGR8888:
138         case DRM_FORMAT_XRGB8888:
139         case DRM_FORMAT_ARGB8888:
140         case DRM_FORMAT_NV12:
141                 return 1;
142         default:
143                 return 0;
144         }
145 }
146
147 static 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                 }
158         }
159 }
160
161 /**
162  * Check for buggy apps that are known to not support modifiers, to avoid surprising them
163  * with a UBWC buffer.
164  */
165 static 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
184 static int msm_init(struct driver *drv)
185 {
186         struct format_metadata metadata;
187         uint64_t render_use_flags = BO_USE_RENDER_MASK | BO_USE_SCANOUT;
188         uint64_t texture_use_flags = BO_USE_TEXTURE_MASK | BO_USE_HW_VIDEO_DECODER;
189         uint64_t sw_flags = (BO_USE_RENDERSCRIPT | BO_USE_SW_WRITE_OFTEN | BO_USE_SW_READ_OFTEN |
190                              BO_USE_LINEAR | BO_USE_PROTECTED);
191
192         drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
193                              &LINEAR_METADATA, render_use_flags);
194
195         drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
196                              &LINEAR_METADATA, texture_use_flags);
197
198         /* The camera stack standardizes on NV12 for YUV buffers. */
199         /* YVU420 and NV12 formats for camera, display and encoding. */
200         drv_modify_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA,
201                                BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_SCANOUT |
202                                    BO_USE_HW_VIDEO_ENCODER);
203
204         /*
205          * R8 format is used for Android's HAL_PIXEL_FORMAT_BLOB and is used for JPEG snapshots
206          * from camera and input/output from hardware decoder/encoder.
207          */
208         drv_modify_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA,
209                                BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
210                                    BO_USE_HW_VIDEO_ENCODER);
211
212         /* Android CTS tests require this. */
213         drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
214
215         drv_modify_linear_combinations(drv);
216
217         if (should_avoid_ubwc())
218                 return 0;
219
220         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
227         /* TODO(hoegsberg): Disable UBWC while we roll out support for
228          * passing modifiers from ARC++.  cros-gralloc actuallly
229          * allocates UBWC buffers in ARC++, but mesa EGL imports
230          * without a modifier and the ARC++ wayland_service hardcodes
231          * modifier 0 (DRM_FORMAT_MOD_LINEAR).  As a result, both
232          * sides think that they have a linear buffer and happly read
233          * and write linear.  It "works" even though the buffer was
234          * allocated as UBWC, since UBWC really just results in a
235          * slightly larger buffer than what we'd allocate for linear.
236          *
237          * As we land support in mesa for importing with modifers,
238          * mesa will start writing UBWC buffers.  Once we land the
239          * wayland_service change in ARC++, exo will start receiving
240          * the right modifier and read the buffers as UBWC.  We can't
241          * synchronize landing these changes and as they land out of
242          * order, the result is corrupted buffers for a while.  To
243          * avoid breakage in the interim, we disable UBWC while the
244          * changes land and then turn it back on when things settle
245          * down.
246          */
247         if (false) {
248                 msm_add_ubwc_combinations(drv, render_target_formats,
249                                           ARRAY_SIZE(render_target_formats),
250                                           &metadata, render_use_flags);
251
252                 msm_add_ubwc_combinations(drv, texture_source_formats,
253                                           ARRAY_SIZE(texture_source_formats),
254                                           &metadata, texture_use_flags);
255         }
256
257         return 0;
258 }
259
260 static int msm_bo_create_for_modifier(struct bo *bo, uint32_t width, uint32_t height,
261                                       uint32_t format, const uint64_t modifier)
262 {
263         struct drm_msm_gem_new req;
264         int ret;
265         size_t i;
266
267         bo->meta.tiling = (modifier == DRM_FORMAT_MOD_QCOM_COMPRESSED) ? MSM_UBWC_TILING : 0;
268
269         msm_calculate_layout(bo);
270
271         memset(&req, 0, sizeof(req));
272         req.flags = MSM_BO_WC | MSM_BO_SCANOUT;
273         req.size = bo->meta.total_size;
274
275         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_NEW, &req);
276         if (ret) {
277                 drv_log("DRM_IOCTL_MSM_GEM_NEW failed with %s\n", strerror(errno));
278                 return -errno;
279         }
280
281         /*
282          * Though we use only one plane, we need to set handle for
283          * all planes to pass kernel checks
284          */
285         for (i = 0; i < bo->meta.num_planes; i++) {
286                 bo->handles[i].u32 = req.handle;
287                 bo->meta.format_modifiers[i] = modifier;
288         }
289
290         return 0;
291 }
292
293 static int msm_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height,
294                                         uint32_t format, const uint64_t *modifiers, uint32_t count)
295 {
296         static const uint64_t modifier_order[] = {
297                 DRM_FORMAT_MOD_QCOM_COMPRESSED,
298                 DRM_FORMAT_MOD_LINEAR,
299         };
300
301         uint64_t modifier =
302             drv_pick_modifier(modifiers, count, modifier_order, ARRAY_SIZE(modifier_order));
303
304         return msm_bo_create_for_modifier(bo, width, height, format, modifier);
305 }
306
307 /* msm_bo_create will create linear buffers for now */
308 static int msm_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
309                          uint64_t flags)
310 {
311         struct combination *combo = drv_get_combination(bo->drv, format, flags);
312
313         if (!combo) {
314                 drv_log("invalid format = %d, flags = %" PRIx64 " combination\n", format, flags);
315                 return -EINVAL;
316         }
317
318         return msm_bo_create_for_modifier(bo, width, height, format, combo->metadata.modifier);
319 }
320
321 static void *msm_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
322 {
323         int ret;
324         struct drm_msm_gem_info req;
325
326         memset(&req, 0, sizeof(req));
327         req.handle = bo->handles[0].u32;
328
329         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_INFO, &req);
330         if (ret) {
331                 drv_log("DRM_IOCLT_MSM_GEM_INFO failed with %s\n", strerror(errno));
332                 return MAP_FAILED;
333         }
334         vma->length = bo->meta.total_size;
335
336         return mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
337                     req.offset);
338 }
339
340 static uint32_t msm_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
341 {
342         switch (format) {
343         case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
344                 /* Camera subsystem requires NV12. */
345                 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
346                         return DRM_FORMAT_NV12;
347                 /*HACK: See b/28671744 */
348                 return DRM_FORMAT_XBGR8888;
349         case DRM_FORMAT_FLEX_YCbCr_420_888:
350                 return DRM_FORMAT_NV12;
351         default:
352                 return format;
353         }
354 }
355
356 const struct backend backend_msm = {
357         .name = "msm",
358         .init = msm_init,
359         .bo_create = msm_bo_create,
360         .bo_create_with_modifiers = msm_bo_create_with_modifiers,
361         .bo_destroy = drv_gem_bo_destroy,
362         .bo_import = drv_prime_bo_import,
363         .bo_map = msm_bo_map,
364         .bo_unmap = drv_bo_munmap,
365         .resolve_format = msm_resolve_format,
366 };
367 #endif /* DRV_MSM */