xref: /openbmc/linux/drivers/vdpa/vdpa_sim/vdpa_sim.h (revision 5497b23e)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2020, Red Hat Inc. All rights reserved.
4  */
5 
6 #ifndef _VDPA_SIM_H
7 #define _VDPA_SIM_H
8 
9 #include <linux/vringh.h>
10 #include <linux/vdpa.h>
11 #include <linux/virtio_byteorder.h>
12 #include <linux/vhost_iotlb.h>
13 #include <uapi/linux/virtio_config.h>
14 
15 #define VDPASIM_FEATURES	((1ULL << VIRTIO_F_ANY_LAYOUT) | \
16 				 (1ULL << VIRTIO_F_VERSION_1)  | \
17 				 (1ULL << VIRTIO_F_ACCESS_PLATFORM))
18 
19 struct vdpasim;
20 
21 struct vdpasim_virtqueue {
22 	struct vringh vring;
23 	struct vringh_kiov in_iov;
24 	struct vringh_kiov out_iov;
25 	unsigned short head;
26 	bool ready;
27 	u64 desc_addr;
28 	u64 device_addr;
29 	u64 driver_addr;
30 	u32 num;
31 	void *private;
32 	irqreturn_t (*cb)(void *data);
33 };
34 
35 struct vdpasim_dev_attr {
36 	struct vdpa_mgmt_dev *mgmt_dev;
37 	const char *name;
38 	u64 supported_features;
39 	size_t config_size;
40 	size_t buffer_size;
41 	int nvqs;
42 	u32 id;
43 
44 	work_func_t work_fn;
45 	void (*get_config)(struct vdpasim *vdpasim, void *config);
46 	void (*set_config)(struct vdpasim *vdpasim, const void *config);
47 };
48 
49 /* State of each vdpasim device */
50 struct vdpasim {
51 	struct vdpa_device vdpa;
52 	struct vdpasim_virtqueue *vqs;
53 	struct work_struct work;
54 	struct vdpasim_dev_attr dev_attr;
55 	/* spinlock to synchronize virtqueue state */
56 	spinlock_t lock;
57 	/* virtio config according to device type */
58 	void *config;
59 	struct vhost_iotlb *iommu;
60 	void *buffer;
61 	u32 status;
62 	u32 generation;
63 	u64 features;
64 	/* spinlock to synchronize iommu table */
65 	spinlock_t iommu_lock;
66 };
67 
68 struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *attr);
69 
70 /* TODO: cross-endian support */
71 static inline bool vdpasim_is_little_endian(struct vdpasim *vdpasim)
72 {
73 	return virtio_legacy_is_little_endian() ||
74 		(vdpasim->features & (1ULL << VIRTIO_F_VERSION_1));
75 }
76 
77 static inline u16 vdpasim16_to_cpu(struct vdpasim *vdpasim, __virtio16 val)
78 {
79 	return __virtio16_to_cpu(vdpasim_is_little_endian(vdpasim), val);
80 }
81 
82 static inline __virtio16 cpu_to_vdpasim16(struct vdpasim *vdpasim, u16 val)
83 {
84 	return __cpu_to_virtio16(vdpasim_is_little_endian(vdpasim), val);
85 }
86 
87 static inline u32 vdpasim32_to_cpu(struct vdpasim *vdpasim, __virtio32 val)
88 {
89 	return __virtio32_to_cpu(vdpasim_is_little_endian(vdpasim), val);
90 }
91 
92 static inline __virtio32 cpu_to_vdpasim32(struct vdpasim *vdpasim, u32 val)
93 {
94 	return __cpu_to_virtio32(vdpasim_is_little_endian(vdpasim), val);
95 }
96 
97 static inline u64 vdpasim64_to_cpu(struct vdpasim *vdpasim, __virtio64 val)
98 {
99 	return __virtio64_to_cpu(vdpasim_is_little_endian(vdpasim), val);
100 }
101 
102 static inline __virtio64 cpu_to_vdpasim64(struct vdpasim *vdpasim, u64 val)
103 {
104 	return __cpu_to_virtio64(vdpasim_is_little_endian(vdpasim), val);
105 }
106 
107 #endif
108