]>
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 | ||
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 | ||
d51fcfac FK |
101 | static void virtio_pci_bus_new(VirtioBusState *bus, VirtIOPCIProxy *dev); |
102 | ||
53c25cea | 103 | /* virtio device */ |
d2a0ccc6 MT |
104 | /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */ |
105 | static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d) | |
106 | { | |
107 | return container_of(d, VirtIOPCIProxy, pci_dev.qdev); | |
108 | } | |
53c25cea | 109 | |
d2a0ccc6 MT |
110 | /* DeviceState to VirtIOPCIProxy. Note: used on datapath, |
111 | * be careful and test performance if you change this. | |
112 | */ | |
113 | static inline VirtIOPCIProxy *to_virtio_pci_proxy_fast(DeviceState *d) | |
53c25cea | 114 | { |
d2a0ccc6 MT |
115 | return container_of(d, VirtIOPCIProxy, pci_dev.qdev); |
116 | } | |
117 | ||
118 | static void virtio_pci_notify(DeviceState *d, uint16_t vector) | |
119 | { | |
120 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy_fast(d); | |
aba800a3 MT |
121 | if (msix_enabled(&proxy->pci_dev)) |
122 | msix_notify(&proxy->pci_dev, vector); | |
123 | else | |
124 | qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1); | |
53c25cea PB |
125 | } |
126 | ||
d2a0ccc6 | 127 | static void virtio_pci_save_config(DeviceState *d, QEMUFile *f) |
ff24bd58 | 128 | { |
d2a0ccc6 | 129 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
ff24bd58 MT |
130 | pci_device_save(&proxy->pci_dev, f); |
131 | msix_save(&proxy->pci_dev, f); | |
132 | if (msix_present(&proxy->pci_dev)) | |
133 | qemu_put_be16(f, proxy->vdev->config_vector); | |
134 | } | |
135 | ||
d2a0ccc6 | 136 | static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f) |
ff24bd58 | 137 | { |
d2a0ccc6 | 138 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
ff24bd58 MT |
139 | if (msix_present(&proxy->pci_dev)) |
140 | qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n)); | |
141 | } | |
142 | ||
d2a0ccc6 | 143 | static int virtio_pci_load_config(DeviceState *d, QEMUFile *f) |
ff24bd58 | 144 | { |
d2a0ccc6 | 145 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
ff24bd58 MT |
146 | int ret; |
147 | ret = pci_device_load(&proxy->pci_dev, f); | |
e6da7680 | 148 | if (ret) { |
ff24bd58 | 149 | return ret; |
e6da7680 | 150 | } |
3cac001e | 151 | msix_unuse_all_vectors(&proxy->pci_dev); |
ff24bd58 | 152 | msix_load(&proxy->pci_dev, f); |
e6da7680 | 153 | if (msix_present(&proxy->pci_dev)) { |
ff24bd58 | 154 | qemu_get_be16s(f, &proxy->vdev->config_vector); |
e6da7680 MT |
155 | } else { |
156 | proxy->vdev->config_vector = VIRTIO_NO_VECTOR; | |
157 | } | |
158 | if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) { | |
159 | return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector); | |
160 | } | |
ff24bd58 MT |
161 | return 0; |
162 | } | |
163 | ||
d2a0ccc6 | 164 | static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f) |
ff24bd58 | 165 | { |
d2a0ccc6 | 166 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
ff24bd58 | 167 | uint16_t vector; |
e6da7680 MT |
168 | if (msix_present(&proxy->pci_dev)) { |
169 | qemu_get_be16s(f, &vector); | |
170 | } else { | |
171 | vector = VIRTIO_NO_VECTOR; | |
172 | } | |
ff24bd58 | 173 | virtio_queue_set_vector(proxy->vdev, n, vector); |
e6da7680 MT |
174 | if (vector != VIRTIO_NO_VECTOR) { |
175 | return msix_vector_use(&proxy->pci_dev, vector); | |
176 | } | |
ff24bd58 MT |
177 | return 0; |
178 | } | |
179 | ||
25db9ebe | 180 | static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy, |
26b9b5fe | 181 | int n, bool assign, bool set_handler) |
25db9ebe SH |
182 | { |
183 | VirtQueue *vq = virtio_get_queue(proxy->vdev, n); | |
184 | EventNotifier *notifier = virtio_queue_get_host_notifier(vq); | |
da146d0a AK |
185 | int r = 0; |
186 | ||
25db9ebe SH |
187 | if (assign) { |
188 | r = event_notifier_init(notifier, 1); | |
189 | if (r < 0) { | |
b36e3914 MT |
190 | error_report("%s: unable to init event notifier: %d", |
191 | __func__, r); | |
25db9ebe SH |
192 | return r; |
193 | } | |
26b9b5fe | 194 | virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler); |
da146d0a | 195 | memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2, |
753d5e14 | 196 | true, n, notifier); |
25db9ebe | 197 | } else { |
da146d0a | 198 | memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2, |
753d5e14 | 199 | true, n, notifier); |
26b9b5fe | 200 | virtio_queue_set_host_notifier_fd_handler(vq, false, false); |
25db9ebe SH |
201 | event_notifier_cleanup(notifier); |
202 | } | |
203 | return r; | |
204 | } | |
205 | ||
b36e3914 | 206 | static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy) |
25db9ebe SH |
207 | { |
208 | int n, r; | |
209 | ||
210 | if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) || | |
211 | proxy->ioeventfd_disabled || | |
212 | proxy->ioeventfd_started) { | |
b36e3914 | 213 | return; |
25db9ebe SH |
214 | } |
215 | ||
216 | for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) { | |
217 | if (!virtio_queue_get_num(proxy->vdev, n)) { | |
218 | continue; | |
219 | } | |
220 | ||
26b9b5fe | 221 | r = virtio_pci_set_host_notifier_internal(proxy, n, true, true); |
25db9ebe SH |
222 | if (r < 0) { |
223 | goto assign_error; | |
224 | } | |
25db9ebe SH |
225 | } |
226 | proxy->ioeventfd_started = true; | |
b36e3914 | 227 | return; |
25db9ebe SH |
228 | |
229 | assign_error: | |
230 | while (--n >= 0) { | |
231 | if (!virtio_queue_get_num(proxy->vdev, n)) { | |
232 | continue; | |
233 | } | |
234 | ||
26b9b5fe | 235 | r = virtio_pci_set_host_notifier_internal(proxy, n, false, false); |
b36e3914 | 236 | assert(r >= 0); |
25db9ebe SH |
237 | } |
238 | proxy->ioeventfd_started = false; | |
b36e3914 | 239 | error_report("%s: failed. Fallback to a userspace (slower).", __func__); |
25db9ebe SH |
240 | } |
241 | ||
b36e3914 | 242 | static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy) |
25db9ebe | 243 | { |
b36e3914 | 244 | int r; |
25db9ebe SH |
245 | int n; |
246 | ||
247 | if (!proxy->ioeventfd_started) { | |
b36e3914 | 248 | return; |
25db9ebe SH |
249 | } |
250 | ||
251 | for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) { | |
252 | if (!virtio_queue_get_num(proxy->vdev, n)) { | |
253 | continue; | |
254 | } | |
255 | ||
26b9b5fe | 256 | r = virtio_pci_set_host_notifier_internal(proxy, n, false, false); |
b36e3914 | 257 | assert(r >= 0); |
25db9ebe SH |
258 | } |
259 | proxy->ioeventfd_started = false; | |
25db9ebe SH |
260 | } |
261 | ||
53c25cea PB |
262 | static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val) |
263 | { | |
264 | VirtIOPCIProxy *proxy = opaque; | |
265 | VirtIODevice *vdev = proxy->vdev; | |
a8170e5e | 266 | hwaddr pa; |
53c25cea | 267 | |
53c25cea PB |
268 | switch (addr) { |
269 | case VIRTIO_PCI_GUEST_FEATURES: | |
181103cd FK |
270 | /* Guest does not negotiate properly? We have to assume nothing. */ |
271 | if (val & (1 << VIRTIO_F_BAD_FEATURE)) { | |
272 | val = virtio_bus_get_vdev_bad_features(&proxy->bus); | |
273 | } | |
ad0c9332 | 274 | virtio_set_features(vdev, val); |
53c25cea PB |
275 | break; |
276 | case VIRTIO_PCI_QUEUE_PFN: | |
a8170e5e | 277 | pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT; |
1b8e9b27 | 278 | if (pa == 0) { |
25db9ebe | 279 | virtio_pci_stop_ioeventfd(proxy); |
1b8e9b27 MT |
280 | virtio_reset(proxy->vdev); |
281 | msix_unuse_all_vectors(&proxy->pci_dev); | |
282 | } | |
7055e687 MT |
283 | else |
284 | virtio_queue_set_addr(vdev, vdev->queue_sel, pa); | |
53c25cea PB |
285 | break; |
286 | case VIRTIO_PCI_QUEUE_SEL: | |
287 | if (val < VIRTIO_PCI_QUEUE_MAX) | |
288 | vdev->queue_sel = val; | |
289 | break; | |
290 | case VIRTIO_PCI_QUEUE_NOTIFY: | |
7157e2e2 SH |
291 | if (val < VIRTIO_PCI_QUEUE_MAX) { |
292 | virtio_queue_notify(vdev, val); | |
293 | } | |
53c25cea PB |
294 | break; |
295 | case VIRTIO_PCI_STATUS: | |
25db9ebe SH |
296 | if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) { |
297 | virtio_pci_stop_ioeventfd(proxy); | |
298 | } | |
299 | ||
3e607cb5 | 300 | virtio_set_status(vdev, val & 0xFF); |
25db9ebe SH |
301 | |
302 | if (val & VIRTIO_CONFIG_S_DRIVER_OK) { | |
303 | virtio_pci_start_ioeventfd(proxy); | |
304 | } | |
305 | ||
1b8e9b27 MT |
306 | if (vdev->status == 0) { |
307 | virtio_reset(proxy->vdev); | |
308 | msix_unuse_all_vectors(&proxy->pci_dev); | |
309 | } | |
c81131db AG |
310 | |
311 | /* Linux before 2.6.34 sets the device as OK without enabling | |
312 | the PCI device bus master bit. In this case we need to disable | |
313 | some safety checks. */ | |
314 | if ((val & VIRTIO_CONFIG_S_DRIVER_OK) && | |
315 | !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { | |
3dbca8e6 | 316 | proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG; |
c81131db | 317 | } |
53c25cea | 318 | break; |
aba800a3 MT |
319 | case VIRTIO_MSI_CONFIG_VECTOR: |
320 | msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); | |
321 | /* Make it possible for guest to discover an error took place. */ | |
322 | if (msix_vector_use(&proxy->pci_dev, val) < 0) | |
323 | val = VIRTIO_NO_VECTOR; | |
324 | vdev->config_vector = val; | |
325 | break; | |
326 | case VIRTIO_MSI_QUEUE_VECTOR: | |
327 | msix_vector_unuse(&proxy->pci_dev, | |
328 | virtio_queue_vector(vdev, vdev->queue_sel)); | |
329 | /* Make it possible for guest to discover an error took place. */ | |
330 | if (msix_vector_use(&proxy->pci_dev, val) < 0) | |
331 | val = VIRTIO_NO_VECTOR; | |
332 | virtio_queue_set_vector(vdev, vdev->queue_sel, val); | |
333 | break; | |
334 | default: | |
4e02d460 SH |
335 | error_report("%s: unexpected address 0x%x value 0x%x", |
336 | __func__, addr, val); | |
aba800a3 | 337 | break; |
53c25cea PB |
338 | } |
339 | } | |
340 | ||
aba800a3 | 341 | static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr) |
53c25cea | 342 | { |
53c25cea PB |
343 | VirtIODevice *vdev = proxy->vdev; |
344 | uint32_t ret = 0xFFFFFFFF; | |
345 | ||
53c25cea PB |
346 | switch (addr) { |
347 | case VIRTIO_PCI_HOST_FEATURES: | |
8172539d | 348 | ret = proxy->host_features; |
53c25cea PB |
349 | break; |
350 | case VIRTIO_PCI_GUEST_FEATURES: | |
704a76fc | 351 | ret = vdev->guest_features; |
53c25cea PB |
352 | break; |
353 | case VIRTIO_PCI_QUEUE_PFN: | |
354 | ret = virtio_queue_get_addr(vdev, vdev->queue_sel) | |
355 | >> VIRTIO_PCI_QUEUE_ADDR_SHIFT; | |
356 | break; | |
357 | case VIRTIO_PCI_QUEUE_NUM: | |
358 | ret = virtio_queue_get_num(vdev, vdev->queue_sel); | |
359 | break; | |
360 | case VIRTIO_PCI_QUEUE_SEL: | |
361 | ret = vdev->queue_sel; | |
362 | break; | |
363 | case VIRTIO_PCI_STATUS: | |
364 | ret = vdev->status; | |
365 | break; | |
366 | case VIRTIO_PCI_ISR: | |
367 | /* reading from the ISR also clears it. */ | |
368 | ret = vdev->isr; | |
369 | vdev->isr = 0; | |
7055e687 | 370 | qemu_set_irq(proxy->pci_dev.irq[0], 0); |
53c25cea | 371 | break; |
aba800a3 MT |
372 | case VIRTIO_MSI_CONFIG_VECTOR: |
373 | ret = vdev->config_vector; | |
374 | break; | |
375 | case VIRTIO_MSI_QUEUE_VECTOR: | |
376 | ret = virtio_queue_vector(vdev, vdev->queue_sel); | |
377 | break; | |
53c25cea PB |
378 | default: |
379 | break; | |
380 | } | |
381 | ||
382 | return ret; | |
383 | } | |
384 | ||
df6db5b3 AG |
385 | static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr, |
386 | unsigned size) | |
53c25cea PB |
387 | { |
388 | VirtIOPCIProxy *proxy = opaque; | |
aba800a3 | 389 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
df6db5b3 | 390 | uint64_t val = 0; |
aba800a3 | 391 | if (addr < config) { |
df6db5b3 | 392 | return virtio_ioport_read(proxy, addr); |
aba800a3 MT |
393 | } |
394 | addr -= config; | |
53c25cea | 395 | |
df6db5b3 AG |
396 | switch (size) { |
397 | case 1: | |
398 | val = virtio_config_readb(proxy->vdev, addr); | |
399 | break; | |
400 | case 2: | |
401 | val = virtio_config_readw(proxy->vdev, addr); | |
8e4a424b BS |
402 | if (virtio_is_big_endian()) { |
403 | val = bswap16(val); | |
404 | } | |
df6db5b3 AG |
405 | break; |
406 | case 4: | |
407 | val = virtio_config_readl(proxy->vdev, addr); | |
8e4a424b BS |
408 | if (virtio_is_big_endian()) { |
409 | val = bswap32(val); | |
410 | } | |
df6db5b3 | 411 | break; |
82afa586 | 412 | } |
df6db5b3 | 413 | return val; |
53c25cea PB |
414 | } |
415 | ||
df6db5b3 AG |
416 | static void virtio_pci_config_write(void *opaque, hwaddr addr, |
417 | uint64_t val, unsigned size) | |
53c25cea PB |
418 | { |
419 | VirtIOPCIProxy *proxy = opaque; | |
aba800a3 | 420 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
aba800a3 MT |
421 | if (addr < config) { |
422 | virtio_ioport_write(proxy, addr, val); | |
423 | return; | |
424 | } | |
425 | addr -= config; | |
df6db5b3 AG |
426 | /* |
427 | * Virtio-PCI is odd. Ioports are LE but config space is target native | |
428 | * endian. | |
429 | */ | |
430 | switch (size) { | |
431 | case 1: | |
432 | virtio_config_writeb(proxy->vdev, addr, val); | |
433 | break; | |
434 | case 2: | |
8e4a424b BS |
435 | if (virtio_is_big_endian()) { |
436 | val = bswap16(val); | |
437 | } | |
df6db5b3 AG |
438 | virtio_config_writew(proxy->vdev, addr, val); |
439 | break; | |
440 | case 4: | |
8e4a424b BS |
441 | if (virtio_is_big_endian()) { |
442 | val = bswap32(val); | |
443 | } | |
df6db5b3 AG |
444 | virtio_config_writel(proxy->vdev, addr, val); |
445 | break; | |
82afa586 | 446 | } |
53c25cea PB |
447 | } |
448 | ||
da146d0a | 449 | static const MemoryRegionOps virtio_pci_config_ops = { |
df6db5b3 AG |
450 | .read = virtio_pci_config_read, |
451 | .write = virtio_pci_config_write, | |
452 | .impl = { | |
453 | .min_access_size = 1, | |
454 | .max_access_size = 4, | |
455 | }, | |
8e4a424b | 456 | .endianness = DEVICE_LITTLE_ENDIAN, |
da146d0a | 457 | }; |
aba800a3 MT |
458 | |
459 | static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, | |
460 | uint32_t val, int len) | |
461 | { | |
ed757e14 YV |
462 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
463 | ||
1129714f MT |
464 | pci_default_write_config(pci_dev, address, val, len); |
465 | ||
466 | if (range_covers_byte(address, len, PCI_COMMAND) && | |
467 | !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) && | |
468 | !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) { | |
469 | virtio_pci_stop_ioeventfd(proxy); | |
470 | virtio_set_status(proxy->vdev, | |
471 | proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK); | |
ed757e14 | 472 | } |
53c25cea PB |
473 | } |
474 | ||
d2a0ccc6 | 475 | static unsigned virtio_pci_get_features(DeviceState *d) |
6d74ca5a | 476 | { |
d2a0ccc6 | 477 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
8172539d | 478 | return proxy->host_features; |
6d74ca5a MT |
479 | } |
480 | ||
7d37d351 JK |
481 | static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy, |
482 | unsigned int queue_no, | |
483 | unsigned int vector, | |
484 | MSIMessage msg) | |
485 | { | |
7d37d351 | 486 | VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; |
15b2bd18 | 487 | int ret; |
7d37d351 JK |
488 | |
489 | if (irqfd->users == 0) { | |
490 | ret = kvm_irqchip_add_msi_route(kvm_state, msg); | |
491 | if (ret < 0) { | |
492 | return ret; | |
493 | } | |
494 | irqfd->virq = ret; | |
495 | } | |
496 | irqfd->users++; | |
7d37d351 JK |
497 | return 0; |
498 | } | |
499 | ||
500 | static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy, | |
7d37d351 | 501 | unsigned int vector) |
774345f9 MT |
502 | { |
503 | VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; | |
504 | if (--irqfd->users == 0) { | |
505 | kvm_irqchip_release_virq(kvm_state, irqfd->virq); | |
506 | } | |
507 | } | |
508 | ||
f1d0f15a MT |
509 | static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy, |
510 | unsigned int queue_no, | |
511 | unsigned int vector) | |
512 | { | |
513 | VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; | |
514 | VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no); | |
515 | EventNotifier *n = virtio_queue_get_guest_notifier(vq); | |
516 | int ret; | |
517 | ret = kvm_irqchip_add_irqfd_notifier(kvm_state, n, irqfd->virq); | |
518 | return ret; | |
519 | } | |
520 | ||
521 | static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy, | |
522 | unsigned int queue_no, | |
523 | unsigned int vector) | |
7d37d351 JK |
524 | { |
525 | VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no); | |
15b2bd18 | 526 | EventNotifier *n = virtio_queue_get_guest_notifier(vq); |
7d37d351 | 527 | VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; |
15b2bd18 | 528 | int ret; |
7d37d351 | 529 | |
b131c74a | 530 | ret = kvm_irqchip_remove_irqfd_notifier(kvm_state, n, irqfd->virq); |
7d37d351 | 531 | assert(ret == 0); |
f1d0f15a | 532 | } |
7d37d351 | 533 | |
774345f9 MT |
534 | static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs) |
535 | { | |
536 | PCIDevice *dev = &proxy->pci_dev; | |
537 | VirtIODevice *vdev = proxy->vdev; | |
181103cd | 538 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
774345f9 MT |
539 | unsigned int vector; |
540 | int ret, queue_no; | |
541 | MSIMessage msg; | |
542 | ||
543 | for (queue_no = 0; queue_no < nvqs; queue_no++) { | |
544 | if (!virtio_queue_get_num(vdev, queue_no)) { | |
545 | break; | |
546 | } | |
547 | vector = virtio_queue_vector(vdev, queue_no); | |
548 | if (vector >= msix_nr_vectors_allocated(dev)) { | |
549 | continue; | |
550 | } | |
551 | msg = msix_get_message(dev, vector); | |
552 | ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg); | |
553 | if (ret < 0) { | |
554 | goto undo; | |
7d37d351 | 555 | } |
f1d0f15a MT |
556 | /* If guest supports masking, set up irqfd now. |
557 | * Otherwise, delay until unmasked in the frontend. | |
558 | */ | |
181103cd | 559 | if (k->guest_notifier_mask) { |
f1d0f15a MT |
560 | ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector); |
561 | if (ret < 0) { | |
562 | kvm_virtio_pci_vq_vector_release(proxy, vector); | |
563 | goto undo; | |
564 | } | |
565 | } | |
7d37d351 | 566 | } |
7d37d351 | 567 | return 0; |
774345f9 MT |
568 | |
569 | undo: | |
570 | while (--queue_no >= 0) { | |
571 | vector = virtio_queue_vector(vdev, queue_no); | |
572 | if (vector >= msix_nr_vectors_allocated(dev)) { | |
573 | continue; | |
574 | } | |
181103cd | 575 | if (k->guest_notifier_mask) { |
e387f99e | 576 | kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); |
f1d0f15a | 577 | } |
774345f9 MT |
578 | kvm_virtio_pci_vq_vector_release(proxy, vector); |
579 | } | |
580 | return ret; | |
7d37d351 JK |
581 | } |
582 | ||
774345f9 MT |
583 | static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs) |
584 | { | |
585 | PCIDevice *dev = &proxy->pci_dev; | |
586 | VirtIODevice *vdev = proxy->vdev; | |
587 | unsigned int vector; | |
588 | int queue_no; | |
181103cd | 589 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
774345f9 MT |
590 | |
591 | for (queue_no = 0; queue_no < nvqs; queue_no++) { | |
592 | if (!virtio_queue_get_num(vdev, queue_no)) { | |
593 | break; | |
594 | } | |
595 | vector = virtio_queue_vector(vdev, queue_no); | |
596 | if (vector >= msix_nr_vectors_allocated(dev)) { | |
597 | continue; | |
598 | } | |
f1d0f15a MT |
599 | /* If guest supports masking, clean up irqfd now. |
600 | * Otherwise, it was cleaned when masked in the frontend. | |
601 | */ | |
181103cd | 602 | if (k->guest_notifier_mask) { |
e387f99e | 603 | kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); |
f1d0f15a | 604 | } |
774345f9 MT |
605 | kvm_virtio_pci_vq_vector_release(proxy, vector); |
606 | } | |
607 | } | |
608 | ||
a38b2c49 MT |
609 | static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy, |
610 | unsigned int queue_no, | |
611 | unsigned int vector, | |
612 | MSIMessage msg) | |
774345f9 | 613 | { |
181103cd | 614 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(proxy->vdev); |
774345f9 MT |
615 | VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no); |
616 | EventNotifier *n = virtio_queue_get_guest_notifier(vq); | |
a38b2c49 | 617 | VirtIOIRQFD *irqfd; |
53510bfc | 618 | int ret = 0; |
774345f9 | 619 | |
a38b2c49 MT |
620 | if (proxy->vector_irqfd) { |
621 | irqfd = &proxy->vector_irqfd[vector]; | |
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 | } | |
774345f9 MT |
627 | } |
628 | } | |
629 | ||
f1d0f15a MT |
630 | /* If guest supports masking, irqfd is already setup, unmask it. |
631 | * Otherwise, set it up now. | |
632 | */ | |
181103cd FK |
633 | if (k->guest_notifier_mask) { |
634 | k->guest_notifier_mask(proxy->vdev, queue_no, false); | |
f1d0f15a | 635 | /* Test after unmasking to avoid losing events. */ |
181103cd FK |
636 | if (k->guest_notifier_pending && |
637 | k->guest_notifier_pending(proxy->vdev, queue_no)) { | |
f1d0f15a MT |
638 | event_notifier_set(n); |
639 | } | |
640 | } else { | |
641 | ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector); | |
7d37d351 | 642 | } |
774345f9 | 643 | return ret; |
7d37d351 JK |
644 | } |
645 | ||
a38b2c49 | 646 | static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy, |
7d37d351 JK |
647 | unsigned int queue_no, |
648 | unsigned int vector) | |
649 | { | |
181103cd FK |
650 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(proxy->vdev); |
651 | ||
f1d0f15a MT |
652 | /* If guest supports masking, keep irqfd but mask it. |
653 | * Otherwise, clean it up now. | |
654 | */ | |
181103cd FK |
655 | if (k->guest_notifier_mask) { |
656 | k->guest_notifier_mask(proxy->vdev, queue_no, true); | |
f1d0f15a | 657 | } else { |
e387f99e | 658 | kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); |
f1d0f15a | 659 | } |
7d37d351 JK |
660 | } |
661 | ||
a38b2c49 MT |
662 | static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector, |
663 | MSIMessage msg) | |
7d37d351 JK |
664 | { |
665 | VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); | |
666 | VirtIODevice *vdev = proxy->vdev; | |
667 | int ret, queue_no; | |
668 | ||
2d620f59 | 669 | for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) { |
7d37d351 JK |
670 | if (!virtio_queue_get_num(vdev, queue_no)) { |
671 | break; | |
672 | } | |
673 | if (virtio_queue_vector(vdev, queue_no) != vector) { | |
674 | continue; | |
675 | } | |
a38b2c49 | 676 | ret = virtio_pci_vq_vector_unmask(proxy, queue_no, vector, msg); |
7d37d351 JK |
677 | if (ret < 0) { |
678 | goto undo; | |
679 | } | |
680 | } | |
681 | return 0; | |
682 | ||
683 | undo: | |
684 | while (--queue_no >= 0) { | |
685 | if (virtio_queue_vector(vdev, queue_no) != vector) { | |
686 | continue; | |
687 | } | |
a38b2c49 | 688 | virtio_pci_vq_vector_mask(proxy, queue_no, vector); |
7d37d351 JK |
689 | } |
690 | return ret; | |
691 | } | |
692 | ||
a38b2c49 | 693 | static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector) |
7d37d351 JK |
694 | { |
695 | VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); | |
696 | VirtIODevice *vdev = proxy->vdev; | |
697 | int queue_no; | |
698 | ||
2d620f59 | 699 | for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) { |
7d37d351 JK |
700 | if (!virtio_queue_get_num(vdev, queue_no)) { |
701 | break; | |
702 | } | |
703 | if (virtio_queue_vector(vdev, queue_no) != vector) { | |
704 | continue; | |
705 | } | |
a38b2c49 | 706 | virtio_pci_vq_vector_mask(proxy, queue_no, vector); |
7d37d351 JK |
707 | } |
708 | } | |
709 | ||
a38b2c49 MT |
710 | static void virtio_pci_vector_poll(PCIDevice *dev, |
711 | unsigned int vector_start, | |
712 | unsigned int vector_end) | |
89d62be9 MT |
713 | { |
714 | VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); | |
715 | VirtIODevice *vdev = proxy->vdev; | |
181103cd | 716 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
89d62be9 MT |
717 | int queue_no; |
718 | unsigned int vector; | |
719 | EventNotifier *notifier; | |
720 | VirtQueue *vq; | |
721 | ||
2d620f59 | 722 | for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) { |
89d62be9 MT |
723 | if (!virtio_queue_get_num(vdev, queue_no)) { |
724 | break; | |
725 | } | |
726 | vector = virtio_queue_vector(vdev, queue_no); | |
727 | if (vector < vector_start || vector >= vector_end || | |
728 | !msix_is_masked(dev, vector)) { | |
729 | continue; | |
730 | } | |
731 | vq = virtio_get_queue(vdev, queue_no); | |
732 | notifier = virtio_queue_get_guest_notifier(vq); | |
181103cd FK |
733 | if (k->guest_notifier_pending) { |
734 | if (k->guest_notifier_pending(vdev, queue_no)) { | |
f1d0f15a MT |
735 | msix_set_pending(dev, vector); |
736 | } | |
737 | } else if (event_notifier_test_and_clear(notifier)) { | |
89d62be9 MT |
738 | msix_set_pending(dev, vector); |
739 | } | |
740 | } | |
741 | } | |
742 | ||
743 | static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign, | |
744 | bool with_irqfd) | |
ade80dc8 | 745 | { |
d2a0ccc6 | 746 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
ade80dc8 MT |
747 | VirtQueue *vq = virtio_get_queue(proxy->vdev, n); |
748 | EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); | |
749 | ||
750 | if (assign) { | |
751 | int r = event_notifier_init(notifier, 0); | |
752 | if (r < 0) { | |
753 | return r; | |
754 | } | |
89d62be9 | 755 | virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd); |
ade80dc8 | 756 | } else { |
89d62be9 | 757 | virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd); |
ade80dc8 MT |
758 | event_notifier_cleanup(notifier); |
759 | } | |
760 | ||
761 | return 0; | |
762 | } | |
763 | ||
d2a0ccc6 | 764 | static bool virtio_pci_query_guest_notifiers(DeviceState *d) |
5430a28f | 765 | { |
d2a0ccc6 | 766 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
5430a28f MT |
767 | return msix_enabled(&proxy->pci_dev); |
768 | } | |
769 | ||
2d620f59 | 770 | static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign) |
54dd9321 | 771 | { |
d2a0ccc6 | 772 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
54dd9321 | 773 | VirtIODevice *vdev = proxy->vdev; |
181103cd | 774 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
54dd9321 | 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 */ |
181103cd | 789 | if ((proxy->vector_irqfd || k->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 */ |
181103cd | 811 | if ((with_irqfd || k->guest_notifier_mask) && assign) { |
a38b2c49 MT |
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 | ||
60653b28 | 882 | #ifdef CONFIG_VIRTFS |
234a336f | 883 | static int virtio_9p_init_pci(VirtIOPCIProxy *vpci_dev) |
60653b28 | 884 | { |
234a336f FK |
885 | V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev); |
886 | DeviceState *vdev = DEVICE(&dev->vdev); | |
60653b28 | 887 | |
234a336f FK |
888 | qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); |
889 | if (qdev_init(vdev) < 0) { | |
890 | return -1; | |
891 | } | |
60653b28 PB |
892 | return 0; |
893 | } | |
894 | ||
234a336f FK |
895 | static Property virtio_9p_pci_properties[] = { |
896 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, | |
897 | VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), | |
60653b28 PB |
898 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), |
899 | DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), | |
234a336f | 900 | DEFINE_VIRTIO_9P_PROPERTIES(V9fsPCIState, vdev.fsconf), |
60653b28 PB |
901 | DEFINE_PROP_END_OF_LIST(), |
902 | }; | |
903 | ||
234a336f | 904 | static void virtio_9p_pci_class_init(ObjectClass *klass, void *data) |
60653b28 PB |
905 | { |
906 | DeviceClass *dc = DEVICE_CLASS(klass); | |
234a336f FK |
907 | PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); |
908 | VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); | |
60653b28 PB |
909 | |
910 | k->init = virtio_9p_init_pci; | |
234a336f FK |
911 | pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; |
912 | pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_9P; | |
913 | pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; | |
914 | pcidev_k->class_id = 0x2; | |
915 | dc->props = virtio_9p_pci_properties; | |
60653b28 PB |
916 | } |
917 | ||
234a336f FK |
918 | static void virtio_9p_pci_instance_init(Object *obj) |
919 | { | |
920 | V9fsPCIState *dev = VIRTIO_9P_PCI(obj); | |
921 | object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_9P); | |
922 | object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); | |
923 | } | |
924 | ||
925 | static const TypeInfo virtio_9p_pci_info = { | |
926 | .name = TYPE_VIRTIO_9P_PCI, | |
927 | .parent = TYPE_VIRTIO_PCI, | |
928 | .instance_size = sizeof(V9fsPCIState), | |
929 | .instance_init = virtio_9p_pci_instance_init, | |
930 | .class_init = virtio_9p_pci_class_init, | |
60653b28 | 931 | }; |
234a336f | 932 | #endif /* CONFIG_VIRTFS */ |
60653b28 | 933 | |
085bccb7 FK |
934 | /* |
935 | * virtio-pci: This is the PCIDevice which has a virtio-pci-bus. | |
936 | */ | |
937 | ||
938 | /* This is called by virtio-bus just after the device is plugged. */ | |
939 | static void virtio_pci_device_plugged(DeviceState *d) | |
940 | { | |
941 | VirtIOPCIProxy *proxy = VIRTIO_PCI(d); | |
942 | VirtioBusState *bus = &proxy->bus; | |
943 | uint8_t *config; | |
944 | uint32_t size; | |
945 | ||
946 | proxy->vdev = bus->vdev; | |
947 | ||
948 | config = proxy->pci_dev.config; | |
949 | if (proxy->class_code) { | |
950 | pci_config_set_class(config, proxy->class_code); | |
951 | } | |
952 | pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID, | |
953 | pci_get_word(config + PCI_VENDOR_ID)); | |
954 | pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus)); | |
955 | config[PCI_INTERRUPT_PIN] = 1; | |
956 | ||
957 | if (proxy->nvectors && | |
958 | msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors, 1)) { | |
959 | proxy->nvectors = 0; | |
960 | } | |
961 | ||
962 | proxy->pci_dev.config_write = virtio_write_config; | |
963 | ||
964 | size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) | |
965 | + virtio_bus_get_vdev_config_len(bus); | |
966 | if (size & (size - 1)) { | |
967 | size = 1 << qemu_fls(size); | |
968 | } | |
969 | ||
970 | memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy, | |
971 | "virtio-pci", size); | |
972 | pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO, | |
973 | &proxy->bar); | |
974 | ||
975 | if (!kvm_has_many_ioeventfds()) { | |
976 | proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD; | |
977 | } | |
978 | ||
979 | proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY; | |
980 | proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE; | |
981 | proxy->host_features = virtio_bus_get_vdev_features(bus, | |
982 | proxy->host_features); | |
983 | } | |
984 | ||
085bccb7 FK |
985 | static int virtio_pci_init(PCIDevice *pci_dev) |
986 | { | |
987 | VirtIOPCIProxy *dev = VIRTIO_PCI(pci_dev); | |
988 | VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev); | |
989 | virtio_pci_bus_new(&dev->bus, dev); | |
990 | if (k->init != NULL) { | |
991 | return k->init(dev); | |
992 | } | |
993 | return 0; | |
994 | } | |
995 | ||
996 | static void virtio_pci_exit(PCIDevice *pci_dev) | |
997 | { | |
998 | VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev); | |
10479a80 | 999 | virtio_pci_stop_ioeventfd(proxy); |
a2f1078b FK |
1000 | memory_region_destroy(&proxy->bar); |
1001 | msix_uninit_exclusive_bar(pci_dev); | |
085bccb7 FK |
1002 | } |
1003 | ||
59ccd20a | 1004 | static void virtio_pci_reset(DeviceState *qdev) |
085bccb7 FK |
1005 | { |
1006 | VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev); | |
1007 | VirtioBusState *bus = VIRTIO_BUS(&proxy->bus); | |
1008 | virtio_pci_stop_ioeventfd(proxy); | |
1009 | virtio_bus_reset(bus); | |
1010 | msix_unuse_all_vectors(&proxy->pci_dev); | |
1011 | proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG; | |
1012 | } | |
1013 | ||
1014 | static void virtio_pci_class_init(ObjectClass *klass, void *data) | |
1015 | { | |
1016 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1017 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); | |
1018 | ||
1019 | k->init = virtio_pci_init; | |
1020 | k->exit = virtio_pci_exit; | |
1021 | k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
1022 | k->revision = VIRTIO_PCI_ABI_VERSION; | |
1023 | k->class_id = PCI_CLASS_OTHERS; | |
59ccd20a | 1024 | dc->reset = virtio_pci_reset; |
085bccb7 FK |
1025 | } |
1026 | ||
1027 | static const TypeInfo virtio_pci_info = { | |
1028 | .name = TYPE_VIRTIO_PCI, | |
1029 | .parent = TYPE_PCI_DEVICE, | |
1030 | .instance_size = sizeof(VirtIOPCIProxy), | |
1031 | .class_init = virtio_pci_class_init, | |
1032 | .class_size = sizeof(VirtioPCIClass), | |
1033 | .abstract = true, | |
1034 | }; | |
1035 | ||
653ced07 FK |
1036 | /* virtio-blk-pci */ |
1037 | ||
1038 | static Property virtio_blk_pci_properties[] = { | |
1039 | DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0), | |
1040 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, | |
1041 | VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), | |
1042 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), | |
1043 | #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE | |
1044 | DEFINE_PROP_BIT("x-data-plane", VirtIOBlkPCI, blk.data_plane, 0, false), | |
1045 | #endif | |
1046 | DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features), | |
1047 | DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlkPCI, blk), | |
1048 | DEFINE_PROP_END_OF_LIST(), | |
1049 | }; | |
1050 | ||
1051 | static int virtio_blk_pci_init(VirtIOPCIProxy *vpci_dev) | |
1052 | { | |
1053 | VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev); | |
1054 | DeviceState *vdev = DEVICE(&dev->vdev); | |
1055 | virtio_blk_set_conf(vdev, &(dev->blk)); | |
1056 | qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); | |
1057 | if (qdev_init(vdev) < 0) { | |
1058 | return -1; | |
1059 | } | |
1060 | return 0; | |
1061 | } | |
1062 | ||
1063 | static void virtio_blk_pci_class_init(ObjectClass *klass, void *data) | |
1064 | { | |
1065 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1066 | VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); | |
1067 | PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); | |
1068 | ||
1069 | dc->props = virtio_blk_pci_properties; | |
1070 | k->init = virtio_blk_pci_init; | |
1071 | pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
1072 | pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK; | |
1073 | pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; | |
1074 | pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; | |
1075 | } | |
1076 | ||
1077 | static void virtio_blk_pci_instance_init(Object *obj) | |
1078 | { | |
1079 | VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj); | |
1080 | object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_BLK); | |
1081 | object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); | |
1082 | } | |
1083 | ||
1084 | static const TypeInfo virtio_blk_pci_info = { | |
1085 | .name = TYPE_VIRTIO_BLK_PCI, | |
1086 | .parent = TYPE_VIRTIO_PCI, | |
1087 | .instance_size = sizeof(VirtIOBlkPCI), | |
1088 | .instance_init = virtio_blk_pci_instance_init, | |
1089 | .class_init = virtio_blk_pci_class_init, | |
1090 | }; | |
1091 | ||
bc7b90a0 FK |
1092 | /* virtio-scsi-pci */ |
1093 | ||
1094 | static Property virtio_scsi_pci_properties[] = { | |
1095 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, | |
1096 | VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), | |
1097 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, | |
1098 | DEV_NVECTORS_UNSPECIFIED), | |
1099 | DEFINE_VIRTIO_SCSI_FEATURES(VirtIOPCIProxy, host_features), | |
292c8e50 | 1100 | DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSIPCI, vdev.parent_obj.conf), |
bc7b90a0 FK |
1101 | DEFINE_PROP_END_OF_LIST(), |
1102 | }; | |
1103 | ||
1104 | static int virtio_scsi_pci_init_pci(VirtIOPCIProxy *vpci_dev) | |
1105 | { | |
1106 | VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev); | |
1107 | DeviceState *vdev = DEVICE(&dev->vdev); | |
292c8e50 | 1108 | VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); |
bc7b90a0 FK |
1109 | |
1110 | if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { | |
292c8e50 | 1111 | vpci_dev->nvectors = vs->conf.num_queues + 3; |
bc7b90a0 FK |
1112 | } |
1113 | ||
1114 | qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); | |
1115 | if (qdev_init(vdev) < 0) { | |
1116 | return -1; | |
1117 | } | |
1118 | return 0; | |
1119 | } | |
1120 | ||
1121 | static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data) | |
1122 | { | |
1123 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1124 | VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); | |
1125 | PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); | |
1126 | k->init = virtio_scsi_pci_init_pci; | |
1127 | dc->props = virtio_scsi_pci_properties; | |
1128 | pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
1129 | pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI; | |
1130 | pcidev_k->revision = 0x00; | |
1131 | pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; | |
1132 | } | |
1133 | ||
1134 | static void virtio_scsi_pci_instance_init(Object *obj) | |
1135 | { | |
1136 | VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj); | |
1137 | object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_SCSI); | |
1138 | object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); | |
1139 | } | |
1140 | ||
1141 | static const TypeInfo virtio_scsi_pci_info = { | |
1142 | .name = TYPE_VIRTIO_SCSI_PCI, | |
1143 | .parent = TYPE_VIRTIO_PCI, | |
1144 | .instance_size = sizeof(VirtIOSCSIPCI), | |
1145 | .instance_init = virtio_scsi_pci_instance_init, | |
1146 | .class_init = virtio_scsi_pci_class_init, | |
1147 | }; | |
1148 | ||
50787628 NB |
1149 | /* vhost-scsi-pci */ |
1150 | ||
1151 | #ifdef CONFIG_VHOST_SCSI | |
1152 | static Property vhost_scsi_pci_properties[] = { | |
1153 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, | |
1154 | DEV_NVECTORS_UNSPECIFIED), | |
1155 | DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), | |
1156 | DEFINE_VHOST_SCSI_PROPERTIES(VHostSCSIPCI, vdev.parent_obj.conf), | |
1157 | DEFINE_PROP_END_OF_LIST(), | |
1158 | }; | |
1159 | ||
1160 | static int vhost_scsi_pci_init_pci(VirtIOPCIProxy *vpci_dev) | |
1161 | { | |
1162 | VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev); | |
1163 | DeviceState *vdev = DEVICE(&dev->vdev); | |
1164 | VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); | |
1165 | ||
1166 | if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { | |
1167 | vpci_dev->nvectors = vs->conf.num_queues + 3; | |
1168 | } | |
1169 | ||
1170 | qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); | |
1171 | if (qdev_init(vdev) < 0) { | |
1172 | return -1; | |
1173 | } | |
1174 | return 0; | |
1175 | } | |
1176 | ||
1177 | static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data) | |
1178 | { | |
1179 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1180 | VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); | |
1181 | PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); | |
1182 | k->init = vhost_scsi_pci_init_pci; | |
1183 | dc->props = vhost_scsi_pci_properties; | |
1184 | pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
1185 | pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI; | |
1186 | pcidev_k->revision = 0x00; | |
1187 | pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; | |
1188 | } | |
1189 | ||
1190 | static void vhost_scsi_pci_instance_init(Object *obj) | |
1191 | { | |
1192 | VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj); | |
1193 | object_initialize(OBJECT(&dev->vdev), TYPE_VHOST_SCSI); | |
1194 | object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); | |
1195 | } | |
1196 | ||
1197 | static const TypeInfo vhost_scsi_pci_info = { | |
1198 | .name = TYPE_VHOST_SCSI_PCI, | |
1199 | .parent = TYPE_VIRTIO_PCI, | |
1200 | .instance_size = sizeof(VHostSCSIPCI), | |
1201 | .instance_init = vhost_scsi_pci_instance_init, | |
1202 | .class_init = vhost_scsi_pci_class_init, | |
1203 | }; | |
1204 | #endif | |
1205 | ||
e378e88d FK |
1206 | /* virtio-balloon-pci */ |
1207 | ||
24a6e7f4 FK |
1208 | static void balloon_pci_stats_get_all(Object *obj, struct Visitor *v, |
1209 | void *opaque, const char *name, | |
1210 | Error **errp) | |
1211 | { | |
1212 | VirtIOBalloonPCI *dev = opaque; | |
1213 | object_property_get(OBJECT(&dev->vdev), v, "guest-stats", errp); | |
1214 | } | |
1215 | ||
1216 | static void balloon_pci_stats_get_poll_interval(Object *obj, struct Visitor *v, | |
1217 | void *opaque, const char *name, | |
1218 | Error **errp) | |
1219 | { | |
1220 | VirtIOBalloonPCI *dev = opaque; | |
1221 | object_property_get(OBJECT(&dev->vdev), v, "guest-stats-polling-interval", | |
1222 | errp); | |
1223 | } | |
1224 | ||
1225 | static void balloon_pci_stats_set_poll_interval(Object *obj, struct Visitor *v, | |
1226 | void *opaque, const char *name, | |
1227 | Error **errp) | |
1228 | { | |
1229 | VirtIOBalloonPCI *dev = opaque; | |
1230 | object_property_set(OBJECT(&dev->vdev), v, "guest-stats-polling-interval", | |
1231 | errp); | |
1232 | } | |
1233 | ||
e378e88d FK |
1234 | static Property virtio_balloon_pci_properties[] = { |
1235 | DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), | |
1236 | DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0), | |
1237 | DEFINE_PROP_END_OF_LIST(), | |
1238 | }; | |
1239 | ||
1240 | static int virtio_balloon_pci_init(VirtIOPCIProxy *vpci_dev) | |
1241 | { | |
1242 | VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev); | |
1243 | DeviceState *vdev = DEVICE(&dev->vdev); | |
1244 | ||
1245 | if (vpci_dev->class_code != PCI_CLASS_OTHERS && | |
1246 | vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */ | |
1247 | vpci_dev->class_code = PCI_CLASS_OTHERS; | |
1248 | } | |
1249 | ||
1250 | qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); | |
1251 | if (qdev_init(vdev) < 0) { | |
1252 | return -1; | |
1253 | } | |
1254 | return 0; | |
1255 | } | |
1256 | ||
1257 | static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data) | |
1258 | { | |
1259 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1260 | VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); | |
1261 | PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); | |
1262 | k->init = virtio_balloon_pci_init; | |
1263 | dc->props = virtio_balloon_pci_properties; | |
1264 | pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
1265 | pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON; | |
1266 | pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; | |
1267 | pcidev_k->class_id = PCI_CLASS_OTHERS; | |
1268 | } | |
1269 | ||
1270 | static void virtio_balloon_pci_instance_init(Object *obj) | |
1271 | { | |
1272 | VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj); | |
1273 | object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_BALLOON); | |
1274 | object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); | |
24a6e7f4 FK |
1275 | |
1276 | object_property_add(obj, "guest-stats", "guest statistics", | |
1277 | balloon_pci_stats_get_all, NULL, NULL, dev, | |
1278 | NULL); | |
1279 | ||
1280 | object_property_add(obj, "guest-stats-polling-interval", "int", | |
1281 | balloon_pci_stats_get_poll_interval, | |
1282 | balloon_pci_stats_set_poll_interval, | |
1283 | NULL, dev, NULL); | |
e378e88d FK |
1284 | } |
1285 | ||
1286 | static const TypeInfo virtio_balloon_pci_info = { | |
1287 | .name = TYPE_VIRTIO_BALLOON_PCI, | |
1288 | .parent = TYPE_VIRTIO_PCI, | |
1289 | .instance_size = sizeof(VirtIOBalloonPCI), | |
1290 | .instance_init = virtio_balloon_pci_instance_init, | |
1291 | .class_init = virtio_balloon_pci_class_init, | |
1292 | }; | |
1293 | ||
f7f7464a FK |
1294 | /* virtio-serial-pci */ |
1295 | ||
1296 | static int virtio_serial_pci_init(VirtIOPCIProxy *vpci_dev) | |
1297 | { | |
1298 | VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev); | |
1299 | DeviceState *vdev = DEVICE(&dev->vdev); | |
80270a19 FK |
1300 | DeviceState *proxy = DEVICE(vpci_dev); |
1301 | char *bus_name; | |
f7f7464a FK |
1302 | |
1303 | if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER && | |
1304 | vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */ | |
1305 | vpci_dev->class_code != PCI_CLASS_OTHERS) { /* qemu-kvm */ | |
1306 | vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER; | |
1307 | } | |
1308 | ||
1309 | /* backwards-compatibility with machines that were created with | |
1310 | DEV_NVECTORS_UNSPECIFIED */ | |
1311 | if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { | |
1312 | vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1; | |
1313 | } | |
1314 | ||
80270a19 FK |
1315 | /* |
1316 | * For command line compatibility, this sets the virtio-serial-device bus | |
1317 | * name as before. | |
1318 | */ | |
1319 | if (proxy->id) { | |
1320 | bus_name = g_strdup_printf("%s.0", proxy->id); | |
1321 | virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name); | |
1322 | g_free(bus_name); | |
1323 | } | |
1324 | ||
f7f7464a FK |
1325 | qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); |
1326 | if (qdev_init(vdev) < 0) { | |
1327 | return -1; | |
1328 | } | |
1329 | return 0; | |
1330 | } | |
1331 | ||
1332 | static Property virtio_serial_pci_properties[] = { | |
1333 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, | |
1334 | VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), | |
1335 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), | |
1336 | DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0), | |
1337 | DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), | |
1338 | DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtIOSerialPCI, vdev.serial), | |
1339 | DEFINE_PROP_END_OF_LIST(), | |
1340 | }; | |
1341 | ||
1342 | static void virtio_serial_pci_class_init(ObjectClass *klass, void *data) | |
1343 | { | |
1344 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1345 | VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); | |
1346 | PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); | |
1347 | k->init = virtio_serial_pci_init; | |
1348 | dc->props = virtio_serial_pci_properties; | |
1349 | pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
1350 | pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE; | |
1351 | pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; | |
1352 | pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER; | |
1353 | } | |
1354 | ||
1355 | static void virtio_serial_pci_instance_init(Object *obj) | |
1356 | { | |
1357 | VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj); | |
1358 | object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_SERIAL); | |
1359 | object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); | |
1360 | } | |
1361 | ||
1362 | static const TypeInfo virtio_serial_pci_info = { | |
1363 | .name = TYPE_VIRTIO_SERIAL_PCI, | |
1364 | .parent = TYPE_VIRTIO_PCI, | |
1365 | .instance_size = sizeof(VirtIOSerialPCI), | |
1366 | .instance_init = virtio_serial_pci_instance_init, | |
1367 | .class_init = virtio_serial_pci_class_init, | |
1368 | }; | |
1369 | ||
e37da394 FK |
1370 | /* virtio-net-pci */ |
1371 | ||
1372 | static Property virtio_net_properties[] = { | |
1373 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, | |
1374 | VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false), | |
1375 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3), | |
1376 | DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features), | |
1377 | DEFINE_NIC_PROPERTIES(VirtIONetPCI, vdev.nic_conf), | |
1378 | DEFINE_VIRTIO_NET_PROPERTIES(VirtIONetPCI, vdev.net_conf), | |
1379 | DEFINE_PROP_END_OF_LIST(), | |
1380 | }; | |
1381 | ||
1382 | static int virtio_net_pci_init(VirtIOPCIProxy *vpci_dev) | |
1383 | { | |
1384 | VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev); | |
1385 | DeviceState *vdev = DEVICE(&dev->vdev); | |
1386 | ||
1387 | virtio_net_set_config_size(&dev->vdev, vpci_dev->host_features); | |
1388 | qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); | |
1389 | if (qdev_init(vdev) < 0) { | |
1390 | return -1; | |
1391 | } | |
1392 | return 0; | |
1393 | } | |
1394 | ||
1395 | static void virtio_net_pci_class_init(ObjectClass *klass, void *data) | |
1396 | { | |
1397 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1398 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); | |
1399 | VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass); | |
1400 | ||
1401 | k->romfile = "efi-virtio.rom"; | |
1402 | k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
1403 | k->device_id = PCI_DEVICE_ID_VIRTIO_NET; | |
1404 | k->revision = VIRTIO_PCI_ABI_VERSION; | |
1405 | k->class_id = PCI_CLASS_NETWORK_ETHERNET; | |
1406 | dc->props = virtio_net_properties; | |
1407 | vpciklass->init = virtio_net_pci_init; | |
1408 | } | |
1409 | ||
1410 | static void virtio_net_pci_instance_init(Object *obj) | |
1411 | { | |
1412 | VirtIONetPCI *dev = VIRTIO_NET_PCI(obj); | |
1413 | object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_NET); | |
1414 | object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); | |
1415 | } | |
1416 | ||
1417 | static const TypeInfo virtio_net_pci_info = { | |
1418 | .name = TYPE_VIRTIO_NET_PCI, | |
1419 | .parent = TYPE_VIRTIO_PCI, | |
1420 | .instance_size = sizeof(VirtIONetPCI), | |
1421 | .instance_init = virtio_net_pci_instance_init, | |
1422 | .class_init = virtio_net_pci_class_init, | |
1423 | }; | |
1424 | ||
59ccd20a FK |
1425 | /* virtio-rng-pci */ |
1426 | ||
1427 | static Property virtio_rng_pci_properties[] = { | |
1428 | DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), | |
1429 | DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORngPCI, vdev.conf), | |
1430 | DEFINE_PROP_END_OF_LIST(), | |
1431 | }; | |
1432 | ||
1433 | static int virtio_rng_pci_init(VirtIOPCIProxy *vpci_dev) | |
1434 | { | |
1435 | VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev); | |
1436 | DeviceState *vdev = DEVICE(&vrng->vdev); | |
1437 | ||
1438 | qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); | |
1439 | if (qdev_init(vdev) < 0) { | |
1440 | return -1; | |
1441 | } | |
1442 | ||
1443 | object_property_set_link(OBJECT(vrng), | |
1444 | OBJECT(vrng->vdev.conf.default_backend), "rng", | |
1445 | NULL); | |
1446 | ||
1447 | return 0; | |
1448 | } | |
1449 | ||
1450 | static void virtio_rng_pci_class_init(ObjectClass *klass, void *data) | |
1451 | { | |
1452 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1453 | VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); | |
1454 | PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); | |
1455 | ||
1456 | k->init = virtio_rng_pci_init; | |
1457 | dc->props = virtio_rng_pci_properties; | |
1458 | ||
1459 | pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
1460 | pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG; | |
1461 | pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; | |
1462 | pcidev_k->class_id = PCI_CLASS_OTHERS; | |
1463 | } | |
1464 | ||
1465 | static void virtio_rng_initfn(Object *obj) | |
1466 | { | |
1467 | VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj); | |
1468 | object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_RNG); | |
1469 | object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); | |
1470 | object_property_add_link(obj, "rng", TYPE_RNG_BACKEND, | |
1471 | (Object **)&dev->vdev.conf.rng, NULL); | |
1472 | ||
1473 | } | |
1474 | ||
1475 | static const TypeInfo virtio_rng_pci_info = { | |
1476 | .name = TYPE_VIRTIO_RNG_PCI, | |
1477 | .parent = TYPE_VIRTIO_PCI, | |
1478 | .instance_size = sizeof(VirtIORngPCI), | |
1479 | .instance_init = virtio_rng_initfn, | |
1480 | .class_init = virtio_rng_pci_class_init, | |
1481 | }; | |
1482 | ||
0a2acf5e FK |
1483 | /* virtio-pci-bus */ |
1484 | ||
d51fcfac | 1485 | static void virtio_pci_bus_new(VirtioBusState *bus, VirtIOPCIProxy *dev) |
0a2acf5e FK |
1486 | { |
1487 | DeviceState *qdev = DEVICE(dev); | |
1488 | BusState *qbus; | |
f4dd69aa FK |
1489 | char virtio_bus_name[] = "virtio-bus"; |
1490 | ||
1491 | qbus_create_inplace((BusState *)bus, TYPE_VIRTIO_PCI_BUS, qdev, | |
1492 | virtio_bus_name); | |
0a2acf5e | 1493 | qbus = BUS(bus); |
cbd19063 | 1494 | qbus->allow_hotplug = 1; |
0a2acf5e FK |
1495 | } |
1496 | ||
1497 | static void virtio_pci_bus_class_init(ObjectClass *klass, void *data) | |
1498 | { | |
1499 | BusClass *bus_class = BUS_CLASS(klass); | |
1500 | VirtioBusClass *k = VIRTIO_BUS_CLASS(klass); | |
1501 | bus_class->max_dev = 1; | |
1502 | k->notify = virtio_pci_notify; | |
1503 | k->save_config = virtio_pci_save_config; | |
1504 | k->load_config = virtio_pci_load_config; | |
1505 | k->save_queue = virtio_pci_save_queue; | |
1506 | k->load_queue = virtio_pci_load_queue; | |
1507 | k->get_features = virtio_pci_get_features; | |
1508 | k->query_guest_notifiers = virtio_pci_query_guest_notifiers; | |
1509 | k->set_host_notifier = virtio_pci_set_host_notifier; | |
1510 | k->set_guest_notifiers = virtio_pci_set_guest_notifiers; | |
1511 | k->vmstate_change = virtio_pci_vmstate_change; | |
085bccb7 | 1512 | k->device_plugged = virtio_pci_device_plugged; |
0a2acf5e FK |
1513 | } |
1514 | ||
1515 | static const TypeInfo virtio_pci_bus_info = { | |
1516 | .name = TYPE_VIRTIO_PCI_BUS, | |
1517 | .parent = TYPE_VIRTIO_BUS, | |
1518 | .instance_size = sizeof(VirtioPCIBusState), | |
1519 | .class_init = virtio_pci_bus_class_init, | |
1520 | }; | |
1521 | ||
83f7d43a | 1522 | static void virtio_pci_register_types(void) |
53c25cea | 1523 | { |
59ccd20a | 1524 | type_register_static(&virtio_rng_pci_info); |
0a2acf5e | 1525 | type_register_static(&virtio_pci_bus_info); |
085bccb7 | 1526 | type_register_static(&virtio_pci_info); |
60653b28 | 1527 | #ifdef CONFIG_VIRTFS |
234a336f | 1528 | type_register_static(&virtio_9p_pci_info); |
60653b28 | 1529 | #endif |
653ced07 | 1530 | type_register_static(&virtio_blk_pci_info); |
bc7b90a0 | 1531 | type_register_static(&virtio_scsi_pci_info); |
e378e88d | 1532 | type_register_static(&virtio_balloon_pci_info); |
f7f7464a | 1533 | type_register_static(&virtio_serial_pci_info); |
e37da394 | 1534 | type_register_static(&virtio_net_pci_info); |
50787628 NB |
1535 | #ifdef CONFIG_VHOST_SCSI |
1536 | type_register_static(&vhost_scsi_pci_info); | |
1537 | #endif | |
53c25cea PB |
1538 | } |
1539 | ||
83f7d43a | 1540 | type_init(virtio_pci_register_types) |