blob: 492e5c7c9166275788e77326bc65e18a6b7459ea [file] [log] [blame]
Gleb Natapov89acfa32010-05-10 11:36:37 +03001#ifndef _VIRTIO_PCI_H
2#define _VIRTIO_PCI_H
3
Kevin O'Connor4ade5232013-09-18 21:41:48 -04004#include "x86.h" // inl
Gerd Hoffmanndaf5cc92015-06-25 09:36:16 +02005#include "biosvar.h" // GET_LOWFLAT
Kevin O'Connor7d09d0e2010-05-10 21:51:38 -04006
Gleb Natapov89acfa32010-05-10 11:36:37 +03007/* The bit of the ISR which indicates a device configuration change. */
8#define VIRTIO_PCI_ISR_CONFIG 0x2
9
Gleb Natapov89acfa32010-05-10 11:36:37 +030010/* Virtio ABI version, this must match exactly */
11#define VIRTIO_PCI_ABI_VERSION 0
12
Gerd Hoffmannd00335e2015-06-30 08:42:50 +020013/* --- virtio 0.9.5 (legacy) struct --------------------------------- */
14
15typedef struct virtio_pci_legacy {
16 u32 host_features;
17 u32 guest_features;
18 u32 queue_pfn;
19 u16 queue_num;
20 u16 queue_sel;
21 u16 queue_notify;
22 u8 status;
23 u8 isr;
24 u8 device[];
25} virtio_pci_legacy;
26
Gerd Hoffmann90ac3d42015-06-25 16:13:51 +020027/* --- virtio 1.0 (modern) structs ---------------------------------- */
28
29/* Common configuration */
30#define VIRTIO_PCI_CAP_COMMON_CFG 1
31/* Notifications */
32#define VIRTIO_PCI_CAP_NOTIFY_CFG 2
33/* ISR access */
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
39
40/* This is the PCI capability header: */
41struct 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. */
50};
51
52struct virtio_pci_notify_cap {
53 struct virtio_pci_cap cap;
54 u32 notify_off_multiplier; /* Multiplier for queue_notify_off. */
55};
56
Gerd Hoffmann0e215482015-07-03 11:07:05 +020057struct virtio_pci_cfg_cap {
58 struct virtio_pci_cap cap;
59 u8 pci_cfg_data[4]; /* Data for BAR access. */
60};
61
Gerd Hoffmann90ac3d42015-06-25 16:13:51 +020062typedef struct virtio_pci_common_cfg {
63 /* About the whole device. */
64 u32 device_feature_select; /* read-write */
65 u32 device_feature; /* read-only */
66 u32 guest_feature_select; /* read-write */
67 u32 guest_feature; /* read-write */
68 u16 msix_config; /* read-write */
69 u16 num_queues; /* read-only */
70 u8 device_status; /* read-write */
71 u8 config_generation; /* read-only */
72
73 /* About a specific virtqueue. */
74 u16 queue_select; /* read-write */
75 u16 queue_size; /* read-write, power of 2. */
76 u16 queue_msix_vector; /* read-write */
77 u16 queue_enable; /* read-write */
78 u16 queue_notify_off; /* read-only */
79 u32 queue_desc_lo; /* read-write */
80 u32 queue_desc_hi; /* read-write */
81 u32 queue_avail_lo; /* read-write */
82 u32 queue_avail_hi; /* read-write */
83 u32 queue_used_lo; /* read-write */
84 u32 queue_used_hi; /* read-write */
85} virtio_pci_common_cfg;
86
87typedef struct virtio_pci_isr {
88 u8 isr;
89} virtio_pci_isr;
90
91/* --- driver structs ----------------------------------------------- */
92
Gerd Hoffmann0e215482015-07-03 11:07:05 +020093#define VP_ACCESS_IO 1
94#define VP_ACCESS_MMIO 2
95#define VP_ACCESS_PCICFG 3
96
Gerd Hoffmannd8966462015-06-25 16:14:21 +020097struct vp_cap {
Kevin O'Connorf46739b2016-02-02 22:34:27 -050098 union {
99 void *memaddr;
100 u32 ioaddr;
Gerd Hoffmann0e215482015-07-03 11:07:05 +0200101 u32 baroff;
Kevin O'Connorf46739b2016-02-02 22:34:27 -0500102 };
Gerd Hoffmann0e215482015-07-03 11:07:05 +0200103 u16 bdf;
Gerd Hoffmannd8966462015-06-25 16:14:21 +0200104 u8 cap;
Gerd Hoffmann0e215482015-07-03 11:07:05 +0200105 u8 cfg;
Gerd Hoffmannd8966462015-06-25 16:14:21 +0200106 u8 bar;
Gerd Hoffmann0e215482015-07-03 11:07:05 +0200107 u8 mode;
Gerd Hoffmannd8966462015-06-25 16:14:21 +0200108};
109
Gerd Hoffmanndaf5cc92015-06-25 09:36:16 +0200110struct vp_device {
Gerd Hoffmann0de78912015-06-30 08:50:09 +0200111 struct vp_cap common, notify, isr, device, legacy;
Gerd Hoffmann2e68fb12015-06-26 08:19:28 +0200112 u32 notify_off_multiplier;
Gerd Hoffmann46d17922015-06-25 12:28:46 +0200113 u8 use_modern;
Gerd Hoffmanndaf5cc92015-06-25 09:36:16 +0200114};
115
Gerd Hoffmannc579d2f2016-06-17 11:45:43 +0200116u64 _vp_read(struct vp_cap *cap, u32 offset, u8 size);
117void _vp_write(struct vp_cap *cap, u32 offset, u8 size, u64 var);
Gerd Hoffmann3bbc8492015-06-25 12:24:59 +0200118
119#define vp_read(_cap, _struct, _field) \
120 _vp_read(_cap, offsetof(_struct, _field), \
121 sizeof(((_struct *)0)->_field))
122
123#define vp_write(_cap, _struct, _field, _var) \
124 _vp_write(_cap, offsetof(_struct, _field), \
125 sizeof(((_struct *)0)->_field), _var)
126
Gerd Hoffmann46d17922015-06-25 12:28:46 +0200127u64 vp_get_features(struct vp_device *vp);
128void vp_set_features(struct vp_device *vp, u64 features);
Gleb Natapov89acfa32010-05-10 11:36:37 +0300129
Gerd Hoffmann6b67c622015-06-26 11:55:41 +0200130static inline void vp_get_legacy(struct vp_device *vp, unsigned offset,
131 void *buf, unsigned len)
Gleb Natapov89acfa32010-05-10 11:36:37 +0300132{
Gerd Hoffmann6b67c622015-06-26 11:55:41 +0200133 u8 *ptr = buf;
134 unsigned i;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300135
Gerd Hoffmann6b67c622015-06-26 11:55:41 +0200136 for (i = 0; i < len; i++)
Gerd Hoffmannfb752e12015-06-30 09:53:03 +0200137 ptr[i] = vp_read(&vp->legacy, virtio_pci_legacy, device[i]);
Gleb Natapov89acfa32010-05-10 11:36:37 +0300138}
139
Gerd Hoffmann6ee97762015-06-25 16:37:41 +0200140u8 vp_get_status(struct vp_device *vp);
141void vp_set_status(struct vp_device *vp, u8 status);
Gerd Hoffmannadeb2e32015-06-25 16:43:17 +0200142u8 vp_get_isr(struct vp_device *vp);
Gerd Hoffmann984db762015-06-25 16:44:06 +0200143void vp_reset(struct vp_device *vp);
Gleb Natapov89acfa32010-05-10 11:36:37 +0300144
Gerd Hoffmann74d0cdc2015-06-25 10:24:27 +0200145struct pci_device;
Kevin O'Connor7d09d0e2010-05-10 21:51:38 -0400146struct vring_virtqueue;
Gerd Hoffmann74d0cdc2015-06-25 10:24:27 +0200147void vp_init_simple(struct vp_device *vp, struct pci_device *pci);
Gerd Hoffmann2e68fb12015-06-26 08:19:28 +0200148void vp_notify(struct vp_device *vp, struct vring_virtqueue *vq);
Gerd Hoffmanndaf5cc92015-06-25 09:36:16 +0200149int vp_find_vq(struct vp_device *vp, int queue_index,
Paolo Bonzinie1a17bb2011-11-16 13:02:57 +0100150 struct vring_virtqueue **p_vq);
Gleb Natapov89acfa32010-05-10 11:36:37 +0300151#endif /* _VIRTIO_PCI_H_ */