aboutsummaryrefslogtreecommitdiff
path: root/api/vfio_api.c
blob: c24b660b3eead6d34e45941d70a3aff0f322b432 (plain)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <linux/vfio.h>
#include <stddef.h>
#include <stdlib.h>

#include <common.h>
#include <uapi/net_mdev.h>

#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
static const char *vfio_fail_str[] = {
	[VFIO_CHECK_EXTENSION] = "Doesn't support the IOMMU driver we want",
	[VFIO_GROUP_GET_STATUS] = "Can't get status",
	[VFIO_GROUP_SET_CONTAINER] = "Failed to set container",
	[VFIO_SET_IOMMU] "Failed to set IOMMU",
	[VFIO_IOMMU_GET_INFO] = "Failed to get IOMMU info",
	[VFIO_GROUP_GET_DEVICE_FD] = "Failed to get device FD",
	[VFIO_DEVICE_GET_INFO] = "Failed to get device info",
	[VFIO_DEVICE_GET_REGION_INFO] = "Failed to get PCI region info",
};

static const struct cap_to_type_subtype {
	__u32 type;
	__u32 subtype;
} tmatch[] = {
	[VFIO_NET_MDEV_RX_REGION_INDEX] = { VFIO_NET_DESCRIPTORS, VFIO_NET_MDEV_RX },
	[VFIO_NET_MDEV_TX_REGION_INDEX] = { VFIO_NET_DESCRIPTORS, VFIO_NET_MDEV_TX },
};

static void vfio_print_fail(int reason)
{
	if (reason > ARRAY_SIZE(vfio_fail_str))
		printf("Unknown\n");
	else
		printf("%s\n", vfio_fail_str[reason]);
}

/**
 * returns a valid VFIO container
 * fd must be closed by caller
 */
int get_container(void)
{
	int ret;
	int container;
	/* Create a new container */
	container = open("/dev/vfio/vfio", O_RDWR);

	if (container < 0)
		return container;

	ret = ioctl(container, VFIO_GET_API_VERSION);
	if (ret != VFIO_API_VERSION) {
		printf("Unknown API version\n");
		goto out;
	}

	if (!ioctl(container, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU)) {
		printf("Doesn't support the IOMMU driver we want\n");
		goto out;
	}

	return container;
out:
	close(container);
	container = -1;
	return ret;

}

/**
 * returns a valid VFIO group
 * fd must be close by caller
 */
int get_group(int grp_id)
{
	char path[64];
	int ret;
	int group;
	struct vfio_group_status group_status = { .argsz = sizeof(group_status) };

	snprintf(path, sizeof(path), "/dev/vfio/%d", grp_id);
	group = open(path, O_RDWR);
	if (group < 0) {
		printf("Failed to open %s, %d (%s)\n",
		       path, group, strerror(errno));
		return group;
	}

	ret = ioctl(group, VFIO_GROUP_GET_STATUS, &group_status);

	if (ret) {
		printf("ioctl(VFIO_GROUP_GET_STATUS) failed\n");
		goto out;
	}

	/* Test the group is viable and available */
	if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
		printf("Group is not viable\n");
		goto out;
	}

	return group;
out:
	close(group);
	group = -1;
	return ret;
}

/**
 * @fd: container fd
 * @sz: requested size
 * @vaddr: virtual address
 */
int dma_map_type1(int fd, unsigned long sz, void **vaddr, uint64_t iova)
{
	int ret;
	struct vfio_iommu_type1_dma_map dma_map;

	/* Allocate some space and setup a DMA mapping */
	*vaddr = mmap(NULL, (size_t)sz, PROT_READ | PROT_WRITE,
		       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
	if (*vaddr == MAP_FAILED) {
		printf("Failed to map memory\n");
		return -ENOMEM;
	}

	memset(&dma_map, 0, sizeof(dma_map));
	dma_map.argsz = sizeof(dma_map);
	dma_map.vaddr = (unsigned long)*vaddr;
	dma_map.size = sz;
	dma_map.iova = iova;
	dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;

	ret = ioctl(fd, VFIO_IOMMU_MAP_DMA, &dma_map);
	if (ret)
		printf("Failed to map DMA memory (%s)\n", strerror(errno));

	return ret;
}

int dma_unmap_type1(int fd, unsigned long sz, void *vaddr, uint64_t iova)
{
	int ret;
	struct vfio_iommu_type1_dma_unmap dma_unmap;

	memset(&dma_unmap, 0, sizeof(dma_unmap));
	dma_unmap.argsz = sizeof(dma_unmap);
	dma_unmap.size = sz;
	dma_unmap.iova = iova;
	ret = ioctl(fd, VFIO_IOMMU_UNMAP_DMA, &dma_unmap);
	if (ret)
		printf("Failed to unmap DMA memory (%s)\n", strerror(errno));

	ret = munmap(vaddr, (size_t)sz);
	if (vaddr == MAP_FAILED) {
		printf("Failed to unmap memory\n");
		return -ENOMEM;
	}

	return ret;
}

static int vfio_match_caps(struct vfio_info_cap_header *hdr, __u32 type,
			   __u32 subtype)
{
	struct vfio_region_info_cap_type *cap_type;

	cap_type = container_of(hdr, struct vfio_region_info_cap_type, header);

	return !(cap_type->type == type && cap_type->subtype == subtype);
}

static struct vfio_info_cap_header *vfio_get_region_info_cap(struct vfio_region_info *info,
							     __u16 id)
{
	struct vfio_info_cap_header *hdr;
	void *ptr = info;

	if (!(info->flags & VFIO_REGION_INFO_FLAG_CAPS))
		return NULL;

	for (hdr = ptr + info->cap_offset; hdr != ptr; hdr = ptr + hdr->next) {
		if (hdr->id == id) {
			return hdr;
		}
	}

	return NULL;
}

/**
 * Get specific region info
 */
int vfio_get_region(int device, struct vfio_region_info *region_info,
		    __u32 region)
{
	int ret;
	struct vfio_info_cap_header *caps = NULL;
	struct vfio_region_info *info;
	int extra_region;

	/* Test and setup the device */
	region_info->index = region;
	extra_region = region - VFIO_PCI_NUM_REGIONS;

	ret = ioctl(device, VFIO_DEVICE_GET_REGION_INFO, region_info);
	printf("Region:%d ", region);
	if (ret) {
		vfio_print_fail(VFIO_DEVICE_GET_REGION_INFO);
		return ret;
	}

	if (!region_info->size) {
		printf("unimplemented PCI BAR\n");
		ret = -EINVAL;
	}

	if (region_info->flags & VFIO_REGION_INFO_FLAG_CAPS &&
			region_info->argsz > sizeof(*region_info)) {
		info = calloc(1, region_info->argsz);
		if (!info)
			return -EINVAL;
		memcpy(info, region_info, region_info->argsz);
		ret = ioctl(device, VFIO_DEVICE_GET_REGION_INFO, info);
		caps = vfio_get_region_info_cap(info, VFIO_REGION_INFO_CAP_TYPE);
		if (!caps)
			return -EINVAL;
		free(info);
		info = NULL;
	}
	if (caps)
		ret = vfio_match_caps(caps, tmatch[extra_region].type,
				      tmatch[extra_region].subtype);

	return ret;
}

/**
 * mmap a PCI region
 */
void *vfio_mmap_region(int device, __u32 region)
{
	int ret;
	struct vfio_region_info region_info = { .argsz = sizeof(region_info) };
	void *mapped;

	ret = vfio_get_region(device, &region_info, region);
	/* api returns -EINVAL for unimplemented bars */
	if (!region_info.size || ret)
		return NULL;

	printf("region:%d size %lu, offset 0x%lx, flags 0x%x\n", region,
	       (unsigned long)region_info.size,
	       (unsigned long)region_info.offset, region_info.flags);
	if (!(region_info.flags & VFIO_REGION_INFO_FLAG_MMAP))
		return NULL;

	mapped = mmap(NULL, region_info.size, PROT_READ | PROT_WRITE,
		      MAP_SHARED, device, region_info.offset);
	if (mapped == MAP_FAILED) {
		printf("mmap failed\n");
		return NULL;
	}

	return mapped;
}

/**
 * Initialize VFIO variables.
 * set IOMMU and get device regions
 */
int vfio_init_dev(int grp, int container, struct vfio_group_status *grp_status,
		  struct vfio_iommu_type1_info *iommu_info,
		  struct vfio_device_info *dev_info, char *grp_uuid)
{
	int ret;
	int device = -1;

	ret = ioctl(container, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU);
	if (!ret) {
		vfio_print_fail(VFIO_CHECK_EXTENSION);
		goto out;
	}

	/* Test the group is viable and available */
	ret = ioctl(grp, VFIO_GROUP_GET_STATUS, grp_status);
	if (ret || !(grp_status->flags & VFIO_GROUP_FLAGS_VIABLE)) {
		vfio_print_fail(VFIO_GROUP_GET_STATUS);
		goto out;

	}

	ret = ioctl(grp, VFIO_GROUP_SET_CONTAINER, &container);
	if (ret) {
		vfio_print_fail(VFIO_GROUP_SET_CONTAINER);
		printf("Failed to set group container\n");
		goto out;
	}

	ret = ioctl(container, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU);
	if (ret) {
		vfio_print_fail(VFIO_SET_IOMMU);
		goto out;
	}

	ret = ioctl(container, VFIO_IOMMU_GET_INFO, iommu_info);
	if (ret) {
		vfio_print_fail(VFIO_IOMMU_GET_INFO);
		goto out;
	}

	printf("iova_pgsizes bitmask=0x%llx\n", iommu_info->iova_pgsizes);
	/* Get a file descriptor for the device */
	device = ioctl(grp, VFIO_GROUP_GET_DEVICE_FD, grp_uuid);
	if (device < 0) {
		vfio_print_fail(VFIO_GROUP_GET_DEVICE_FD);
		goto out;
	}

	/* Test and setup the device */
	ret = ioctl(device, VFIO_DEVICE_GET_INFO, dev_info);
	if (ret) {
		vfio_print_fail(VFIO_DEVICE_GET_INFO);
		goto out;
	}

	printf("Device %d Regions: %d, irqs:%d\n", device,
	       dev_info->num_regions, dev_info->num_irqs);
out:
	return device;
}