1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (c) 2020, Red Hat Inc. All rights reserved.
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>
15 #define VDPASIM_FEATURES ((1ULL << VIRTIO_F_ANY_LAYOUT) | \
16 (1ULL << VIRTIO_F_VERSION_1) | \
17 (1ULL << VIRTIO_F_ACCESS_PLATFORM))
21 struct vdpasim_virtqueue
{
23 struct vringh_kiov in_iov
;
24 struct vringh_kiov out_iov
;
32 irqreturn_t (*cb
)(void *data
);
35 struct vdpasim_dev_attr
{
36 u64 supported_features
;
43 void (*get_config
)(struct vdpasim
*vdpasim
, void *config
);
44 void (*set_config
)(struct vdpasim
*vdpasim
, const void *config
);
47 /* State of each vdpasim device */
49 struct vdpa_device vdpa
;
50 struct vdpasim_virtqueue
*vqs
;
51 struct work_struct work
;
52 struct vdpasim_dev_attr dev_attr
;
53 /* spinlock to synchronize virtqueue state */
55 /* virtio config according to device type */
57 struct vhost_iotlb
*iommu
;
62 /* spinlock to synchronize iommu table */
63 spinlock_t iommu_lock
;
66 struct vdpasim
*vdpasim_create(struct vdpasim_dev_attr
*attr
);
68 /* TODO: cross-endian support */
69 static inline bool vdpasim_is_little_endian(struct vdpasim
*vdpasim
)
71 return virtio_legacy_is_little_endian() ||
72 (vdpasim
->features
& (1ULL << VIRTIO_F_VERSION_1
));
75 static inline u16
vdpasim16_to_cpu(struct vdpasim
*vdpasim
, __virtio16 val
)
77 return __virtio16_to_cpu(vdpasim_is_little_endian(vdpasim
), val
);
80 static inline __virtio16
cpu_to_vdpasim16(struct vdpasim
*vdpasim
, u16 val
)
82 return __cpu_to_virtio16(vdpasim_is_little_endian(vdpasim
), val
);
85 static inline u32
vdpasim32_to_cpu(struct vdpasim
*vdpasim
, __virtio32 val
)
87 return __virtio32_to_cpu(vdpasim_is_little_endian(vdpasim
), val
);
90 static inline __virtio32
cpu_to_vdpasim32(struct vdpasim
*vdpasim
, u32 val
)
92 return __cpu_to_virtio32(vdpasim_is_little_endian(vdpasim
), val
);
95 static inline u64
vdpasim64_to_cpu(struct vdpasim
*vdpasim
, __virtio64 val
)
97 return __virtio64_to_cpu(vdpasim_is_little_endian(vdpasim
), val
);
100 static inline __virtio64
cpu_to_vdpasim64(struct vdpasim
*vdpasim
, u64 val
)
102 return __cpu_to_virtio64(vdpasim_is_little_endian(vdpasim
), val
);