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,
33 DRM_FORMAT_YVU420_ANDROID };
35 static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_R8, DRM_FORMAT_RG88,
36 DRM_FORMAT_YVU420_ANDROID };
38 struct virtio_gpu_priv {
42 static uint32_t translate_format(uint32_t 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;
56 return VIRGL_FORMAT_R8_UNORM;
58 return VIRGL_FORMAT_R8G8_UNORM;
60 return VIRGL_FORMAT_NV12;
61 case DRM_FORMAT_YVU420:
62 case DRM_FORMAT_YVU420_ANDROID:
63 return VIRGL_FORMAT_YV12;
69 static int virtio_dumb_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
72 if (bo->format != DRM_FORMAT_R8) {
73 width = ALIGN(width, MESA_LLVMPIPE_TILE_SIZE);
74 height = ALIGN(height, MESA_LLVMPIPE_TILE_SIZE);
77 return drv_dumb_bo_create(bo, width, height, format, use_flags);
80 static inline void handle_flag(uint64_t *flag, uint64_t check_flag, uint32_t *bind,
83 if ((*flag) & check_flag) {
84 (*flag) &= ~check_flag;
85 (*bind) |= virgl_bind;
89 static uint32_t use_flags_to_bind(uint64_t use_flags)
91 /* In crosvm, VIRGL_BIND_SHARED means minigbm will allocate, not virglrenderer. */
92 uint32_t bind = VIRGL_BIND_SHARED;
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.
99 drv_log("Unhandled bo use flag: %llx\n", (unsigned long long)use_flags);
105 static int virtio_virgl_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
110 struct drm_virtgpu_resource_create res_create;
112 stride = drv_stride_from_format(format, width, 0);
113 drv_bo_from_format(bo, stride, height, format);
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.
122 memset(&res_create, 0, sizeof(res_create));
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;
131 res_create.depth = 1;
132 res_create.array_size = 1;
133 res_create.last_level = 0;
134 res_create.nr_samples = 0;
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);
139 drv_log("DRM_IOCTL_VIRTGPU_RESOURCE_CREATE failed with %s\n", strerror(errno));
143 for (uint32_t plane = 0; plane < bo->num_planes; plane++)
144 bo->handles[plane].u32 = res_create.bo_handle;
149 static void *virtio_virgl_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
152 struct drm_virtgpu_map gem_map;
154 memset(&gem_map, 0, sizeof(gem_map));
155 gem_map.handle = bo->handles[0].u32;
157 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_MAP, &gem_map);
159 drv_log("DRM_IOCTL_VIRTGPU_MAP failed with %s\n", strerror(errno));
163 vma->length = bo->total_size;
164 return mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
168 static int virtio_gpu_init(struct driver *drv)
171 struct virtio_gpu_priv *priv;
172 struct drm_virtgpu_getparam args;
174 priv = calloc(1, sizeof(*priv));
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);
182 drv_log("virtio 3D acceleration is not available\n");
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);
193 drv_add_combinations(drv, texture_source_formats,
194 ARRAY_SIZE(texture_source_formats), &LINEAR_METADATA,
195 BO_USE_TEXTURE_MASK);
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);
204 /* Android CTS tests require this. */
205 drv_add_combination(drv, DRM_FORMAT_BGR888, &LINEAR_METADATA, BO_USE_SW_MASK);
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);
212 return drv_modify_linear_combinations(drv);
215 static void virtio_gpu_close(struct driver *drv)
221 static int virtio_gpu_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
224 struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
226 return virtio_virgl_bo_create(bo, width, height, format, use_flags);
228 return virtio_dumb_bo_create(bo, width, height, format, use_flags);
231 static int virtio_gpu_bo_destroy(struct bo *bo)
233 struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
235 return drv_gem_bo_destroy(bo);
237 return drv_dumb_bo_destroy(bo);
240 static void *virtio_gpu_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
242 struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
244 return virtio_virgl_bo_map(bo, vma, plane, map_flags);
246 return drv_dumb_bo_map(bo, vma, plane, map_flags);
249 static int virtio_gpu_bo_invalidate(struct bo *bo, struct mapping *mapping)
252 struct drm_virtgpu_3d_transfer_from_host xfer;
253 struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
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;
266 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST, &xfer);
268 drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_FROM_HOST failed with %s\n", strerror(errno));
275 static int virtio_gpu_bo_flush(struct bo *bo, struct mapping *mapping)
278 struct drm_virtgpu_3d_transfer_to_host xfer;
279 struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)bo->drv->priv;
284 if (!(mapping->vma->map_flags & BO_MAP_WRITE))
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 ret = drmIoctl(bo->drv->fd, DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST, &xfer);
297 drv_log("DRM_IOCTL_VIRTGPU_TRANSFER_TO_HOST failed with %s\n", strerror(errno));
304 static uint32_t virtio_gpu_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
306 struct virtio_gpu_priv *priv = (struct virtio_gpu_priv *)drv->priv;
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:
317 * All of our host drivers prefer NV12 as their flexible media format.
318 * If that changes, this will need to be modified.
321 return DRM_FORMAT_NV12;
323 return DRM_FORMAT_YVU420;
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,