xref: /openbmc/qemu/include/hw/virtio/virtio-dmabuf.h (revision 2e1cacfb)
1 /*
2  * Virtio Shared dma-buf
3  *
4  * Copyright Red Hat, Inc. 2023
5  *
6  * Authors:
7  *     Albert Esteve <aesteve@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.
10  * See the COPYING file in the top-level directory.
11  */
12 
13 #ifndef VIRTIO_DMABUF_H
14 #define VIRTIO_DMABUF_H
15 
16 #include "qemu/uuid.h"
17 #include "vhost.h"
18 
19 typedef enum SharedObjectType {
20     TYPE_INVALID = 0,
21     TYPE_DMABUF,
22     TYPE_VHOST_DEV,
23 } SharedObjectType;
24 
25 typedef struct VirtioSharedObject {
26     SharedObjectType type;
27     gpointer value;
28 } VirtioSharedObject;
29 
30 /**
31  * virtio_add_dmabuf() - Add a new dma-buf resource to the lookup table
32  * @uuid: new resource's UUID
33  * @dmabuf_fd: the dma-buf descriptor that will be stored and shared with
34  *             other virtio devices. The caller retains ownership over the
35  *             descriptor and its lifecycle.
36  *
37  * Note: @dmabuf_fd must be a valid (non-negative) file descriptor.
38  *
39  * Return: true if the UUID did not exist and the resource has been added,
40  * false if another resource with the same UUID already existed.
41  * Note that if it finds a repeated UUID, the resource is not inserted in
42  * the lookup table.
43  */
44 bool virtio_add_dmabuf(QemuUUID *uuid, int dmabuf_fd);
45 
46 /**
47  * virtio_add_vhost_device() - Add a new exporter vhost device that holds the
48  * resource with the associated UUID
49  * @uuid: new resource's UUID
50  * @dev: the pointer to the vhost device that holds the resource. The caller
51  *       retains ownership over the device struct and its lifecycle.
52  *
53  * Return: true if the UUID did not exist and the device has been tracked,
54  * false if another resource with the same UUID already existed.
55  * Note that if it finds a repeated UUID, the resource is not inserted in
56  * the lookup table.
57  */
58 bool virtio_add_vhost_device(QemuUUID *uuid, struct vhost_dev *dev);
59 
60 /**
61  * virtio_remove_resource() - Removes a resource from the lookup table
62  * @uuid: resource's UUID
63  *
64  * Return: true if the UUID has been found and removed from the lookup table.
65  */
66 bool virtio_remove_resource(const QemuUUID *uuid);
67 
68 /**
69  * virtio_lookup_dmabuf() - Looks for a dma-buf resource in the lookup table
70  * @uuid: resource's UUID
71  *
72  * Return: the dma-buf file descriptor integer, or -1 if the key is not found.
73  */
74 int virtio_lookup_dmabuf(const QemuUUID *uuid);
75 
76 /**
77  * virtio_lookup_vhost_device() - Looks for an exporter vhost device in the
78  * lookup table
79  * @uuid: resource's UUID
80  *
81  * Return: pointer to the vhost_dev struct, or NULL if the key is not found.
82  */
83 struct vhost_dev *virtio_lookup_vhost_device(const QemuUUID *uuid);
84 
85 /**
86  * virtio_object_type() - Looks for the type of resource in the lookup table
87  * @uuid: resource's UUID
88  *
89  * Return: the type of resource associated with the UUID, or TYPE_INVALID if
90  * the key is not found.
91  */
92 SharedObjectType virtio_object_type(const QemuUUID *uuid);
93 
94 /**
95  * virtio_free_resources() - Destroys all keys and values of the shared
96  * resources lookup table, and frees them
97  */
98 void virtio_free_resources(void);
99 
100 #endif /* VIRTIO_DMABUF_H */
101