| 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/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> |
| 15 |
|
| 16 |
#define VDPASIM_FEATURES ((1ULL << VIRTIO_F_ANY_LAYOUT) | \ |
| 17 |
(1ULL << VIRTIO_F_VERSION_1) | \ |
| 18 |
(1ULL << VIRTIO_F_ACCESS_PLATFORM)) |
| 19 |
|
| 20 |
struct vdpasim; |
| 21 |
|
| 22 |
struct vdpasim_virtqueue { |
| 23 |
struct vringh vring; |
| 24 |
struct vringh_kiov in_iov; |
| 25 |
struct vringh_kiov out_iov; |
| 26 |
unsigned short head; |
| 27 |
bool ready; |
| 28 |
u64 desc_addr; |
| 29 |
u64 device_addr; |
| 30 |
u64 driver_addr; |
| 31 |
u32 num; |
| 32 |
void *private; |
| 33 |
irqreturn_t (*cb)(void *data); |
| 34 |
}; |
| 35 |
|
| 36 |
struct vdpasim_dev_attr { |
| 37 |
struct vdpa_mgmt_dev *mgmt_dev; |
| 38 |
const char *name; |
| 39 |
u64 supported_features; |
| 40 |
size_t alloc_size; |
| 41 |
size_t config_size; |
| 42 |
int nvqs; |
| 43 |
u32 id; |
| 44 |
u32 ngroups; |
| 45 |
u32 nas; |
| 46 |
|
| 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, |
| 51 |
struct sk_buff *msg, |
| 52 |
struct netlink_ext_ack *extack); |
| 53 |
void (*free)(struct vdpasim *vdpasim); |
| 54 |
}; |
| 55 |
|
| 56 |
/* State of each vdpasim device */ |
| 57 |
struct vdpasim { |
| 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 */ |
| 65 |
struct mutex mutex; |
| 66 |
/* virtio config according to device type */ |
| 67 |
void *config; |
| 68 |
struct vhost_iotlb *iommu; |
| 69 |
bool *iommu_pt; |
| 70 |
u32 status; |
| 71 |
u32 generation; |
| 72 |
u64 features; |
| 73 |
u32 groups; |
| 74 |
bool running; |
| 75 |
bool pending_kick; |
| 76 |
/* spinlock to synchronize iommu table */ |
| 77 |
spinlock_t iommu_lock; |
| 78 |
}; |
| 79 |
|
| 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); |
| 83 |
|
| 84 |
/* TODO: cross-endian support */ |
| 85 |
static inline bool vdpasim_is_little_endian(struct vdpasim *vdpasim) |
| 86 |
{ |
| 87 |
return virtio_legacy_is_little_endian() || |
| 88 |
(vdpasim->features & (1ULL << VIRTIO_F_VERSION_1)); |
| 89 |
} |
| 90 |
|
| 91 |
static inline u16 vdpasim16_to_cpu(struct vdpasim *vdpasim, __virtio16 val) |
| 92 |
{ |
| 93 |
return __virtio16_to_cpu(vdpasim_is_little_endian(vdpasim), val); |
| 94 |
} |
| 95 |
|
| 96 |
static inline __virtio16 cpu_to_vdpasim16(struct vdpasim *vdpasim, u16 val) |
| 97 |
{ |
| 98 |
return __cpu_to_virtio16(vdpasim_is_little_endian(vdpasim), val); |
| 99 |
} |
| 100 |
|
| 101 |
static inline u32 vdpasim32_to_cpu(struct vdpasim *vdpasim, __virtio32 val) |
| 102 |
{ |
| 103 |
return __virtio32_to_cpu(vdpasim_is_little_endian(vdpasim), val); |
| 104 |
} |
| 105 |
|
| 106 |
static inline __virtio32 cpu_to_vdpasim32(struct vdpasim *vdpasim, u32 val) |
| 107 |
{ |
| 108 |
return __cpu_to_virtio32(vdpasim_is_little_endian(vdpasim), val); |
| 109 |
} |
| 110 |
|
| 111 |
static inline u64 vdpasim64_to_cpu(struct vdpasim *vdpasim, __virtio64 val) |
| 112 |
{ |
| 113 |
return __virtio64_to_cpu(vdpasim_is_little_endian(vdpasim), val); |
| 114 |
} |
| 115 |
|
| 116 |
static inline __virtio64 cpu_to_vdpasim64(struct vdpasim *vdpasim, u64 val) |
| 117 |
{ |
| 118 |
return __cpu_to_virtio64(vdpasim_is_little_endian(vdpasim), val); |
| 119 |
} |
| 120 |
|
| 121 |
#endif |