• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 <stdbool.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <xf86drm.h>
13 
14 #include "drv_priv.h"
15 #include "external/virtgpu_drm.h"
16 #include "util.h"
17 #include "virtgpu.h"
18 
19 #define PARAM(x)                                                                                   \
20 	(struct virtgpu_param)                                                                     \
21 	{                                                                                          \
22 		x, #x, 0                                                                           \
23 	}
24 
25 struct virtgpu_param params[] = {
26 	PARAM(VIRTGPU_PARAM_3D_FEATURES),	   PARAM(VIRTGPU_PARAM_CAPSET_QUERY_FIX),
27 	PARAM(VIRTGPU_PARAM_RESOURCE_BLOB),	   PARAM(VIRTGPU_PARAM_HOST_VISIBLE),
28 	PARAM(VIRTGPU_PARAM_CROSS_DEVICE),	   PARAM(VIRTGPU_PARAM_CONTEXT_INIT),
29 	PARAM(VIRTGPU_PARAM_SUPPORTED_CAPSET_IDs), PARAM(VIRTGPU_PARAM_CREATE_GUEST_HANDLE),
30 	PARAM(VIRTGPU_PARAM_RESOURCE_SYNC),	   PARAM(VIRTGPU_PARAM_GUEST_VRAM),
31 };
32 
33 extern const struct backend virtgpu_virgl;
34 extern const struct backend virtgpu_cross_domain;
35 
virtgpu_init(struct driver * drv)36 static int virtgpu_init(struct driver *drv)
37 {
38 	int ret = 0;
39 	const struct backend *virtgpu_backends[2] = {
40 		&virtgpu_cross_domain,
41 		&virtgpu_virgl,
42 	};
43 
44 	for (uint32_t i = 0; i < ARRAY_SIZE(params); i++) {
45 		struct drm_virtgpu_getparam get_param = { 0 };
46 
47 		get_param.param = params[i].param;
48 		get_param.value = (uint64_t)(uintptr_t)&params[i].value;
49 		int ret = drmIoctl(drv->fd, DRM_IOCTL_VIRTGPU_GETPARAM, &get_param);
50 		if (ret)
51 			drv_logi("virtgpu backend not enabling %s\n", params[i].name);
52 	}
53 
54 	for (uint32_t i = 0; i < ARRAY_SIZE(virtgpu_backends); i++) {
55 		const struct backend *backend = virtgpu_backends[i];
56 		ret = backend->init(drv);
57 		if (ret)
58 			continue;
59 
60 		drv->backend = backend;
61 		return 0;
62 	}
63 
64 	return ret;
65 }
66 
67 const struct backend backend_virtgpu = {
68 	.name = "virtio_gpu",
69 	.init = virtgpu_init,
70 };
71