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