summaryrefslogtreecommitdiffstats
path: root/drivers/vdpa/vdpa_sim/vdpa_sim.h
blob: b02142293d5bc4cfe2fe4a2b32eb0726f42de369 (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
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Copyright (c) 2020, Red Hat Inc. All rights reserved.
 */

#ifndef _VDPA_SIM_H
#define _VDPA_SIM_H

#include <linux/vringh.h>
#include <linux/vdpa.h>
#include <linux/virtio_byteorder.h>
#include <linux/vhost_iotlb.h>
#include <uapi/linux/virtio_config.h>

#define VDPASIM_FEATURES	((1ULL << VIRTIO_F_ANY_LAYOUT) | \
				 (1ULL << VIRTIO_F_VERSION_1)  | \
				 (1ULL << VIRTIO_F_ACCESS_PLATFORM))

struct vdpasim;

struct vdpasim_virtqueue {
	struct vringh vring;
	struct vringh_kiov in_iov;
	struct vringh_kiov out_iov;
	unsigned short head;
	bool ready;
	u64 desc_addr;
	u64 device_addr;
	u64 driver_addr;
	u32 num;
	void *private;
	irqreturn_t (*cb)(void *data);
};

struct vdpasim_dev_attr {
	u64 supported_features;
	size_t config_size;
	size_t buffer_size;
	int nvqs;
	u32 id;

	work_func_t work_fn;
	void (*get_config)(struct vdpasim *vdpasim, void *config);
	void (*set_config)(struct vdpasim *vdpasim, const void *config);
};

/* State of each vdpasim device */
struct vdpasim {
	struct vdpa_device vdpa;
	struct vdpasim_virtqueue *vqs;
	struct work_struct work;
	struct vdpasim_dev_attr dev_attr;
	/* spinlock to synchronize virtqueue state */
	spinlock_t lock;
	/* virtio config according to device type */
	void *config;
	struct vhost_iotlb *iommu;
	void *buffer;
	u32 status;
	u32 generation;
	u64 features;
	/* spinlock to synchronize iommu table */
	spinlock_t iommu_lock;
};

struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *attr);

/* TODO: cross-endian support */
static inline bool vdpasim_is_little_endian(struct vdpasim *vdpasim)
{
	return virtio_legacy_is_little_endian() ||
		(vdpasim->features & (1ULL << VIRTIO_F_VERSION_1));
}

static inline u16 vdpasim16_to_cpu(struct vdpasim *vdpasim, __virtio16 val)
{
	return __virtio16_to_cpu(vdpasim_is_little_endian(vdpasim), val);
}

static inline __virtio16 cpu_to_vdpasim16(struct vdpasim *vdpasim, u16 val)
{
	return __cpu_to_virtio16(vdpasim_is_little_endian(vdpasim), val);
}

static inline u32 vdpasim32_to_cpu(struct vdpasim *vdpasim, __virtio32 val)
{
	return __virtio32_to_cpu(vdpasim_is_little_endian(vdpasim), val);
}

static inline __virtio32 cpu_to_vdpasim32(struct vdpasim *vdpasim, u32 val)
{
	return __cpu_to_virtio32(vdpasim_is_little_endian(vdpasim), val);
}

static inline u64 vdpasim64_to_cpu(struct vdpasim *vdpasim, __virtio64 val)
{
	return __virtio64_to_cpu(vdpasim_is_little_endian(vdpasim), val);
}

static inline __virtio64 cpu_to_vdpasim64(struct vdpasim *vdpasim, u64 val)
{
	return __cpu_to_virtio64(vdpasim_is_little_endian(vdpasim), val);
}

#endif