blob: 70b40f72424e47ce1e98af4d1323c7fa8c654654 [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
57typedef 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 */
67
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;
81
82typedef struct virtio_pci_isr {
83 u8 isr;
84} virtio_pci_isr;
85
86/* --- driver structs ----------------------------------------------- */
87
Gerd Hoffmannd8966462015-06-25 16:14:21 +020088struct vp_cap {
Kevin O'Connorf46739b2016-02-02 22:34:27 -050089 union {
90 void *memaddr;
91 u32 ioaddr;
92 };
Gerd Hoffmannd8966462015-06-25 16:14:21 +020093 u8 cap;
94 u8 bar;
95 u8 is_io;
96};
97
Gerd Hoffmanndaf5cc92015-06-25 09:36:16 +020098struct vp_device {
Gerd Hoffmann0de78912015-06-30 08:50:09 +020099 struct vp_cap common, notify, isr, device, legacy;
Gerd Hoffmann2e68fb12015-06-26 08:19:28 +0200100 u32 notify_off_multiplier;
Gerd Hoffmann46d17922015-06-25 12:28:46 +0200101 u8 use_modern;
Gerd Hoffmanndaf5cc92015-06-25 09:36:16 +0200102};
103
Gerd Hoffmannc579d2f2016-06-17 11:45:43 +0200104u64 _vp_read(struct vp_cap *cap, u32 offset, u8 size);
105void _vp_write(struct vp_cap *cap, u32 offset, u8 size, u64 var);
Gerd Hoffmann3bbc8492015-06-25 12:24:59 +0200106
107#define vp_read(_cap, _struct, _field) \
108 _vp_read(_cap, offsetof(_struct, _field), \
109 sizeof(((_struct *)0)->_field))
110
111#define vp_write(_cap, _struct, _field, _var) \
112 _vp_write(_cap, offsetof(_struct, _field), \
113 sizeof(((_struct *)0)->_field), _var)
114
Gerd Hoffmann46d17922015-06-25 12:28:46 +0200115u64 vp_get_features(struct vp_device *vp);
116void vp_set_features(struct vp_device *vp, u64 features);
Gleb Natapov89acfa32010-05-10 11:36:37 +0300117
Gerd Hoffmann6b67c622015-06-26 11:55:41 +0200118static inline void vp_get_legacy(struct vp_device *vp, unsigned offset,
119 void *buf, unsigned len)
Gleb Natapov89acfa32010-05-10 11:36:37 +0300120{
Gerd Hoffmann6b67c622015-06-26 11:55:41 +0200121 u8 *ptr = buf;
122 unsigned i;
Gleb Natapov89acfa32010-05-10 11:36:37 +0300123
Gerd Hoffmann6b67c622015-06-26 11:55:41 +0200124 for (i = 0; i < len; i++)
Gerd Hoffmannfb752e12015-06-30 09:53:03 +0200125 ptr[i] = vp_read(&vp->legacy, virtio_pci_legacy, device[i]);
Gleb Natapov89acfa32010-05-10 11:36:37 +0300126}
127
Gerd Hoffmann6ee97762015-06-25 16:37:41 +0200128u8 vp_get_status(struct vp_device *vp);
129void vp_set_status(struct vp_device *vp, u8 status);
Gerd Hoffmannadeb2e32015-06-25 16:43:17 +0200130u8 vp_get_isr(struct vp_device *vp);
Gerd Hoffmann984db762015-06-25 16:44:06 +0200131void vp_reset(struct vp_device *vp);
Gleb Natapov89acfa32010-05-10 11:36:37 +0300132
Gerd Hoffmann74d0cdc2015-06-25 10:24:27 +0200133struct pci_device;
Kevin O'Connor7d09d0e2010-05-10 21:51:38 -0400134struct vring_virtqueue;
Gerd Hoffmann74d0cdc2015-06-25 10:24:27 +0200135void vp_init_simple(struct vp_device *vp, struct pci_device *pci);
Gerd Hoffmann2e68fb12015-06-26 08:19:28 +0200136void vp_notify(struct vp_device *vp, struct vring_virtqueue *vq);
Gerd Hoffmanndaf5cc92015-06-25 09:36:16 +0200137int vp_find_vq(struct vp_device *vp, int queue_index,
Paolo Bonzinie1a17bb2011-11-16 13:02:57 +0100138 struct vring_virtqueue **p_vq);
Gleb Natapov89acfa32010-05-10 11:36:37 +0300139#endif /* _VIRTIO_PCI_H_ */