]>
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 | * | |
6b620ca3 PB |
14 | * Contributions after 2012-01-13 are licensed under the terms of the |
15 | * GNU GPL, version 2 or (at your option) any later version. | |
53c25cea PB |
16 | */ |
17 | ||
18 | #include <inttypes.h> | |
19 | ||
0d09e41a PB |
20 | #include "hw/virtio/virtio.h" |
21 | #include "hw/virtio/virtio-blk.h" | |
22 | #include "hw/virtio/virtio-net.h" | |
23 | #include "hw/virtio/virtio-serial.h" | |
24 | #include "hw/virtio/virtio-scsi.h" | |
25 | #include "hw/virtio/virtio-balloon.h" | |
83c9f4ca | 26 | #include "hw/pci/pci.h" |
1de7afc9 | 27 | #include "qemu/error-report.h" |
83c9f4ca PB |
28 | #include "hw/pci/msi.h" |
29 | #include "hw/pci/msix.h" | |
30 | #include "hw/loader.h" | |
9c17d615 PB |
31 | #include "sysemu/kvm.h" |
32 | #include "sysemu/blockdev.h" | |
47b43a1f | 33 | #include "virtio-pci.h" |
1de7afc9 | 34 | #include "qemu/range.h" |
0d09e41a | 35 | #include "hw/virtio/virtio-bus.h" |
24a6e7f4 | 36 | #include "qapi/visitor.h" |
53c25cea PB |
37 | |
38 | /* from Linux's linux/virtio_pci.h */ | |
39 | ||
40 | /* A 32-bit r/o bitmask of the features supported by the host */ | |
41 | #define VIRTIO_PCI_HOST_FEATURES 0 | |
42 | ||
43 | /* A 32-bit r/w bitmask of features activated by the guest */ | |
44 | #define VIRTIO_PCI_GUEST_FEATURES 4 | |
45 | ||
46 | /* A 32-bit r/w PFN for the currently selected queue */ | |
47 | #define VIRTIO_PCI_QUEUE_PFN 8 | |
48 | ||
49 | /* A 16-bit r/o queue size for the currently selected queue */ | |
50 | #define VIRTIO_PCI_QUEUE_NUM 12 | |
51 | ||
52 | /* A 16-bit r/w queue selector */ | |
53 | #define VIRTIO_PCI_QUEUE_SEL 14 | |
54 | ||
55 | /* A 16-bit r/w queue notifier */ | |
56 | #define VIRTIO_PCI_QUEUE_NOTIFY 16 | |
57 | ||
58 | /* An 8-bit device status register. */ | |
59 | #define VIRTIO_PCI_STATUS 18 | |
60 | ||
61 | /* An 8-bit r/o interrupt status register. Reading the value will return the | |
62 | * current contents of the ISR and will also clear it. This is effectively | |
63 | * a read-and-acknowledge. */ | |
64 | #define VIRTIO_PCI_ISR 19 | |
65 | ||
aba800a3 MT |
66 | /* MSI-X registers: only enabled if MSI-X is enabled. */ |
67 | /* A 16-bit vector for configuration changes. */ | |
68 | #define VIRTIO_MSI_CONFIG_VECTOR 20 | |
69 | /* A 16-bit vector for selected queue notifications. */ | |
70 | #define VIRTIO_MSI_QUEUE_VECTOR 22 | |
71 | ||
72 | /* Config space size */ | |
73 | #define VIRTIO_PCI_CONFIG_NOMSI 20 | |
74 | #define VIRTIO_PCI_CONFIG_MSI 24 | |
75 | #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \ | |
76 | VIRTIO_PCI_CONFIG_MSI : \ | |
77 | VIRTIO_PCI_CONFIG_NOMSI) | |
78 | ||
79 | /* The remaining space is defined by each driver as the per-driver | |
80 | * configuration space */ | |
81 | #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \ | |
82 | VIRTIO_PCI_CONFIG_MSI : \ | |
83 | VIRTIO_PCI_CONFIG_NOMSI) | |
53c25cea | 84 | |
53c25cea PB |
85 | /* How many bits to shift physical queue address written to QUEUE_PFN. |
86 | * 12 is historical, and due to x86 page size. */ | |
87 | #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12 | |
88 | ||
3dbca8e6 SH |
89 | /* Flags track per-device state like workarounds for quirks in older guests. */ |
90 | #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0) | |
c81131db | 91 | |
53c25cea PB |
92 | /* QEMU doesn't strictly need write barriers since everything runs in |
93 | * lock-step. We'll leave the calls to wmb() in though to make it obvious for | |
94 | * KVM or if kqemu gets SMP support. | |
95 | */ | |
96 | #define wmb() do { } while (0) | |
97 | ||
8e4a424b BS |
98 | /* HACK for virtio to determine if it's running a big endian guest */ |
99 | bool virtio_is_big_endian(void); | |
100 | ||
53c25cea | 101 | /* virtio device */ |
d2a0ccc6 MT |
102 | /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */ |
103 | static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d) | |
104 | { | |
105 | return container_of(d, VirtIOPCIProxy, pci_dev.qdev); | |
106 | } | |
53c25cea | 107 | |
d2a0ccc6 MT |
108 | /* DeviceState to VirtIOPCIProxy. Note: used on datapath, |
109 | * be careful and test performance if you change this. | |
110 | */ | |
111 | static inline VirtIOPCIProxy *to_virtio_pci_proxy_fast(DeviceState *d) | |
53c25cea | 112 | { |
d2a0ccc6 MT |
113 | return container_of(d, VirtIOPCIProxy, pci_dev.qdev); |
114 | } | |
115 | ||
116 | static void virtio_pci_notify(DeviceState *d, uint16_t vector) | |
117 | { | |
118 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy_fast(d); | |
aba800a3 MT |
119 | if (msix_enabled(&proxy->pci_dev)) |
120 | msix_notify(&proxy->pci_dev, vector); | |
121 | else | |
122 | qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1); | |
53c25cea PB |
123 | } |
124 | ||
d2a0ccc6 | 125 | static void virtio_pci_save_config(DeviceState *d, QEMUFile *f) |
ff24bd58 | 126 | { |
d2a0ccc6 | 127 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
ff24bd58 MT |
128 | pci_device_save(&proxy->pci_dev, f); |
129 | msix_save(&proxy->pci_dev, f); | |
130 | if (msix_present(&proxy->pci_dev)) | |
131 | qemu_put_be16(f, proxy->vdev->config_vector); | |
132 | } | |
133 | ||
d2a0ccc6 | 134 | static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f) |
ff24bd58 | 135 | { |
d2a0ccc6 | 136 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
ff24bd58 MT |
137 | if (msix_present(&proxy->pci_dev)) |
138 | qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n)); | |
139 | } | |
140 | ||
d2a0ccc6 | 141 | static int virtio_pci_load_config(DeviceState *d, QEMUFile *f) |
ff24bd58 | 142 | { |
d2a0ccc6 | 143 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
ff24bd58 MT |
144 | int ret; |
145 | ret = pci_device_load(&proxy->pci_dev, f); | |
e6da7680 | 146 | if (ret) { |
ff24bd58 | 147 | return ret; |
e6da7680 | 148 | } |
3cac001e | 149 | msix_unuse_all_vectors(&proxy->pci_dev); |
ff24bd58 | 150 | msix_load(&proxy->pci_dev, f); |
e6da7680 | 151 | if (msix_present(&proxy->pci_dev)) { |
ff24bd58 | 152 | qemu_get_be16s(f, &proxy->vdev->config_vector); |
e6da7680 MT |
153 | } else { |
154 | proxy->vdev->config_vector = VIRTIO_NO_VECTOR; | |
155 | } | |
156 | if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) { | |
157 | return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector); | |
158 | } | |
ff24bd58 MT |
159 | return 0; |
160 | } | |
161 | ||
d2a0ccc6 | 162 | static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f) |
ff24bd58 | 163 | { |
d2a0ccc6 | 164 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
ff24bd58 | 165 | uint16_t vector; |
e6da7680 MT |
166 | if (msix_present(&proxy->pci_dev)) { |
167 | qemu_get_be16s(f, &vector); | |
168 | } else { | |
169 | vector = VIRTIO_NO_VECTOR; | |
170 | } | |
ff24bd58 | 171 | virtio_queue_set_vector(proxy->vdev, n, vector); |
e6da7680 MT |
172 | if (vector != VIRTIO_NO_VECTOR) { |
173 | return msix_vector_use(&proxy->pci_dev, vector); | |
174 | } | |
ff24bd58 MT |
175 | return 0; |
176 | } | |
177 | ||
25db9ebe | 178 | static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy, |
26b9b5fe | 179 | int n, bool assign, bool set_handler) |
25db9ebe SH |
180 | { |
181 | VirtQueue *vq = virtio_get_queue(proxy->vdev, n); | |
182 | EventNotifier *notifier = virtio_queue_get_host_notifier(vq); | |
da146d0a AK |
183 | int r = 0; |
184 | ||
25db9ebe SH |
185 | if (assign) { |
186 | r = event_notifier_init(notifier, 1); | |
187 | if (r < 0) { | |
b36e3914 MT |
188 | error_report("%s: unable to init event notifier: %d", |
189 | __func__, r); | |
25db9ebe SH |
190 | return r; |
191 | } | |
26b9b5fe | 192 | virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler); |
da146d0a | 193 | memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2, |
753d5e14 | 194 | true, n, notifier); |
25db9ebe | 195 | } else { |
da146d0a | 196 | memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2, |
753d5e14 | 197 | true, n, notifier); |
26b9b5fe | 198 | virtio_queue_set_host_notifier_fd_handler(vq, false, false); |
25db9ebe SH |
199 | event_notifier_cleanup(notifier); |
200 | } | |
201 | return r; | |
202 | } | |
203 | ||
b36e3914 | 204 | static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy) |
25db9ebe SH |
205 | { |
206 | int n, r; | |
207 | ||
208 | if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) || | |
209 | proxy->ioeventfd_disabled || | |
210 | proxy->ioeventfd_started) { | |
b36e3914 | 211 | return; |
25db9ebe SH |
212 | } |
213 | ||
214 | for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) { | |
215 | if (!virtio_queue_get_num(proxy->vdev, n)) { | |
216 | continue; | |
217 | } | |
218 | ||
26b9b5fe | 219 | r = virtio_pci_set_host_notifier_internal(proxy, n, true, true); |
25db9ebe SH |
220 | if (r < 0) { |
221 | goto assign_error; | |
222 | } | |
25db9ebe SH |
223 | } |
224 | proxy->ioeventfd_started = true; | |
b36e3914 | 225 | return; |
25db9ebe SH |
226 | |
227 | assign_error: | |
228 | while (--n >= 0) { | |
229 | if (!virtio_queue_get_num(proxy->vdev, n)) { | |
230 | continue; | |
231 | } | |
232 | ||
26b9b5fe | 233 | r = virtio_pci_set_host_notifier_internal(proxy, n, false, false); |
b36e3914 | 234 | assert(r >= 0); |
25db9ebe SH |
235 | } |
236 | proxy->ioeventfd_started = false; | |
b36e3914 | 237 | error_report("%s: failed. Fallback to a userspace (slower).", __func__); |
25db9ebe SH |
238 | } |
239 | ||
b36e3914 | 240 | static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy) |
25db9ebe | 241 | { |
b36e3914 | 242 | int r; |
25db9ebe SH |
243 | int n; |
244 | ||
245 | if (!proxy->ioeventfd_started) { | |
b36e3914 | 246 | return; |
25db9ebe SH |
247 | } |
248 | ||
249 | for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) { | |
250 | if (!virtio_queue_get_num(proxy->vdev, n)) { | |
251 | continue; | |
252 | } | |
253 | ||
26b9b5fe | 254 | r = virtio_pci_set_host_notifier_internal(proxy, n, false, false); |
b36e3914 | 255 | assert(r >= 0); |
25db9ebe SH |
256 | } |
257 | proxy->ioeventfd_started = false; | |
25db9ebe SH |
258 | } |
259 | ||
60653b28 | 260 | static void virtio_pci_reset(DeviceState *d) |
7055e687 | 261 | { |
d2a0ccc6 | 262 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
25db9ebe | 263 | virtio_pci_stop_ioeventfd(proxy); |
7055e687 | 264 | virtio_reset(proxy->vdev); |
3cac001e | 265 | msix_unuse_all_vectors(&proxy->pci_dev); |
25db9ebe | 266 | proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG; |
7055e687 MT |
267 | } |
268 | ||
53c25cea PB |
269 | static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val) |
270 | { | |
271 | VirtIOPCIProxy *proxy = opaque; | |
272 | VirtIODevice *vdev = proxy->vdev; | |
a8170e5e | 273 | hwaddr pa; |
53c25cea | 274 | |
53c25cea PB |
275 | switch (addr) { |
276 | case VIRTIO_PCI_GUEST_FEATURES: | |
277 | /* Guest does not negotiate properly? We have to assume nothing. */ | |
278 | if (val & (1 << VIRTIO_F_BAD_FEATURE)) { | |
ad0c9332 | 279 | val = vdev->bad_features ? vdev->bad_features(vdev) : 0; |
53c25cea | 280 | } |
ad0c9332 | 281 | virtio_set_features(vdev, val); |
53c25cea PB |
282 | break; |
283 | case VIRTIO_PCI_QUEUE_PFN: | |
a8170e5e | 284 | pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT; |
1b8e9b27 | 285 | if (pa == 0) { |
25db9ebe | 286 | virtio_pci_stop_ioeventfd(proxy); |
1b8e9b27 MT |
287 | virtio_reset(proxy->vdev); |
288 | msix_unuse_all_vectors(&proxy->pci_dev); | |
289 | } | |
7055e687 MT |
290 | else |
291 | virtio_queue_set_addr(vdev, vdev->queue_sel, pa); | |
53c25cea PB |
292 | break; |
293 | case VIRTIO_PCI_QUEUE_SEL: | |
294 | if (val < VIRTIO_PCI_QUEUE_MAX) | |
295 | vdev->queue_sel = val; | |
296 | break; | |
297 | case VIRTIO_PCI_QUEUE_NOTIFY: | |
7157e2e2 SH |
298 | if (val < VIRTIO_PCI_QUEUE_MAX) { |
299 | virtio_queue_notify(vdev, val); | |
300 | } | |
53c25cea PB |
301 | break; |
302 | case VIRTIO_PCI_STATUS: | |
25db9ebe SH |
303 | if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) { |
304 | virtio_pci_stop_ioeventfd(proxy); | |
305 | } | |
306 | ||
3e607cb5 | 307 | virtio_set_status(vdev, val & 0xFF); |
25db9ebe SH |
308 | |
309 | if (val & VIRTIO_CONFIG_S_DRIVER_OK) { | |
310 | virtio_pci_start_ioeventfd(proxy); | |
311 | } | |
312 | ||
1b8e9b27 MT |
313 | if (vdev->status == 0) { |
314 | virtio_reset(proxy->vdev); | |
315 | msix_unuse_all_vectors(&proxy->pci_dev); | |
316 | } | |
c81131db AG |
317 | |
318 | /* Linux before 2.6.34 sets the device as OK without enabling | |
319 | the PCI device bus master bit. In this case we need to disable | |
320 | some safety checks. */ | |
321 | if ((val & VIRTIO_CONFIG_S_DRIVER_OK) && | |
322 | !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { | |
3dbca8e6 | 323 | proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG; |
c81131db | 324 | } |
53c25cea | 325 | break; |
aba800a3 MT |
326 | case VIRTIO_MSI_CONFIG_VECTOR: |
327 | msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); | |
328 | /* Make it possible for guest to discover an error took place. */ | |
329 | if (msix_vector_use(&proxy->pci_dev, val) < 0) | |
330 | val = VIRTIO_NO_VECTOR; | |
331 | vdev->config_vector = val; | |
332 | break; | |
333 | case VIRTIO_MSI_QUEUE_VECTOR: | |
334 | msix_vector_unuse(&proxy->pci_dev, | |
335 | virtio_queue_vector(vdev, vdev->queue_sel)); | |
336 | /* Make it possible for guest to discover an error took place. */ | |
337 | if (msix_vector_use(&proxy->pci_dev, val) < 0) | |
338 | val = VIRTIO_NO_VECTOR; | |
339 | virtio_queue_set_vector(vdev, vdev->queue_sel, val); | |
340 | break; | |
341 | default: | |
4e02d460 SH |
342 | error_report("%s: unexpected address 0x%x value 0x%x", |
343 | __func__, addr, val); | |
aba800a3 | 344 | break; |
53c25cea PB |
345 | } |
346 | } | |
347 | ||
aba800a3 | 348 | static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr) |
53c25cea | 349 | { |
53c25cea PB |
350 | VirtIODevice *vdev = proxy->vdev; |
351 | uint32_t ret = 0xFFFFFFFF; | |
352 | ||
53c25cea PB |
353 | switch (addr) { |
354 | case VIRTIO_PCI_HOST_FEATURES: | |
8172539d | 355 | ret = proxy->host_features; |
53c25cea PB |
356 | break; |
357 | case VIRTIO_PCI_GUEST_FEATURES: | |
704a76fc | 358 | ret = vdev->guest_features; |
53c25cea PB |
359 | break; |
360 | case VIRTIO_PCI_QUEUE_PFN: | |
361 | ret = virtio_queue_get_addr(vdev, vdev->queue_sel) | |
362 | >> VIRTIO_PCI_QUEUE_ADDR_SHIFT; | |
363 | break; | |
364 | case VIRTIO_PCI_QUEUE_NUM: | |
365 | ret = virtio_queue_get_num(vdev, vdev->queue_sel); | |
366 | break; | |
367 | case VIRTIO_PCI_QUEUE_SEL: | |
368 | ret = vdev->queue_sel; | |
369 | break; | |
370 | case VIRTIO_PCI_STATUS: | |
371 | ret = vdev->status; | |
372 | break; | |
373 | case VIRTIO_PCI_ISR: | |
374 | /* reading from the ISR also clears it. */ | |
375 | ret = vdev->isr; | |
376 | vdev->isr = 0; | |
7055e687 | 377 | qemu_set_irq(proxy->pci_dev.irq[0], 0); |
53c25cea | 378 | break; |
aba800a3 MT |
379 | case VIRTIO_MSI_CONFIG_VECTOR: |
380 | ret = vdev->config_vector; | |
381 | break; | |
382 | case VIRTIO_MSI_QUEUE_VECTOR: | |
383 | ret = virtio_queue_vector(vdev, vdev->queue_sel); | |
384 | break; | |
53c25cea PB |
385 | default: |
386 | break; | |
387 | } | |
388 | ||
389 | return ret; | |
390 | } | |
391 | ||
df6db5b3 AG |
392 | static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr, |
393 | unsigned size) | |
53c25cea PB |
394 | { |
395 | VirtIOPCIProxy *proxy = opaque; | |
aba800a3 | 396 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
df6db5b3 | 397 | uint64_t val = 0; |
aba800a3 | 398 | if (addr < config) { |
df6db5b3 | 399 | return virtio_ioport_read(proxy, addr); |
aba800a3 MT |
400 | } |
401 | addr -= config; | |
53c25cea | 402 | |
df6db5b3 AG |
403 | switch (size) { |
404 | case 1: | |
405 | val = virtio_config_readb(proxy->vdev, addr); | |
406 | break; | |
407 | case 2: | |
408 | val = virtio_config_readw(proxy->vdev, addr); | |
8e4a424b BS |
409 | if (virtio_is_big_endian()) { |
410 | val = bswap16(val); | |
411 | } | |
df6db5b3 AG |
412 | break; |
413 | case 4: | |
414 | val = virtio_config_readl(proxy->vdev, addr); | |
8e4a424b BS |
415 | if (virtio_is_big_endian()) { |
416 | val = bswap32(val); | |
417 | } | |
df6db5b3 | 418 | break; |
82afa586 | 419 | } |
df6db5b3 | 420 | return val; |
53c25cea PB |
421 | } |
422 | ||
df6db5b3 AG |
423 | static void virtio_pci_config_write(void *opaque, hwaddr addr, |
424 | uint64_t val, unsigned size) | |
53c25cea PB |
425 | { |
426 | VirtIOPCIProxy *proxy = opaque; | |
aba800a3 | 427 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
aba800a3 MT |
428 | if (addr < config) { |
429 | virtio_ioport_write(proxy, addr, val); | |
430 | return; | |
431 | } | |
432 | addr -= config; | |
df6db5b3 AG |
433 | /* |
434 | * Virtio-PCI is odd. Ioports are LE but config space is target native | |
435 | * endian. | |
436 | */ | |
437 | switch (size) { | |
438 | case 1: | |
439 | virtio_config_writeb(proxy->vdev, addr, val); | |
440 | break; | |
441 | case 2: | |
8e4a424b BS |
442 | if (virtio_is_big_endian()) { |
443 | val = bswap16(val); | |
444 | } | |
df6db5b3 AG |
445 | virtio_config_writew(proxy->vdev, addr, val); |
446 | break; | |
447 | case 4: | |
8e4a424b BS |
448 | if (virtio_is_big_endian()) { |
449 | val = bswap32(val); | |
450 | } | |
df6db5b3 AG |
451 | virtio_config_writel(proxy->vdev, addr, val); |
452 | break; | |
82afa586 | 453 | } |
53c25cea PB |
454 | } |
455 | ||
da146d0a | 456 | static const MemoryRegionOps virtio_pci_config_ops = { |
df6db5b3 AG |
457 | .read = virtio_pci_config_read, |
458 | .write = virtio_pci_config_write, | |
459 | .impl = { | |
460 | .min_access_size = 1, | |
461 | .max_access_size = 4, | |
462 | }, | |
8e4a424b | 463 | .endianness = DEVICE_LITTLE_ENDIAN, |
da146d0a | 464 | }; |
aba800a3 MT |
465 | |
466 | static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, | |
467 | uint32_t val, int len) | |
468 | { | |
ed757e14 YV |
469 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
470 | ||
1129714f MT |
471 | pci_default_write_config(pci_dev, address, val, len); |
472 | ||
473 | if (range_covers_byte(address, len, PCI_COMMAND) && | |
474 | !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) && | |
475 | !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) { | |
476 | virtio_pci_stop_ioeventfd(proxy); | |
477 | virtio_set_status(proxy->vdev, | |
478 | proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK); | |
ed757e14 | 479 | } |
53c25cea PB |
480 | } |
481 | ||
d2a0ccc6 | 482 | static unsigned virtio_pci_get_features(DeviceState *d) |
6d74ca5a | 483 | { |
d2a0ccc6 | 484 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
8172539d | 485 | return proxy->host_features; |
6d74ca5a MT |
486 | } |
487 | ||
7d37d351 JK |
488 | static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy, |
489 | unsigned int queue_no, | |
490 | unsigned int vector, | |
491 | MSIMessage msg) | |
492 | { | |
7d37d351 | 493 | VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; |
15b2bd18 | 494 | int ret; |
7d37d351 JK |
495 | |
496 | if (irqfd->users == 0) { | |
497 | ret = kvm_irqchip_add_msi_route(kvm_state, msg); | |
498 | if (ret < 0) { | |
499 | return ret; | |
500 | } | |
501 | irqfd->virq = ret; | |
502 | } | |
503 | irqfd->users++; | |
7d37d351 JK |
504 | return 0; |
505 | } | |
506 | ||
507 | static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy, | |
7d37d351 | 508 | unsigned int vector) |
774345f9 MT |
509 | { |
510 | VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; | |
511 | if (--irqfd->users == 0) { | |
512 | kvm_irqchip_release_virq(kvm_state, irqfd->virq); | |
513 | } | |
514 | } | |
515 | ||
f1d0f15a MT |
516 | static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy, |
517 | unsigned int queue_no, | |
518 | unsigned int vector) | |
519 | { | |
520 | VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; | |
521 | VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no); | |
522 | EventNotifier *n = virtio_queue_get_guest_notifier(vq); | |
523 | int ret; | |
524 | ret = kvm_irqchip_add_irqfd_notifier(kvm_state, n, irqfd->virq); | |
525 | return ret; | |
526 | } | |
527 | ||
528 | static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy, | |
529 | unsigned int queue_no, | |
530 | unsigned int vector) | |
7d37d351 JK |
531 | { |
532 | VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no); | |
15b2bd18 | 533 | EventNotifier *n = virtio_queue_get_guest_notifier(vq); |
7d37d351 | 534 | VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; |
15b2bd18 | 535 | int ret; |
7d37d351 | 536 | |
b131c74a | 537 | ret = kvm_irqchip_remove_irqfd_notifier(kvm_state, n, irqfd->virq); |
7d37d351 | 538 | assert(ret == 0); |
f1d0f15a | 539 | } |
7d37d351 | 540 | |
774345f9 MT |
541 | static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs) |
542 | { | |
543 | PCIDevice *dev = &proxy->pci_dev; | |
544 | VirtIODevice *vdev = proxy->vdev; | |
545 | unsigned int vector; | |
546 | int ret, queue_no; | |
547 | MSIMessage msg; | |
548 | ||
549 | for (queue_no = 0; queue_no < nvqs; queue_no++) { | |
550 | if (!virtio_queue_get_num(vdev, queue_no)) { | |
551 | break; | |
552 | } | |
553 | vector = virtio_queue_vector(vdev, queue_no); | |
554 | if (vector >= msix_nr_vectors_allocated(dev)) { | |
555 | continue; | |
556 | } | |
557 | msg = msix_get_message(dev, vector); | |
558 | ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg); | |
559 | if (ret < 0) { | |
560 | goto undo; | |
7d37d351 | 561 | } |
f1d0f15a MT |
562 | /* If guest supports masking, set up irqfd now. |
563 | * Otherwise, delay until unmasked in the frontend. | |
564 | */ | |
565 | if (proxy->vdev->guest_notifier_mask) { | |
566 | ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector); | |
567 | if (ret < 0) { | |
568 | kvm_virtio_pci_vq_vector_release(proxy, vector); | |
569 | goto undo; | |
570 | } | |
571 | } | |
7d37d351 | 572 | } |
7d37d351 | 573 | return 0; |
774345f9 MT |
574 | |
575 | undo: | |
576 | while (--queue_no >= 0) { | |
577 | vector = virtio_queue_vector(vdev, queue_no); | |
578 | if (vector >= msix_nr_vectors_allocated(dev)) { | |
579 | continue; | |
580 | } | |
f1d0f15a | 581 | if (proxy->vdev->guest_notifier_mask) { |
e387f99e | 582 | kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); |
f1d0f15a | 583 | } |
774345f9 MT |
584 | kvm_virtio_pci_vq_vector_release(proxy, vector); |
585 | } | |
586 | return ret; | |
7d37d351 JK |
587 | } |
588 | ||
774345f9 MT |
589 | static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs) |
590 | { | |
591 | PCIDevice *dev = &proxy->pci_dev; | |
592 | VirtIODevice *vdev = proxy->vdev; | |
593 | unsigned int vector; | |
594 | int queue_no; | |
595 | ||
596 | for (queue_no = 0; queue_no < nvqs; queue_no++) { | |
597 | if (!virtio_queue_get_num(vdev, queue_no)) { | |
598 | break; | |
599 | } | |
600 | vector = virtio_queue_vector(vdev, queue_no); | |
601 | if (vector >= msix_nr_vectors_allocated(dev)) { | |
602 | continue; | |
603 | } | |
f1d0f15a MT |
604 | /* If guest supports masking, clean up irqfd now. |
605 | * Otherwise, it was cleaned when masked in the frontend. | |
606 | */ | |
607 | if (proxy->vdev->guest_notifier_mask) { | |
e387f99e | 608 | kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); |
f1d0f15a | 609 | } |
774345f9 MT |
610 | kvm_virtio_pci_vq_vector_release(proxy, vector); |
611 | } | |
612 | } | |
613 | ||
a38b2c49 MT |
614 | static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy, |
615 | unsigned int queue_no, | |
616 | unsigned int vector, | |
617 | MSIMessage msg) | |
774345f9 MT |
618 | { |
619 | VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no); | |
620 | EventNotifier *n = virtio_queue_get_guest_notifier(vq); | |
a38b2c49 | 621 | VirtIOIRQFD *irqfd; |
53510bfc | 622 | int ret = 0; |
774345f9 | 623 | |
a38b2c49 MT |
624 | if (proxy->vector_irqfd) { |
625 | irqfd = &proxy->vector_irqfd[vector]; | |
626 | if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) { | |
627 | ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg); | |
628 | if (ret < 0) { | |
629 | return ret; | |
630 | } | |
774345f9 MT |
631 | } |
632 | } | |
633 | ||
f1d0f15a MT |
634 | /* If guest supports masking, irqfd is already setup, unmask it. |
635 | * Otherwise, set it up now. | |
636 | */ | |
637 | if (proxy->vdev->guest_notifier_mask) { | |
638 | proxy->vdev->guest_notifier_mask(proxy->vdev, queue_no, false); | |
639 | /* Test after unmasking to avoid losing events. */ | |
640 | if (proxy->vdev->guest_notifier_pending && | |
641 | proxy->vdev->guest_notifier_pending(proxy->vdev, queue_no)) { | |
642 | event_notifier_set(n); | |
643 | } | |
644 | } else { | |
645 | ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector); | |
7d37d351 | 646 | } |
774345f9 | 647 | return ret; |
7d37d351 JK |
648 | } |
649 | ||
a38b2c49 | 650 | static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy, |
7d37d351 JK |
651 | unsigned int queue_no, |
652 | unsigned int vector) | |
653 | { | |
f1d0f15a MT |
654 | /* If guest supports masking, keep irqfd but mask it. |
655 | * Otherwise, clean it up now. | |
656 | */ | |
657 | if (proxy->vdev->guest_notifier_mask) { | |
658 | proxy->vdev->guest_notifier_mask(proxy->vdev, queue_no, true); | |
659 | } else { | |
e387f99e | 660 | kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); |
f1d0f15a | 661 | } |
7d37d351 JK |
662 | } |
663 | ||
a38b2c49 MT |
664 | static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector, |
665 | MSIMessage msg) | |
7d37d351 JK |
666 | { |
667 | VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); | |
668 | VirtIODevice *vdev = proxy->vdev; | |
669 | int ret, queue_no; | |
670 | ||
2d620f59 | 671 | for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) { |
7d37d351 JK |
672 | if (!virtio_queue_get_num(vdev, queue_no)) { |
673 | break; | |
674 | } | |
675 | if (virtio_queue_vector(vdev, queue_no) != vector) { | |
676 | continue; | |
677 | } | |
a38b2c49 | 678 | ret = virtio_pci_vq_vector_unmask(proxy, queue_no, vector, msg); |
7d37d351 JK |
679 | if (ret < 0) { |
680 | goto undo; | |
681 | } | |
682 | } | |
683 | return 0; | |
684 | ||
685 | undo: | |
686 | while (--queue_no >= 0) { | |
687 | if (virtio_queue_vector(vdev, queue_no) != vector) { | |
688 | continue; | |
689 | } | |
a38b2c49 | 690 | virtio_pci_vq_vector_mask(proxy, queue_no, vector); |
7d37d351 JK |
691 | } |
692 | return ret; | |
693 | } | |
694 | ||
a38b2c49 | 695 | static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector) |
7d37d351 JK |
696 | { |
697 | VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); | |
698 | VirtIODevice *vdev = proxy->vdev; | |
699 | int queue_no; | |
700 | ||
2d620f59 | 701 | for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) { |
7d37d351 JK |
702 | if (!virtio_queue_get_num(vdev, queue_no)) { |
703 | break; | |
704 | } | |
705 | if (virtio_queue_vector(vdev, queue_no) != vector) { | |
706 | continue; | |
707 | } | |
a38b2c49 | 708 | virtio_pci_vq_vector_mask(proxy, queue_no, vector); |
7d37d351 JK |
709 | } |
710 | } | |
711 | ||
a38b2c49 MT |
712 | static void virtio_pci_vector_poll(PCIDevice *dev, |
713 | unsigned int vector_start, | |
714 | unsigned int vector_end) | |
89d62be9 MT |
715 | { |
716 | VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); | |
717 | VirtIODevice *vdev = proxy->vdev; | |
718 | int queue_no; | |
719 | unsigned int vector; | |
720 | EventNotifier *notifier; | |
721 | VirtQueue *vq; | |
722 | ||
2d620f59 | 723 | for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) { |
89d62be9 MT |
724 | if (!virtio_queue_get_num(vdev, queue_no)) { |
725 | break; | |
726 | } | |
727 | vector = virtio_queue_vector(vdev, queue_no); | |
728 | if (vector < vector_start || vector >= vector_end || | |
729 | !msix_is_masked(dev, vector)) { | |
730 | continue; | |
731 | } | |
732 | vq = virtio_get_queue(vdev, queue_no); | |
733 | notifier = virtio_queue_get_guest_notifier(vq); | |
f1d0f15a MT |
734 | if (vdev->guest_notifier_pending) { |
735 | if (vdev->guest_notifier_pending(vdev, queue_no)) { | |
736 | msix_set_pending(dev, vector); | |
737 | } | |
738 | } else if (event_notifier_test_and_clear(notifier)) { | |
89d62be9 MT |
739 | msix_set_pending(dev, vector); |
740 | } | |
741 | } | |
742 | } | |
743 | ||
744 | static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign, | |
745 | bool with_irqfd) | |
ade80dc8 | 746 | { |
d2a0ccc6 | 747 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
ade80dc8 MT |
748 | VirtQueue *vq = virtio_get_queue(proxy->vdev, n); |
749 | EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); | |
750 | ||
751 | if (assign) { | |
752 | int r = event_notifier_init(notifier, 0); | |
753 | if (r < 0) { | |
754 | return r; | |
755 | } | |
89d62be9 | 756 | virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd); |
ade80dc8 | 757 | } else { |
89d62be9 | 758 | virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd); |
ade80dc8 MT |
759 | event_notifier_cleanup(notifier); |
760 | } | |
761 | ||
762 | return 0; | |
763 | } | |
764 | ||
d2a0ccc6 | 765 | static bool virtio_pci_query_guest_notifiers(DeviceState *d) |
5430a28f | 766 | { |
d2a0ccc6 | 767 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
5430a28f | 768 | return msix_enabled(&proxy->pci_dev); |
769 | } | |
770 | ||
2d620f59 | 771 | static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign) |
54dd9321 | 772 | { |
d2a0ccc6 | 773 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
54dd9321 MT |
774 | VirtIODevice *vdev = proxy->vdev; |
775 | int r, n; | |
89d62be9 MT |
776 | bool with_irqfd = msix_enabled(&proxy->pci_dev) && |
777 | kvm_msi_via_irqfd_enabled(); | |
54dd9321 | 778 | |
2d620f59 MT |
779 | nvqs = MIN(nvqs, VIRTIO_PCI_QUEUE_MAX); |
780 | ||
781 | /* When deassigning, pass a consistent nvqs value | |
782 | * to avoid leaking notifiers. | |
783 | */ | |
784 | assert(assign || nvqs == proxy->nvqs_with_notifiers); | |
785 | ||
786 | proxy->nvqs_with_notifiers = nvqs; | |
787 | ||
7d37d351 | 788 | /* Must unset vector notifier while guest notifier is still assigned */ |
a38b2c49 | 789 | if ((proxy->vector_irqfd || vdev->guest_notifier_mask) && !assign) { |
7d37d351 | 790 | msix_unset_vector_notifiers(&proxy->pci_dev); |
a38b2c49 MT |
791 | if (proxy->vector_irqfd) { |
792 | kvm_virtio_pci_vector_release(proxy, nvqs); | |
793 | g_free(proxy->vector_irqfd); | |
794 | proxy->vector_irqfd = NULL; | |
795 | } | |
7d37d351 JK |
796 | } |
797 | ||
2d620f59 | 798 | for (n = 0; n < nvqs; n++) { |
54dd9321 MT |
799 | if (!virtio_queue_get_num(vdev, n)) { |
800 | break; | |
801 | } | |
802 | ||
89d62be9 MT |
803 | r = virtio_pci_set_guest_notifier(d, n, assign, |
804 | kvm_msi_via_irqfd_enabled()); | |
54dd9321 MT |
805 | if (r < 0) { |
806 | goto assign_error; | |
807 | } | |
808 | } | |
809 | ||
7d37d351 | 810 | /* Must set vector notifier after guest notifier has been assigned */ |
a38b2c49 MT |
811 | if ((with_irqfd || vdev->guest_notifier_mask) && assign) { |
812 | if (with_irqfd) { | |
813 | proxy->vector_irqfd = | |
814 | g_malloc0(sizeof(*proxy->vector_irqfd) * | |
815 | msix_nr_vectors_allocated(&proxy->pci_dev)); | |
816 | r = kvm_virtio_pci_vector_use(proxy, nvqs); | |
817 | if (r < 0) { | |
818 | goto assign_error; | |
819 | } | |
774345f9 | 820 | } |
7d37d351 | 821 | r = msix_set_vector_notifiers(&proxy->pci_dev, |
a38b2c49 MT |
822 | virtio_pci_vector_unmask, |
823 | virtio_pci_vector_mask, | |
824 | virtio_pci_vector_poll); | |
7d37d351 | 825 | if (r < 0) { |
774345f9 | 826 | goto notifiers_error; |
7d37d351 JK |
827 | } |
828 | } | |
829 | ||
54dd9321 MT |
830 | return 0; |
831 | ||
774345f9 | 832 | notifiers_error: |
a38b2c49 MT |
833 | if (with_irqfd) { |
834 | assert(assign); | |
835 | kvm_virtio_pci_vector_release(proxy, nvqs); | |
836 | } | |
774345f9 | 837 | |
54dd9321 MT |
838 | assign_error: |
839 | /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */ | |
7d37d351 | 840 | assert(assign); |
54dd9321 | 841 | while (--n >= 0) { |
89d62be9 | 842 | virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd); |
54dd9321 MT |
843 | } |
844 | return r; | |
845 | } | |
846 | ||
d2a0ccc6 | 847 | static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign) |
ade80dc8 | 848 | { |
d2a0ccc6 | 849 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
25db9ebe SH |
850 | |
851 | /* Stop using ioeventfd for virtqueue kick if the device starts using host | |
852 | * notifiers. This makes it easy to avoid stepping on each others' toes. | |
853 | */ | |
854 | proxy->ioeventfd_disabled = assign; | |
ade80dc8 | 855 | if (assign) { |
25db9ebe SH |
856 | virtio_pci_stop_ioeventfd(proxy); |
857 | } | |
858 | /* We don't need to start here: it's not needed because backend | |
859 | * currently only stops on status change away from ok, | |
860 | * reset, vmstop and such. If we do add code to start here, | |
861 | * need to check vmstate, device state etc. */ | |
26b9b5fe | 862 | return virtio_pci_set_host_notifier_internal(proxy, n, assign, false); |
25db9ebe SH |
863 | } |
864 | ||
d2a0ccc6 | 865 | static void virtio_pci_vmstate_change(DeviceState *d, bool running) |
25db9ebe | 866 | { |
d2a0ccc6 | 867 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
25db9ebe SH |
868 | |
869 | if (running) { | |
89c473fd MT |
870 | /* Try to find out if the guest has bus master disabled, but is |
871 | in ready state. Then we have a buggy guest OS. */ | |
872 | if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) && | |
873 | !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { | |
874 | proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG; | |
875 | } | |
25db9ebe | 876 | virtio_pci_start_ioeventfd(proxy); |
ade80dc8 | 877 | } else { |
25db9ebe | 878 | virtio_pci_stop_ioeventfd(proxy); |
ade80dc8 | 879 | } |
ade80dc8 MT |
880 | } |
881 | ||
53c25cea | 882 | static const VirtIOBindings virtio_pci_bindings = { |
ff24bd58 MT |
883 | .notify = virtio_pci_notify, |
884 | .save_config = virtio_pci_save_config, | |
885 | .load_config = virtio_pci_load_config, | |
886 | .save_queue = virtio_pci_save_queue, | |
887 | .load_queue = virtio_pci_load_queue, | |
6d74ca5a | 888 | .get_features = virtio_pci_get_features, |
5430a28f | 889 | .query_guest_notifiers = virtio_pci_query_guest_notifiers, |
ade80dc8 | 890 | .set_host_notifier = virtio_pci_set_host_notifier, |
54dd9321 | 891 | .set_guest_notifiers = virtio_pci_set_guest_notifiers, |
25db9ebe | 892 | .vmstate_change = virtio_pci_vmstate_change, |
53c25cea PB |
893 | }; |
894 | ||
befeac45 | 895 | void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev) |
53c25cea PB |
896 | { |
897 | uint8_t *config; | |
898 | uint32_t size; | |
899 | ||
900 | proxy->vdev = vdev; | |
901 | ||
902 | config = proxy->pci_dev.config; | |
53c25cea | 903 | |
e75ccf2c IY |
904 | if (proxy->class_code) { |
905 | pci_config_set_class(config, proxy->class_code); | |
906 | } | |
ad3d11e6 HKR |
907 | pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID, |
908 | pci_get_word(config + PCI_VENDOR_ID)); | |
909 | pci_set_word(config + PCI_SUBSYSTEM_ID, vdev->device_id); | |
910 | config[PCI_INTERRUPT_PIN] = 1; | |
53c25cea | 911 | |
b2357c48 AW |
912 | if (vdev->nvectors && |
913 | msix_init_exclusive_bar(&proxy->pci_dev, vdev->nvectors, 1)) { | |
aba800a3 | 914 | vdev->nvectors = 0; |
b2357c48 | 915 | } |
aba800a3 | 916 | |
ed757e14 YV |
917 | proxy->pci_dev.config_write = virtio_write_config; |
918 | ||
aba800a3 | 919 | size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len; |
53c25cea PB |
920 | if (size & (size-1)) |
921 | size = 1 << qemu_fls(size); | |
922 | ||
da146d0a AK |
923 | memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy, |
924 | "virtio-pci", size); | |
e824b2cc AK |
925 | pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO, |
926 | &proxy->bar); | |
53c25cea | 927 | |
25db9ebe SH |
928 | if (!kvm_has_many_ioeventfds()) { |
929 | proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD; | |
930 | } | |
931 | ||
d2a0ccc6 | 932 | virtio_bind_device(vdev, &virtio_pci_bindings, DEVICE(proxy)); |
8172539d MT |
933 | proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY; |
934 | proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE; | |
935 | proxy->host_features = vdev->get_features(vdev, proxy->host_features); | |
53c25cea PB |
936 | } |
937 | ||
f90c2bcd | 938 | static void virtio_exit_pci(PCIDevice *pci_dev) |
0f457d91 | 939 | { |
da146d0a AK |
940 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
941 | ||
942 | memory_region_destroy(&proxy->bar); | |
b2357c48 | 943 | msix_uninit_exclusive_bar(pci_dev); |
0f457d91 MT |
944 | } |
945 | ||
16c915ba AS |
946 | static int virtio_rng_init_pci(PCIDevice *pci_dev) |
947 | { | |
948 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); | |
949 | VirtIODevice *vdev; | |
950 | ||
500054f1 AL |
951 | if (proxy->rng.rng == NULL) { |
952 | proxy->rng.default_backend = RNG_RANDOM(object_new(TYPE_RNG_RANDOM)); | |
953 | ||
954 | object_property_add_child(OBJECT(pci_dev), | |
955 | "default-backend", | |
956 | OBJECT(proxy->rng.default_backend), | |
957 | NULL); | |
958 | ||
959 | object_property_set_link(OBJECT(pci_dev), | |
960 | OBJECT(proxy->rng.default_backend), | |
961 | "rng", NULL); | |
962 | } | |
963 | ||
16c915ba AS |
964 | vdev = virtio_rng_init(&pci_dev->qdev, &proxy->rng); |
965 | if (!vdev) { | |
966 | return -1; | |
967 | } | |
968 | virtio_init_pci(proxy, vdev); | |
969 | return 0; | |
970 | } | |
971 | ||
972 | static void virtio_rng_exit_pci(PCIDevice *pci_dev) | |
973 | { | |
974 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); | |
975 | ||
976 | virtio_pci_stop_ioeventfd(proxy); | |
977 | virtio_rng_exit(proxy->vdev); | |
978 | virtio_exit_pci(pci_dev); | |
979 | } | |
980 | ||
16c915ba AS |
981 | static void virtio_rng_initfn(Object *obj) |
982 | { | |
983 | PCIDevice *pci_dev = PCI_DEVICE(obj); | |
984 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); | |
985 | ||
986 | object_property_add_link(obj, "rng", TYPE_RNG_BACKEND, | |
987 | (Object **)&proxy->rng.rng, NULL); | |
988 | } | |
989 | ||
990 | static Property virtio_rng_properties[] = { | |
991 | DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), | |
904d6f58 AL |
992 | /* Set a default rate limit of 2^47 bytes per minute or roughly 2TB/s. If |
993 | you have an entropy source capable of generating more entropy than this | |
994 | and you can pass it through via virtio-rng, then hats off to you. Until | |
995 | then, this is unlimited for all practical purposes. | |
996 | */ | |
997 | DEFINE_PROP_UINT64("max-bytes", VirtIOPCIProxy, rng.max_bytes, INT64_MAX), | |
998 | DEFINE_PROP_UINT32("period", VirtIOPCIProxy, rng.period_ms, 1 << 16), | |
16c915ba AS |
999 | DEFINE_PROP_END_OF_LIST(), |
1000 | }; | |
1001 | ||
1002 | static void virtio_rng_class_init(ObjectClass *klass, void *data) | |
1003 | { | |
1004 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1005 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); | |
1006 | ||
1007 | k->init = virtio_rng_init_pci; | |
1008 | k->exit = virtio_rng_exit_pci; | |
1009 | k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
1010 | k->device_id = PCI_DEVICE_ID_VIRTIO_RNG; | |
1011 | k->revision = VIRTIO_PCI_ABI_VERSION; | |
1012 | k->class_id = PCI_CLASS_OTHERS; | |
1013 | dc->reset = virtio_pci_reset; | |
1014 | dc->props = virtio_rng_properties; | |
1015 | } | |
1016 | ||
8c43a6f0 | 1017 | static const TypeInfo virtio_rng_info = { |
16c915ba AS |
1018 | .name = "virtio-rng-pci", |
1019 | .parent = TYPE_PCI_DEVICE, | |
1020 | .instance_size = sizeof(VirtIOPCIProxy), | |
1021 | .instance_init = virtio_rng_initfn, | |
1022 | .class_init = virtio_rng_class_init, | |
1023 | }; | |
1024 | ||
60653b28 PB |
1025 | #ifdef CONFIG_VIRTFS |
1026 | static int virtio_9p_init_pci(PCIDevice *pci_dev) | |
1027 | { | |
1028 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); | |
1029 | VirtIODevice *vdev; | |
1030 | ||
1031 | vdev = virtio_9p_init(&pci_dev->qdev, &proxy->fsconf); | |
1032 | vdev->nvectors = proxy->nvectors; | |
1033 | virtio_init_pci(proxy, vdev); | |
1034 | /* make the actual value visible */ | |
1035 | proxy->nvectors = vdev->nvectors; | |
1036 | return 0; | |
1037 | } | |
1038 | ||
1039 | static Property virtio_9p_properties[] = { | |
1040 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), | |
1041 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), | |
1042 | DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), | |
1043 | DEFINE_PROP_STRING("mount_tag", VirtIOPCIProxy, fsconf.tag), | |
1044 | DEFINE_PROP_STRING("fsdev", VirtIOPCIProxy, fsconf.fsdev_id), | |
1045 | DEFINE_PROP_END_OF_LIST(), | |
1046 | }; | |
1047 | ||
1048 | static void virtio_9p_class_init(ObjectClass *klass, void *data) | |
1049 | { | |
1050 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1051 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); | |
1052 | ||
1053 | k->init = virtio_9p_init_pci; | |
1054 | k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
1055 | k->device_id = PCI_DEVICE_ID_VIRTIO_9P; | |
1056 | k->revision = VIRTIO_PCI_ABI_VERSION; | |
1057 | k->class_id = 0x2; | |
1058 | dc->props = virtio_9p_properties; | |
1059 | dc->reset = virtio_pci_reset; | |
1060 | } | |
1061 | ||
1062 | static const TypeInfo virtio_9p_info = { | |
1063 | .name = "virtio-9p-pci", | |
1064 | .parent = TYPE_PCI_DEVICE, | |
1065 | .instance_size = sizeof(VirtIOPCIProxy), | |
1066 | .class_init = virtio_9p_class_init, | |
1067 | }; | |
1068 | #endif | |
1069 | ||
085bccb7 KF |
1070 | /* |
1071 | * virtio-pci: This is the PCIDevice which has a virtio-pci-bus. | |
1072 | */ | |
1073 | ||
1074 | /* This is called by virtio-bus just after the device is plugged. */ | |
1075 | static void virtio_pci_device_plugged(DeviceState *d) | |
1076 | { | |
1077 | VirtIOPCIProxy *proxy = VIRTIO_PCI(d); | |
1078 | VirtioBusState *bus = &proxy->bus; | |
1079 | uint8_t *config; | |
1080 | uint32_t size; | |
1081 | ||
1082 | proxy->vdev = bus->vdev; | |
1083 | ||
1084 | config = proxy->pci_dev.config; | |
1085 | if (proxy->class_code) { | |
1086 | pci_config_set_class(config, proxy->class_code); | |
1087 | } | |
1088 | pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID, | |
1089 | pci_get_word(config + PCI_VENDOR_ID)); | |
1090 | pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus)); | |
1091 | config[PCI_INTERRUPT_PIN] = 1; | |
1092 | ||
1093 | if (proxy->nvectors && | |
1094 | msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors, 1)) { | |
1095 | proxy->nvectors = 0; | |
1096 | } | |
1097 | ||
1098 | proxy->pci_dev.config_write = virtio_write_config; | |
1099 | ||
1100 | size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) | |
1101 | + virtio_bus_get_vdev_config_len(bus); | |
1102 | if (size & (size - 1)) { | |
1103 | size = 1 << qemu_fls(size); | |
1104 | } | |
1105 | ||
1106 | memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy, | |
1107 | "virtio-pci", size); | |
1108 | pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO, | |
1109 | &proxy->bar); | |
1110 | ||
1111 | if (!kvm_has_many_ioeventfds()) { | |
1112 | proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD; | |
1113 | } | |
1114 | ||
1115 | proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY; | |
1116 | proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE; | |
1117 | proxy->host_features = virtio_bus_get_vdev_features(bus, | |
1118 | proxy->host_features); | |
1119 | } | |
1120 | ||
085bccb7 KF |
1121 | static int virtio_pci_init(PCIDevice *pci_dev) |
1122 | { | |
1123 | VirtIOPCIProxy *dev = VIRTIO_PCI(pci_dev); | |
1124 | VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev); | |
1125 | virtio_pci_bus_new(&dev->bus, dev); | |
1126 | if (k->init != NULL) { | |
1127 | return k->init(dev); | |
1128 | } | |
1129 | return 0; | |
1130 | } | |
1131 | ||
1132 | static void virtio_pci_exit(PCIDevice *pci_dev) | |
1133 | { | |
1134 | VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev); | |
10479a80 | 1135 | virtio_pci_stop_ioeventfd(proxy); |
085bccb7 KF |
1136 | virtio_exit_pci(pci_dev); |
1137 | } | |
1138 | ||
1139 | /* | |
1140 | * This will be renamed virtio_pci_reset at the end of the series. | |
1141 | * virtio_pci_reset is still in use at this moment. | |
1142 | */ | |
1143 | static void virtio_pci_rst(DeviceState *qdev) | |
1144 | { | |
1145 | VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev); | |
1146 | VirtioBusState *bus = VIRTIO_BUS(&proxy->bus); | |
1147 | virtio_pci_stop_ioeventfd(proxy); | |
1148 | virtio_bus_reset(bus); | |
1149 | msix_unuse_all_vectors(&proxy->pci_dev); | |
1150 | proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG; | |
1151 | } | |
1152 | ||
1153 | static void virtio_pci_class_init(ObjectClass *klass, void *data) | |
1154 | { | |
1155 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1156 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); | |
1157 | ||
1158 | k->init = virtio_pci_init; | |
1159 | k->exit = virtio_pci_exit; | |
1160 | k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
1161 | k->revision = VIRTIO_PCI_ABI_VERSION; | |
1162 | k->class_id = PCI_CLASS_OTHERS; | |
1163 | dc->reset = virtio_pci_rst; | |
1164 | } | |
1165 | ||
1166 | static const TypeInfo virtio_pci_info = { | |
1167 | .name = TYPE_VIRTIO_PCI, | |
1168 | .parent = TYPE_PCI_DEVICE, | |
1169 | .instance_size = sizeof(VirtIOPCIProxy), | |
1170 | .class_init = virtio_pci_class_init, | |
1171 | .class_size = sizeof(VirtioPCIClass), | |
1172 | .abstract = true, | |
1173 | }; | |
1174 | ||
653ced07 KF |
1175 | /* virtio-blk-pci */ |
1176 | ||
1177 | static Property virtio_blk_pci_properties[] = { | |
1178 | DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0), | |
1179 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, | |
1180 | VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), | |
1181 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), | |
1182 | #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE | |
1183 | DEFINE_PROP_BIT("x-data-plane", VirtIOBlkPCI, blk.data_plane, 0, false), | |
1184 | #endif | |
1185 | DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features), | |
1186 | DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlkPCI, blk), | |
1187 | DEFINE_PROP_END_OF_LIST(), | |
1188 | }; | |
1189 | ||
1190 | static int virtio_blk_pci_init(VirtIOPCIProxy *vpci_dev) | |
1191 | { | |
1192 | VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev); | |
1193 | DeviceState *vdev = DEVICE(&dev->vdev); | |
1194 | virtio_blk_set_conf(vdev, &(dev->blk)); | |
1195 | qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); | |
1196 | if (qdev_init(vdev) < 0) { | |
1197 | return -1; | |
1198 | } | |
1199 | return 0; | |
1200 | } | |
1201 | ||
1202 | static void virtio_blk_pci_class_init(ObjectClass *klass, void *data) | |
1203 | { | |
1204 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1205 | VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); | |
1206 | PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); | |
1207 | ||
1208 | dc->props = virtio_blk_pci_properties; | |
1209 | k->init = virtio_blk_pci_init; | |
1210 | pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
1211 | pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK; | |
1212 | pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; | |
1213 | pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; | |
1214 | } | |
1215 | ||
1216 | static void virtio_blk_pci_instance_init(Object *obj) | |
1217 | { | |
1218 | VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj); | |
1219 | object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_BLK); | |
1220 | object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); | |
1221 | } | |
1222 | ||
1223 | static const TypeInfo virtio_blk_pci_info = { | |
1224 | .name = TYPE_VIRTIO_BLK_PCI, | |
1225 | .parent = TYPE_VIRTIO_PCI, | |
1226 | .instance_size = sizeof(VirtIOBlkPCI), | |
1227 | .instance_init = virtio_blk_pci_instance_init, | |
1228 | .class_init = virtio_blk_pci_class_init, | |
1229 | }; | |
1230 | ||
bc7b90a0 KF |
1231 | /* virtio-scsi-pci */ |
1232 | ||
1233 | static Property virtio_scsi_pci_properties[] = { | |
1234 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, | |
1235 | VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), | |
1236 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, | |
1237 | DEV_NVECTORS_UNSPECIFIED), | |
1238 | DEFINE_VIRTIO_SCSI_FEATURES(VirtIOPCIProxy, host_features), | |
292c8e50 | 1239 | DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSIPCI, vdev.parent_obj.conf), |
bc7b90a0 KF |
1240 | DEFINE_PROP_END_OF_LIST(), |
1241 | }; | |
1242 | ||
1243 | static int virtio_scsi_pci_init_pci(VirtIOPCIProxy *vpci_dev) | |
1244 | { | |
1245 | VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev); | |
1246 | DeviceState *vdev = DEVICE(&dev->vdev); | |
292c8e50 | 1247 | VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); |
bc7b90a0 KF |
1248 | |
1249 | if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { | |
292c8e50 | 1250 | vpci_dev->nvectors = vs->conf.num_queues + 3; |
bc7b90a0 KF |
1251 | } |
1252 | ||
1253 | qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); | |
1254 | if (qdev_init(vdev) < 0) { | |
1255 | return -1; | |
1256 | } | |
1257 | return 0; | |
1258 | } | |
1259 | ||
1260 | static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data) | |
1261 | { | |
1262 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1263 | VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); | |
1264 | PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); | |
1265 | k->init = virtio_scsi_pci_init_pci; | |
1266 | dc->props = virtio_scsi_pci_properties; | |
1267 | pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
1268 | pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI; | |
1269 | pcidev_k->revision = 0x00; | |
1270 | pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; | |
1271 | } | |
1272 | ||
1273 | static void virtio_scsi_pci_instance_init(Object *obj) | |
1274 | { | |
1275 | VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj); | |
1276 | object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_SCSI); | |
1277 | object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); | |
1278 | } | |
1279 | ||
1280 | static const TypeInfo virtio_scsi_pci_info = { | |
1281 | .name = TYPE_VIRTIO_SCSI_PCI, | |
1282 | .parent = TYPE_VIRTIO_PCI, | |
1283 | .instance_size = sizeof(VirtIOSCSIPCI), | |
1284 | .instance_init = virtio_scsi_pci_instance_init, | |
1285 | .class_init = virtio_scsi_pci_class_init, | |
1286 | }; | |
1287 | ||
e378e88d KF |
1288 | /* virtio-balloon-pci */ |
1289 | ||
24a6e7f4 KF |
1290 | static void balloon_pci_stats_get_all(Object *obj, struct Visitor *v, |
1291 | void *opaque, const char *name, | |
1292 | Error **errp) | |
1293 | { | |
1294 | VirtIOBalloonPCI *dev = opaque; | |
1295 | object_property_get(OBJECT(&dev->vdev), v, "guest-stats", errp); | |
1296 | } | |
1297 | ||
1298 | static void balloon_pci_stats_get_poll_interval(Object *obj, struct Visitor *v, | |
1299 | void *opaque, const char *name, | |
1300 | Error **errp) | |
1301 | { | |
1302 | VirtIOBalloonPCI *dev = opaque; | |
1303 | object_property_get(OBJECT(&dev->vdev), v, "guest-stats-polling-interval", | |
1304 | errp); | |
1305 | } | |
1306 | ||
1307 | static void balloon_pci_stats_set_poll_interval(Object *obj, struct Visitor *v, | |
1308 | void *opaque, const char *name, | |
1309 | Error **errp) | |
1310 | { | |
1311 | VirtIOBalloonPCI *dev = opaque; | |
1312 | object_property_set(OBJECT(&dev->vdev), v, "guest-stats-polling-interval", | |
1313 | errp); | |
1314 | } | |
1315 | ||
e378e88d KF |
1316 | static Property virtio_balloon_pci_properties[] = { |
1317 | DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), | |
1318 | DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0), | |
1319 | DEFINE_PROP_END_OF_LIST(), | |
1320 | }; | |
1321 | ||
1322 | static int virtio_balloon_pci_init(VirtIOPCIProxy *vpci_dev) | |
1323 | { | |
1324 | VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev); | |
1325 | DeviceState *vdev = DEVICE(&dev->vdev); | |
1326 | ||
1327 | if (vpci_dev->class_code != PCI_CLASS_OTHERS && | |
1328 | vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */ | |
1329 | vpci_dev->class_code = PCI_CLASS_OTHERS; | |
1330 | } | |
1331 | ||
1332 | qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); | |
1333 | if (qdev_init(vdev) < 0) { | |
1334 | return -1; | |
1335 | } | |
1336 | return 0; | |
1337 | } | |
1338 | ||
1339 | static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data) | |
1340 | { | |
1341 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1342 | VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); | |
1343 | PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); | |
1344 | k->init = virtio_balloon_pci_init; | |
1345 | dc->props = virtio_balloon_pci_properties; | |
1346 | pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
1347 | pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON; | |
1348 | pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; | |
1349 | pcidev_k->class_id = PCI_CLASS_OTHERS; | |
1350 | } | |
1351 | ||
1352 | static void virtio_balloon_pci_instance_init(Object *obj) | |
1353 | { | |
1354 | VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj); | |
1355 | object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_BALLOON); | |
1356 | object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); | |
24a6e7f4 KF |
1357 | |
1358 | object_property_add(obj, "guest-stats", "guest statistics", | |
1359 | balloon_pci_stats_get_all, NULL, NULL, dev, | |
1360 | NULL); | |
1361 | ||
1362 | object_property_add(obj, "guest-stats-polling-interval", "int", | |
1363 | balloon_pci_stats_get_poll_interval, | |
1364 | balloon_pci_stats_set_poll_interval, | |
1365 | NULL, dev, NULL); | |
e378e88d KF |
1366 | } |
1367 | ||
1368 | static const TypeInfo virtio_balloon_pci_info = { | |
1369 | .name = TYPE_VIRTIO_BALLOON_PCI, | |
1370 | .parent = TYPE_VIRTIO_PCI, | |
1371 | .instance_size = sizeof(VirtIOBalloonPCI), | |
1372 | .instance_init = virtio_balloon_pci_instance_init, | |
1373 | .class_init = virtio_balloon_pci_class_init, | |
1374 | }; | |
1375 | ||
f7f7464a KF |
1376 | /* virtio-serial-pci */ |
1377 | ||
1378 | static int virtio_serial_pci_init(VirtIOPCIProxy *vpci_dev) | |
1379 | { | |
1380 | VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev); | |
1381 | DeviceState *vdev = DEVICE(&dev->vdev); | |
1382 | ||
1383 | if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER && | |
1384 | vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */ | |
1385 | vpci_dev->class_code != PCI_CLASS_OTHERS) { /* qemu-kvm */ | |
1386 | vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER; | |
1387 | } | |
1388 | ||
1389 | /* backwards-compatibility with machines that were created with | |
1390 | DEV_NVECTORS_UNSPECIFIED */ | |
1391 | if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { | |
1392 | vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1; | |
1393 | } | |
1394 | ||
1395 | qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); | |
1396 | if (qdev_init(vdev) < 0) { | |
1397 | return -1; | |
1398 | } | |
1399 | return 0; | |
1400 | } | |
1401 | ||
1402 | static Property virtio_serial_pci_properties[] = { | |
1403 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, | |
1404 | VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), | |
1405 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), | |
1406 | DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0), | |
1407 | DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), | |
1408 | DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtIOSerialPCI, vdev.serial), | |
1409 | DEFINE_PROP_END_OF_LIST(), | |
1410 | }; | |
1411 | ||
1412 | static void virtio_serial_pci_class_init(ObjectClass *klass, void *data) | |
1413 | { | |
1414 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1415 | VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); | |
1416 | PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); | |
1417 | k->init = virtio_serial_pci_init; | |
1418 | dc->props = virtio_serial_pci_properties; | |
1419 | pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
1420 | pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE; | |
1421 | pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; | |
1422 | pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER; | |
1423 | } | |
1424 | ||
1425 | static void virtio_serial_pci_instance_init(Object *obj) | |
1426 | { | |
1427 | VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj); | |
1428 | object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_SERIAL); | |
1429 | object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); | |
1430 | } | |
1431 | ||
1432 | static const TypeInfo virtio_serial_pci_info = { | |
1433 | .name = TYPE_VIRTIO_SERIAL_PCI, | |
1434 | .parent = TYPE_VIRTIO_PCI, | |
1435 | .instance_size = sizeof(VirtIOSerialPCI), | |
1436 | .instance_init = virtio_serial_pci_instance_init, | |
1437 | .class_init = virtio_serial_pci_class_init, | |
1438 | }; | |
1439 | ||
e37da394 KF |
1440 | /* virtio-net-pci */ |
1441 | ||
1442 | static Property virtio_net_properties[] = { | |
1443 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, | |
1444 | VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false), | |
1445 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3), | |
1446 | DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features), | |
1447 | DEFINE_NIC_PROPERTIES(VirtIONetPCI, vdev.nic_conf), | |
1448 | DEFINE_VIRTIO_NET_PROPERTIES(VirtIONetPCI, vdev.net_conf), | |
1449 | DEFINE_PROP_END_OF_LIST(), | |
1450 | }; | |
1451 | ||
1452 | static int virtio_net_pci_init(VirtIOPCIProxy *vpci_dev) | |
1453 | { | |
1454 | VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev); | |
1455 | DeviceState *vdev = DEVICE(&dev->vdev); | |
1456 | ||
1457 | virtio_net_set_config_size(&dev->vdev, vpci_dev->host_features); | |
1458 | qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); | |
1459 | if (qdev_init(vdev) < 0) { | |
1460 | return -1; | |
1461 | } | |
1462 | return 0; | |
1463 | } | |
1464 | ||
1465 | static void virtio_net_pci_class_init(ObjectClass *klass, void *data) | |
1466 | { | |
1467 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1468 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); | |
1469 | VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass); | |
1470 | ||
1471 | k->romfile = "efi-virtio.rom"; | |
1472 | k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
1473 | k->device_id = PCI_DEVICE_ID_VIRTIO_NET; | |
1474 | k->revision = VIRTIO_PCI_ABI_VERSION; | |
1475 | k->class_id = PCI_CLASS_NETWORK_ETHERNET; | |
1476 | dc->props = virtio_net_properties; | |
1477 | vpciklass->init = virtio_net_pci_init; | |
1478 | } | |
1479 | ||
1480 | static void virtio_net_pci_instance_init(Object *obj) | |
1481 | { | |
1482 | VirtIONetPCI *dev = VIRTIO_NET_PCI(obj); | |
1483 | object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_NET); | |
1484 | object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); | |
1485 | } | |
1486 | ||
1487 | static const TypeInfo virtio_net_pci_info = { | |
1488 | .name = TYPE_VIRTIO_NET_PCI, | |
1489 | .parent = TYPE_VIRTIO_PCI, | |
1490 | .instance_size = sizeof(VirtIONetPCI), | |
1491 | .instance_init = virtio_net_pci_instance_init, | |
1492 | .class_init = virtio_net_pci_class_init, | |
1493 | }; | |
1494 | ||
0a2acf5e KF |
1495 | /* virtio-pci-bus */ |
1496 | ||
1497 | void virtio_pci_bus_new(VirtioBusState *bus, VirtIOPCIProxy *dev) | |
1498 | { | |
1499 | DeviceState *qdev = DEVICE(dev); | |
1500 | BusState *qbus; | |
1501 | qbus_create_inplace((BusState *)bus, TYPE_VIRTIO_PCI_BUS, qdev, NULL); | |
1502 | qbus = BUS(bus); | |
cbd19063 | 1503 | qbus->allow_hotplug = 1; |
0a2acf5e KF |
1504 | } |
1505 | ||
1506 | static void virtio_pci_bus_class_init(ObjectClass *klass, void *data) | |
1507 | { | |
1508 | BusClass *bus_class = BUS_CLASS(klass); | |
1509 | VirtioBusClass *k = VIRTIO_BUS_CLASS(klass); | |
1510 | bus_class->max_dev = 1; | |
1511 | k->notify = virtio_pci_notify; | |
1512 | k->save_config = virtio_pci_save_config; | |
1513 | k->load_config = virtio_pci_load_config; | |
1514 | k->save_queue = virtio_pci_save_queue; | |
1515 | k->load_queue = virtio_pci_load_queue; | |
1516 | k->get_features = virtio_pci_get_features; | |
1517 | k->query_guest_notifiers = virtio_pci_query_guest_notifiers; | |
1518 | k->set_host_notifier = virtio_pci_set_host_notifier; | |
1519 | k->set_guest_notifiers = virtio_pci_set_guest_notifiers; | |
1520 | k->vmstate_change = virtio_pci_vmstate_change; | |
085bccb7 | 1521 | k->device_plugged = virtio_pci_device_plugged; |
0a2acf5e KF |
1522 | } |
1523 | ||
1524 | static const TypeInfo virtio_pci_bus_info = { | |
1525 | .name = TYPE_VIRTIO_PCI_BUS, | |
1526 | .parent = TYPE_VIRTIO_BUS, | |
1527 | .instance_size = sizeof(VirtioPCIBusState), | |
1528 | .class_init = virtio_pci_bus_class_init, | |
1529 | }; | |
1530 | ||
83f7d43a | 1531 | static void virtio_pci_register_types(void) |
53c25cea | 1532 | { |
16c915ba | 1533 | type_register_static(&virtio_rng_info); |
0a2acf5e | 1534 | type_register_static(&virtio_pci_bus_info); |
085bccb7 | 1535 | type_register_static(&virtio_pci_info); |
60653b28 PB |
1536 | #ifdef CONFIG_VIRTFS |
1537 | type_register_static(&virtio_9p_info); | |
1538 | #endif | |
653ced07 | 1539 | type_register_static(&virtio_blk_pci_info); |
bc7b90a0 | 1540 | type_register_static(&virtio_scsi_pci_info); |
e378e88d | 1541 | type_register_static(&virtio_balloon_pci_info); |
f7f7464a | 1542 | type_register_static(&virtio_serial_pci_info); |
e37da394 | 1543 | type_register_static(&virtio_net_pci_info); |
53c25cea PB |
1544 | } |
1545 | ||
83f7d43a | 1546 | type_init(virtio_pci_register_types) |