1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (c) 2020, Red Hat Inc. All rights reserved.
9 #include <linux/iova.h>
10 #include <linux/vringh.h>
11 #include <linux/vdpa.h>
12 #include <linux/virtio_byteorder.h>
13 #include <linux/vhost_iotlb.h>
14 #include <uapi/linux/virtio_config.h>
16 #define VDPASIM_FEATURES ((1ULL << VIRTIO_F_ANY_LAYOUT) | \
17 (1ULL << VIRTIO_F_VERSION_1) | \
18 (1ULL << VIRTIO_F_ACCESS_PLATFORM))
22 struct vdpasim_virtqueue {
24 struct vringh_kiov in_iov;
25 struct vringh_kiov out_iov;
33 irqreturn_t (*cb)(void *data);
36 struct vdpasim_dev_attr {
37 struct vdpa_mgmt_dev *mgmt_dev;
39 u64 supported_features;
48 void (*get_config)(struct vdpasim *vdpasim, void *config);
49 void (*set_config)(struct vdpasim *vdpasim, const void *config);
52 /* State of each vdpasim device */
54 struct vdpa_device vdpa;
55 struct vdpasim_virtqueue *vqs;
56 struct work_struct work;
57 struct vdpasim_dev_attr dev_attr;
58 /* spinlock to synchronize virtqueue state */
60 /* virtio config according to device type */
62 struct vhost_iotlb *iommu;
63 struct iova_domain iova;
70 /* spinlock to synchronize iommu table */
71 spinlock_t iommu_lock;
74 struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *attr,
75 const struct vdpa_dev_set_config *config);
77 /* TODO: cross-endian support */
78 static inline bool vdpasim_is_little_endian(struct vdpasim *vdpasim)
80 return virtio_legacy_is_little_endian() ||
81 (vdpasim->features & (1ULL << VIRTIO_F_VERSION_1));
84 static inline u16 vdpasim16_to_cpu(struct vdpasim *vdpasim, __virtio16 val)
86 return __virtio16_to_cpu(vdpasim_is_little_endian(vdpasim), val);
89 static inline __virtio16 cpu_to_vdpasim16(struct vdpasim *vdpasim, u16 val)
91 return __cpu_to_virtio16(vdpasim_is_little_endian(vdpasim), val);
94 static inline u32 vdpasim32_to_cpu(struct vdpasim *vdpasim, __virtio32 val)
96 return __virtio32_to_cpu(vdpasim_is_little_endian(vdpasim), val);
99 static inline __virtio32 cpu_to_vdpasim32(struct vdpasim *vdpasim, u32 val)
101 return __cpu_to_virtio32(vdpasim_is_little_endian(vdpasim), val);
104 static inline u64 vdpasim64_to_cpu(struct vdpasim *vdpasim, __virtio64 val)
106 return __virtio64_to_cpu(vdpasim_is_little_endian(vdpasim), val);
109 static inline __virtio64 cpu_to_vdpasim64(struct vdpasim *vdpasim, u64 val)
111 return __cpu_to_virtio64(vdpasim_is_little_endian(vdpasim), val);