OSDN Git Service

205f53a9126a6d2c834db1e0fb2ad8f8696cedd0
[android-x86/external-minigbm.git] / virtio_gpu.c
1 /*
2  * Copyright 2017 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 #include <errno.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <sys/mman.h>
12 #include <virtgpu_drm.h>
13 #include <xf86drm.h>
14
15 #include "drv_priv.h"
16 #include "helpers.h"
17 #include "util.h"
18 #include "virgl_hw.h"
19
20 #ifndef PAGE_SIZE
21 #define PAGE_SIZE 0x1000
22 #endif
23 #define PIPE_TEXTURE_2D 2
24
25 #define MESA_LLVMPIPE_TILE_ORDER 6
26 #define MESA_LLVMPIPE_TILE_SIZE (1 << MESA_LLVMPIPE_TILE_ORDER)
27
28 static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
29                                                   DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
30                                                   DRM_FORMAT_XRGB8888 };
31
32 static const uint32_t dumb_texture_source_formats[] = { DRM_FORMAT_R8, DRM_FORMAT_YVU420,
33                                                         DRM_FORMAT_YVU420_ANDROID };
34
35 static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_R8, DRM_FORMAT_RG88,
36                                                    DRM_FORMAT_YVU420_ANDROID };
37
38 struct virtio_gpu_priv {
39         int has_3d;
40 };
41
42 static uint32_t translate_format(uint32_t drm_fourcc)
43 {
44         switch (drm_fourcc) {
45         case DRM_FORMAT_XRGB8888:
46                 return VIRGL_FORMAT_B8G8R8X8_UNORM;
47         case DRM_FORMAT_ARGB8888:
48                 return VIRGL_FORMAT_B8G8R8A8_UNORM;
49         case DRM_FORMAT_XBGR8888:
50                 return VIRGL_FORMAT_R8G8B8X8_UNORM;
51         case DRM_FORMAT_ABGR8888:
52                 return VIRGL_FORMAT_R8G8B8A8_UNORM;
53         case DRM_FORMAT_RGB565:
54                 return VIRGL_FORMAT_B5G6R5_UNORM;
55         case DRM_FORMAT_R8:
56                 return VIRGL_FORMAT_R8_UNORM;
57         case DRM_FORMAT_RG88:
58                 return VIRGL_FORMAT_R8G8_UNORM;
59         case DRM_FORMAT_NV12:
60                 return VIRGL_FORMAT_NV12;
61         case DRM_FORMAT_YVU420:
62         case DRM_FORMAT_YVU420_ANDROID:
63                 return VIRGL_FORMAT_YV12;
64         default:
65                 return 0;
66         }
67 }
68
69 static int virtio_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
70                                  uint64_t use_flags)
71 {
72         if (bo->format != DRM_FORMAT_R8) {
73                 width = ALIGN(width, MESA_LLVMPIPE_TILE_SIZE);
74                 height = ALIGN(height, MESA_LLVMPIPE_TILE_SIZE);
75         }
76
77         return drv_dumb_bo_create(bo, width, height, format, use_flags);
78 }
79
80 static inline void handle_flag(uint64_t *flag, uint64_t check_flag, uint32_t *bind,
81                                uint32_t virgl_bind)
82 {
83         if ((*flag) & check_flag) {
84                 (*flag) &= ~check_flag;
85                 (*bind) |= virgl_bind;
86         }
87 }
88
89 static uint32_t use_flags_to_bind(uint64_t use_flags)
90 {
91         /* In crosvm, VIRGL_BIND_SHARED means minigbm will allocate, not virglrenderer. */
92         uint32_t bind = VIRGL_BIND_SHARED;
93
94         handle_flag(&use_flags, BO_USE_TEXTURE, &bind, VIRGL_BIND_SAMPLER_VIEW);
95         handle_flag(&use_flags, BO_USE_RENDERING, &bind, VIRGL_BIND_RENDER_TARGET);
96         handle_flag(&use_flags, BO_USE_SCANOUT, &bind, VIRGL_BIND_SCANOUT);
97         // TODO (b/12983436): handle other use flags.
98         if (use_flags) {
99                 drv_log("Unhandled bo use flag: %llx\n", (unsigned long long)use_flags);
100         }
101
102         return bind;
103 }
104
105 static int virtio_virgl_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
106                                   uint64_t use_flags)
107 {
108         int ret;
109         uint32_t stride;
110         struct drm_virtgpu_resource_create res_create;
111
112         stride = drv_stride_from_format(format, width, 0);
113         drv_bo_from_format(bo, stride, height, format);
114
115         /*
116          * Setting the target is intended to ensure this resource gets bound as a 2D
117          * texture in the host renderer's GL state. All of these resource properties are
118          * sent unchanged by the kernel to the host, which in turn sends them unchanged to
119          * virglrenderer. When virglrenderer makes a resource, it will convert the target
120          * enum to the equivalent one in GL and then bind the resource to that target.
121          */
122         memset(&res_create, 0, sizeof(res_create));
123
124         res_create.target = PIPE_TEXTURE_2D;
125         res_create.format = translate_format(format);
126         res_create.bind = use_flags_to_bind(use_flags);
127         res_create.width = width;
128         res_create.height = height;
129
130         /* For virgl 3D */
131         res_create.depth = 1;
132         res_create.array_size = 1;
133         res_create.last_level = 0;
134         res_create.nr_samples = 0;
135
136         res_create.size = ALIGN(bo->total_size, PAGE_SIZE); // PAGE_SIZE = 0x1000
137         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_CREATE, &res_create);
138         if (ret) {
139                 drv_log("DRM_IOCTL_VIRTGPU_RESOURCE_CREATE failed with %s\n", strerror(errno));
140                 return ret;
141         }
142
143         for (uint32_t plane = 0; plane < bo->num_planes; plane++)
144                 bo->handles[plane].u32 = res_create.bo_handle;
145
146         return 0;
147 }
148
149 static void *virtio_virgl_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
150 {
151         int ret;
152         struct drm_virtgpu_map gem_map;
153
154         memset(&gem_map, 0, sizeof(gem_map));
155         gem_map.handle = bo->handles[0].u32;
156
157         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_MAP, &gem_map);
158         if (ret) {
159                 drv_log("DRM_IOCTL_VIRTGPU_MAP failed with %s\n", strerror(errno));
160                 return MAP_FAILED;
161         }
162
163         vma->length = bo->total_size;
164         return mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
165                     gem_map.offset);
166 }
167
168 static int virtio_gpu_init(struct driver *drv)
169 {
170         int ret;
171         struct virtio_gpu_priv *priv;
172         struct drm_virtgpu_getparam args;
173
174         priv = calloc(1, sizeof(*priv));
175         drv->priv = priv;
176
177         memset(&args, 0, sizeof(args));
178         args.param = VIRTGPU_PARAM_3D_FEATURES;
179         args.value = (uint64_t)(uintptr_t)&priv->has_3d;
180         ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_GETPARAM, &args);
181         if (ret) {
182                 drv_log("virtio 3D acceleration is not available\n");
183                 /* Be paranoid */
184                 priv->has_3d = 0;
185         }
186
187         /* This doesn't mean host can scanout everything, it just means host
188          * hypervisor can show it. */
189         drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
190                              &LINEAR_METADATA, BO_USE_RENDER_MASK | BO_USE_SCANOUT);
191
192         if (priv->has_3d) {
193                 drv_add_combinations(drv, texture_source_formats,
194                                      ARRAY_SIZE(texture_source_formats), &LINEAR_METADATA,
195                                      BO_USE_TEXTURE_MASK);
196         } else {
197                 drv_add_combinations(drv, dumb_texture_source_formats,
198                                      ARRAY_SIZE(dumb_texture_source_formats), &LINEAR_METADATA,
199                                      BO_USE_TEXTURE_MASK);
200                 drv_add_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA,
201                                     BO_USE_SW_MASK | BO_USE_LINEAR);
202         }
203
204         /* Android CTS tests require this. */
205         drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
206
207         drv_modify_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA,
208                                BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
209         drv_modify_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA,
210                                BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
211
212         return drv_modify_linear_combinations(drv);
213 }
214
215 static void virtio_gpu_close(struct driver *drv)
216 {
217         free(drv->priv);
218         drv->priv = NULL;
219 }
220
221 static int virtio_gpu_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
222                                 uint64_t use_flags)
223 {
224         struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
225         if (priv->has_3d)
226                 return virtio_virgl_bo_create(bo, width, height, format, use_flags);
227         else
228                 return virtio_dumb_bo_create(bo, width, height, format, use_flags);
229 }
230
231 static int virtio_gpu_bo_destroy(struct bo *bo)
232 {
233         struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
234         if (priv->has_3d)
235                 return drv_gem_bo_destroy(bo);
236         else
237                 return drv_dumb_bo_destroy(bo);
238 }
239
240 static void *virtio_gpu_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
241 {
242         struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
243         if (priv->has_3d)
244                 return virtio_virgl_bo_map(bo, vma, plane, map_flags);
245         else
246                 return drv_dumb_bo_map(bo, vma, plane, map_flags);
247 }
248
249 static int virtio_gpu_bo_invalidate(struct bo *bo, struct mapping *mapping)
250 {
251         int ret;
252         struct drm_virtgpu_3d_transfer_from_host xfer;
253         struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
254
255         if (!priv->has_3d)
256                 return 0;
257
258         memset(&xfer, 0, sizeof(xfer));
259         xfer.bo_handle = mapping->vma->handle;
260         xfer.box.x = mapping->rect.x;
261         xfer.box.y = mapping->rect.y;
262         xfer.box.w = mapping->rect.width;
263         xfer.box.h = mapping->rect.height;
264         xfer.box.d = 1;
265
266         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST, &xfer);
267         if (ret) {
268                 drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST failed with %s\n", strerror(errno));
269                 return -errno;
270         }
271
272         return 0;
273 }
274
275 static int virtio_gpu_bo_flush(struct bo *bo, struct mapping *mapping)
276 {
277         int ret;
278         struct drm_virtgpu_3d_transfer_to_host xfer;
279         struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
280
281         if (!priv->has_3d)
282                 return 0;
283
284         if (!(mapping->vma->map_flags & BO_MAP_WRITE))
285                 return 0;
286
287         memset(&xfer, 0, sizeof(xfer));
288         xfer.bo_handle = mapping->vma->handle;
289         xfer.box.x = mapping->rect.x;
290         xfer.box.y = mapping->rect.y;
291         xfer.box.w = mapping->rect.width;
292         xfer.box.h = mapping->rect.height;
293         xfer.box.d = 1;
294
295         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST, &xfer);
296         if (ret) {
297                 drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST failed with %s\n", strerror(errno));
298                 return -errno;
299         }
300
301         return 0;
302 }
303
304 static uint32_t virtio_gpu_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
305 {
306         struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)drv->priv;
307
308         switch (format) {
309         case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
310                 /* Camera subsystem requires NV12. */
311                 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
312                         return DRM_FORMAT_NV12;
313                 /*HACK: See b/28671744 */
314                 return DRM_FORMAT_XBGR8888;
315         case DRM_FORMAT_FLEX_YCbCr_420_888:
316                 /*
317                  * All of our host drivers prefer NV12 as their flexible media format.
318                  * If that changes, this will need to be modified.
319                  */
320                 if (priv->has_3d)
321                         return DRM_FORMAT_NV12;
322                 else
323                         return DRM_FORMAT_YVU420;
324         default:
325                 return format;
326         }
327 }
328
329 const struct backend backend_virtio_gpu = {
330         .name = "virtio_gpu",
331         .init = virtio_gpu_init,
332         .close = virtio_gpu_close,
333         .bo_create = virtio_gpu_bo_create,
334         .bo_destroy = virtio_gpu_bo_destroy,
335         .bo_import = drv_prime_bo_import,
336         .bo_map = virtio_gpu_bo_map,
337         .bo_unmap = drv_bo_munmap,
338         .bo_invalidate = virtio_gpu_bo_invalidate,
339         .bo_flush = virtio_gpu_bo_flush,
340         .resolve_format = virtio_gpu_resolve_format,
341 };