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 u64 supported_features; 37 size_t config_size; 38 size_t buffer_size; 39 int nvqs; 40 u32 id; 41 42 work_func_t work_fn; 43 void (*get_config)(struct vdpasim *vdpasim, void *config); 44 void (*set_config)(struct vdpasim *vdpasim, const void *config); 45 }; 46 47 /* State of each vdpasim device */ 48 struct vdpasim { 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 */ 54 spinlock_t lock; 55 /* virtio config according to device type */ 56 void *config; 57 struct vhost_iotlb *iommu; 58 void *buffer; 59 u32 status; 60 u32 generation; 61 u64 features; 62 /* spinlock to synchronize iommu table */ 63 spinlock_t iommu_lock; 64 }; 65 66 struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *attr); 67 68 /* TODO: cross-endian support */ 69 static inline bool vdpasim_is_little_endian(struct vdpasim *vdpasim) 70 { 71 return virtio_legacy_is_little_endian() || 72 (vdpasim->features & (1ULL << VIRTIO_F_VERSION_1)); 73 } 74 75 static inline u16 vdpasim16_to_cpu(struct vdpasim *vdpasim, __virtio16 val) 76 { 77 return __virtio16_to_cpu(vdpasim_is_little_endian(vdpasim), val); 78 } 79 80 static inline __virtio16 cpu_to_vdpasim16(struct vdpasim *vdpasim, u16 val) 81 { 82 return __cpu_to_virtio16(vdpasim_is_little_endian(vdpasim), val); 83 } 84 85 static inline u32 vdpasim32_to_cpu(struct vdpasim *vdpasim, __virtio32 val) 86 { 87 return __virtio32_to_cpu(vdpasim_is_little_endian(vdpasim), val); 88 } 89 90 static inline __virtio32 cpu_to_vdpasim32(struct vdpasim *vdpasim, u32 val) 91 { 92 return __cpu_to_virtio32(vdpasim_is_little_endian(vdpasim), val); 93 } 94 95 static inline u64 vdpasim64_to_cpu(struct vdpasim *vdpasim, __virtio64 val) 96 { 97 return __virtio64_to_cpu(vdpasim_is_little_endian(vdpasim), val); 98 } 99 100 static inline __virtio64 cpu_to_vdpasim64(struct vdpasim *vdpasim, u64 val) 101 { 102 return __cpu_to_virtio64(vdpasim_is_little_endian(vdpasim), val); 103 } 104 105 #endif 106