-
Notifications
You must be signed in to change notification settings - Fork 8
/
vfio.c
262 lines (208 loc) · 6.06 KB
/
vfio.c
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (C) 2018-2019 Arm Ltd.
* Author: Vincent Stehlé <vincent.stehle@arm.com>
*
* This is inspired from Linux Documentation/vfio.txt
*/
#include "vfio.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/vfio.h>
#include <assert.h>
#include <stdint.h>
#include <unistd.h>
#include "cmdline.h"
void vfio_setup(struct vfio *v, const struct cmdline *cl)
{
int s;
struct vfio_group_status group_status = {
.argsz = sizeof(group_status),
};
struct vfio_iommu_type1_info iommu_info = {
.argsz = sizeof(iommu_info),
};
struct vfio_device_info device_info = {
.argsz = sizeof(device_info),
};
assert(v);
assert(cl);
memset(v, 0, sizeof(*v));
/* Create a new container */
v->container_fd = open("/dev/vfio/vfio", O_RDWR);
if (v->container_fd == -1)
err(1, "open /dev/vfio/vfio");
if (ioctl(v->container_fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION)
err(1, "Unknown API version");
if (!ioctl(v->container_fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU))
err(1, "Doesn't support the IOMMU driver we want");
/* Open the group */
v->group_fd = open(cl->vfio_group, O_RDWR);
if (v->group_fd == -1)
err(1, "open %s", cl->vfio_group);
/* Test the group is viable and available */
s = ioctl(v->group_fd, VFIO_GROUP_GET_STATUS, &group_status);
if (s)
err(1, "get group status: %d", s);
if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
errx(1, "Group is not viable");
/* Add the group to the container */
s = ioctl(v->group_fd, VFIO_GROUP_SET_CONTAINER, &v->container_fd);
if (s)
err(1, "set container: %d", s);
/* Enable the IOMMU model we want */
s = ioctl(v->container_fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU);
if (s)
err(1, "set iommu: %d", s);
/* Get addition IOMMU info */
s = ioctl(v->container_fd, VFIO_IOMMU_GET_INFO, &iommu_info);
if (s)
err(1, "get iommu info: %d", s);
if (iommu_info.flags & VFIO_IOMMU_INFO_PGSIZES)
printf("Supported page sizes bitmap: %#llx\n",
iommu_info.iova_pgsizes);
/* Get a file descriptor for the device */
v->device_fd = ioctl(v->group_fd, VFIO_GROUP_GET_DEVICE_FD,
cl->device);
if (v->device_fd <= 0)
err(1, "get device fd: %d", v->device_fd);
/* Test and setup the device */
s = ioctl(v->device_fd, VFIO_DEVICE_GET_INFO, &device_info);
if (s)
err(1, "get device info: %d", s);
printf("num_regions: %d, num_irqs: %d\n", device_info.num_regions,
device_info.num_irqs);
if (!(device_info.flags & VFIO_DEVICE_FLAGS_PCI))
errx(1, "not a pci device");
// Reset device if supported
if (device_info.flags & VFIO_DEVICE_FLAGS_RESET) {
s = ioctl(v->device_fd, VFIO_DEVICE_RESET);
if (s)
err(1, "reset device: %d", s);
}
// Config region
v->config_region = (struct vfio_region_info){
.argsz = sizeof(v->bar0_region),
.index = VFIO_PCI_CONFIG_REGION_INDEX,
};
s = ioctl(v->device_fd, VFIO_DEVICE_GET_REGION_INFO, &v->config_region);
if (s)
err(1, "get config region info: %d", s);
if (!(v->config_region.flags & VFIO_REGION_INFO_FLAG_READ))
errx(1, "config region not readable");
if (!(v->config_region.flags & VFIO_REGION_INFO_FLAG_WRITE))
errx(1, "config region not writable");
printf("Config size: %lld, offset: %#llx\n", v->config_region.size,
v->config_region.offset);
}
// Setup a DMA mapping
void vfio_map_dma(struct vfio *v, void *bufs, size_t size, uint64_t iova)
{
int s;
assert(v);
assert(bufs);
assert(size);
assert(iova);
v->dma_map = (struct vfio_iommu_type1_dma_map){
.argsz = sizeof(v->dma_map),
.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
.iova = iova, /* Starting address from device view */
.size = size,
.vaddr = (__u64)bufs,
};
s = ioctl(v->container_fd, VFIO_IOMMU_MAP_DMA, &v->dma_map);
if (s)
err(1, "map dma: %d", s);
printf("Buffers @%p, size: %ld, iova: %#lx\n", bufs, size, iova);
}
void *vfio_map_bar0(struct vfio *v)
{
int s;
assert(v);
assert(!v->bar0);
v->bar0_region = (struct vfio_region_info){
.argsz = sizeof(v->bar0_region),
.index = VFIO_PCI_BAR0_REGION_INDEX,
};
s = ioctl(v->device_fd, VFIO_DEVICE_GET_REGION_INFO, &v->bar0_region);
if (s)
err(1, "get bar0 region info: %d", s);
if (!(v->bar0_region.flags & VFIO_REGION_INFO_FLAG_READ))
errx(1, "bar0 region not readable");
if (!(v->bar0_region.flags & VFIO_REGION_INFO_FLAG_WRITE))
errx(1, "bar0 region not writable");
if (!(v->bar0_region.flags & VFIO_REGION_INFO_FLAG_MMAP))
errx(1, "bar0 region not mmap'able");
v->bar0 = mmap(0, v->bar0_region.size, PROT_READ | PROT_WRITE,
MAP_SHARED, v->device_fd, v->bar0_region.offset);
if (v->bar0 == MAP_FAILED)
err(1, "mmap bar0 failed");
printf("BAR0 @%p, size: %lld, offset: %#llx\n", v->bar0,
v->bar0_region.size, v->bar0_region.offset);
return v->bar0;
}
uint8_t vfio_read_config(struct vfio *v, unsigned int off)
{
ssize_t s;
uint8_t r;
off_t o;
assert(v);
o = v->config_region.offset + off;
s = pread(v->device_fd, &r, 1, o);
if (s != 1)
err(1, "config read: %ld", s);
return r;
}
void vfio_write_config(struct vfio *v, unsigned int off, uint8_t val)
{
ssize_t s;
off_t o;
assert(v);
o = v->config_region.offset + off;
s = pwrite(v->device_fd, &val, 1, o);
if (s != 1)
err(1, "config write: %ld", s);
}
void vfio_teardown(struct vfio *v)
{
int s;
struct vfio_iommu_type1_dma_unmap dma_unmap = {
.argsz = sizeof(dma_unmap),
.flags = 0,
.iova = v->dma_map.iova,
.size = v->dma_map.size,
};
assert(v);
if (v->bar0) {
s = munmap(v->bar0, v->bar0_region.size);
if (s)
warn("munmap bar0: %d", s);
}
if (v->dma_map.size) {
s = ioctl(v->container_fd, VFIO_IOMMU_UNMAP_DMA, &dma_unmap);
if (s)
warn("unmap dma: %d", s);
}
s = close(v->device_fd);
if (s)
warn("close device: %d", s);
s = ioctl(v->group_fd, VFIO_GROUP_UNSET_CONTAINER);
if (s)
warn("unset container: %d", s);
s = close(v->group_fd);
if (s)
warn("close group: %d", s);
s = close(v->container_fd);
if (s)
warn("close container: %d", s);
#ifndef NDEBUG
memset(v, 0, sizeof(*v));
#endif
}