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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
/*
* Container for vfio-user IOMMU type: rather than communicating with the kernel
* vfio driver, we communicate over a socket to a server using the vfio-user
* protocol.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <sys/ioctl.h>
#include <linux/vfio.h>
#include "qemu/osdep.h"
#include "hw/vfio-user/container.h"
#include "hw/vfio-user/device.h"
#include "hw/vfio/vfio-cpr.h"
#include "hw/vfio/vfio-device.h"
#include "hw/vfio/vfio-listener.h"
#include "qapi/error.h"
static int vfio_user_dma_unmap(const VFIOContainerBase *bcontainer,
hwaddr iova, ram_addr_t size,
IOMMUTLBEntry *iotlb, bool unmap_all)
{
return -ENOTSUP;
}
static int vfio_user_dma_map(const VFIOContainerBase *bcontainer, hwaddr iova,
ram_addr_t size, void *vaddr, bool readonly,
MemoryRegion *mrp)
{
return -ENOTSUP;
}
static int
vfio_user_set_dirty_page_tracking(const VFIOContainerBase *bcontainer,
bool start, Error **errp)
{
error_setg_errno(errp, ENOTSUP, "Not supported");
return -ENOTSUP;
}
static int vfio_user_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
VFIOBitmap *vbmap, hwaddr iova,
hwaddr size, Error **errp)
{
error_setg_errno(errp, ENOTSUP, "Not supported");
return -ENOTSUP;
}
static bool vfio_user_setup(VFIOContainerBase *bcontainer, Error **errp)
{
error_setg_errno(errp, ENOTSUP, "Not supported");
return -ENOTSUP;
}
static VFIOUserContainer *vfio_user_create_container(Error **errp)
{
VFIOUserContainer *container;
container = VFIO_IOMMU_USER(object_new(TYPE_VFIO_IOMMU_USER));
return container;
}
/*
* Try to mirror vfio_container_connect() as much as possible.
*/
static VFIOUserContainer *
vfio_user_container_connect(AddressSpace *as, Error **errp)
{
VFIOContainerBase *bcontainer;
VFIOUserContainer *container;
VFIOAddressSpace *space;
VFIOIOMMUClass *vioc;
space = vfio_address_space_get(as);
container = vfio_user_create_container(errp);
if (!container) {
goto put_space_exit;
}
bcontainer = &container->bcontainer;
if (!vfio_cpr_register_container(bcontainer, errp)) {
goto free_container_exit;
}
vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
assert(vioc->setup);
if (!vioc->setup(bcontainer, errp)) {
goto unregister_container_exit;
}
vfio_address_space_insert(space, bcontainer);
if (!vfio_listener_register(bcontainer, errp)) {
goto listener_release_exit;
}
bcontainer->initialized = true;
return container;
listener_release_exit:
vfio_listener_unregister(bcontainer);
if (vioc->release) {
vioc->release(bcontainer);
}
unregister_container_exit:
vfio_cpr_unregister_container(bcontainer);
free_container_exit:
object_unref(container);
put_space_exit:
vfio_address_space_put(space);
return NULL;
}
static void vfio_user_container_disconnect(VFIOUserContainer *container)
{
VFIOContainerBase *bcontainer = &container->bcontainer;
VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer);
vfio_listener_unregister(bcontainer);
if (vioc->release) {
vioc->release(bcontainer);
}
VFIOAddressSpace *space = bcontainer->space;
vfio_cpr_unregister_container(bcontainer);
object_unref(container);
vfio_address_space_put(space);
}
static bool vfio_user_device_get(VFIOUserContainer *container,
VFIODevice *vbasedev, Error **errp)
{
struct vfio_device_info info = { .argsz = sizeof(info) };
if (!vfio_user_get_device_info(vbasedev->proxy, &info, errp)) {
return false;
}
vbasedev->fd = -1;
vfio_device_prepare(vbasedev, &container->bcontainer, &info);
return true;
}
/*
* vfio_user_device_attach: attach a device to a new container.
*/
static bool vfio_user_device_attach(const char *name, VFIODevice *vbasedev,
AddressSpace *as, Error **errp)
{
VFIOUserContainer *container;
container = vfio_user_container_connect(as, errp);
if (container == NULL) {
error_prepend(errp, "failed to connect proxy");
return false;
}
return vfio_user_device_get(container, vbasedev, errp);
}
static void vfio_user_device_detach(VFIODevice *vbasedev)
{
VFIOUserContainer *container = container_of(vbasedev->bcontainer,
VFIOUserContainer, bcontainer);
vfio_device_unprepare(vbasedev);
vfio_user_container_disconnect(container);
}
static int vfio_user_pci_hot_reset(VFIODevice *vbasedev, bool single)
{
/* ->needs_reset is always false for vfio-user. */
return 0;
}
static void vfio_iommu_user_class_init(ObjectClass *klass, const void *data)
{
VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass);
vioc->setup = vfio_user_setup;
vioc->dma_map = vfio_user_dma_map;
vioc->dma_unmap = vfio_user_dma_unmap;
vioc->attach_device = vfio_user_device_attach;
vioc->detach_device = vfio_user_device_detach;
vioc->set_dirty_page_tracking = vfio_user_set_dirty_page_tracking;
vioc->query_dirty_bitmap = vfio_user_query_dirty_bitmap;
vioc->pci_hot_reset = vfio_user_pci_hot_reset;
};
static const TypeInfo types[] = {
{
.name = TYPE_VFIO_IOMMU_USER,
.parent = TYPE_VFIO_IOMMU,
.instance_size = sizeof(VFIOUserContainer),
.class_init = vfio_iommu_user_class_init,
},
};
DEFINE_TYPES(types)
|