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