]>
Commit | Line | Data |
---|---|---|
53c25cea PB |
1 | /* |
2 | * Virtio PCI Bindings | |
3 | * | |
4 | * Copyright IBM, Corp. 2007 | |
5 | * Copyright (c) 2009 CodeSourcery | |
6 | * | |
7 | * Authors: | |
8 | * Anthony Liguori <aliguori@us.ibm.com> | |
9 | * Paul Brook <paul@codesourcery.com> | |
10 | * | |
11 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
12 | * the COPYING file in the top-level directory. | |
13 | * | |
14 | */ | |
15 | ||
16 | #include <inttypes.h> | |
17 | ||
18 | #include "virtio.h" | |
8172539d MT |
19 | #include "virtio-blk.h" |
20 | #include "virtio-net.h" | |
6b331efb | 21 | #include "virtio-serial.h" |
53c25cea | 22 | #include "pci.h" |
2f792016 | 23 | #include "qemu-error.h" |
aba800a3 | 24 | #include "msix.h" |
a1e0fea5 | 25 | #include "net.h" |
97b15621 | 26 | #include "loader.h" |
ade80dc8 | 27 | #include "kvm.h" |
2446333c | 28 | #include "blockdev.h" |
9fe1ebeb | 29 | #include "virtio-pci.h" |
1129714f | 30 | #include "range.h" |
53c25cea PB |
31 | |
32 | /* from Linux's linux/virtio_pci.h */ | |
33 | ||
34 | /* A 32-bit r/o bitmask of the features supported by the host */ | |
35 | #define VIRTIO_PCI_HOST_FEATURES 0 | |
36 | ||
37 | /* A 32-bit r/w bitmask of features activated by the guest */ | |
38 | #define VIRTIO_PCI_GUEST_FEATURES 4 | |
39 | ||
40 | /* A 32-bit r/w PFN for the currently selected queue */ | |
41 | #define VIRTIO_PCI_QUEUE_PFN 8 | |
42 | ||
43 | /* A 16-bit r/o queue size for the currently selected queue */ | |
44 | #define VIRTIO_PCI_QUEUE_NUM 12 | |
45 | ||
46 | /* A 16-bit r/w queue selector */ | |
47 | #define VIRTIO_PCI_QUEUE_SEL 14 | |
48 | ||
49 | /* A 16-bit r/w queue notifier */ | |
50 | #define VIRTIO_PCI_QUEUE_NOTIFY 16 | |
51 | ||
52 | /* An 8-bit device status register. */ | |
53 | #define VIRTIO_PCI_STATUS 18 | |
54 | ||
55 | /* An 8-bit r/o interrupt status register. Reading the value will return the | |
56 | * current contents of the ISR and will also clear it. This is effectively | |
57 | * a read-and-acknowledge. */ | |
58 | #define VIRTIO_PCI_ISR 19 | |
59 | ||
aba800a3 MT |
60 | /* MSI-X registers: only enabled if MSI-X is enabled. */ |
61 | /* A 16-bit vector for configuration changes. */ | |
62 | #define VIRTIO_MSI_CONFIG_VECTOR 20 | |
63 | /* A 16-bit vector for selected queue notifications. */ | |
64 | #define VIRTIO_MSI_QUEUE_VECTOR 22 | |
65 | ||
66 | /* Config space size */ | |
67 | #define VIRTIO_PCI_CONFIG_NOMSI 20 | |
68 | #define VIRTIO_PCI_CONFIG_MSI 24 | |
69 | #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \ | |
70 | VIRTIO_PCI_CONFIG_MSI : \ | |
71 | VIRTIO_PCI_CONFIG_NOMSI) | |
72 | ||
73 | /* The remaining space is defined by each driver as the per-driver | |
74 | * configuration space */ | |
75 | #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \ | |
76 | VIRTIO_PCI_CONFIG_MSI : \ | |
77 | VIRTIO_PCI_CONFIG_NOMSI) | |
53c25cea | 78 | |
53c25cea PB |
79 | /* How many bits to shift physical queue address written to QUEUE_PFN. |
80 | * 12 is historical, and due to x86 page size. */ | |
81 | #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12 | |
82 | ||
3dbca8e6 SH |
83 | /* Flags track per-device state like workarounds for quirks in older guests. */ |
84 | #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0) | |
c81131db | 85 | |
53c25cea PB |
86 | /* QEMU doesn't strictly need write barriers since everything runs in |
87 | * lock-step. We'll leave the calls to wmb() in though to make it obvious for | |
88 | * KVM or if kqemu gets SMP support. | |
89 | */ | |
90 | #define wmb() do { } while (0) | |
91 | ||
53c25cea PB |
92 | /* virtio device */ |
93 | ||
7055e687 | 94 | static void virtio_pci_notify(void *opaque, uint16_t vector) |
53c25cea PB |
95 | { |
96 | VirtIOPCIProxy *proxy = opaque; | |
aba800a3 MT |
97 | if (msix_enabled(&proxy->pci_dev)) |
98 | msix_notify(&proxy->pci_dev, vector); | |
99 | else | |
100 | qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1); | |
53c25cea PB |
101 | } |
102 | ||
ff24bd58 MT |
103 | static void virtio_pci_save_config(void * opaque, QEMUFile *f) |
104 | { | |
105 | VirtIOPCIProxy *proxy = opaque; | |
106 | pci_device_save(&proxy->pci_dev, f); | |
107 | msix_save(&proxy->pci_dev, f); | |
108 | if (msix_present(&proxy->pci_dev)) | |
109 | qemu_put_be16(f, proxy->vdev->config_vector); | |
110 | } | |
111 | ||
112 | static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f) | |
113 | { | |
114 | VirtIOPCIProxy *proxy = opaque; | |
115 | if (msix_present(&proxy->pci_dev)) | |
116 | qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n)); | |
117 | } | |
118 | ||
119 | static int virtio_pci_load_config(void * opaque, QEMUFile *f) | |
120 | { | |
121 | VirtIOPCIProxy *proxy = opaque; | |
122 | int ret; | |
123 | ret = pci_device_load(&proxy->pci_dev, f); | |
e6da7680 | 124 | if (ret) { |
ff24bd58 | 125 | return ret; |
e6da7680 | 126 | } |
ff24bd58 | 127 | msix_load(&proxy->pci_dev, f); |
e6da7680 | 128 | if (msix_present(&proxy->pci_dev)) { |
ff24bd58 | 129 | qemu_get_be16s(f, &proxy->vdev->config_vector); |
e6da7680 MT |
130 | } else { |
131 | proxy->vdev->config_vector = VIRTIO_NO_VECTOR; | |
132 | } | |
133 | if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) { | |
134 | return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector); | |
135 | } | |
ff24bd58 MT |
136 | return 0; |
137 | } | |
138 | ||
139 | static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f) | |
140 | { | |
141 | VirtIOPCIProxy *proxy = opaque; | |
142 | uint16_t vector; | |
e6da7680 MT |
143 | if (msix_present(&proxy->pci_dev)) { |
144 | qemu_get_be16s(f, &vector); | |
145 | } else { | |
146 | vector = VIRTIO_NO_VECTOR; | |
147 | } | |
ff24bd58 | 148 | virtio_queue_set_vector(proxy->vdev, n, vector); |
e6da7680 MT |
149 | if (vector != VIRTIO_NO_VECTOR) { |
150 | return msix_vector_use(&proxy->pci_dev, vector); | |
151 | } | |
ff24bd58 MT |
152 | return 0; |
153 | } | |
154 | ||
25db9ebe SH |
155 | static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy, |
156 | int n, bool assign) | |
157 | { | |
158 | VirtQueue *vq = virtio_get_queue(proxy->vdev, n); | |
159 | EventNotifier *notifier = virtio_queue_get_host_notifier(vq); | |
da146d0a AK |
160 | int r = 0; |
161 | ||
25db9ebe SH |
162 | if (assign) { |
163 | r = event_notifier_init(notifier, 1); | |
164 | if (r < 0) { | |
b36e3914 MT |
165 | error_report("%s: unable to init event notifier: %d", |
166 | __func__, r); | |
25db9ebe SH |
167 | return r; |
168 | } | |
da146d0a AK |
169 | memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2, |
170 | true, n, event_notifier_get_fd(notifier)); | |
25db9ebe | 171 | } else { |
da146d0a AK |
172 | memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2, |
173 | true, n, event_notifier_get_fd(notifier)); | |
25db9ebe SH |
174 | /* Handle the race condition where the guest kicked and we deassigned |
175 | * before we got around to handling the kick. | |
176 | */ | |
177 | if (event_notifier_test_and_clear(notifier)) { | |
178 | virtio_queue_notify_vq(vq); | |
179 | } | |
180 | ||
181 | event_notifier_cleanup(notifier); | |
182 | } | |
183 | return r; | |
184 | } | |
185 | ||
186 | static void virtio_pci_host_notifier_read(void *opaque) | |
187 | { | |
188 | VirtQueue *vq = opaque; | |
189 | EventNotifier *n = virtio_queue_get_host_notifier(vq); | |
190 | if (event_notifier_test_and_clear(n)) { | |
191 | virtio_queue_notify_vq(vq); | |
192 | } | |
193 | } | |
194 | ||
195 | static void virtio_pci_set_host_notifier_fd_handler(VirtIOPCIProxy *proxy, | |
196 | int n, bool assign) | |
197 | { | |
198 | VirtQueue *vq = virtio_get_queue(proxy->vdev, n); | |
199 | EventNotifier *notifier = virtio_queue_get_host_notifier(vq); | |
200 | if (assign) { | |
201 | qemu_set_fd_handler(event_notifier_get_fd(notifier), | |
202 | virtio_pci_host_notifier_read, NULL, vq); | |
203 | } else { | |
204 | qemu_set_fd_handler(event_notifier_get_fd(notifier), | |
205 | NULL, NULL, NULL); | |
206 | } | |
207 | } | |
208 | ||
b36e3914 | 209 | static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy) |
25db9ebe SH |
210 | { |
211 | int n, r; | |
212 | ||
213 | if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) || | |
214 | proxy->ioeventfd_disabled || | |
215 | proxy->ioeventfd_started) { | |
b36e3914 | 216 | return; |
25db9ebe SH |
217 | } |
218 | ||
219 | for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) { | |
220 | if (!virtio_queue_get_num(proxy->vdev, n)) { | |
221 | continue; | |
222 | } | |
223 | ||
224 | r = virtio_pci_set_host_notifier_internal(proxy, n, true); | |
225 | if (r < 0) { | |
226 | goto assign_error; | |
227 | } | |
228 | ||
229 | virtio_pci_set_host_notifier_fd_handler(proxy, n, true); | |
230 | } | |
231 | proxy->ioeventfd_started = true; | |
b36e3914 | 232 | return; |
25db9ebe SH |
233 | |
234 | assign_error: | |
235 | while (--n >= 0) { | |
236 | if (!virtio_queue_get_num(proxy->vdev, n)) { | |
237 | continue; | |
238 | } | |
239 | ||
240 | virtio_pci_set_host_notifier_fd_handler(proxy, n, false); | |
b36e3914 MT |
241 | r = virtio_pci_set_host_notifier_internal(proxy, n, false); |
242 | assert(r >= 0); | |
25db9ebe SH |
243 | } |
244 | proxy->ioeventfd_started = false; | |
b36e3914 | 245 | error_report("%s: failed. Fallback to a userspace (slower).", __func__); |
25db9ebe SH |
246 | } |
247 | ||
b36e3914 | 248 | static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy) |
25db9ebe | 249 | { |
b36e3914 | 250 | int r; |
25db9ebe SH |
251 | int n; |
252 | ||
253 | if (!proxy->ioeventfd_started) { | |
b36e3914 | 254 | return; |
25db9ebe SH |
255 | } |
256 | ||
257 | for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) { | |
258 | if (!virtio_queue_get_num(proxy->vdev, n)) { | |
259 | continue; | |
260 | } | |
261 | ||
262 | virtio_pci_set_host_notifier_fd_handler(proxy, n, false); | |
b36e3914 MT |
263 | r = virtio_pci_set_host_notifier_internal(proxy, n, false); |
264 | assert(r >= 0); | |
25db9ebe SH |
265 | } |
266 | proxy->ioeventfd_started = false; | |
25db9ebe SH |
267 | } |
268 | ||
e489030d | 269 | static void virtio_pci_reset(DeviceState *d) |
7055e687 | 270 | { |
e489030d | 271 | VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev); |
25db9ebe | 272 | virtio_pci_stop_ioeventfd(proxy); |
7055e687 | 273 | virtio_reset(proxy->vdev); |
aba800a3 | 274 | msix_reset(&proxy->pci_dev); |
25db9ebe | 275 | proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG; |
7055e687 MT |
276 | } |
277 | ||
53c25cea PB |
278 | static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val) |
279 | { | |
280 | VirtIOPCIProxy *proxy = opaque; | |
281 | VirtIODevice *vdev = proxy->vdev; | |
c227f099 | 282 | target_phys_addr_t pa; |
53c25cea | 283 | |
53c25cea PB |
284 | switch (addr) { |
285 | case VIRTIO_PCI_GUEST_FEATURES: | |
286 | /* Guest does not negotiate properly? We have to assume nothing. */ | |
287 | if (val & (1 << VIRTIO_F_BAD_FEATURE)) { | |
288 | if (vdev->bad_features) | |
8172539d | 289 | val = proxy->host_features & vdev->bad_features(vdev); |
53c25cea PB |
290 | else |
291 | val = 0; | |
292 | } | |
293 | if (vdev->set_features) | |
294 | vdev->set_features(vdev, val); | |
704a76fc | 295 | vdev->guest_features = val; |
53c25cea PB |
296 | break; |
297 | case VIRTIO_PCI_QUEUE_PFN: | |
c227f099 | 298 | pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT; |
1b8e9b27 | 299 | if (pa == 0) { |
25db9ebe | 300 | virtio_pci_stop_ioeventfd(proxy); |
1b8e9b27 MT |
301 | virtio_reset(proxy->vdev); |
302 | msix_unuse_all_vectors(&proxy->pci_dev); | |
303 | } | |
7055e687 MT |
304 | else |
305 | virtio_queue_set_addr(vdev, vdev->queue_sel, pa); | |
53c25cea PB |
306 | break; |
307 | case VIRTIO_PCI_QUEUE_SEL: | |
308 | if (val < VIRTIO_PCI_QUEUE_MAX) | |
309 | vdev->queue_sel = val; | |
310 | break; | |
311 | case VIRTIO_PCI_QUEUE_NOTIFY: | |
7157e2e2 SH |
312 | if (val < VIRTIO_PCI_QUEUE_MAX) { |
313 | virtio_queue_notify(vdev, val); | |
314 | } | |
53c25cea PB |
315 | break; |
316 | case VIRTIO_PCI_STATUS: | |
25db9ebe SH |
317 | if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) { |
318 | virtio_pci_stop_ioeventfd(proxy); | |
319 | } | |
320 | ||
3e607cb5 | 321 | virtio_set_status(vdev, val & 0xFF); |
25db9ebe SH |
322 | |
323 | if (val & VIRTIO_CONFIG_S_DRIVER_OK) { | |
324 | virtio_pci_start_ioeventfd(proxy); | |
325 | } | |
326 | ||
1b8e9b27 MT |
327 | if (vdev->status == 0) { |
328 | virtio_reset(proxy->vdev); | |
329 | msix_unuse_all_vectors(&proxy->pci_dev); | |
330 | } | |
c81131db AG |
331 | |
332 | /* Linux before 2.6.34 sets the device as OK without enabling | |
333 | the PCI device bus master bit. In this case we need to disable | |
334 | some safety checks. */ | |
335 | if ((val & VIRTIO_CONFIG_S_DRIVER_OK) && | |
336 | !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { | |
3dbca8e6 | 337 | proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG; |
c81131db | 338 | } |
53c25cea | 339 | break; |
aba800a3 MT |
340 | case VIRTIO_MSI_CONFIG_VECTOR: |
341 | msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); | |
342 | /* Make it possible for guest to discover an error took place. */ | |
343 | if (msix_vector_use(&proxy->pci_dev, val) < 0) | |
344 | val = VIRTIO_NO_VECTOR; | |
345 | vdev->config_vector = val; | |
346 | break; | |
347 | case VIRTIO_MSI_QUEUE_VECTOR: | |
348 | msix_vector_unuse(&proxy->pci_dev, | |
349 | virtio_queue_vector(vdev, vdev->queue_sel)); | |
350 | /* Make it possible for guest to discover an error took place. */ | |
351 | if (msix_vector_use(&proxy->pci_dev, val) < 0) | |
352 | val = VIRTIO_NO_VECTOR; | |
353 | virtio_queue_set_vector(vdev, vdev->queue_sel, val); | |
354 | break; | |
355 | default: | |
4e02d460 SH |
356 | error_report("%s: unexpected address 0x%x value 0x%x", |
357 | __func__, addr, val); | |
aba800a3 | 358 | break; |
53c25cea PB |
359 | } |
360 | } | |
361 | ||
aba800a3 | 362 | static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr) |
53c25cea | 363 | { |
53c25cea PB |
364 | VirtIODevice *vdev = proxy->vdev; |
365 | uint32_t ret = 0xFFFFFFFF; | |
366 | ||
53c25cea PB |
367 | switch (addr) { |
368 | case VIRTIO_PCI_HOST_FEATURES: | |
8172539d | 369 | ret = proxy->host_features; |
53c25cea PB |
370 | break; |
371 | case VIRTIO_PCI_GUEST_FEATURES: | |
704a76fc | 372 | ret = vdev->guest_features; |
53c25cea PB |
373 | break; |
374 | case VIRTIO_PCI_QUEUE_PFN: | |
375 | ret = virtio_queue_get_addr(vdev, vdev->queue_sel) | |
376 | >> VIRTIO_PCI_QUEUE_ADDR_SHIFT; | |
377 | break; | |
378 | case VIRTIO_PCI_QUEUE_NUM: | |
379 | ret = virtio_queue_get_num(vdev, vdev->queue_sel); | |
380 | break; | |
381 | case VIRTIO_PCI_QUEUE_SEL: | |
382 | ret = vdev->queue_sel; | |
383 | break; | |
384 | case VIRTIO_PCI_STATUS: | |
385 | ret = vdev->status; | |
386 | break; | |
387 | case VIRTIO_PCI_ISR: | |
388 | /* reading from the ISR also clears it. */ | |
389 | ret = vdev->isr; | |
390 | vdev->isr = 0; | |
7055e687 | 391 | qemu_set_irq(proxy->pci_dev.irq[0], 0); |
53c25cea | 392 | break; |
aba800a3 MT |
393 | case VIRTIO_MSI_CONFIG_VECTOR: |
394 | ret = vdev->config_vector; | |
395 | break; | |
396 | case VIRTIO_MSI_QUEUE_VECTOR: | |
397 | ret = virtio_queue_vector(vdev, vdev->queue_sel); | |
398 | break; | |
53c25cea PB |
399 | default: |
400 | break; | |
401 | } | |
402 | ||
403 | return ret; | |
404 | } | |
405 | ||
406 | static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr) | |
407 | { | |
408 | VirtIOPCIProxy *proxy = opaque; | |
aba800a3 | 409 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
aba800a3 MT |
410 | if (addr < config) |
411 | return virtio_ioport_read(proxy, addr); | |
412 | addr -= config; | |
53c25cea PB |
413 | return virtio_config_readb(proxy->vdev, addr); |
414 | } | |
415 | ||
416 | static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr) | |
417 | { | |
418 | VirtIOPCIProxy *proxy = opaque; | |
aba800a3 | 419 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
aba800a3 MT |
420 | if (addr < config) |
421 | return virtio_ioport_read(proxy, addr); | |
422 | addr -= config; | |
53c25cea PB |
423 | return virtio_config_readw(proxy->vdev, addr); |
424 | } | |
425 | ||
426 | static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr) | |
427 | { | |
428 | VirtIOPCIProxy *proxy = opaque; | |
aba800a3 | 429 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
aba800a3 MT |
430 | if (addr < config) |
431 | return virtio_ioport_read(proxy, addr); | |
432 | addr -= config; | |
53c25cea PB |
433 | return virtio_config_readl(proxy->vdev, addr); |
434 | } | |
435 | ||
436 | static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val) | |
437 | { | |
438 | VirtIOPCIProxy *proxy = opaque; | |
aba800a3 | 439 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
aba800a3 MT |
440 | if (addr < config) { |
441 | virtio_ioport_write(proxy, addr, val); | |
442 | return; | |
443 | } | |
444 | addr -= config; | |
53c25cea PB |
445 | virtio_config_writeb(proxy->vdev, addr, val); |
446 | } | |
447 | ||
448 | static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val) | |
449 | { | |
450 | VirtIOPCIProxy *proxy = opaque; | |
aba800a3 | 451 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
aba800a3 MT |
452 | if (addr < config) { |
453 | virtio_ioport_write(proxy, addr, val); | |
454 | return; | |
455 | } | |
456 | addr -= config; | |
53c25cea PB |
457 | virtio_config_writew(proxy->vdev, addr, val); |
458 | } | |
459 | ||
460 | static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val) | |
461 | { | |
462 | VirtIOPCIProxy *proxy = opaque; | |
aba800a3 | 463 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
aba800a3 MT |
464 | if (addr < config) { |
465 | virtio_ioport_write(proxy, addr, val); | |
466 | return; | |
467 | } | |
468 | addr -= config; | |
53c25cea PB |
469 | virtio_config_writel(proxy->vdev, addr, val); |
470 | } | |
471 | ||
da146d0a AK |
472 | const MemoryRegionPortio virtio_portio[] = { |
473 | { 0, 0x10000, 1, .write = virtio_pci_config_writeb, }, | |
474 | { 0, 0x10000, 2, .write = virtio_pci_config_writew, }, | |
475 | { 0, 0x10000, 4, .write = virtio_pci_config_writel, }, | |
476 | { 0, 0x10000, 1, .read = virtio_pci_config_readb, }, | |
477 | { 0, 0x10000, 2, .read = virtio_pci_config_readw, }, | |
478 | { 0, 0x10000, 4, .read = virtio_pci_config_readl, }, | |
479 | PORTIO_END_OF_LIST() | |
480 | }; | |
53c25cea | 481 | |
da146d0a AK |
482 | static const MemoryRegionOps virtio_pci_config_ops = { |
483 | .old_portio = virtio_portio, | |
484 | .endianness = DEVICE_LITTLE_ENDIAN, | |
485 | }; | |
aba800a3 MT |
486 | |
487 | static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, | |
488 | uint32_t val, int len) | |
489 | { | |
ed757e14 YV |
490 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
491 | ||
1129714f MT |
492 | pci_default_write_config(pci_dev, address, val, len); |
493 | ||
494 | if (range_covers_byte(address, len, PCI_COMMAND) && | |
495 | !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) && | |
496 | !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) { | |
497 | virtio_pci_stop_ioeventfd(proxy); | |
498 | virtio_set_status(proxy->vdev, | |
499 | proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK); | |
ed757e14 YV |
500 | } |
501 | ||
85352471 | 502 | msix_write_config(pci_dev, address, val, len); |
53c25cea PB |
503 | } |
504 | ||
6d74ca5a MT |
505 | static unsigned virtio_pci_get_features(void *opaque) |
506 | { | |
8172539d MT |
507 | VirtIOPCIProxy *proxy = opaque; |
508 | return proxy->host_features; | |
6d74ca5a MT |
509 | } |
510 | ||
ade80dc8 MT |
511 | static void virtio_pci_guest_notifier_read(void *opaque) |
512 | { | |
513 | VirtQueue *vq = opaque; | |
514 | EventNotifier *n = virtio_queue_get_guest_notifier(vq); | |
515 | if (event_notifier_test_and_clear(n)) { | |
516 | virtio_irq(vq); | |
517 | } | |
518 | } | |
519 | ||
520 | static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign) | |
521 | { | |
522 | VirtIOPCIProxy *proxy = opaque; | |
523 | VirtQueue *vq = virtio_get_queue(proxy->vdev, n); | |
524 | EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); | |
525 | ||
526 | if (assign) { | |
527 | int r = event_notifier_init(notifier, 0); | |
528 | if (r < 0) { | |
529 | return r; | |
530 | } | |
531 | qemu_set_fd_handler(event_notifier_get_fd(notifier), | |
532 | virtio_pci_guest_notifier_read, NULL, vq); | |
533 | } else { | |
534 | qemu_set_fd_handler(event_notifier_get_fd(notifier), | |
535 | NULL, NULL, NULL); | |
536 | event_notifier_cleanup(notifier); | |
537 | } | |
538 | ||
539 | return 0; | |
540 | } | |
541 | ||
5430a28f | 542 | static bool virtio_pci_query_guest_notifiers(void *opaque) |
543 | { | |
544 | VirtIOPCIProxy *proxy = opaque; | |
545 | return msix_enabled(&proxy->pci_dev); | |
546 | } | |
547 | ||
54dd9321 MT |
548 | static int virtio_pci_set_guest_notifiers(void *opaque, bool assign) |
549 | { | |
550 | VirtIOPCIProxy *proxy = opaque; | |
551 | VirtIODevice *vdev = proxy->vdev; | |
552 | int r, n; | |
553 | ||
554 | for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) { | |
555 | if (!virtio_queue_get_num(vdev, n)) { | |
556 | break; | |
557 | } | |
558 | ||
559 | r = virtio_pci_set_guest_notifier(opaque, n, assign); | |
560 | if (r < 0) { | |
561 | goto assign_error; | |
562 | } | |
563 | } | |
564 | ||
565 | return 0; | |
566 | ||
567 | assign_error: | |
568 | /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */ | |
569 | while (--n >= 0) { | |
570 | virtio_pci_set_guest_notifier(opaque, n, !assign); | |
571 | } | |
572 | return r; | |
573 | } | |
574 | ||
ade80dc8 MT |
575 | static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign) |
576 | { | |
577 | VirtIOPCIProxy *proxy = opaque; | |
25db9ebe SH |
578 | |
579 | /* Stop using ioeventfd for virtqueue kick if the device starts using host | |
580 | * notifiers. This makes it easy to avoid stepping on each others' toes. | |
581 | */ | |
582 | proxy->ioeventfd_disabled = assign; | |
ade80dc8 | 583 | if (assign) { |
25db9ebe SH |
584 | virtio_pci_stop_ioeventfd(proxy); |
585 | } | |
586 | /* We don't need to start here: it's not needed because backend | |
587 | * currently only stops on status change away from ok, | |
588 | * reset, vmstop and such. If we do add code to start here, | |
589 | * need to check vmstate, device state etc. */ | |
590 | return virtio_pci_set_host_notifier_internal(proxy, n, assign); | |
591 | } | |
592 | ||
593 | static void virtio_pci_vmstate_change(void *opaque, bool running) | |
594 | { | |
595 | VirtIOPCIProxy *proxy = opaque; | |
596 | ||
597 | if (running) { | |
89c473fd MT |
598 | /* Try to find out if the guest has bus master disabled, but is |
599 | in ready state. Then we have a buggy guest OS. */ | |
600 | if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) && | |
601 | !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { | |
602 | proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG; | |
603 | } | |
25db9ebe | 604 | virtio_pci_start_ioeventfd(proxy); |
ade80dc8 | 605 | } else { |
25db9ebe | 606 | virtio_pci_stop_ioeventfd(proxy); |
ade80dc8 | 607 | } |
ade80dc8 MT |
608 | } |
609 | ||
53c25cea | 610 | static const VirtIOBindings virtio_pci_bindings = { |
ff24bd58 MT |
611 | .notify = virtio_pci_notify, |
612 | .save_config = virtio_pci_save_config, | |
613 | .load_config = virtio_pci_load_config, | |
614 | .save_queue = virtio_pci_save_queue, | |
615 | .load_queue = virtio_pci_load_queue, | |
6d74ca5a | 616 | .get_features = virtio_pci_get_features, |
5430a28f | 617 | .query_guest_notifiers = virtio_pci_query_guest_notifiers, |
ade80dc8 | 618 | .set_host_notifier = virtio_pci_set_host_notifier, |
54dd9321 | 619 | .set_guest_notifiers = virtio_pci_set_guest_notifiers, |
25db9ebe | 620 | .vmstate_change = virtio_pci_vmstate_change, |
53c25cea PB |
621 | }; |
622 | ||
befeac45 | 623 | void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev) |
53c25cea PB |
624 | { |
625 | uint8_t *config; | |
626 | uint32_t size; | |
627 | ||
628 | proxy->vdev = vdev; | |
629 | ||
630 | config = proxy->pci_dev.config; | |
53c25cea | 631 | |
e75ccf2c IY |
632 | if (proxy->class_code) { |
633 | pci_config_set_class(config, proxy->class_code); | |
634 | } | |
635 | pci_set_word(config + 0x2c, pci_get_word(config + PCI_VENDOR_ID)); | |
636 | pci_set_word(config + 0x2e, vdev->device_id); | |
53c25cea PB |
637 | config[0x3d] = 1; |
638 | ||
95524ae8 AK |
639 | memory_region_init(&proxy->msix_bar, "virtio-msix", 4096); |
640 | if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors, | |
641 | &proxy->msix_bar, 1, 0)) { | |
e824b2cc AK |
642 | pci_register_bar(&proxy->pci_dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY, |
643 | &proxy->msix_bar); | |
aba800a3 MT |
644 | } else |
645 | vdev->nvectors = 0; | |
646 | ||
ed757e14 YV |
647 | proxy->pci_dev.config_write = virtio_write_config; |
648 | ||
aba800a3 | 649 | size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len; |
53c25cea PB |
650 | if (size & (size-1)) |
651 | size = 1 << qemu_fls(size); | |
652 | ||
da146d0a AK |
653 | memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy, |
654 | "virtio-pci", size); | |
e824b2cc AK |
655 | pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO, |
656 | &proxy->bar); | |
53c25cea | 657 | |
25db9ebe SH |
658 | if (!kvm_has_many_ioeventfds()) { |
659 | proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD; | |
660 | } | |
661 | ||
53c25cea | 662 | virtio_bind_device(vdev, &virtio_pci_bindings, proxy); |
8172539d MT |
663 | proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY; |
664 | proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE; | |
665 | proxy->host_features = vdev->get_features(vdev, proxy->host_features); | |
53c25cea PB |
666 | } |
667 | ||
81a322d4 | 668 | static int virtio_blk_init_pci(PCIDevice *pci_dev) |
53c25cea PB |
669 | { |
670 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); | |
671 | VirtIODevice *vdev; | |
672 | ||
ab73ff29 GH |
673 | if (proxy->class_code != PCI_CLASS_STORAGE_SCSI && |
674 | proxy->class_code != PCI_CLASS_STORAGE_OTHER) | |
675 | proxy->class_code = PCI_CLASS_STORAGE_SCSI; | |
53c25cea | 676 | |
a8686a9b MA |
677 | vdev = virtio_blk_init(&pci_dev->qdev, &proxy->block, |
678 | &proxy->block_serial); | |
ac0c14d7 MA |
679 | if (!vdev) { |
680 | return -1; | |
681 | } | |
177539e0 | 682 | vdev->nvectors = proxy->nvectors; |
e75ccf2c | 683 | virtio_init_pci(proxy, vdev); |
177539e0 GH |
684 | /* make the actual value visible */ |
685 | proxy->nvectors = vdev->nvectors; | |
81a322d4 | 686 | return 0; |
21d58b57 MM |
687 | } |
688 | ||
0f457d91 MT |
689 | static int virtio_exit_pci(PCIDevice *pci_dev) |
690 | { | |
da146d0a | 691 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
95524ae8 | 692 | int r; |
da146d0a AK |
693 | |
694 | memory_region_destroy(&proxy->bar); | |
95524ae8 AK |
695 | r = msix_uninit(pci_dev, &proxy->msix_bar); |
696 | memory_region_destroy(&proxy->msix_bar); | |
697 | return r; | |
0f457d91 MT |
698 | } |
699 | ||
56a14938 GH |
700 | static int virtio_blk_exit_pci(PCIDevice *pci_dev) |
701 | { | |
702 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); | |
703 | ||
25db9ebe | 704 | virtio_pci_stop_ioeventfd(proxy); |
9d0d3138 | 705 | virtio_blk_exit(proxy->vdev); |
f8b6cc00 | 706 | blockdev_mark_auto_del(proxy->block.bs); |
0f457d91 | 707 | return virtio_exit_pci(pci_dev); |
56a14938 GH |
708 | } |
709 | ||
98b19252 | 710 | static int virtio_serial_init_pci(PCIDevice *pci_dev) |
21d58b57 | 711 | { |
d6beee99 | 712 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
85c2c735 MM |
713 | VirtIODevice *vdev; |
714 | ||
d6beee99 GH |
715 | if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER && |
716 | proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */ | |
717 | proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */ | |
718 | proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER; | |
719 | ||
6b331efb | 720 | vdev = virtio_serial_init(&pci_dev->qdev, &proxy->serial); |
25fe3654 AS |
721 | if (!vdev) { |
722 | return -1; | |
723 | } | |
573fb60c | 724 | vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED |
6b331efb | 725 | ? proxy->serial.max_virtserial_ports + 1 |
573fb60c | 726 | : proxy->nvectors; |
e75ccf2c | 727 | virtio_init_pci(proxy, vdev); |
a1829205 | 728 | proxy->nvectors = vdev->nvectors; |
81a322d4 | 729 | return 0; |
53c25cea PB |
730 | } |
731 | ||
8b53a865 AS |
732 | static int virtio_serial_exit_pci(PCIDevice *pci_dev) |
733 | { | |
734 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); | |
735 | ||
32059220 | 736 | virtio_pci_stop_ioeventfd(proxy); |
8b53a865 AS |
737 | virtio_serial_exit(proxy->vdev); |
738 | return virtio_exit_pci(pci_dev); | |
739 | } | |
740 | ||
81a322d4 | 741 | static int virtio_net_init_pci(PCIDevice *pci_dev) |
53c25cea PB |
742 | { |
743 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); | |
744 | VirtIODevice *vdev; | |
745 | ||
f0c07c7c | 746 | vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net); |
a1e0fea5 | 747 | |
97b15621 | 748 | vdev->nvectors = proxy->nvectors; |
e75ccf2c | 749 | virtio_init_pci(proxy, vdev); |
a1e0fea5 GH |
750 | |
751 | /* make the actual value visible */ | |
752 | proxy->nvectors = vdev->nvectors; | |
81a322d4 | 753 | return 0; |
53c25cea PB |
754 | } |
755 | ||
97b15621 GH |
756 | static int virtio_net_exit_pci(PCIDevice *pci_dev) |
757 | { | |
758 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); | |
759 | ||
25db9ebe | 760 | virtio_pci_stop_ioeventfd(proxy); |
97b15621 GH |
761 | virtio_net_exit(proxy->vdev); |
762 | return virtio_exit_pci(pci_dev); | |
763 | } | |
764 | ||
81a322d4 | 765 | static int virtio_balloon_init_pci(PCIDevice *pci_dev) |
53c25cea PB |
766 | { |
767 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); | |
768 | VirtIODevice *vdev; | |
769 | ||
770 | vdev = virtio_balloon_init(&pci_dev->qdev); | |
f76f6655 AS |
771 | if (!vdev) { |
772 | return -1; | |
773 | } | |
e75ccf2c | 774 | virtio_init_pci(proxy, vdev); |
81a322d4 | 775 | return 0; |
53c25cea PB |
776 | } |
777 | ||
855d7e25 AS |
778 | static int virtio_balloon_exit_pci(PCIDevice *pci_dev) |
779 | { | |
780 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); | |
781 | ||
782 | virtio_pci_stop_ioeventfd(proxy); | |
783 | virtio_balloon_exit(proxy->vdev); | |
784 | return virtio_exit_pci(pci_dev); | |
785 | } | |
786 | ||
0aab0d3a GH |
787 | static PCIDeviceInfo virtio_info[] = { |
788 | { | |
789 | .qdev.name = "virtio-blk-pci", | |
779206de | 790 | .qdev.alias = "virtio-blk", |
0aab0d3a GH |
791 | .qdev.size = sizeof(VirtIOPCIProxy), |
792 | .init = virtio_blk_init_pci, | |
56a14938 | 793 | .exit = virtio_blk_exit_pci, |
e75ccf2c IY |
794 | .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET, |
795 | .device_id = PCI_DEVICE_ID_VIRTIO_BLOCK, | |
796 | .revision = VIRTIO_PCI_ABI_VERSION, | |
797 | .class_id = PCI_CLASS_STORAGE_SCSI, | |
ab73ff29 | 798 | .qdev.props = (Property[]) { |
72c61d0b | 799 | DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0), |
428c149b | 800 | DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, block), |
a8686a9b | 801 | DEFINE_PROP_STRING("serial", VirtIOPCIProxy, block_serial), |
25db9ebe SH |
802 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, |
803 | VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), | |
177539e0 | 804 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), |
8172539d | 805 | DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features), |
72c61d0b | 806 | DEFINE_PROP_END_OF_LIST(), |
ab73ff29 | 807 | }, |
e489030d | 808 | .qdev.reset = virtio_pci_reset, |
0aab0d3a | 809 | },{ |
a1e0fea5 | 810 | .qdev.name = "virtio-net-pci", |
29f82b37 | 811 | .qdev.alias = "virtio-net", |
a1e0fea5 GH |
812 | .qdev.size = sizeof(VirtIOPCIProxy), |
813 | .init = virtio_net_init_pci, | |
97b15621 | 814 | .exit = virtio_net_exit_pci, |
5ee8ad71 | 815 | .romfile = "pxe-virtio.rom", |
e75ccf2c IY |
816 | .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET, |
817 | .device_id = PCI_DEVICE_ID_VIRTIO_NET, | |
818 | .revision = VIRTIO_PCI_ABI_VERSION, | |
819 | .class_id = PCI_CLASS_NETWORK_ETHERNET, | |
a1e0fea5 | 820 | .qdev.props = (Property[]) { |
25db9ebe SH |
821 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, |
822 | VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false), | |
97b15621 | 823 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3), |
8172539d | 824 | DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features), |
97b15621 | 825 | DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic), |
f0c07c7c AW |
826 | DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy, |
827 | net.txtimer, TX_TIMER_INTERVAL), | |
e3f30488 AW |
828 | DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy, |
829 | net.txburst, TX_BURST), | |
a697a334 | 830 | DEFINE_PROP_STRING("tx", VirtIOPCIProxy, net.tx), |
72c61d0b | 831 | DEFINE_PROP_END_OF_LIST(), |
a1e0fea5 | 832 | }, |
e489030d | 833 | .qdev.reset = virtio_pci_reset, |
0aab0d3a | 834 | },{ |
98b19252 AS |
835 | .qdev.name = "virtio-serial-pci", |
836 | .qdev.alias = "virtio-serial", | |
0aab0d3a | 837 | .qdev.size = sizeof(VirtIOPCIProxy), |
98b19252 | 838 | .init = virtio_serial_init_pci, |
8b53a865 | 839 | .exit = virtio_serial_exit_pci, |
e75ccf2c IY |
840 | .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET, |
841 | .device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE, | |
842 | .revision = VIRTIO_PCI_ABI_VERSION, | |
843 | .class_id = PCI_CLASS_COMMUNICATION_OTHER, | |
d6beee99 | 844 | .qdev.props = (Property[]) { |
32059220 AS |
845 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, |
846 | VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), | |
573fb60c AS |
847 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, |
848 | DEV_NVECTORS_UNSPECIFIED), | |
72c61d0b | 849 | DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0), |
8172539d | 850 | DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), |
6b331efb AS |
851 | DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy, |
852 | serial.max_virtserial_ports, 31), | |
72c61d0b | 853 | DEFINE_PROP_END_OF_LIST(), |
d6beee99 | 854 | }, |
e489030d | 855 | .qdev.reset = virtio_pci_reset, |
0aab0d3a GH |
856 | },{ |
857 | .qdev.name = "virtio-balloon-pci", | |
29f82b37 | 858 | .qdev.alias = "virtio-balloon", |
0aab0d3a GH |
859 | .qdev.size = sizeof(VirtIOPCIProxy), |
860 | .init = virtio_balloon_init_pci, | |
855d7e25 | 861 | .exit = virtio_balloon_exit_pci, |
e75ccf2c IY |
862 | .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET, |
863 | .device_id = PCI_DEVICE_ID_VIRTIO_BALLOON, | |
864 | .revision = VIRTIO_PCI_ABI_VERSION, | |
865 | .class_id = PCI_CLASS_MEMORY_RAM, | |
8172539d MT |
866 | .qdev.props = (Property[]) { |
867 | DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), | |
868 | DEFINE_PROP_END_OF_LIST(), | |
869 | }, | |
e489030d | 870 | .qdev.reset = virtio_pci_reset, |
0aab0d3a GH |
871 | },{ |
872 | /* end of list */ | |
873 | } | |
874 | }; | |
875 | ||
53c25cea PB |
876 | static void virtio_pci_register_devices(void) |
877 | { | |
0aab0d3a | 878 | pci_qdev_register_many(virtio_info); |
53c25cea PB |
879 | } |
880 | ||
881 | device_init(virtio_pci_register_devices) |