1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
/*
* Virtio Shared dma-buf
*
* Copyright Red Hat, Inc. 2023
*
* Authors:
* Albert Esteve <aesteve@redhat.com>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include "qemu/osdep.h"
#include "hw/virtio/virtio-dmabuf.h"
static GMutex lock;
static GHashTable *resource_uuids;
/*
* uuid_equal_func: wrapper for UUID is_equal function to
* satisfy g_hash_table_new expected parameters signatures.
*/
static int uuid_equal_func(const void *lhv, const void *rhv)
{
return qemu_uuid_is_equal(lhv, rhv);
}
static bool virtio_add_resource(QemuUUID *uuid, VirtioSharedObject *value)
{
bool result = true;
g_mutex_lock(&lock);
if (resource_uuids == NULL) {
resource_uuids = g_hash_table_new_full(qemu_uuid_hash,
uuid_equal_func,
NULL,
g_free);
}
if (g_hash_table_lookup(resource_uuids, uuid) == NULL) {
g_hash_table_insert(resource_uuids, uuid, value);
} else {
result = false;
}
g_mutex_unlock(&lock);
return result;
}
bool virtio_add_dmabuf(QemuUUID *uuid, int udmabuf_fd)
{
bool result;
VirtioSharedObject *vso;
if (udmabuf_fd < 0) {
return false;
}
vso = g_new(VirtioSharedObject, 1);
vso->type = TYPE_DMABUF;
vso->value = GINT_TO_POINTER(udmabuf_fd);
result = virtio_add_resource(uuid, vso);
if (!result) {
g_free(vso);
}
return result;
}
bool virtio_add_vhost_device(QemuUUID *uuid, struct vhost_dev *dev)
{
bool result;
VirtioSharedObject *vso;
if (dev == NULL) {
return false;
}
vso = g_new(VirtioSharedObject, 1);
vso->type = TYPE_VHOST_DEV;
vso->value = dev;
result = virtio_add_resource(uuid, vso);
if (!result) {
g_free(vso);
}
return result;
}
bool virtio_remove_resource(const QemuUUID *uuid)
{
bool result;
g_mutex_lock(&lock);
result = g_hash_table_remove(resource_uuids, uuid);
g_mutex_unlock(&lock);
return result;
}
static VirtioSharedObject *get_shared_object(const QemuUUID *uuid)
{
gpointer lookup_res = NULL;
g_mutex_lock(&lock);
if (resource_uuids != NULL) {
lookup_res = g_hash_table_lookup(resource_uuids, uuid);
}
g_mutex_unlock(&lock);
return (VirtioSharedObject *) lookup_res;
}
int virtio_lookup_dmabuf(const QemuUUID *uuid)
{
VirtioSharedObject *vso = get_shared_object(uuid);
if (vso == NULL) {
return -1;
}
assert(vso->type == TYPE_DMABUF);
return GPOINTER_TO_INT(vso->value);
}
struct vhost_dev *virtio_lookup_vhost_device(const QemuUUID *uuid)
{
VirtioSharedObject *vso = get_shared_object(uuid);
if (vso == NULL) {
return NULL;
}
assert(vso->type == TYPE_VHOST_DEV);
return (struct vhost_dev *) vso->value;
}
SharedObjectType virtio_object_type(const QemuUUID *uuid)
{
VirtioSharedObject *vso = get_shared_object(uuid);
if (vso == NULL) {
return TYPE_INVALID;
}
return vso->type;
}
void virtio_free_resources(void)
{
g_mutex_lock(&lock);
g_hash_table_destroy(resource_uuids);
/* Reference count shall be 0 after the implicit unref on destroy */
resource_uuids = NULL;
g_mutex_unlock(&lock);
}
|