These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / seabios / src / hw / virtio-pci.h
1 #ifndef _VIRTIO_PCI_H
2 #define _VIRTIO_PCI_H
3
4 #include "x86.h" // inl
5 #include "biosvar.h" // GET_LOWFLAT
6
7 /* The bit of the ISR which indicates a device configuration change. */
8 #define VIRTIO_PCI_ISR_CONFIG           0x2
9
10 /* Virtio ABI version, this must match exactly */
11 #define VIRTIO_PCI_ABI_VERSION          0
12
13 /* --- virtio 0.9.5 (legacy) struct --------------------------------- */
14
15 typedef 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
27 /* --- 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: */
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. */
50 };
51
52 struct virtio_pci_notify_cap {
53     struct virtio_pci_cap cap;
54     u32 notify_off_multiplier;   /* Multiplier for queue_notify_off. */
55 };
56
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 */
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
82 typedef struct virtio_pci_isr {
83     u8 isr;
84 } virtio_pci_isr;
85
86 /* --- driver structs ----------------------------------------------- */
87
88 struct vp_cap {
89     u32 addr;
90     u8 cap;
91     u8 bar;
92     u8 is_io;
93 };
94
95 struct vp_device {
96     struct vp_cap common, notify, isr, device, legacy;
97     u32 notify_off_multiplier;
98     u8 use_modern;
99 };
100
101 static inline u64 _vp_read(struct vp_cap *cap, u32 offset, u8 size)
102 {
103     u32 addr = cap->addr + offset;
104     u64 var;
105
106     if (cap->is_io) {
107         switch (size) {
108         case 8:
109             var = inl(addr);
110             var |= (u64)inl(addr+4) << 32;
111             break;
112         case 4:
113             var = inl(addr);
114             break;
115         case 2:
116             var = inw(addr);
117             break;
118         case 1:
119             var = inb(addr);
120             break;
121         default:
122             var = 0;
123         }
124     } else {
125         switch (size) {
126         case 8:
127             var = readl((void*)addr);
128             var |= (u64)readl((void*)(addr+4)) << 32;
129             break;
130         case 4:
131             var = readl((void*)addr);
132             break;
133         case 2:
134             var = readw((void*)addr);
135             break;
136         case 1:
137             var = readb((void*)addr);
138             break;
139         default:
140             var = 0;
141         }
142     }
143     dprintf(9, "vp read   %x (%d) -> 0x%llx\n", addr, size, var);
144     return var;
145 }
146
147 static inline void _vp_write(struct vp_cap *cap, u32 offset, u8 size, u64 var)
148 {
149     u32 addr = cap->addr + offset;
150
151     dprintf(9, "vp write  %x (%d) <- 0x%llx\n", addr, size, var);
152     if (cap->is_io) {
153         switch (size) {
154         case 4:
155             outl(var, addr);
156             break;
157         case 2:
158             outw(var, addr);
159             break;
160         case 1:
161             outb(var, addr);
162             break;
163         }
164     } else {
165         switch (size) {
166         case 4:
167             writel((void*)addr, var);
168             break;
169         case 2:
170             writew((void*)addr, var);
171             break;
172         case 1:
173             writeb((void*)addr, var);
174             break;
175         }
176     }
177 }
178
179 #define vp_read(_cap, _struct, _field)        \
180     _vp_read(_cap, offsetof(_struct, _field), \
181              sizeof(((_struct *)0)->_field))
182
183 #define vp_write(_cap, _struct, _field, _var)           \
184     _vp_write(_cap, offsetof(_struct, _field),          \
185              sizeof(((_struct *)0)->_field), _var)
186
187 u64 vp_get_features(struct vp_device *vp);
188 void vp_set_features(struct vp_device *vp, u64 features);
189
190 static inline void vp_get_legacy(struct vp_device *vp, unsigned offset,
191                                  void *buf, unsigned len)
192 {
193     u8 *ptr = buf;
194     unsigned i;
195
196     for (i = 0; i < len; i++)
197         ptr[i] = vp_read(&vp->legacy, virtio_pci_legacy, device[i]);
198 }
199
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);
204
205 struct pci_device;
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_ */