4 #include "x86.h" // inl
5 #include "biosvar.h" // GET_LOWFLAT
7 /* The bit of the ISR which indicates a device configuration change. */
8 #define VIRTIO_PCI_ISR_CONFIG 0x2
10 /* Virtio ABI version, this must match exactly */
11 #define VIRTIO_PCI_ABI_VERSION 0
13 /* --- virtio 0.9.5 (legacy) struct --------------------------------- */
15 typedef struct virtio_pci_legacy {
27 /* --- virtio 1.0 (modern) structs ---------------------------------- */
29 /* Common configuration */
30 #define VIRTIO_PCI_CAP_COMMON_CFG 1
32 #define VIRTIO_PCI_CAP_NOTIFY_CFG 2
34 #define VIRTIO_PCI_CAP_ISR_CFG 3
35 /* Device specific configuration */
36 #define VIRTIO_PCI_CAP_DEVICE_CFG 4
37 /* PCI configuration access */
38 #define VIRTIO_PCI_CAP_PCI_CFG 5
40 /* This is the PCI capability header: */
41 struct virtio_pci_cap {
42 u8 cap_vndr; /* Generic PCI field: PCI_CAP_ID_VNDR */
43 u8 cap_next; /* Generic PCI field: next ptr. */
44 u8 cap_len; /* Generic PCI field: capability length */
45 u8 cfg_type; /* Identifies the structure. */
46 u8 bar; /* Where to find it. */
47 u8 padding[3]; /* Pad to full dword. */
48 u32 offset; /* Offset within bar. */
49 u32 length; /* Length of the structure, in bytes. */
52 struct virtio_pci_notify_cap {
53 struct virtio_pci_cap cap;
54 u32 notify_off_multiplier; /* Multiplier for queue_notify_off. */
57 typedef struct virtio_pci_common_cfg {
58 /* About the whole device. */
59 u32 device_feature_select; /* read-write */
60 u32 device_feature; /* read-only */
61 u32 guest_feature_select; /* read-write */
62 u32 guest_feature; /* read-write */
63 u16 msix_config; /* read-write */
64 u16 num_queues; /* read-only */
65 u8 device_status; /* read-write */
66 u8 config_generation; /* read-only */
68 /* About a specific virtqueue. */
69 u16 queue_select; /* read-write */
70 u16 queue_size; /* read-write, power of 2. */
71 u16 queue_msix_vector; /* read-write */
72 u16 queue_enable; /* read-write */
73 u16 queue_notify_off; /* read-only */
74 u32 queue_desc_lo; /* read-write */
75 u32 queue_desc_hi; /* read-write */
76 u32 queue_avail_lo; /* read-write */
77 u32 queue_avail_hi; /* read-write */
78 u32 queue_used_lo; /* read-write */
79 u32 queue_used_hi; /* read-write */
80 } virtio_pci_common_cfg;
82 typedef struct virtio_pci_isr {
86 /* --- driver structs ----------------------------------------------- */
96 struct vp_cap common, notify, isr, device, legacy;
97 u32 notify_off_multiplier;
101 static inline u64 _vp_read(struct vp_cap *cap, u32 offset, u8 size)
103 u32 addr = cap->addr + offset;
110 var |= (u64)inl(addr+4) << 32;
127 var = readl((void*)addr);
128 var |= (u64)readl((void*)(addr+4)) << 32;
131 var = readl((void*)addr);
134 var = readw((void*)addr);
137 var = readb((void*)addr);
143 dprintf(9, "vp read %x (%d) -> 0x%llx\n", addr, size, var);
147 static inline void _vp_write(struct vp_cap *cap, u32 offset, u8 size, u64 var)
149 u32 addr = cap->addr + offset;
151 dprintf(9, "vp write %x (%d) <- 0x%llx\n", addr, size, var);
167 writel((void*)addr, var);
170 writew((void*)addr, var);
173 writeb((void*)addr, var);
179 #define vp_read(_cap, _struct, _field) \
180 _vp_read(_cap, offsetof(_struct, _field), \
181 sizeof(((_struct *)0)->_field))
183 #define vp_write(_cap, _struct, _field, _var) \
184 _vp_write(_cap, offsetof(_struct, _field), \
185 sizeof(((_struct *)0)->_field), _var)
187 u64 vp_get_features(struct vp_device *vp);
188 void vp_set_features(struct vp_device *vp, u64 features);
190 static inline void vp_get_legacy(struct vp_device *vp, unsigned offset,
191 void *buf, unsigned len)
196 for (i = 0; i < len; i++)
197 ptr[i] = vp_read(&vp->legacy, virtio_pci_legacy, device[i]);
200 u8 vp_get_status(struct vp_device *vp);
201 void vp_set_status(struct vp_device *vp, u8 status);
202 u8 vp_get_isr(struct vp_device *vp);
203 void vp_reset(struct vp_device *vp);
206 struct vring_virtqueue;
207 void vp_init_simple(struct vp_device *vp, struct pci_device *pci);
208 void vp_notify(struct vp_device *vp, struct vring_virtqueue *vq);
209 int vp_find_vq(struct vp_device *vp, int queue_index,
210 struct vring_virtqueue **p_vq);
211 #endif /* _VIRTIO_PCI_H_ */