OpenAMP Library
virtio.h
Go to the documentation of this file.
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 /* VirtIO device IDs. */
18 #define VIRTIO_ID_NETWORK 0x01UL
19 #define VIRTIO_ID_BLOCK 0x02UL
20 #define VIRTIO_ID_CONSOLE 0x03UL
21 #define VIRTIO_ID_ENTROPY 0x04UL
22 #define VIRTIO_ID_BALLOON 0x05UL
23 #define VIRTIO_ID_IOMEMORY 0x06UL
24 #define VIRTIO_ID_RPMSG 0x07UL /* remote processor messaging */
25 #define VIRTIO_ID_SCSI 0x08UL
26 #define VIRTIO_ID_9P 0x09UL
27 #define VIRTIO_DEV_ANY_ID (-1)UL
28 
29 /* Status byte for guest to report progress. */
30 #define VIRTIO_CONFIG_STATUS_ACK 0x01
31 #define VIRTIO_CONFIG_STATUS_DRIVER 0x02
32 #define VIRTIO_CONFIG_STATUS_DRIVER_OK 0x04
33 #define VIRTIO_CONFIG_STATUS_NEEDS_RESET 0x40
34 #define VIRTIO_CONFIG_STATUS_FAILED 0x80
35 
36 /* Virtio device role */
37 #define VIRTIO_DEV_DRIVER 0UL
38 #define VIRTIO_DEV_DEVICE 1UL
39 
40 #define VIRTIO_DEV_MASTER deprecated_virtio_dev_master()
41 #define VIRTIO_DEV_SLAVE deprecated_virtio_dev_slave()
42 
43 __deprecated static inline int deprecated_virtio_dev_master(void)
44 {
45  /* "VIRTIO_DEV_MASTER is deprecated, please use VIRTIO_DEV_DRIVER" */
46  return VIRTIO_DEV_DRIVER;
47 }
48 
49 __deprecated static inline int deprecated_virtio_dev_slave(void)
50 {
51  /* "VIRTIO_DEV_SLAVE is deprecated, please use VIRTIO_DEV_DEVICE" */
52  return VIRTIO_DEV_DEVICE;
53 }
54 
55 #ifdef VIRTIO_MASTER_ONLY
56 #define VIRTIO_DRIVER_ONLY
57 #warning "VIRTIO_MASTER_ONLY is deprecated, please use VIRTIO_DRIVER_ONLY"
58 #endif
59 
60 #ifdef VIRTIO_SLAVE_ONLY
61 #define VIRTIO_DEVICE_ONLY
62 #warning "VIRTIO_SLAVE_ONLY is deprecated, please use VIRTIO_DEVICE_ONLY"
63 #endif
64 
66  uint32_t device;
67  uint32_t vendor;
68 };
69 
70 /*
71  * Generate interrupt when the virtqueue ring is
72  * completely used, even if we've suppressed them.
73  */
74 #define VIRTIO_F_NOTIFY_ON_EMPTY (1 << 24)
75 
76 /*
77  * The guest should never negotiate this feature; it
78  * is used to detect faulty drivers.
79  */
80 #define VIRTIO_F_BAD_FEATURE (1 << 30)
81 
82 /*
83  * Some VirtIO feature bits (currently bits 28 through 31) are
84  * reserved for the transport being used (eg. virtio_ring), the
85  * rest are per-device feature bits.
86  */
87 #define VIRTIO_TRANSPORT_F_START 28
88 #define VIRTIO_TRANSPORT_F_END 32
89 
90 typedef void (*virtio_dev_reset_cb)(struct virtio_device *vdev);
91 
92 struct virtio_dispatch;
93 
95  uint32_t vfd_val;
96  const char *vfd_str;
97 };
98 
107  struct virtqueue *vq;
108  struct vring_alloc_info info;
109  uint32_t notifyid;
110  struct metal_io_region *io;
111 };
112 
113 /*
114  * Structure definition for virtio devices for use by the
115  * applications/drivers
116  */
117 
119  uint32_t notifyid;
120  struct virtio_device_id id;
123  uint64_t features;
124  unsigned int role;
126  const struct virtio_dispatch *func;
127  void *priv;
128  unsigned int vrings_num;
130 };
131 
132 /*
133  * Helper functions.
134  */
135 const char *virtio_dev_name(uint16_t devid);
136 void virtio_describe(struct virtio_device *dev, const char *msg,
137  uint32_t features,
138  struct virtio_feature_desc *feature_desc);
139 
140 /*
141  * Functions for virtio device configuration as defined in Rusty Russell's
142  * paper.
143  * Drivers are expected to implement these functions in their respective codes.
144  */
145 
147  uint8_t (*get_status)(struct virtio_device *dev);
148  void (*set_status)(struct virtio_device *dev, uint8_t status);
149  uint32_t (*get_features)(struct virtio_device *dev);
150  void (*set_features)(struct virtio_device *dev, uint32_t feature);
151  uint32_t (*negotiate_features)(struct virtio_device *dev,
152  uint32_t features);
153 
154  /*
155  * Read/write a variable amount from the device specific (ie, network)
156  * configuration region. This region is encoded in the same endian as
157  * the guest.
158  */
159  void (*read_config)(struct virtio_device *dev, uint32_t offset,
160  void *dst, int length);
161  void (*write_config)(struct virtio_device *dev, uint32_t offset,
162  void *src, int length);
163  void (*reset_device)(struct virtio_device *dev);
164  void (*notify)(struct virtqueue *vq);
165 };
166 
167 int virtio_create_virtqueues(struct virtio_device *vdev, unsigned int flags,
168  unsigned int nvqs, const char *names[],
169  vq_callback callbacks[]);
170 
171 #if defined __cplusplus
172 }
173 #endif
174 
175 #endif /* _VIRTIO_H_ */
Definition: virtio.h:65
uint32_t device
Definition: virtio.h:66
uint32_t vendor
Definition: virtio.h:67
Definition: virtio.h:118
unsigned int role
Definition: virtio.h:124
virtio_dev_reset_cb reset_cb
Definition: virtio.h:125
void * priv
Definition: virtio.h:127
struct virtio_vring_info * vrings_info
Definition: virtio.h:129
struct virtio_device_id id
Definition: virtio.h:120
uint32_t notifyid
Definition: virtio.h:119
const struct virtio_dispatch * func
Definition: virtio.h:126
unsigned int vrings_num
Definition: virtio.h:128
uint64_t features
Definition: virtio.h:123
Definition: virtio.h:146
void(* read_config)(struct virtio_device *dev, uint32_t offset, void *dst, int length)
Definition: virtio.h:159
void(* reset_device)(struct virtio_device *dev)
Definition: virtio.h:163
uint32_t(* negotiate_features)(struct virtio_device *dev, uint32_t features)
Definition: virtio.h:151
void(* notify)(struct virtqueue *vq)
Definition: virtio.h:164
uint8_t(* get_status)(struct virtio_device *dev)
Definition: virtio.h:147
void(* set_features)(struct virtio_device *dev, uint32_t feature)
Definition: virtio.h:150
uint32_t(* get_features)(struct virtio_device *dev)
Definition: virtio.h:149
void(* set_status)(struct virtio_device *dev, uint8_t status)
Definition: virtio.h:148
void(* write_config)(struct virtio_device *dev, uint32_t offset, void *src, int length)
Definition: virtio.h:161
Definition: virtio.h:94
const char * vfd_str
Definition: virtio.h:96
uint32_t vfd_val
Definition: virtio.h:95
Definition: virtio.h:106
struct vring_alloc_info info
Definition: virtio.h:108
uint32_t notifyid
Definition: virtio.h:109
struct virtqueue * vq
Definition: virtio.h:107
struct metal_io_region * io
Definition: virtio.h:110
Definition: virtqueue.h:60
Definition: virtqueue.h:105
void(* virtio_dev_reset_cb)(struct virtio_device *vdev)
Definition: virtio.h:90
#define VIRTIO_DEV_DRIVER
Definition: virtio.h:37
const char * virtio_dev_name(uint16_t devid)
static __deprecated int deprecated_virtio_dev_master(void)
Definition: virtio.h:43
int virtio_create_virtqueues(struct virtio_device *vdev, unsigned int flags, unsigned int nvqs, const char *names[], vq_callback callbacks[])
Definition: virtio.c:86
static __deprecated int deprecated_virtio_dev_slave(void)
Definition: virtio.h:49
#define VIRTIO_DEV_DEVICE
Definition: virtio.h:38
void virtio_describe(struct virtio_device *dev, const char *msg, uint32_t features, struct virtio_feature_desc *feature_desc)
Definition: virtio.c:75
void(* vq_callback)(struct virtqueue *)
Definition: virtqueue.h:112