]>
git.proxmox.com Git - mirror_qemu.git/blob - hw/display/virtio-dmabuf.c
2 * Virtio Shared dma-buf
4 * Copyright Red Hat, Inc. 2023
7 * Albert Esteve <aesteve@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
15 #include "hw/virtio/virtio-dmabuf.h"
19 static GHashTable
*resource_uuids
;
22 * uuid_equal_func: wrapper for UUID is_equal function to
23 * satisfy g_hash_table_new expected parameters signatures.
25 static int uuid_equal_func(const void *lhv
, const void *rhv
)
27 return qemu_uuid_is_equal(lhv
, rhv
);
30 static bool virtio_add_resource(QemuUUID
*uuid
, VirtioSharedObject
*value
)
35 if (resource_uuids
== NULL
) {
36 resource_uuids
= g_hash_table_new_full(qemu_uuid_hash
,
41 if (g_hash_table_lookup(resource_uuids
, uuid
) == NULL
) {
42 g_hash_table_insert(resource_uuids
, uuid
, value
);
46 g_mutex_unlock(&lock
);
51 bool virtio_add_dmabuf(QemuUUID
*uuid
, int udmabuf_fd
)
54 VirtioSharedObject
*vso
;
58 vso
= g_new(VirtioSharedObject
, 1);
59 vso
->type
= TYPE_DMABUF
;
60 vso
->value
= GINT_TO_POINTER(udmabuf_fd
);
61 result
= virtio_add_resource(uuid
, vso
);
69 bool virtio_add_vhost_device(QemuUUID
*uuid
, struct vhost_dev
*dev
)
72 VirtioSharedObject
*vso
;
76 vso
= g_new(VirtioSharedObject
, 1);
77 vso
->type
= TYPE_VHOST_DEV
;
79 result
= virtio_add_resource(uuid
, vso
);
87 bool virtio_remove_resource(const QemuUUID
*uuid
)
91 result
= g_hash_table_remove(resource_uuids
, uuid
);
92 g_mutex_unlock(&lock
);
97 static VirtioSharedObject
*get_shared_object(const QemuUUID
*uuid
)
99 gpointer lookup_res
= NULL
;
102 if (resource_uuids
!= NULL
) {
103 lookup_res
= g_hash_table_lookup(resource_uuids
, uuid
);
105 g_mutex_unlock(&lock
);
107 return (VirtioSharedObject
*) lookup_res
;
110 int virtio_lookup_dmabuf(const QemuUUID
*uuid
)
112 VirtioSharedObject
*vso
= get_shared_object(uuid
);
116 assert(vso
->type
== TYPE_DMABUF
);
117 return GPOINTER_TO_INT(vso
->value
);
120 struct vhost_dev
*virtio_lookup_vhost_device(const QemuUUID
*uuid
)
122 VirtioSharedObject
*vso
= get_shared_object(uuid
);
126 assert(vso
->type
== TYPE_VHOST_DEV
);
127 return (struct vhost_dev
*) vso
->value
;
130 SharedObjectType
virtio_object_type(const QemuUUID
*uuid
)
132 VirtioSharedObject
*vso
= get_shared_object(uuid
);
139 void virtio_free_resources(void)
142 g_hash_table_destroy(resource_uuids
);
143 /* Reference count shall be 0 after the implicit unref on destroy */
144 resource_uuids
= NULL
;
145 g_mutex_unlock(&lock
);