root / hw / virtio-pci.c @ 7fc2f2c0
History | View | Annotate | Download (16.9 kB)
1 | 53c25cea | Paul Brook | /*
|
---|---|---|---|
2 | 53c25cea | Paul Brook | * Virtio PCI Bindings
|
3 | 53c25cea | Paul Brook | *
|
4 | 53c25cea | Paul Brook | * Copyright IBM, Corp. 2007
|
5 | 53c25cea | Paul Brook | * Copyright (c) 2009 CodeSourcery
|
6 | 53c25cea | Paul Brook | *
|
7 | 53c25cea | Paul Brook | * Authors:
|
8 | 53c25cea | Paul Brook | * Anthony Liguori <aliguori@us.ibm.com>
|
9 | 53c25cea | Paul Brook | * Paul Brook <paul@codesourcery.com>
|
10 | 53c25cea | Paul Brook | *
|
11 | 53c25cea | Paul Brook | * This work is licensed under the terms of the GNU GPL, version 2. See
|
12 | 53c25cea | Paul Brook | * the COPYING file in the top-level directory.
|
13 | 53c25cea | Paul Brook | *
|
14 | 53c25cea | Paul Brook | */
|
15 | 53c25cea | Paul Brook | |
16 | 53c25cea | Paul Brook | #include <inttypes.h> |
17 | 53c25cea | Paul Brook | |
18 | 53c25cea | Paul Brook | #include "virtio.h" |
19 | 53c25cea | Paul Brook | #include "pci.h" |
20 | d176c495 | Gerd Hoffmann | #include "sysemu.h" |
21 | aba800a3 | Michael S. Tsirkin | #include "msix.h" |
22 | a1e0fea5 | Gerd Hoffmann | #include "net.h" |
23 | 53c25cea | Paul Brook | |
24 | 53c25cea | Paul Brook | /* from Linux's linux/virtio_pci.h */
|
25 | 53c25cea | Paul Brook | |
26 | 53c25cea | Paul Brook | /* A 32-bit r/o bitmask of the features supported by the host */
|
27 | 53c25cea | Paul Brook | #define VIRTIO_PCI_HOST_FEATURES 0 |
28 | 53c25cea | Paul Brook | |
29 | 53c25cea | Paul Brook | /* A 32-bit r/w bitmask of features activated by the guest */
|
30 | 53c25cea | Paul Brook | #define VIRTIO_PCI_GUEST_FEATURES 4 |
31 | 53c25cea | Paul Brook | |
32 | 53c25cea | Paul Brook | /* A 32-bit r/w PFN for the currently selected queue */
|
33 | 53c25cea | Paul Brook | #define VIRTIO_PCI_QUEUE_PFN 8 |
34 | 53c25cea | Paul Brook | |
35 | 53c25cea | Paul Brook | /* A 16-bit r/o queue size for the currently selected queue */
|
36 | 53c25cea | Paul Brook | #define VIRTIO_PCI_QUEUE_NUM 12 |
37 | 53c25cea | Paul Brook | |
38 | 53c25cea | Paul Brook | /* A 16-bit r/w queue selector */
|
39 | 53c25cea | Paul Brook | #define VIRTIO_PCI_QUEUE_SEL 14 |
40 | 53c25cea | Paul Brook | |
41 | 53c25cea | Paul Brook | /* A 16-bit r/w queue notifier */
|
42 | 53c25cea | Paul Brook | #define VIRTIO_PCI_QUEUE_NOTIFY 16 |
43 | 53c25cea | Paul Brook | |
44 | 53c25cea | Paul Brook | /* An 8-bit device status register. */
|
45 | 53c25cea | Paul Brook | #define VIRTIO_PCI_STATUS 18 |
46 | 53c25cea | Paul Brook | |
47 | 53c25cea | Paul Brook | /* An 8-bit r/o interrupt status register. Reading the value will return the
|
48 | 53c25cea | Paul Brook | * current contents of the ISR and will also clear it. This is effectively
|
49 | 53c25cea | Paul Brook | * a read-and-acknowledge. */
|
50 | 53c25cea | Paul Brook | #define VIRTIO_PCI_ISR 19 |
51 | 53c25cea | Paul Brook | |
52 | aba800a3 | Michael S. Tsirkin | /* MSI-X registers: only enabled if MSI-X is enabled. */
|
53 | aba800a3 | Michael S. Tsirkin | /* A 16-bit vector for configuration changes. */
|
54 | aba800a3 | Michael S. Tsirkin | #define VIRTIO_MSI_CONFIG_VECTOR 20 |
55 | aba800a3 | Michael S. Tsirkin | /* A 16-bit vector for selected queue notifications. */
|
56 | aba800a3 | Michael S. Tsirkin | #define VIRTIO_MSI_QUEUE_VECTOR 22 |
57 | aba800a3 | Michael S. Tsirkin | |
58 | aba800a3 | Michael S. Tsirkin | /* Config space size */
|
59 | aba800a3 | Michael S. Tsirkin | #define VIRTIO_PCI_CONFIG_NOMSI 20 |
60 | aba800a3 | Michael S. Tsirkin | #define VIRTIO_PCI_CONFIG_MSI 24 |
61 | aba800a3 | Michael S. Tsirkin | #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
|
62 | aba800a3 | Michael S. Tsirkin | VIRTIO_PCI_CONFIG_MSI : \ |
63 | aba800a3 | Michael S. Tsirkin | VIRTIO_PCI_CONFIG_NOMSI) |
64 | aba800a3 | Michael S. Tsirkin | |
65 | aba800a3 | Michael S. Tsirkin | /* The remaining space is defined by each driver as the per-driver
|
66 | aba800a3 | Michael S. Tsirkin | * configuration space */
|
67 | aba800a3 | Michael S. Tsirkin | #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
|
68 | aba800a3 | Michael S. Tsirkin | VIRTIO_PCI_CONFIG_MSI : \ |
69 | aba800a3 | Michael S. Tsirkin | VIRTIO_PCI_CONFIG_NOMSI) |
70 | 53c25cea | Paul Brook | |
71 | 53c25cea | Paul Brook | /* Virtio ABI version, if we increment this, we break the guest driver. */
|
72 | 53c25cea | Paul Brook | #define VIRTIO_PCI_ABI_VERSION 0 |
73 | 53c25cea | Paul Brook | |
74 | 53c25cea | Paul Brook | /* How many bits to shift physical queue address written to QUEUE_PFN.
|
75 | 53c25cea | Paul Brook | * 12 is historical, and due to x86 page size. */
|
76 | 53c25cea | Paul Brook | #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12 |
77 | 53c25cea | Paul Brook | |
78 | 53c25cea | Paul Brook | /* QEMU doesn't strictly need write barriers since everything runs in
|
79 | 53c25cea | Paul Brook | * lock-step. We'll leave the calls to wmb() in though to make it obvious for
|
80 | 53c25cea | Paul Brook | * KVM or if kqemu gets SMP support.
|
81 | 53c25cea | Paul Brook | */
|
82 | 53c25cea | Paul Brook | #define wmb() do { } while (0) |
83 | 53c25cea | Paul Brook | |
84 | 53c25cea | Paul Brook | /* PCI bindings. */
|
85 | 53c25cea | Paul Brook | |
86 | 53c25cea | Paul Brook | typedef struct { |
87 | 53c25cea | Paul Brook | PCIDevice pci_dev; |
88 | 53c25cea | Paul Brook | VirtIODevice *vdev; |
89 | 53c25cea | Paul Brook | uint32_t addr; |
90 | ab73ff29 | Gerd Hoffmann | uint32_t class_code; |
91 | a1e0fea5 | Gerd Hoffmann | uint32_t nvectors; |
92 | d176c495 | Gerd Hoffmann | DriveInfo *dinfo; |
93 | 53c25cea | Paul Brook | } VirtIOPCIProxy; |
94 | 53c25cea | Paul Brook | |
95 | 53c25cea | Paul Brook | /* virtio device */
|
96 | 53c25cea | Paul Brook | |
97 | 7055e687 | Michael S. Tsirkin | static void virtio_pci_notify(void *opaque, uint16_t vector) |
98 | 53c25cea | Paul Brook | { |
99 | 53c25cea | Paul Brook | VirtIOPCIProxy *proxy = opaque; |
100 | aba800a3 | Michael S. Tsirkin | if (msix_enabled(&proxy->pci_dev))
|
101 | aba800a3 | Michael S. Tsirkin | msix_notify(&proxy->pci_dev, vector); |
102 | aba800a3 | Michael S. Tsirkin | else
|
103 | aba800a3 | Michael S. Tsirkin | qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1); |
104 | 53c25cea | Paul Brook | } |
105 | 53c25cea | Paul Brook | |
106 | ff24bd58 | Michael S. Tsirkin | static void virtio_pci_save_config(void * opaque, QEMUFile *f) |
107 | ff24bd58 | Michael S. Tsirkin | { |
108 | ff24bd58 | Michael S. Tsirkin | VirtIOPCIProxy *proxy = opaque; |
109 | ff24bd58 | Michael S. Tsirkin | pci_device_save(&proxy->pci_dev, f); |
110 | ff24bd58 | Michael S. Tsirkin | msix_save(&proxy->pci_dev, f); |
111 | ff24bd58 | Michael S. Tsirkin | if (msix_present(&proxy->pci_dev))
|
112 | ff24bd58 | Michael S. Tsirkin | qemu_put_be16(f, proxy->vdev->config_vector); |
113 | ff24bd58 | Michael S. Tsirkin | } |
114 | ff24bd58 | Michael S. Tsirkin | |
115 | ff24bd58 | Michael S. Tsirkin | static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f) |
116 | ff24bd58 | Michael S. Tsirkin | { |
117 | ff24bd58 | Michael S. Tsirkin | VirtIOPCIProxy *proxy = opaque; |
118 | ff24bd58 | Michael S. Tsirkin | if (msix_present(&proxy->pci_dev))
|
119 | ff24bd58 | Michael S. Tsirkin | qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n)); |
120 | ff24bd58 | Michael S. Tsirkin | } |
121 | ff24bd58 | Michael S. Tsirkin | |
122 | ff24bd58 | Michael S. Tsirkin | static int virtio_pci_load_config(void * opaque, QEMUFile *f) |
123 | ff24bd58 | Michael S. Tsirkin | { |
124 | ff24bd58 | Michael S. Tsirkin | VirtIOPCIProxy *proxy = opaque; |
125 | ff24bd58 | Michael S. Tsirkin | int ret;
|
126 | ff24bd58 | Michael S. Tsirkin | ret = pci_device_load(&proxy->pci_dev, f); |
127 | e6da7680 | Michael S. Tsirkin | if (ret) {
|
128 | ff24bd58 | Michael S. Tsirkin | return ret;
|
129 | e6da7680 | Michael S. Tsirkin | } |
130 | ff24bd58 | Michael S. Tsirkin | msix_load(&proxy->pci_dev, f); |
131 | e6da7680 | Michael S. Tsirkin | if (msix_present(&proxy->pci_dev)) {
|
132 | ff24bd58 | Michael S. Tsirkin | qemu_get_be16s(f, &proxy->vdev->config_vector); |
133 | e6da7680 | Michael S. Tsirkin | } else {
|
134 | e6da7680 | Michael S. Tsirkin | proxy->vdev->config_vector = VIRTIO_NO_VECTOR; |
135 | e6da7680 | Michael S. Tsirkin | } |
136 | e6da7680 | Michael S. Tsirkin | if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
|
137 | e6da7680 | Michael S. Tsirkin | return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
|
138 | e6da7680 | Michael S. Tsirkin | } |
139 | ff24bd58 | Michael S. Tsirkin | return 0; |
140 | ff24bd58 | Michael S. Tsirkin | } |
141 | ff24bd58 | Michael S. Tsirkin | |
142 | ff24bd58 | Michael S. Tsirkin | static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f) |
143 | ff24bd58 | Michael S. Tsirkin | { |
144 | ff24bd58 | Michael S. Tsirkin | VirtIOPCIProxy *proxy = opaque; |
145 | ff24bd58 | Michael S. Tsirkin | uint16_t vector; |
146 | e6da7680 | Michael S. Tsirkin | if (msix_present(&proxy->pci_dev)) {
|
147 | e6da7680 | Michael S. Tsirkin | qemu_get_be16s(f, &vector); |
148 | e6da7680 | Michael S. Tsirkin | } else {
|
149 | e6da7680 | Michael S. Tsirkin | vector = VIRTIO_NO_VECTOR; |
150 | e6da7680 | Michael S. Tsirkin | } |
151 | ff24bd58 | Michael S. Tsirkin | virtio_queue_set_vector(proxy->vdev, n, vector); |
152 | e6da7680 | Michael S. Tsirkin | if (vector != VIRTIO_NO_VECTOR) {
|
153 | e6da7680 | Michael S. Tsirkin | return msix_vector_use(&proxy->pci_dev, vector);
|
154 | e6da7680 | Michael S. Tsirkin | } |
155 | ff24bd58 | Michael S. Tsirkin | return 0; |
156 | ff24bd58 | Michael S. Tsirkin | } |
157 | ff24bd58 | Michael S. Tsirkin | |
158 | 7055e687 | Michael S. Tsirkin | static void virtio_pci_reset(void *opaque) |
159 | 7055e687 | Michael S. Tsirkin | { |
160 | 7055e687 | Michael S. Tsirkin | VirtIOPCIProxy *proxy = opaque; |
161 | 7055e687 | Michael S. Tsirkin | virtio_reset(proxy->vdev); |
162 | aba800a3 | Michael S. Tsirkin | msix_reset(&proxy->pci_dev); |
163 | 7055e687 | Michael S. Tsirkin | } |
164 | 7055e687 | Michael S. Tsirkin | |
165 | 53c25cea | Paul Brook | static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val) |
166 | 53c25cea | Paul Brook | { |
167 | 53c25cea | Paul Brook | VirtIOPCIProxy *proxy = opaque; |
168 | 53c25cea | Paul Brook | VirtIODevice *vdev = proxy->vdev; |
169 | 53c25cea | Paul Brook | target_phys_addr_t pa; |
170 | 53c25cea | Paul Brook | |
171 | 53c25cea | Paul Brook | switch (addr) {
|
172 | 53c25cea | Paul Brook | case VIRTIO_PCI_GUEST_FEATURES:
|
173 | 53c25cea | Paul Brook | /* Guest does not negotiate properly? We have to assume nothing. */
|
174 | 53c25cea | Paul Brook | if (val & (1 << VIRTIO_F_BAD_FEATURE)) { |
175 | 53c25cea | Paul Brook | if (vdev->bad_features)
|
176 | 53c25cea | Paul Brook | val = vdev->bad_features(vdev); |
177 | 53c25cea | Paul Brook | else
|
178 | 53c25cea | Paul Brook | val = 0;
|
179 | 53c25cea | Paul Brook | } |
180 | 53c25cea | Paul Brook | if (vdev->set_features)
|
181 | 53c25cea | Paul Brook | vdev->set_features(vdev, val); |
182 | 53c25cea | Paul Brook | vdev->features = val; |
183 | 53c25cea | Paul Brook | break;
|
184 | 53c25cea | Paul Brook | case VIRTIO_PCI_QUEUE_PFN:
|
185 | 53c25cea | Paul Brook | pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT; |
186 | 7055e687 | Michael S. Tsirkin | if (pa == 0) |
187 | 7055e687 | Michael S. Tsirkin | virtio_pci_reset(proxy); |
188 | 7055e687 | Michael S. Tsirkin | else
|
189 | 7055e687 | Michael S. Tsirkin | virtio_queue_set_addr(vdev, vdev->queue_sel, pa); |
190 | 53c25cea | Paul Brook | break;
|
191 | 53c25cea | Paul Brook | case VIRTIO_PCI_QUEUE_SEL:
|
192 | 53c25cea | Paul Brook | if (val < VIRTIO_PCI_QUEUE_MAX)
|
193 | 53c25cea | Paul Brook | vdev->queue_sel = val; |
194 | 53c25cea | Paul Brook | break;
|
195 | 53c25cea | Paul Brook | case VIRTIO_PCI_QUEUE_NOTIFY:
|
196 | 53c25cea | Paul Brook | virtio_queue_notify(vdev, val); |
197 | 53c25cea | Paul Brook | break;
|
198 | 53c25cea | Paul Brook | case VIRTIO_PCI_STATUS:
|
199 | 53c25cea | Paul Brook | vdev->status = val & 0xFF;
|
200 | 53c25cea | Paul Brook | if (vdev->status == 0) |
201 | 7055e687 | Michael S. Tsirkin | virtio_pci_reset(proxy); |
202 | 53c25cea | Paul Brook | break;
|
203 | aba800a3 | Michael S. Tsirkin | case VIRTIO_MSI_CONFIG_VECTOR:
|
204 | aba800a3 | Michael S. Tsirkin | msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); |
205 | aba800a3 | Michael S. Tsirkin | /* Make it possible for guest to discover an error took place. */
|
206 | aba800a3 | Michael S. Tsirkin | if (msix_vector_use(&proxy->pci_dev, val) < 0) |
207 | aba800a3 | Michael S. Tsirkin | val = VIRTIO_NO_VECTOR; |
208 | aba800a3 | Michael S. Tsirkin | vdev->config_vector = val; |
209 | aba800a3 | Michael S. Tsirkin | break;
|
210 | aba800a3 | Michael S. Tsirkin | case VIRTIO_MSI_QUEUE_VECTOR:
|
211 | aba800a3 | Michael S. Tsirkin | msix_vector_unuse(&proxy->pci_dev, |
212 | aba800a3 | Michael S. Tsirkin | virtio_queue_vector(vdev, vdev->queue_sel)); |
213 | aba800a3 | Michael S. Tsirkin | /* Make it possible for guest to discover an error took place. */
|
214 | aba800a3 | Michael S. Tsirkin | if (msix_vector_use(&proxy->pci_dev, val) < 0) |
215 | aba800a3 | Michael S. Tsirkin | val = VIRTIO_NO_VECTOR; |
216 | aba800a3 | Michael S. Tsirkin | virtio_queue_set_vector(vdev, vdev->queue_sel, val); |
217 | aba800a3 | Michael S. Tsirkin | break;
|
218 | aba800a3 | Michael S. Tsirkin | default:
|
219 | aba800a3 | Michael S. Tsirkin | fprintf(stderr, "%s: unexpected address 0x%x value 0x%x\n",
|
220 | aba800a3 | Michael S. Tsirkin | __func__, addr, val); |
221 | aba800a3 | Michael S. Tsirkin | break;
|
222 | 53c25cea | Paul Brook | } |
223 | 53c25cea | Paul Brook | } |
224 | 53c25cea | Paul Brook | |
225 | aba800a3 | Michael S. Tsirkin | static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
|
226 | 53c25cea | Paul Brook | { |
227 | 53c25cea | Paul Brook | VirtIODevice *vdev = proxy->vdev; |
228 | 53c25cea | Paul Brook | uint32_t ret = 0xFFFFFFFF;
|
229 | 53c25cea | Paul Brook | |
230 | 53c25cea | Paul Brook | switch (addr) {
|
231 | 53c25cea | Paul Brook | case VIRTIO_PCI_HOST_FEATURES:
|
232 | 53c25cea | Paul Brook | ret = vdev->get_features(vdev); |
233 | efeea6d0 | Mark McLoughlin | ret |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY);
|
234 | efeea6d0 | Mark McLoughlin | ret |= (1 << VIRTIO_RING_F_INDIRECT_DESC);
|
235 | efeea6d0 | Mark McLoughlin | ret |= (1 << VIRTIO_F_BAD_FEATURE);
|
236 | 53c25cea | Paul Brook | break;
|
237 | 53c25cea | Paul Brook | case VIRTIO_PCI_GUEST_FEATURES:
|
238 | 53c25cea | Paul Brook | ret = vdev->features; |
239 | 53c25cea | Paul Brook | break;
|
240 | 53c25cea | Paul Brook | case VIRTIO_PCI_QUEUE_PFN:
|
241 | 53c25cea | Paul Brook | ret = virtio_queue_get_addr(vdev, vdev->queue_sel) |
242 | 53c25cea | Paul Brook | >> VIRTIO_PCI_QUEUE_ADDR_SHIFT; |
243 | 53c25cea | Paul Brook | break;
|
244 | 53c25cea | Paul Brook | case VIRTIO_PCI_QUEUE_NUM:
|
245 | 53c25cea | Paul Brook | ret = virtio_queue_get_num(vdev, vdev->queue_sel); |
246 | 53c25cea | Paul Brook | break;
|
247 | 53c25cea | Paul Brook | case VIRTIO_PCI_QUEUE_SEL:
|
248 | 53c25cea | Paul Brook | ret = vdev->queue_sel; |
249 | 53c25cea | Paul Brook | break;
|
250 | 53c25cea | Paul Brook | case VIRTIO_PCI_STATUS:
|
251 | 53c25cea | Paul Brook | ret = vdev->status; |
252 | 53c25cea | Paul Brook | break;
|
253 | 53c25cea | Paul Brook | case VIRTIO_PCI_ISR:
|
254 | 53c25cea | Paul Brook | /* reading from the ISR also clears it. */
|
255 | 53c25cea | Paul Brook | ret = vdev->isr; |
256 | 53c25cea | Paul Brook | vdev->isr = 0;
|
257 | 7055e687 | Michael S. Tsirkin | qemu_set_irq(proxy->pci_dev.irq[0], 0); |
258 | 53c25cea | Paul Brook | break;
|
259 | aba800a3 | Michael S. Tsirkin | case VIRTIO_MSI_CONFIG_VECTOR:
|
260 | aba800a3 | Michael S. Tsirkin | ret = vdev->config_vector; |
261 | aba800a3 | Michael S. Tsirkin | break;
|
262 | aba800a3 | Michael S. Tsirkin | case VIRTIO_MSI_QUEUE_VECTOR:
|
263 | aba800a3 | Michael S. Tsirkin | ret = virtio_queue_vector(vdev, vdev->queue_sel); |
264 | aba800a3 | Michael S. Tsirkin | break;
|
265 | 53c25cea | Paul Brook | default:
|
266 | 53c25cea | Paul Brook | break;
|
267 | 53c25cea | Paul Brook | } |
268 | 53c25cea | Paul Brook | |
269 | 53c25cea | Paul Brook | return ret;
|
270 | 53c25cea | Paul Brook | } |
271 | 53c25cea | Paul Brook | |
272 | 53c25cea | Paul Brook | static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr) |
273 | 53c25cea | Paul Brook | { |
274 | 53c25cea | Paul Brook | VirtIOPCIProxy *proxy = opaque; |
275 | aba800a3 | Michael S. Tsirkin | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
276 | aba800a3 | Michael S. Tsirkin | addr -= proxy->addr; |
277 | aba800a3 | Michael S. Tsirkin | if (addr < config)
|
278 | aba800a3 | Michael S. Tsirkin | return virtio_ioport_read(proxy, addr);
|
279 | aba800a3 | Michael S. Tsirkin | addr -= config; |
280 | 53c25cea | Paul Brook | return virtio_config_readb(proxy->vdev, addr);
|
281 | 53c25cea | Paul Brook | } |
282 | 53c25cea | Paul Brook | |
283 | 53c25cea | Paul Brook | static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr) |
284 | 53c25cea | Paul Brook | { |
285 | 53c25cea | Paul Brook | VirtIOPCIProxy *proxy = opaque; |
286 | aba800a3 | Michael S. Tsirkin | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
287 | aba800a3 | Michael S. Tsirkin | addr -= proxy->addr; |
288 | aba800a3 | Michael S. Tsirkin | if (addr < config)
|
289 | aba800a3 | Michael S. Tsirkin | return virtio_ioport_read(proxy, addr);
|
290 | aba800a3 | Michael S. Tsirkin | addr -= config; |
291 | 53c25cea | Paul Brook | return virtio_config_readw(proxy->vdev, addr);
|
292 | 53c25cea | Paul Brook | } |
293 | 53c25cea | Paul Brook | |
294 | 53c25cea | Paul Brook | static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr) |
295 | 53c25cea | Paul Brook | { |
296 | 53c25cea | Paul Brook | VirtIOPCIProxy *proxy = opaque; |
297 | aba800a3 | Michael S. Tsirkin | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
298 | aba800a3 | Michael S. Tsirkin | addr -= proxy->addr; |
299 | aba800a3 | Michael S. Tsirkin | if (addr < config)
|
300 | aba800a3 | Michael S. Tsirkin | return virtio_ioport_read(proxy, addr);
|
301 | aba800a3 | Michael S. Tsirkin | addr -= config; |
302 | 53c25cea | Paul Brook | return virtio_config_readl(proxy->vdev, addr);
|
303 | 53c25cea | Paul Brook | } |
304 | 53c25cea | Paul Brook | |
305 | 53c25cea | Paul Brook | static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val) |
306 | 53c25cea | Paul Brook | { |
307 | 53c25cea | Paul Brook | VirtIOPCIProxy *proxy = opaque; |
308 | aba800a3 | Michael S. Tsirkin | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
309 | aba800a3 | Michael S. Tsirkin | addr -= proxy->addr; |
310 | aba800a3 | Michael S. Tsirkin | if (addr < config) {
|
311 | aba800a3 | Michael S. Tsirkin | virtio_ioport_write(proxy, addr, val); |
312 | aba800a3 | Michael S. Tsirkin | return;
|
313 | aba800a3 | Michael S. Tsirkin | } |
314 | aba800a3 | Michael S. Tsirkin | addr -= config; |
315 | 53c25cea | Paul Brook | virtio_config_writeb(proxy->vdev, addr, val); |
316 | 53c25cea | Paul Brook | } |
317 | 53c25cea | Paul Brook | |
318 | 53c25cea | Paul Brook | static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val) |
319 | 53c25cea | Paul Brook | { |
320 | 53c25cea | Paul Brook | VirtIOPCIProxy *proxy = opaque; |
321 | aba800a3 | Michael S. Tsirkin | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
322 | aba800a3 | Michael S. Tsirkin | addr -= proxy->addr; |
323 | aba800a3 | Michael S. Tsirkin | if (addr < config) {
|
324 | aba800a3 | Michael S. Tsirkin | virtio_ioport_write(proxy, addr, val); |
325 | aba800a3 | Michael S. Tsirkin | return;
|
326 | aba800a3 | Michael S. Tsirkin | } |
327 | aba800a3 | Michael S. Tsirkin | addr -= config; |
328 | 53c25cea | Paul Brook | virtio_config_writew(proxy->vdev, addr, val); |
329 | 53c25cea | Paul Brook | } |
330 | 53c25cea | Paul Brook | |
331 | 53c25cea | Paul Brook | static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val) |
332 | 53c25cea | Paul Brook | { |
333 | 53c25cea | Paul Brook | VirtIOPCIProxy *proxy = opaque; |
334 | aba800a3 | Michael S. Tsirkin | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
335 | aba800a3 | Michael S. Tsirkin | addr -= proxy->addr; |
336 | aba800a3 | Michael S. Tsirkin | if (addr < config) {
|
337 | aba800a3 | Michael S. Tsirkin | virtio_ioport_write(proxy, addr, val); |
338 | aba800a3 | Michael S. Tsirkin | return;
|
339 | aba800a3 | Michael S. Tsirkin | } |
340 | aba800a3 | Michael S. Tsirkin | addr -= config; |
341 | 53c25cea | Paul Brook | virtio_config_writel(proxy->vdev, addr, val); |
342 | 53c25cea | Paul Brook | } |
343 | 53c25cea | Paul Brook | |
344 | 53c25cea | Paul Brook | static void virtio_map(PCIDevice *pci_dev, int region_num, |
345 | 53c25cea | Paul Brook | uint32_t addr, uint32_t size, int type)
|
346 | 53c25cea | Paul Brook | { |
347 | 53c25cea | Paul Brook | VirtIOPCIProxy *proxy = container_of(pci_dev, VirtIOPCIProxy, pci_dev); |
348 | 53c25cea | Paul Brook | VirtIODevice *vdev = proxy->vdev; |
349 | aba800a3 | Michael S. Tsirkin | unsigned config_len = VIRTIO_PCI_REGION_SIZE(pci_dev) + vdev->config_len;
|
350 | 53c25cea | Paul Brook | |
351 | 53c25cea | Paul Brook | proxy->addr = addr; |
352 | 53c25cea | Paul Brook | |
353 | aba800a3 | Michael S. Tsirkin | register_ioport_write(addr, config_len, 1, virtio_pci_config_writeb, proxy);
|
354 | aba800a3 | Michael S. Tsirkin | register_ioport_write(addr, config_len, 2, virtio_pci_config_writew, proxy);
|
355 | aba800a3 | Michael S. Tsirkin | register_ioport_write(addr, config_len, 4, virtio_pci_config_writel, proxy);
|
356 | aba800a3 | Michael S. Tsirkin | register_ioport_read(addr, config_len, 1, virtio_pci_config_readb, proxy);
|
357 | aba800a3 | Michael S. Tsirkin | register_ioport_read(addr, config_len, 2, virtio_pci_config_readw, proxy);
|
358 | aba800a3 | Michael S. Tsirkin | register_ioport_read(addr, config_len, 4, virtio_pci_config_readl, proxy);
|
359 | 53c25cea | Paul Brook | |
360 | aba800a3 | Michael S. Tsirkin | if (vdev->config_len)
|
361 | 53c25cea | Paul Brook | vdev->get_config(vdev, vdev->config); |
362 | aba800a3 | Michael S. Tsirkin | } |
363 | aba800a3 | Michael S. Tsirkin | |
364 | aba800a3 | Michael S. Tsirkin | static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, |
365 | aba800a3 | Michael S. Tsirkin | uint32_t val, int len)
|
366 | aba800a3 | Michael S. Tsirkin | { |
367 | aba800a3 | Michael S. Tsirkin | pci_default_write_config(pci_dev, address, val, len); |
368 | aba800a3 | Michael S. Tsirkin | msix_write_config(pci_dev, address, val, len); |
369 | 53c25cea | Paul Brook | } |
370 | 53c25cea | Paul Brook | |
371 | 53c25cea | Paul Brook | static const VirtIOBindings virtio_pci_bindings = { |
372 | ff24bd58 | Michael S. Tsirkin | .notify = virtio_pci_notify, |
373 | ff24bd58 | Michael S. Tsirkin | .save_config = virtio_pci_save_config, |
374 | ff24bd58 | Michael S. Tsirkin | .load_config = virtio_pci_load_config, |
375 | ff24bd58 | Michael S. Tsirkin | .save_queue = virtio_pci_save_queue, |
376 | ff24bd58 | Michael S. Tsirkin | .load_queue = virtio_pci_load_queue, |
377 | 53c25cea | Paul Brook | }; |
378 | 53c25cea | Paul Brook | |
379 | 53c25cea | Paul Brook | static void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev, |
380 | 53c25cea | Paul Brook | uint16_t vendor, uint16_t device, |
381 | 53c25cea | Paul Brook | uint16_t class_code, uint8_t pif) |
382 | 53c25cea | Paul Brook | { |
383 | 53c25cea | Paul Brook | uint8_t *config; |
384 | 53c25cea | Paul Brook | uint32_t size; |
385 | 53c25cea | Paul Brook | |
386 | 53c25cea | Paul Brook | proxy->vdev = vdev; |
387 | 53c25cea | Paul Brook | |
388 | 53c25cea | Paul Brook | config = proxy->pci_dev.config; |
389 | 53c25cea | Paul Brook | pci_config_set_vendor_id(config, vendor); |
390 | 53c25cea | Paul Brook | pci_config_set_device_id(config, device); |
391 | 53c25cea | Paul Brook | |
392 | 53c25cea | Paul Brook | config[0x08] = VIRTIO_PCI_ABI_VERSION;
|
393 | 53c25cea | Paul Brook | |
394 | 53c25cea | Paul Brook | config[0x09] = pif;
|
395 | 53c25cea | Paul Brook | pci_config_set_class(config, class_code); |
396 | 53c25cea | Paul Brook | config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; |
397 | 53c25cea | Paul Brook | |
398 | 53c25cea | Paul Brook | config[0x2c] = vendor & 0xFF; |
399 | 53c25cea | Paul Brook | config[0x2d] = (vendor >> 8) & 0xFF; |
400 | 53c25cea | Paul Brook | config[0x2e] = vdev->device_id & 0xFF; |
401 | 53c25cea | Paul Brook | config[0x2f] = (vdev->device_id >> 8) & 0xFF; |
402 | 53c25cea | Paul Brook | |
403 | 53c25cea | Paul Brook | config[0x3d] = 1; |
404 | 53c25cea | Paul Brook | |
405 | aba800a3 | Michael S. Tsirkin | if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors, 1, 0)) { |
406 | aba800a3 | Michael S. Tsirkin | pci_register_bar(&proxy->pci_dev, 1,
|
407 | aba800a3 | Michael S. Tsirkin | msix_bar_size(&proxy->pci_dev), |
408 | aba800a3 | Michael S. Tsirkin | PCI_ADDRESS_SPACE_MEM, |
409 | aba800a3 | Michael S. Tsirkin | msix_mmio_map); |
410 | aba800a3 | Michael S. Tsirkin | proxy->pci_dev.config_write = virtio_write_config; |
411 | aba800a3 | Michael S. Tsirkin | proxy->pci_dev.unregister = msix_uninit; |
412 | aba800a3 | Michael S. Tsirkin | } else
|
413 | aba800a3 | Michael S. Tsirkin | vdev->nvectors = 0;
|
414 | aba800a3 | Michael S. Tsirkin | |
415 | aba800a3 | Michael S. Tsirkin | size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len; |
416 | 53c25cea | Paul Brook | if (size & (size-1)) |
417 | 53c25cea | Paul Brook | size = 1 << qemu_fls(size);
|
418 | 53c25cea | Paul Brook | |
419 | 28c2c264 | Avi Kivity | pci_register_bar(&proxy->pci_dev, 0, size, PCI_ADDRESS_SPACE_IO,
|
420 | 53c25cea | Paul Brook | virtio_map); |
421 | 53c25cea | Paul Brook | |
422 | a08d4367 | Jan Kiszka | qemu_register_reset(virtio_pci_reset, proxy); |
423 | 7055e687 | Michael S. Tsirkin | |
424 | 53c25cea | Paul Brook | virtio_bind_device(vdev, &virtio_pci_bindings, proxy); |
425 | 53c25cea | Paul Brook | } |
426 | 53c25cea | Paul Brook | |
427 | 81a322d4 | Gerd Hoffmann | static int virtio_blk_init_pci(PCIDevice *pci_dev) |
428 | 53c25cea | Paul Brook | { |
429 | 53c25cea | Paul Brook | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
430 | 53c25cea | Paul Brook | VirtIODevice *vdev; |
431 | 53c25cea | Paul Brook | |
432 | ab73ff29 | Gerd Hoffmann | if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
|
433 | ab73ff29 | Gerd Hoffmann | proxy->class_code != PCI_CLASS_STORAGE_OTHER) |
434 | ab73ff29 | Gerd Hoffmann | proxy->class_code = PCI_CLASS_STORAGE_SCSI; |
435 | 53c25cea | Paul Brook | |
436 | d176c495 | Gerd Hoffmann | if (!proxy->dinfo) {
|
437 | 84fc5589 | Gerd Hoffmann | qemu_error("virtio-blk-pci: drive property not set\n");
|
438 | 81a322d4 | Gerd Hoffmann | return -1; |
439 | d176c495 | Gerd Hoffmann | } |
440 | d176c495 | Gerd Hoffmann | vdev = virtio_blk_init(&pci_dev->qdev, proxy->dinfo); |
441 | 177539e0 | Gerd Hoffmann | vdev->nvectors = proxy->nvectors; |
442 | 53c25cea | Paul Brook | virtio_init_pci(proxy, vdev, |
443 | 53c25cea | Paul Brook | PCI_VENDOR_ID_REDHAT_QUMRANET, |
444 | 85c2c735 | Mark McLoughlin | PCI_DEVICE_ID_VIRTIO_BLOCK, |
445 | 85c2c735 | Mark McLoughlin | proxy->class_code, 0x00);
|
446 | 177539e0 | Gerd Hoffmann | /* make the actual value visible */
|
447 | 177539e0 | Gerd Hoffmann | proxy->nvectors = vdev->nvectors; |
448 | 81a322d4 | Gerd Hoffmann | return 0; |
449 | 21d58b57 | Mark McLoughlin | } |
450 | 21d58b57 | Mark McLoughlin | |
451 | 81a322d4 | Gerd Hoffmann | static int virtio_console_init_pci(PCIDevice *pci_dev) |
452 | 21d58b57 | Mark McLoughlin | { |
453 | d6beee99 | Gerd Hoffmann | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
454 | 85c2c735 | Mark McLoughlin | VirtIODevice *vdev; |
455 | 85c2c735 | Mark McLoughlin | |
456 | d6beee99 | Gerd Hoffmann | if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
|
457 | d6beee99 | Gerd Hoffmann | proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
|
458 | d6beee99 | Gerd Hoffmann | proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */
|
459 | d6beee99 | Gerd Hoffmann | proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER; |
460 | d6beee99 | Gerd Hoffmann | |
461 | 85c2c735 | Mark McLoughlin | vdev = virtio_console_init(&pci_dev->qdev); |
462 | 85c2c735 | Mark McLoughlin | virtio_init_pci(proxy, vdev, |
463 | 85c2c735 | Mark McLoughlin | PCI_VENDOR_ID_REDHAT_QUMRANET, |
464 | 85c2c735 | Mark McLoughlin | PCI_DEVICE_ID_VIRTIO_CONSOLE, |
465 | 85c2c735 | Mark McLoughlin | proxy->class_code, 0x00);
|
466 | 81a322d4 | Gerd Hoffmann | return 0; |
467 | 53c25cea | Paul Brook | } |
468 | 53c25cea | Paul Brook | |
469 | 81a322d4 | Gerd Hoffmann | static int virtio_net_init_pci(PCIDevice *pci_dev) |
470 | 53c25cea | Paul Brook | { |
471 | 53c25cea | Paul Brook | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
472 | 53c25cea | Paul Brook | VirtIODevice *vdev; |
473 | 53c25cea | Paul Brook | |
474 | 53c25cea | Paul Brook | vdev = virtio_net_init(&pci_dev->qdev); |
475 | a1e0fea5 | Gerd Hoffmann | |
476 | a1e0fea5 | Gerd Hoffmann | /* set nvectors from property, unless the user specified something
|
477 | a1e0fea5 | Gerd Hoffmann | * via -net nic,model=virtio,vectors=n command line option */
|
478 | a1e0fea5 | Gerd Hoffmann | if (pci_dev->qdev.nd->nvectors == NIC_NVECTORS_UNSPECIFIED)
|
479 | a1e0fea5 | Gerd Hoffmann | if (proxy->nvectors != NIC_NVECTORS_UNSPECIFIED)
|
480 | a1e0fea5 | Gerd Hoffmann | vdev->nvectors = proxy->nvectors; |
481 | a1e0fea5 | Gerd Hoffmann | |
482 | 53c25cea | Paul Brook | virtio_init_pci(proxy, vdev, |
483 | 53c25cea | Paul Brook | PCI_VENDOR_ID_REDHAT_QUMRANET, |
484 | 53c25cea | Paul Brook | PCI_DEVICE_ID_VIRTIO_NET, |
485 | 53c25cea | Paul Brook | PCI_CLASS_NETWORK_ETHERNET, |
486 | 53c25cea | Paul Brook | 0x00);
|
487 | a1e0fea5 | Gerd Hoffmann | |
488 | a1e0fea5 | Gerd Hoffmann | /* make the actual value visible */
|
489 | a1e0fea5 | Gerd Hoffmann | proxy->nvectors = vdev->nvectors; |
490 | 81a322d4 | Gerd Hoffmann | return 0; |
491 | 53c25cea | Paul Brook | } |
492 | 53c25cea | Paul Brook | |
493 | 81a322d4 | Gerd Hoffmann | static int virtio_balloon_init_pci(PCIDevice *pci_dev) |
494 | 53c25cea | Paul Brook | { |
495 | 53c25cea | Paul Brook | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
496 | 53c25cea | Paul Brook | VirtIODevice *vdev; |
497 | 53c25cea | Paul Brook | |
498 | 53c25cea | Paul Brook | vdev = virtio_balloon_init(&pci_dev->qdev); |
499 | 53c25cea | Paul Brook | virtio_init_pci(proxy, vdev, |
500 | 53c25cea | Paul Brook | PCI_VENDOR_ID_REDHAT_QUMRANET, |
501 | 53c25cea | Paul Brook | PCI_DEVICE_ID_VIRTIO_BALLOON, |
502 | 53c25cea | Paul Brook | PCI_CLASS_MEMORY_RAM, |
503 | 53c25cea | Paul Brook | 0x00);
|
504 | 81a322d4 | Gerd Hoffmann | return 0; |
505 | 53c25cea | Paul Brook | } |
506 | 53c25cea | Paul Brook | |
507 | 0aab0d3a | Gerd Hoffmann | static PCIDeviceInfo virtio_info[] = {
|
508 | 0aab0d3a | Gerd Hoffmann | { |
509 | 0aab0d3a | Gerd Hoffmann | .qdev.name = "virtio-blk-pci",
|
510 | 0aab0d3a | Gerd Hoffmann | .qdev.size = sizeof(VirtIOPCIProxy),
|
511 | 0aab0d3a | Gerd Hoffmann | .init = virtio_blk_init_pci, |
512 | ab73ff29 | Gerd Hoffmann | .qdev.props = (Property[]) { |
513 | 72c61d0b | Gerd Hoffmann | DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0), |
514 | 72c61d0b | Gerd Hoffmann | DEFINE_PROP_DRIVE("drive", VirtIOPCIProxy, dinfo),
|
515 | 177539e0 | Gerd Hoffmann | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), |
516 | 72c61d0b | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST(), |
517 | ab73ff29 | Gerd Hoffmann | }, |
518 | 0aab0d3a | Gerd Hoffmann | },{ |
519 | a1e0fea5 | Gerd Hoffmann | .qdev.name = "virtio-net-pci",
|
520 | a1e0fea5 | Gerd Hoffmann | .qdev.size = sizeof(VirtIOPCIProxy),
|
521 | a1e0fea5 | Gerd Hoffmann | .init = virtio_net_init_pci, |
522 | a1e0fea5 | Gerd Hoffmann | .qdev.props = (Property[]) { |
523 | 177539e0 | Gerd Hoffmann | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
|
524 | 177539e0 | Gerd Hoffmann | NIC_NVECTORS_UNSPECIFIED), |
525 | 72c61d0b | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST(), |
526 | a1e0fea5 | Gerd Hoffmann | }, |
527 | 0aab0d3a | Gerd Hoffmann | },{ |
528 | 0aab0d3a | Gerd Hoffmann | .qdev.name = "virtio-console-pci",
|
529 | 0aab0d3a | Gerd Hoffmann | .qdev.size = sizeof(VirtIOPCIProxy),
|
530 | 0aab0d3a | Gerd Hoffmann | .init = virtio_console_init_pci, |
531 | d6beee99 | Gerd Hoffmann | .qdev.props = (Property[]) { |
532 | 72c61d0b | Gerd Hoffmann | DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0), |
533 | 72c61d0b | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST(), |
534 | d6beee99 | Gerd Hoffmann | }, |
535 | 0aab0d3a | Gerd Hoffmann | },{ |
536 | 0aab0d3a | Gerd Hoffmann | .qdev.name = "virtio-balloon-pci",
|
537 | 0aab0d3a | Gerd Hoffmann | .qdev.size = sizeof(VirtIOPCIProxy),
|
538 | 0aab0d3a | Gerd Hoffmann | .init = virtio_balloon_init_pci, |
539 | 0aab0d3a | Gerd Hoffmann | },{ |
540 | 0aab0d3a | Gerd Hoffmann | /* end of list */
|
541 | 0aab0d3a | Gerd Hoffmann | } |
542 | 0aab0d3a | Gerd Hoffmann | }; |
543 | 0aab0d3a | Gerd Hoffmann | |
544 | 53c25cea | Paul Brook | static void virtio_pci_register_devices(void) |
545 | 53c25cea | Paul Brook | { |
546 | 0aab0d3a | Gerd Hoffmann | pci_qdev_register_many(virtio_info); |
547 | 53c25cea | Paul Brook | } |
548 | 53c25cea | Paul Brook | |
549 | 53c25cea | Paul Brook | device_init(virtio_pci_register_devices) |