OSDN Git Service

1eeba26f4528d045d1aefd24d65cb99a2719bfd6
[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_NV12,
34                                                         DRM_FORMAT_YVU420_ANDROID };
35
36 static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_R8, DRM_FORMAT_RG88,
37                                                    DRM_FORMAT_YVU420_ANDROID };
38
39 struct virtio_gpu_priv {
40         int has_3d;
41 };
42
43 static uint32_t translate_format(uint32_t drm_fourcc)
44 {
45         switch (drm_fourcc) {
46         case DRM_FORMAT_XRGB8888:
47                 return VIRGL_FORMAT_B8G8R8X8_UNORM;
48         case DRM_FORMAT_ARGB8888:
49                 return VIRGL_FORMAT_B8G8R8A8_UNORM;
50         case DRM_FORMAT_XBGR8888:
51                 return VIRGL_FORMAT_R8G8B8X8_UNORM;
52         case DRM_FORMAT_ABGR8888:
53                 return VIRGL_FORMAT_R8G8B8A8_UNORM;
54         case DRM_FORMAT_RGB565:
55                 return VIRGL_FORMAT_B5G6R5_UNORM;
56         case DRM_FORMAT_R8:
57                 return VIRGL_FORMAT_R8_UNORM;
58         case DRM_FORMAT_RG88:
59                 return VIRGL_FORMAT_R8G8_UNORM;
60         case DRM_FORMAT_NV12:
61                 return VIRGL_FORMAT_NV12;
62         case DRM_FORMAT_YVU420:
63         case DRM_FORMAT_YVU420_ANDROID:
64                 return VIRGL_FORMAT_YV12;
65         default:
66                 return 0;
67         }
68 }
69
70 static int virtio_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
71                                  uint64_t use_flags)
72 {
73         if (bo->meta.format != DRM_FORMAT_R8) {
74                 width = ALIGN(width, MESA_LLVMPIPE_TILE_SIZE);
75                 height = ALIGN(height, MESA_LLVMPIPE_TILE_SIZE);
76         }
77
78         return drv_dumb_bo_create_ex(bo, width, height, format, use_flags, BO_QUIRK_DUMB32BPP);
79 }
80
81 static inline void handle_flag(uint64_t *flag, uint64_t check_flag, uint32_t *bind,
82                                uint32_t virgl_bind)
83 {
84         if ((*flag) & check_flag) {
85                 (*flag) &= ~check_flag;
86                 (*bind) |= virgl_bind;
87         }
88 }
89
90 static uint32_t use_flags_to_bind(uint64_t use_flags)
91 {
92         /* In crosvm, VIRGL_BIND_SHARED means minigbm will allocate, not virglrenderer. */
93         uint32_t bind = VIRGL_BIND_SHARED;
94
95         handle_flag(&use_flags, BO_USE_TEXTURE, &bind, VIRGL_BIND_SAMPLER_VIEW);
96         handle_flag(&use_flags, BO_USE_RENDERING, &bind, VIRGL_BIND_RENDER_TARGET);
97         handle_flag(&use_flags, BO_USE_SCANOUT, &bind, VIRGL_BIND_SCANOUT);
98         handle_flag(&use_flags, BO_USE_CURSOR, &bind, VIRGL_BIND_CURSOR);
99         handle_flag(&use_flags, BO_USE_LINEAR, &bind, VIRGL_BIND_LINEAR);
100
101         handle_flag(&use_flags, BO_USE_SW_READ_OFTEN, &bind, VIRGL_BIND_LINEAR);
102         handle_flag(&use_flags, BO_USE_SW_READ_RARELY, &bind, VIRGL_BIND_LINEAR);
103         handle_flag(&use_flags, BO_USE_SW_WRITE_OFTEN, &bind, VIRGL_BIND_LINEAR);
104         handle_flag(&use_flags, BO_USE_SW_WRITE_RARELY, &bind, VIRGL_BIND_LINEAR);
105
106         // All host drivers only support linear camera buffer formats. If
107         // that changes, this will need to be modified.
108         handle_flag(&use_flags, BO_USE_CAMERA_READ, &bind, VIRGL_BIND_LINEAR);
109         handle_flag(&use_flags, BO_USE_CAMERA_WRITE, &bind, VIRGL_BIND_LINEAR);
110
111         if (use_flags) {
112                 drv_log("Unhandled bo use flag: %llx\n", (unsigned long long)use_flags);
113         }
114
115         return bind;
116 }
117
118 static int virtio_virgl_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
119                                   uint64_t use_flags)
120 {
121         int ret;
122         uint32_t stride;
123         struct drm_virtgpu_resource_create res_create;
124
125         stride = drv_stride_from_format(format, width, 0);
126         drv_bo_from_format(bo, stride, height, format);
127
128         /*
129          * Setting the target is intended to ensure this resource gets bound as a 2D
130          * texture in the host renderer's GL state. All of these resource properties are
131          * sent unchanged by the kernel to the host, which in turn sends them unchanged to
132          * virglrenderer. When virglrenderer makes a resource, it will convert the target
133          * enum to the equivalent one in GL and then bind the resource to that target.
134          */
135         memset(&res_create, 0, sizeof(res_create));
136
137         res_create.target = PIPE_TEXTURE_2D;
138         res_create.format = translate_format(format);
139         res_create.bind = use_flags_to_bind(use_flags);
140         res_create.width = width;
141         res_create.height = height;
142
143         /* For virgl 3D */
144         res_create.depth = 1;
145         res_create.array_size = 1;
146         res_create.last_level = 0;
147         res_create.nr_samples = 0;
148
149         res_create.size = ALIGN(bo->meta.total_size, PAGE_SIZE); // PAGE_SIZE = 0x1000
150         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_RESOURCE_CREATE, &res_create);
151         if (ret) {
152                 drv_log("DRM_IOCTL_VIRTGPU_RESOURCE_CREATE failed with %s\n", strerror(errno));
153                 return ret;
154         }
155
156         for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++)
157                 bo->handles[plane].u32 = res_create.bo_handle;
158
159         return 0;
160 }
161
162 static void *virtio_virgl_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
163 {
164         int ret;
165         struct drm_virtgpu_map gem_map;
166
167         memset(&gem_map, 0, sizeof(gem_map));
168         gem_map.handle = bo->handles[0].u32;
169
170         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_MAP, &gem_map);
171         if (ret) {
172                 drv_log("DRM_IOCTL_VIRTGPU_MAP failed with %s\n", strerror(errno));
173                 return MAP_FAILED;
174         }
175
176         vma->length = bo->meta.total_size;
177         return mmap(0, bo->meta.total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
178                     gem_map.offset);
179 }
180
181 static int virtio_gpu_init(struct driver *drv)
182 {
183         int ret;
184         struct virtio_gpu_priv *priv;
185         struct drm_virtgpu_getparam args;
186
187         priv = calloc(1, sizeof(*priv));
188         drv->priv = priv;
189
190         memset(&args, 0, sizeof(args));
191         args.param = VIRTGPU_PARAM_3D_FEATURES;
192         args.value = (uint64_t)(uintptr_t)&priv->has_3d;
193         ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_GETPARAM, &args);
194         if (ret) {
195                 drv_log("virtio 3D acceleration is not available\n");
196                 /* Be paranoid */
197                 priv->has_3d = 0;
198         }
199
200         if (priv->has_3d) {
201                 /* This doesn't mean host can scanout everything, it just means host
202                  * hypervisor can show it. */
203                 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
204                                      &LINEAR_METADATA, BO_USE_RENDER_MASK | BO_USE_SCANOUT);
205                 drv_add_combinations(drv, texture_source_formats,
206                                      ARRAY_SIZE(texture_source_formats), &LINEAR_METADATA,
207                                      BO_USE_TEXTURE_MASK);
208         } else {
209                 /* Virtio primary plane only allows this format. */
210                 drv_add_combination(drv, DRM_FORMAT_XRGB8888, &LINEAR_METADATA,
211                                     BO_USE_RENDER_MASK | BO_USE_SCANOUT);
212                 /* Virtio cursor plane only allows this format and Chrome cannot live without
213                  * ARGB888 renderable format. */
214                 drv_add_combination(drv, DRM_FORMAT_ARGB8888, &LINEAR_METADATA,
215                                     BO_USE_RENDER_MASK | BO_USE_CURSOR);
216                 /* Android needs more, but they cannot be bound as scanouts anymore after
217                  * "drm/virtio: fix DRM_FORMAT_* handling" */
218                 drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
219                                      &LINEAR_METADATA, BO_USE_RENDER_MASK);
220                 drv_add_combinations(drv, dumb_texture_source_formats,
221                                      ARRAY_SIZE(dumb_texture_source_formats), &LINEAR_METADATA,
222                                      BO_USE_TEXTURE_MASK);
223                 drv_add_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA,
224                                     BO_USE_SW_MASK | BO_USE_LINEAR);
225         }
226
227         /* Android CTS tests require this. */
228         drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
229
230         drv_modify_combination(drv, DRM_FORMAT_NV12, &LINEAR_METADATA,
231                                BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE | BO_USE_HW_VIDEO_DECODER |
232                                    BO_USE_HW_VIDEO_ENCODER);
233         drv_modify_combination(drv, DRM_FORMAT_R8, &LINEAR_METADATA,
234                                BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE);
235
236         return drv_modify_linear_combinations(drv);
237 }
238
239 static void virtio_gpu_close(struct driver *drv)
240 {
241         free(drv->priv);
242         drv->priv = NULL;
243 }
244
245 static int virtio_gpu_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
246                                 uint64_t use_flags)
247 {
248         struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
249         if (priv->has_3d)
250                 return virtio_virgl_bo_create(bo, width, height, format, use_flags);
251         else
252                 return virtio_dumb_bo_create(bo, width, height, format, use_flags);
253 }
254
255 static int virtio_gpu_bo_destroy(struct bo *bo)
256 {
257         struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
258         if (priv->has_3d)
259                 return drv_gem_bo_destroy(bo);
260         else
261                 return drv_dumb_bo_destroy(bo);
262 }
263
264 static void *virtio_gpu_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
265 {
266         struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
267         if (priv->has_3d)
268                 return virtio_virgl_bo_map(bo, vma, plane, map_flags);
269         else
270                 return drv_dumb_bo_map(bo, vma, plane, map_flags);
271 }
272
273 static int virtio_gpu_bo_invalidate(struct bo *bo, struct mapping *mapping)
274 {
275         int ret;
276         struct drm_virtgpu_3d_transfer_from_host xfer;
277         struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
278         struct drm_virtgpu_3d_wait waitcmd;
279
280         if (!priv->has_3d)
281                 return 0;
282
283         // Invalidate is only necessary if the host writes to the buffer.
284         if ((bo->meta.use_flags & BO_USE_RENDERING) == 0)
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         // TODO(b/145993887): Send also stride when the patches are landed
296         // When BO_USE_RENDERING is set, the level=stride hack is not needed
297
298         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST, &xfer);
299         if (ret) {
300                 drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST failed with %s\n", strerror(errno));
301                 return -errno;
302         }
303
304         // The transfer needs to complete before invalidate returns so that any host changes
305         // are visible and to ensure the host doesn't overwrite subsequent guest changes.
306         // TODO(b/136733358): Support returning fences from transfers
307         memset(&waitcmd, 0, sizeof(waitcmd));
308         waitcmd.handle = mapping->vma->handle;
309         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_WAIT, &waitcmd);
310         if (ret) {
311                 drv_log("DRM_IOCTL_VIRTGPU_WAIT failed with %s\n", strerror(errno));
312                 return -errno;
313         }
314
315         return 0;
316 }
317
318 static int virtio_gpu_bo_flush(struct bo *bo, struct mapping *mapping)
319 {
320         int ret;
321         struct drm_virtgpu_3d_transfer_to_host xfer;
322         struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
323
324         if (!priv->has_3d)
325                 return 0;
326
327         if (!(mapping->vma->map_flags & BO_MAP_WRITE))
328                 return 0;
329
330         memset(&xfer, 0, sizeof(xfer));
331         xfer.bo_handle = mapping->vma->handle;
332         xfer.box.x = mapping->rect.x;
333         xfer.box.y = mapping->rect.y;
334         xfer.box.w = mapping->rect.width;
335         xfer.box.h = mapping->rect.height;
336         xfer.box.d = 1;
337
338         // Unfortunately, the kernel doesn't actually pass the guest layer_stride and
339         // guest stride to the host (compare virtio_gpu.h and virtgpu_drm.h). We can use
340         // the level to work around this.
341         xfer.level = bo->meta.strides[0];
342
343         ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST, &xfer);
344         if (ret) {
345                 drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST failed with %s\n", strerror(errno));
346                 return -errno;
347         }
348
349         return 0;
350 }
351
352 static uint32_t virtio_gpu_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
353 {
354         struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)drv->priv;
355
356         switch (format) {
357         case DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED:
358                 /* Camera subsystem requires NV12. */
359                 if (use_flags & (BO_USE_CAMERA_READ | BO_USE_CAMERA_WRITE))
360                         return DRM_FORMAT_NV12;
361                 /*HACK: See b/28671744 */
362                 return DRM_FORMAT_XBGR8888;
363         case DRM_FORMAT_FLEX_YCbCr_420_888:
364                 /*
365                  * All of our host drivers prefer NV12 as their flexible media format.
366                  * If that changes, this will need to be modified.
367                  */
368                 if (priv->has_3d)
369                         return DRM_FORMAT_NV12;
370                 else
371                         return DRM_FORMAT_YVU420;
372         default:
373                 return format;
374         }
375 }
376
377 const struct backend backend_virtio_gpu = {
378         .name = "virtio_gpu",
379         .init = virtio_gpu_init,
380         .close = virtio_gpu_close,
381         .bo_create = virtio_gpu_bo_create,
382         .bo_destroy = virtio_gpu_bo_destroy,
383         .bo_import = drv_prime_bo_import,
384         .bo_map = virtio_gpu_bo_map,
385         .bo_unmap = drv_bo_munmap,
386         .bo_invalidate = virtio_gpu_bo_invalidate,
387         .bo_flush = virtio_gpu_bo_flush,
388         .resolve_format = virtio_gpu_resolve_format,
389 };