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
;
47 void (*work_fn
)(struct vdpasim
*vdpasim
);
48 void (*get_config
)(struct vdpasim
*vdpasim
, void *config
);
49 void (*set_config
)(struct vdpasim
*vdpasim
, const void *config
);
50 int (*get_stats
)(struct vdpasim
*vdpasim
, u16 idx
,
52 struct netlink_ext_ack
*extack
);
53 void (*free
)(struct vdpasim
*vdpasim
);
56 /* State of each vdpasim device */
58 struct vdpa_device vdpa
;
59 struct vdpasim_virtqueue
*vqs
;
60 struct kthread_worker
*worker
;
61 struct kthread_work work
;
62 struct mm_struct
*mm_bound
;
63 struct vdpasim_dev_attr dev_attr
;
64 /* mutex to synchronize virtqueue state */
66 /* virtio config according to device type */
68 struct vhost_iotlb
*iommu
;
76 /* spinlock to synchronize iommu table */
77 spinlock_t iommu_lock
;
80 struct vdpasim
*vdpasim_create(struct vdpasim_dev_attr
*attr
,
81 const struct vdpa_dev_set_config
*config
);
82 void vdpasim_schedule_work(struct vdpasim
*vdpasim
);
84 /* TODO: cross-endian support */
85 static inline bool vdpasim_is_little_endian(struct vdpasim
*vdpasim
)
87 return virtio_legacy_is_little_endian() ||
88 (vdpasim
->features
& (1ULL << VIRTIO_F_VERSION_1
));
91 static inline u16
vdpasim16_to_cpu(struct vdpasim
*vdpasim
, __virtio16 val
)
93 return __virtio16_to_cpu(vdpasim_is_little_endian(vdpasim
), val
);
96 static inline __virtio16
cpu_to_vdpasim16(struct vdpasim
*vdpasim
, u16 val
)
98 return __cpu_to_virtio16(vdpasim_is_little_endian(vdpasim
), val
);
101 static inline u32
vdpasim32_to_cpu(struct vdpasim
*vdpasim
, __virtio32 val
)
103 return __virtio32_to_cpu(vdpasim_is_little_endian(vdpasim
), val
);
106 static inline __virtio32
cpu_to_vdpasim32(struct vdpasim
*vdpasim
, u32 val
)
108 return __cpu_to_virtio32(vdpasim_is_little_endian(vdpasim
), val
);
111 static inline u64
vdpasim64_to_cpu(struct vdpasim
*vdpasim
, __virtio64 val
)
113 return __virtio64_to_cpu(vdpasim_is_little_endian(vdpasim
), val
);
116 static inline __virtio64
cpu_to_vdpasim64(struct vdpasim
*vdpasim
, u64 val
)
118 return __cpu_to_virtio64(vdpasim_is_little_endian(vdpasim
), val
);