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