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.
12 #include <virtgpu_drm.h>
21 #define PAGE_SIZE 0x1000
23 #define PIPE_TEXTURE_2D 2
25 #define MESA_LLVMPIPE_TILE_ORDER 6
26 #define MESA_LLVMPIPE_TILE_SIZE (1 << MESA_LLVMPIPE_TILE_ORDER)
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 };
32 static const uint32_t dumb_texture_source_formats[] = { DRM_FORMAT_R8, DRM_FORMAT_YVU420,
34 DRM_FORMAT_YVU420_ANDROID };
36 static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_R8, DRM_FORMAT_RG88,
37 DRM_FORMAT_YVU420_ANDROID };
39 struct virtio_gpu_priv {
43 static uint32_t translate_format(uint32_t 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;
57 return VIRGL_FORMAT_R8_UNORM;
59 return VIRGL_FORMAT_R8G8_UNORM;
61 return VIRGL_FORMAT_NV12;
62 case DRM_FORMAT_YVU420:
63 case DRM_FORMAT_YVU420_ANDROID:
64 return VIRGL_FORMAT_YV12;
70 static int virtio_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
73 if (bo->meta.format != DRM_FORMAT_R8) {
74 width = ALIGN(width, MESA_LLVMPIPE_TILE_SIZE);
75 height = ALIGN(height, MESA_LLVMPIPE_TILE_SIZE);
78 return drv_dumb_bo_create_ex(bo, width, height, format, use_flags, BO_QUIRK_DUMB32BPP);
81 static inline void handle_flag(uint64_t *flag, uint64_t check_flag, uint32_t *bind,
84 if ((*flag) & check_flag) {
85 (*flag) &= ~check_flag;
86 (*bind) |= virgl_bind;
90 static uint32_t use_flags_to_bind(uint64_t use_flags)
92 /* In crosvm, VIRGL_BIND_SHARED means minigbm will allocate, not virglrenderer. */
93 uint32_t bind = VIRGL_BIND_SHARED;
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);
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);
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);
112 drv_log("Unhandled bo use flag: %llx\n", (unsigned long long)use_flags);
118 static int virtio_virgl_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
123 struct drm_virtgpu_resource_create res_create;
125 stride = drv_stride_from_format(format, width, 0);
126 drv_bo_from_format(bo, stride, height, format);
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.
135 memset(&res_create, 0, sizeof(res_create));
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;
144 res_create.depth = 1;
145 res_create.array_size = 1;
146 res_create.last_level = 0;
147 res_create.nr_samples = 0;
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);
152 drv_log("DRM_IOCTL_VIRTGPU_RESOURCE_CREATE failed with %s\n", strerror(errno));
156 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++)
157 bo->handles[plane].u32 = res_create.bo_handle;
162 static void *virtio_virgl_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
165 struct drm_virtgpu_map gem_map;
167 memset(&gem_map, 0, sizeof(gem_map));
168 gem_map.handle = bo->handles[0].u32;
170 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_MAP, &gem_map);
172 drv_log("DRM_IOCTL_VIRTGPU_MAP failed with %s\n", strerror(errno));
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,
181 static int virtio_gpu_init(struct driver *drv)
184 struct virtio_gpu_priv *priv;
185 struct drm_virtgpu_getparam args;
187 priv = calloc(1, sizeof(*priv));
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);
195 drv_log("virtio 3D acceleration is not available\n");
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);
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);
227 /* Android CTS tests require this. */
228 drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
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);
236 return drv_modify_linear_combinations(drv);
239 static void virtio_gpu_close(struct driver *drv)
245 static int virtio_gpu_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
248 struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
250 return virtio_virgl_bo_create(bo, width, height, format, use_flags);
252 return virtio_dumb_bo_create(bo, width, height, format, use_flags);
255 static int virtio_gpu_bo_destroy(struct bo *bo)
257 struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
259 return drv_gem_bo_destroy(bo);
261 return drv_dumb_bo_destroy(bo);
264 static void *virtio_gpu_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
266 struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
268 return virtio_virgl_bo_map(bo, vma, plane, map_flags);
270 return drv_dumb_bo_map(bo, vma, plane, map_flags);
273 static int virtio_gpu_bo_invalidate(struct bo *bo, struct mapping *mapping)
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;
283 // Invalidate is only necessary if the host writes to the buffer.
284 if ((bo->meta.use_flags & BO_USE_RENDERING) == 0)
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;
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
298 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST, &xfer);
300 drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST failed with %s\n", strerror(errno));
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);
311 drv_log("DRM_IOCTL_VIRTGPU_WAIT failed with %s\n", strerror(errno));
318 static int virtio_gpu_bo_flush(struct bo *bo, struct mapping *mapping)
321 struct drm_virtgpu_3d_transfer_to_host xfer;
322 struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
327 if (!(mapping->vma->map_flags & BO_MAP_WRITE))
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;
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];
343 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST, &xfer);
345 drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST failed with %s\n", strerror(errno));
352 static uint32_t virtio_gpu_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
354 struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)drv->priv;
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:
365 * All of our host drivers prefer NV12 as their flexible media format.
366 * If that changes, this will need to be modified.
369 return DRM_FORMAT_NV12;
371 return DRM_FORMAT_YVU420;
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,