1 /* 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * $FreeBSD$ 5 */ 6 7 #ifndef _VIRTIO_H_ 8 #define _VIRTIO_H_ 9 10 #include <openamp/virtqueue.h> 11 #include <metal/spinlock.h> 12 13 #if defined __cplusplus 14 extern "C" { 15 #endif 16 17 /* TODO: define this as compiler flags */ 18 #ifndef VIRTIO_MAX_NUM_VRINGS 19 #define VIRTIO_MAX_NUM_VRINGS 2 20 #endif 21 22 /* VirtIO device IDs. */ 23 #define VIRTIO_ID_NETWORK 0x01UL 24 #define VIRTIO_ID_BLOCK 0x02UL 25 #define VIRTIO_ID_CONSOLE 0x03UL 26 #define VIRTIO_ID_ENTROPY 0x04UL 27 #define VIRTIO_ID_BALLOON 0x05UL 28 #define VIRTIO_ID_IOMEMORY 0x06UL 29 #define VIRTIO_ID_RPMSG 0x07UL /* remote processor messaging */ 30 #define VIRTIO_ID_SCSI 0x08UL 31 #define VIRTIO_ID_9P 0x09UL 32 #define VIRTIO_DEV_ANY_ID (-1)UL 33 34 /* Status byte for guest to report progress. */ 35 #define VIRTIO_CONFIG_STATUS_ACK 0x01 36 #define VIRTIO_CONFIG_STATUS_DRIVER 0x02 37 #define VIRTIO_CONFIG_STATUS_DRIVER_OK 0x04 38 #define VIRTIO_CONFIG_STATUS_NEEDS_RESET 0x40 39 #define VIRTIO_CONFIG_STATUS_FAILED 0x80 40 41 /* Virtio device role */ 42 #define VIRTIO_DEV_MASTER 0UL 43 #define VIRTIO_DEV_SLAVE 1UL 44 45 struct virtio_device_id { 46 uint32_t device; 47 uint32_t vendor; 48 }; 49 50 /* 51 * Generate interrupt when the virtqueue ring is 52 * completely used, even if we've suppressed them. 53 */ 54 #define VIRTIO_F_NOTIFY_ON_EMPTY (1 << 24) 55 56 /* 57 * The guest should never negotiate this feature; it 58 * is used to detect faulty drivers. 59 */ 60 #define VIRTIO_F_BAD_FEATURE (1 << 30) 61 62 /* 63 * Some VirtIO feature bits (currently bits 28 through 31) are 64 * reserved for the transport being used (eg. virtio_ring), the 65 * rest are per-device feature bits. 66 */ 67 #define VIRTIO_TRANSPORT_F_START 28 68 #define VIRTIO_TRANSPORT_F_END 32 69 70 typedef void (*virtio_dev_reset_cb)(struct virtio_device *vdev); 71 72 struct virtio_dispatch; 73 74 struct virtio_feature_desc { 75 uint32_t vfd_val; 76 const char *vfd_str; 77 }; 78 79 /** 80 * struct proc_shm 81 * 82 * This structure is maintained by hardware interface layer for 83 * shared memory information. The shared memory provides buffers 84 * for use by the vring to exchange messages between the cores. 85 * 86 */ 87 struct virtio_buffer_info { 88 /* Start address of shared memory used for buffers. */ 89 void *vaddr; 90 /* Start physical address of shared memory used for buffers. */ 91 metal_phys_addr_t paddr; 92 /* sharmed memory I/O region */ 93 struct metal_io_region *io; 94 /* Size of shared memory. */ 95 unsigned long size; 96 }; 97 98 /** 99 * struct remoteproc_vring - remoteproc vring structure 100 * @vq virtio queue 101 * @va logical address 102 * @notifyid vring notify id 103 * @num_descs number of descriptors 104 * @align vring alignment 105 * @io metal I/O region of the vring memory, can be NULL 106 */ 107 struct virtio_vring_info { 108 struct virtqueue *vq; 109 struct vring_alloc_info info; 110 uint32_t notifyid; 111 struct metal_io_region *io; 112 }; 113 114 /* 115 * Structure definition for virtio devices for use by the 116 * applications/drivers 117 */ 118 119 struct virtio_device { 120 uint32_t index; /**< unique position on the virtio bus */ 121 struct virtio_device_id id; /**< the device type identification 122 * (used to match it with a driver 123 */ 124 uint64_t features; /**< the features supported by both ends. */ 125 unsigned int role; /**< if it is virtio backend or front end. */ 126 virtio_dev_reset_cb reset_cb; /**< user registered device callback */ 127 const struct virtio_dispatch *func; /**< Virtio dispatch table */ 128 void *priv; /**< TODO: remove pointer to virtio_device private data */ 129 unsigned int vrings_num; /**< number of vrings */ 130 struct virtio_vring_info *vrings_info; 131 }; 132 133 /* 134 * Helper functions. 135 */ 136 const char *virtio_dev_name(uint16_t devid); 137 void virtio_describe(struct virtio_device *dev, const char *msg, 138 uint32_t features, 139 struct virtio_feature_desc *feature_desc); 140 141 /* 142 * Functions for virtio device configuration as defined in Rusty Russell's 143 * paper. 144 * Drivers are expected to implement these functions in their respective codes. 145 */ 146 147 struct virtio_dispatch { 148 uint8_t (*get_status)(struct virtio_device *dev); 149 void (*set_status)(struct virtio_device *dev, uint8_t status); 150 uint32_t (*get_features)(struct virtio_device *dev); 151 void (*set_features)(struct virtio_device *dev, uint32_t feature); 152 uint32_t (*negotiate_features)(struct virtio_device *dev, 153 uint32_t features); 154 155 /* 156 * Read/write a variable amount from the device specific (ie, network) 157 * configuration region. This region is encoded in the same endian as 158 * the guest. 159 */ 160 void (*read_config)(struct virtio_device *dev, uint32_t offset, 161 void *dst, int length); 162 void (*write_config)(struct virtio_device *dev, uint32_t offset, 163 void *src, int length); 164 void (*reset_device)(struct virtio_device *dev); 165 void (*notify)(struct virtqueue *vq); 166 }; 167 168 int virtio_create_virtqueues(struct virtio_device *vdev, unsigned int flags, 169 unsigned int nvqs, const char *names[], 170 vq_callback *callbacks[]); 171 172 #if defined __cplusplus 173 } 174 #endif 175 176 #endif /* _VIRTIO_H_ */ 177