OSDN Git Service

minigbm: Fix consistency in return values
[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 <msm_drm.h>
13 #include <stdbool.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <sys/mman.h>
17 #include <xf86drm.h>
18
19 #include "drv_priv.h"
20 #include "helpers.h"
21 #include "util.h"
22
23 /* Alignment values are based on SDM845 Gfx IP */
24 #define DEFAULT_ALIGNMENT 64
25 #define BUFFER_SIZE_ALIGN 4096
26
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
34
35 #define MSM_UBWC_TILING 1
36
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 };
40
41 static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_R8,
42                                                    DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID };
43
44 /*
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).
49  */
50 static uint32_t get_ubwc_meta_size(uint32_t width, uint32_t height, uint32_t tile_width,
51                                    uint32_t tile_height)
52 {
53         uint32_t macrotile_width, macrotile_height;
54
55         macrotile_width = DIV_ROUND_UP(width, tile_width);
56         macrotile_height = DIV_ROUND_UP(height, tile_height);
57
58         // Align meta buffer width to 64 blocks
59         macrotile_width = ALIGN(macrotile_width, MACROTILE_WIDTH_ALIGN);
60
61         // Align meta buffer height to 16 blocks
62         macrotile_height = ALIGN(macrotile_height, MACROTILE_HEIGHT_ALIGN);
63
64         return ALIGN(macrotile_width * macrotile_height, PLANE_SIZE_ALIGN);
65 }
66
67 static void msm_calculate_layout(struct bo *bo)
68 {
69         uint32_t width, height;
70
71         width = bo->width;
72         height = bo->height;
73
74         /* NV12 format requires extra padding with platform
75          * specific alignments for venus driver
76          */
77         if (bo->format == DRM_FORMAT_NV12) {
78                 uint32_t y_stride, uv_stride, y_scanline, uv_scanline, y_plane, uv_plane, size,
79                     extra_padding;
80
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;
87
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);
92                 } else {
93                         extra_padding = NV12_LINEAR_PADDING;
94                 }
95
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];
103         } else {
104                 uint32_t stride, alignw, alignh;
105
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) {
109                         alignh = height;
110                 } else {
111                         alignh = ALIGN(height, DEFAULT_ALIGNMENT);
112                 }
113
114                 stride = drv_stride_from_format(bo->format, alignw, 0);
115
116                 /* Calculate size and assign stride, size, offset to each plane based on format */
117                 drv_bo_from_format(bo, stride, alignh, bo->format);
118
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));
124                 }
125         }
126 }
127
128 static bool is_ubwc_fmt(uint32_t format)
129 {
130         switch (format) {
131         case DRM_FORMAT_XBGR8888:
132         case DRM_FORMAT_ABGR8888:
133         case DRM_FORMAT_NV12:
134                 return 1;
135         default:
136                 return 0;
137         }
138 }
139
140 static void msm_add_ubwc_combinations(struct driver *drv, const uint32_t *formats,
141                                       uint32_t num_formats, struct format_metadata *metadata,
142                                       uint64_t use_flags)
143 {
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);
150                 }
151         }
152 }
153
154 static int msm_init(struct driver *drv)
155 {
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);
161
162         drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
163                              &LINEAR_METADATA, render_use_flags);
164
165         drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
166                              &LINEAR_METADATA, texture_use_flags);
167
168         /* Android CTS tests require this. */
169         drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
170
171         drv_modify_linear_combinations(drv);
172
173         metadata.tiling = MSM_UBWC_TILING;
174         metadata.priority = 2;
175         metadata.modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
176
177         render_use_flags &= ~sw_flags;
178         texture_use_flags &= ~sw_flags;
179
180         msm_add_ubwc_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
181                                   &metadata, render_use_flags);
182
183         msm_add_ubwc_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
184                                   &metadata, texture_use_flags);
185
186         return 0;
187 }
188
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)
191 {
192         struct drm_msm_gem_new req;
193         int ret;
194         size_t i;
195
196         bo->tiling = (modifier == DRM_FORMAT_MOD_QCOM_COMPRESSED) ? MSM_UBWC_TILING : 0;
197
198         msm_calculate_layout(bo);
199
200         memset(&req, 0, sizeof(req));
201         req.flags = MSM_BO_WC | MSM_BO_SCANOUT;
202         req.size = bo->total_size;
203
204         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_NEW, &req);
205         if (ret) {
206                 drv_log("DRM_IOCTL_MSM_GEM_NEW failed with %s\n", strerror(errno));
207                 return -errno;
208         }
209
210         /*
211          * Though we use only one plane, we need to set handle for
212          * all planes to pass kernel checks
213          */
214         for (i = 0; i < bo->num_planes; i++) {
215                 bo->handles[i].u32 = req.handle;
216                 bo->format_modifiers[i] = modifier;
217         }
218
219         return 0;
220 }
221
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)
224 {
225         static const uint64_t modifier_order[] = {
226                 DRM_FORMAT_MOD_QCOM_COMPRESSED,
227                 DRM_FORMAT_MOD_LINEAR,
228         };
229
230         uint64_t modifier =
231             drv_pick_modifier(modifiers, count, modifier_order, ARRAY_SIZE(modifier_order));
232
233         return msm_bo_create_for_modifier(bo, width, height, format, modifier);
234 }
235
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,
238                          uint64_t flags)
239 {
240         struct combination *combo = drv_get_combination(bo->drv, format, flags);
241
242         if (!combo) {
243                 drv_log("invalid format = %d, flags = %llx combination\n", format, flags);
244                 return -EINVAL;
245         }
246
247         return msm_bo_create_for_modifier(bo, width, height, format, combo->metadata.modifier);
248 }
249
250 static void *msm_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
251 {
252         int ret;
253         struct drm_msm_gem_info req;
254
255         memset(&req, 0, sizeof(req));
256         req.handle = bo->handles[0].u32;
257
258         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_INFO, &req);
259         if (ret) {
260                 drv_log("DRM_IOCLT_MSM_GEM_INFO failed with %s\n", strerror(errno));
261                 return MAP_FAILED;
262         }
263         vma->length = bo->total_size;
264
265         return mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
266                     req.offset);
267 }
268
269 const struct backend backend_msm = {
270         .name = "msm",
271         .init = msm_init,
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,
278 };
279 #endif /* DRV_MSM */