]>
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 | ||
9b8bfe21 | 18 | #include "qemu/osdep.h" |
53c25cea | 19 | |
062c08d1 | 20 | #include "exec/memop.h" |
cbbe4f50 | 21 | #include "standard-headers/linux/virtio_pci.h" |
0d09e41a | 22 | #include "hw/virtio/virtio.h" |
ca77ee28 | 23 | #include "migration/qemu-file-types.h" |
83c9f4ca | 24 | #include "hw/pci/pci.h" |
b0e5196a | 25 | #include "hw/pci/pci_bus.h" |
a27bd6c7 | 26 | #include "hw/qdev-properties.h" |
da34e65c | 27 | #include "qapi/error.h" |
1de7afc9 | 28 | #include "qemu/error-report.h" |
0b8fa32f | 29 | #include "qemu/module.h" |
83c9f4ca PB |
30 | #include "hw/pci/msi.h" |
31 | #include "hw/pci/msix.h" | |
32 | #include "hw/loader.h" | |
9c17d615 | 33 | #include "sysemu/kvm.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 | 40 | |
c17bef33 MT |
41 | #undef VIRTIO_PCI_CONFIG |
42 | ||
aba800a3 MT |
43 | /* The remaining space is defined by each driver as the per-driver |
44 | * configuration space */ | |
cbbe4f50 | 45 | #define VIRTIO_PCI_CONFIG_SIZE(dev) VIRTIO_PCI_CONFIG_OFF(msix_enabled(dev)) |
53c25cea | 46 | |
ac7af112 AF |
47 | static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size, |
48 | VirtIOPCIProxy *dev); | |
75fd6f13 | 49 | static void virtio_pci_reset(DeviceState *qdev); |
d51fcfac | 50 | |
53c25cea | 51 | /* virtio device */ |
d2a0ccc6 MT |
52 | /* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */ |
53 | static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d) | |
54 | { | |
55 | return container_of(d, VirtIOPCIProxy, pci_dev.qdev); | |
56 | } | |
53c25cea | 57 | |
d2a0ccc6 MT |
58 | /* DeviceState to VirtIOPCIProxy. Note: used on datapath, |
59 | * be careful and test performance if you change this. | |
60 | */ | |
61 | static inline VirtIOPCIProxy *to_virtio_pci_proxy_fast(DeviceState *d) | |
53c25cea | 62 | { |
d2a0ccc6 MT |
63 | return container_of(d, VirtIOPCIProxy, pci_dev.qdev); |
64 | } | |
65 | ||
66 | static void virtio_pci_notify(DeviceState *d, uint16_t vector) | |
67 | { | |
68 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy_fast(d); | |
a3fc66d9 | 69 | |
aba800a3 MT |
70 | if (msix_enabled(&proxy->pci_dev)) |
71 | msix_notify(&proxy->pci_dev, vector); | |
a3fc66d9 PB |
72 | else { |
73 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); | |
0687c37c | 74 | pci_set_irq(&proxy->pci_dev, atomic_read(&vdev->isr) & 1); |
a3fc66d9 | 75 | } |
53c25cea PB |
76 | } |
77 | ||
d2a0ccc6 | 78 | static void virtio_pci_save_config(DeviceState *d, QEMUFile *f) |
ff24bd58 | 79 | { |
d2a0ccc6 | 80 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
a3fc66d9 PB |
81 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
82 | ||
ff24bd58 MT |
83 | pci_device_save(&proxy->pci_dev, f); |
84 | msix_save(&proxy->pci_dev, f); | |
85 | if (msix_present(&proxy->pci_dev)) | |
a3fc66d9 | 86 | qemu_put_be16(f, vdev->config_vector); |
ff24bd58 MT |
87 | } |
88 | ||
b81b948e DDAG |
89 | static const VMStateDescription vmstate_virtio_pci_modern_queue_state = { |
90 | .name = "virtio_pci/modern_queue_state", | |
91 | .version_id = 1, | |
92 | .minimum_version_id = 1, | |
93 | .fields = (VMStateField[]) { | |
94 | VMSTATE_UINT16(num, VirtIOPCIQueue), | |
95 | VMSTATE_UNUSED(1), /* enabled was stored as be16 */ | |
96 | VMSTATE_BOOL(enabled, VirtIOPCIQueue), | |
97 | VMSTATE_UINT32_ARRAY(desc, VirtIOPCIQueue, 2), | |
98 | VMSTATE_UINT32_ARRAY(avail, VirtIOPCIQueue, 2), | |
99 | VMSTATE_UINT32_ARRAY(used, VirtIOPCIQueue, 2), | |
100 | VMSTATE_END_OF_LIST() | |
a6df8adf | 101 | } |
a6df8adf JW |
102 | }; |
103 | ||
104 | static bool virtio_pci_modern_state_needed(void *opaque) | |
105 | { | |
106 | VirtIOPCIProxy *proxy = opaque; | |
107 | ||
9a4c0e22 | 108 | return virtio_pci_modern(proxy); |
a6df8adf JW |
109 | } |
110 | ||
b81b948e | 111 | static const VMStateDescription vmstate_virtio_pci_modern_state_sub = { |
a6df8adf JW |
112 | .name = "virtio_pci/modern_state", |
113 | .version_id = 1, | |
114 | .minimum_version_id = 1, | |
115 | .needed = &virtio_pci_modern_state_needed, | |
116 | .fields = (VMStateField[]) { | |
b81b948e DDAG |
117 | VMSTATE_UINT32(dfselect, VirtIOPCIProxy), |
118 | VMSTATE_UINT32(gfselect, VirtIOPCIProxy), | |
119 | VMSTATE_UINT32_ARRAY(guest_features, VirtIOPCIProxy, 2), | |
120 | VMSTATE_STRUCT_ARRAY(vqs, VirtIOPCIProxy, VIRTIO_QUEUE_MAX, 0, | |
121 | vmstate_virtio_pci_modern_queue_state, | |
122 | VirtIOPCIQueue), | |
a6df8adf JW |
123 | VMSTATE_END_OF_LIST() |
124 | } | |
125 | }; | |
126 | ||
127 | static const VMStateDescription vmstate_virtio_pci = { | |
128 | .name = "virtio_pci", | |
129 | .version_id = 1, | |
130 | .minimum_version_id = 1, | |
131 | .minimum_version_id_old = 1, | |
132 | .fields = (VMStateField[]) { | |
133 | VMSTATE_END_OF_LIST() | |
134 | }, | |
135 | .subsections = (const VMStateDescription*[]) { | |
b81b948e | 136 | &vmstate_virtio_pci_modern_state_sub, |
a6df8adf JW |
137 | NULL |
138 | } | |
139 | }; | |
140 | ||
b81b948e DDAG |
141 | static bool virtio_pci_has_extra_state(DeviceState *d) |
142 | { | |
143 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); | |
144 | ||
145 | return proxy->flags & VIRTIO_PCI_FLAG_MIGRATE_EXTRA; | |
146 | } | |
147 | ||
a6df8adf JW |
148 | static void virtio_pci_save_extra_state(DeviceState *d, QEMUFile *f) |
149 | { | |
150 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); | |
151 | ||
152 | vmstate_save_state(f, &vmstate_virtio_pci, proxy, NULL); | |
153 | } | |
154 | ||
155 | static int virtio_pci_load_extra_state(DeviceState *d, QEMUFile *f) | |
156 | { | |
157 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); | |
158 | ||
159 | return vmstate_load_state(f, &vmstate_virtio_pci, proxy, 1); | |
160 | } | |
161 | ||
d2a0ccc6 | 162 | static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f) |
ff24bd58 | 163 | { |
d2a0ccc6 | 164 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
a3fc66d9 PB |
165 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
166 | ||
ff24bd58 | 167 | if (msix_present(&proxy->pci_dev)) |
a3fc66d9 | 168 | qemu_put_be16(f, virtio_queue_vector(vdev, n)); |
ff24bd58 MT |
169 | } |
170 | ||
d2a0ccc6 | 171 | static int virtio_pci_load_config(DeviceState *d, QEMUFile *f) |
ff24bd58 | 172 | { |
d2a0ccc6 | 173 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
a3fc66d9 PB |
174 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
175 | ||
ff24bd58 MT |
176 | int ret; |
177 | ret = pci_device_load(&proxy->pci_dev, f); | |
e6da7680 | 178 | if (ret) { |
ff24bd58 | 179 | return ret; |
e6da7680 | 180 | } |
3cac001e | 181 | msix_unuse_all_vectors(&proxy->pci_dev); |
ff24bd58 | 182 | msix_load(&proxy->pci_dev, f); |
e6da7680 | 183 | if (msix_present(&proxy->pci_dev)) { |
a3fc66d9 | 184 | qemu_get_be16s(f, &vdev->config_vector); |
e6da7680 | 185 | } else { |
a3fc66d9 | 186 | vdev->config_vector = VIRTIO_NO_VECTOR; |
e6da7680 | 187 | } |
a3fc66d9 PB |
188 | if (vdev->config_vector != VIRTIO_NO_VECTOR) { |
189 | return msix_vector_use(&proxy->pci_dev, vdev->config_vector); | |
e6da7680 | 190 | } |
ff24bd58 MT |
191 | return 0; |
192 | } | |
193 | ||
d2a0ccc6 | 194 | static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f) |
ff24bd58 | 195 | { |
d2a0ccc6 | 196 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
a3fc66d9 PB |
197 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
198 | ||
ff24bd58 | 199 | uint16_t vector; |
e6da7680 MT |
200 | if (msix_present(&proxy->pci_dev)) { |
201 | qemu_get_be16s(f, &vector); | |
202 | } else { | |
203 | vector = VIRTIO_NO_VECTOR; | |
204 | } | |
a3fc66d9 | 205 | virtio_queue_set_vector(vdev, n, vector); |
e6da7680 MT |
206 | if (vector != VIRTIO_NO_VECTOR) { |
207 | return msix_vector_use(&proxy->pci_dev, vector); | |
208 | } | |
a6df8adf | 209 | |
ff24bd58 MT |
210 | return 0; |
211 | } | |
212 | ||
8e93cef1 | 213 | static bool virtio_pci_ioeventfd_enabled(DeviceState *d) |
9f06e71a CH |
214 | { |
215 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); | |
216 | ||
8e93cef1 | 217 | return (proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) != 0; |
9f06e71a CH |
218 | } |
219 | ||
975acc0a JW |
220 | #define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000 |
221 | ||
d9997d89 MA |
222 | static inline int virtio_pci_queue_mem_mult(struct VirtIOPCIProxy *proxy) |
223 | { | |
224 | return (proxy->flags & VIRTIO_PCI_FLAG_PAGE_PER_VQ) ? | |
225 | QEMU_VIRTIO_PCI_QUEUE_MEM_MULT : 4; | |
226 | } | |
227 | ||
9f06e71a CH |
228 | static int virtio_pci_ioeventfd_assign(DeviceState *d, EventNotifier *notifier, |
229 | int n, bool assign) | |
25db9ebe | 230 | { |
9f06e71a | 231 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
a3fc66d9 PB |
232 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
233 | VirtQueue *vq = virtio_get_queue(vdev, n); | |
9a4c0e22 MA |
234 | bool legacy = virtio_pci_legacy(proxy); |
235 | bool modern = virtio_pci_modern(proxy); | |
bc85ccfd | 236 | bool fast_mmio = kvm_ioeventfd_any_length_enabled(); |
9824d2a3 | 237 | bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY; |
588255ad | 238 | MemoryRegion *modern_mr = &proxy->notify.mr; |
9824d2a3 | 239 | MemoryRegion *modern_notify_mr = &proxy->notify_pio.mr; |
975acc0a | 240 | MemoryRegion *legacy_mr = &proxy->bar; |
d9997d89 | 241 | hwaddr modern_addr = virtio_pci_queue_mem_mult(proxy) * |
975acc0a JW |
242 | virtio_get_queue_index(vq); |
243 | hwaddr legacy_addr = VIRTIO_PCI_QUEUE_NOTIFY; | |
da146d0a | 244 | |
25db9ebe | 245 | if (assign) { |
975acc0a | 246 | if (modern) { |
bc85ccfd JW |
247 | if (fast_mmio) { |
248 | memory_region_add_eventfd(modern_mr, modern_addr, 0, | |
249 | false, n, notifier); | |
250 | } else { | |
251 | memory_region_add_eventfd(modern_mr, modern_addr, 2, | |
252 | false, n, notifier); | |
253 | } | |
9824d2a3 JW |
254 | if (modern_pio) { |
255 | memory_region_add_eventfd(modern_notify_mr, 0, 2, | |
256 | true, n, notifier); | |
257 | } | |
975acc0a JW |
258 | } |
259 | if (legacy) { | |
260 | memory_region_add_eventfd(legacy_mr, legacy_addr, 2, | |
261 | true, n, notifier); | |
262 | } | |
25db9ebe | 263 | } else { |
975acc0a | 264 | if (modern) { |
bc85ccfd JW |
265 | if (fast_mmio) { |
266 | memory_region_del_eventfd(modern_mr, modern_addr, 0, | |
267 | false, n, notifier); | |
268 | } else { | |
269 | memory_region_del_eventfd(modern_mr, modern_addr, 2, | |
270 | false, n, notifier); | |
271 | } | |
9824d2a3 JW |
272 | if (modern_pio) { |
273 | memory_region_del_eventfd(modern_notify_mr, 0, 2, | |
274 | true, n, notifier); | |
275 | } | |
975acc0a JW |
276 | } |
277 | if (legacy) { | |
278 | memory_region_del_eventfd(legacy_mr, legacy_addr, 2, | |
279 | true, n, notifier); | |
280 | } | |
25db9ebe | 281 | } |
9f06e71a | 282 | return 0; |
25db9ebe SH |
283 | } |
284 | ||
b36e3914 | 285 | static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy) |
25db9ebe | 286 | { |
9f06e71a | 287 | virtio_bus_start_ioeventfd(&proxy->bus); |
25db9ebe SH |
288 | } |
289 | ||
b36e3914 | 290 | static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy) |
25db9ebe | 291 | { |
9f06e71a | 292 | virtio_bus_stop_ioeventfd(&proxy->bus); |
25db9ebe SH |
293 | } |
294 | ||
53c25cea PB |
295 | static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val) |
296 | { | |
297 | VirtIOPCIProxy *proxy = opaque; | |
a3fc66d9 | 298 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
a8170e5e | 299 | hwaddr pa; |
53c25cea | 300 | |
53c25cea PB |
301 | switch (addr) { |
302 | case VIRTIO_PCI_GUEST_FEATURES: | |
181103cd FK |
303 | /* Guest does not negotiate properly? We have to assume nothing. */ |
304 | if (val & (1 << VIRTIO_F_BAD_FEATURE)) { | |
305 | val = virtio_bus_get_vdev_bad_features(&proxy->bus); | |
306 | } | |
ad0c9332 | 307 | virtio_set_features(vdev, val); |
53c25cea PB |
308 | break; |
309 | case VIRTIO_PCI_QUEUE_PFN: | |
a8170e5e | 310 | pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT; |
1b8e9b27 | 311 | if (pa == 0) { |
75fd6f13 | 312 | virtio_pci_reset(DEVICE(proxy)); |
1b8e9b27 | 313 | } |
7055e687 MT |
314 | else |
315 | virtio_queue_set_addr(vdev, vdev->queue_sel, pa); | |
53c25cea PB |
316 | break; |
317 | case VIRTIO_PCI_QUEUE_SEL: | |
87b3bd1c | 318 | if (val < VIRTIO_QUEUE_MAX) |
53c25cea PB |
319 | vdev->queue_sel = val; |
320 | break; | |
321 | case VIRTIO_PCI_QUEUE_NOTIFY: | |
87b3bd1c | 322 | if (val < VIRTIO_QUEUE_MAX) { |
7157e2e2 SH |
323 | virtio_queue_notify(vdev, val); |
324 | } | |
53c25cea PB |
325 | break; |
326 | case VIRTIO_PCI_STATUS: | |
25db9ebe SH |
327 | if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) { |
328 | virtio_pci_stop_ioeventfd(proxy); | |
329 | } | |
330 | ||
3e607cb5 | 331 | virtio_set_status(vdev, val & 0xFF); |
25db9ebe SH |
332 | |
333 | if (val & VIRTIO_CONFIG_S_DRIVER_OK) { | |
334 | virtio_pci_start_ioeventfd(proxy); | |
335 | } | |
336 | ||
1b8e9b27 | 337 | if (vdev->status == 0) { |
75fd6f13 | 338 | virtio_pci_reset(DEVICE(proxy)); |
1b8e9b27 | 339 | } |
c81131db | 340 | |
e43c0b2e MT |
341 | /* Linux before 2.6.34 drives the device without enabling |
342 | the PCI device bus master bit. Enable it automatically | |
343 | for the guest. This is a PCI spec violation but so is | |
344 | initiating DMA with bus master bit clear. */ | |
345 | if (val == (VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER)) { | |
346 | pci_default_write_config(&proxy->pci_dev, PCI_COMMAND, | |
347 | proxy->pci_dev.config[PCI_COMMAND] | | |
348 | PCI_COMMAND_MASTER, 1); | |
349 | } | |
53c25cea | 350 | break; |
aba800a3 MT |
351 | case VIRTIO_MSI_CONFIG_VECTOR: |
352 | msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); | |
353 | /* Make it possible for guest to discover an error took place. */ | |
354 | if (msix_vector_use(&proxy->pci_dev, val) < 0) | |
355 | val = VIRTIO_NO_VECTOR; | |
356 | vdev->config_vector = val; | |
357 | break; | |
358 | case VIRTIO_MSI_QUEUE_VECTOR: | |
359 | msix_vector_unuse(&proxy->pci_dev, | |
360 | virtio_queue_vector(vdev, vdev->queue_sel)); | |
361 | /* Make it possible for guest to discover an error took place. */ | |
362 | if (msix_vector_use(&proxy->pci_dev, val) < 0) | |
363 | val = VIRTIO_NO_VECTOR; | |
364 | virtio_queue_set_vector(vdev, vdev->queue_sel, val); | |
365 | break; | |
366 | default: | |
4e02d460 SH |
367 | error_report("%s: unexpected address 0x%x value 0x%x", |
368 | __func__, addr, val); | |
aba800a3 | 369 | break; |
53c25cea PB |
370 | } |
371 | } | |
372 | ||
aba800a3 | 373 | static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr) |
53c25cea | 374 | { |
a3fc66d9 | 375 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
53c25cea PB |
376 | uint32_t ret = 0xFFFFFFFF; |
377 | ||
53c25cea PB |
378 | switch (addr) { |
379 | case VIRTIO_PCI_HOST_FEATURES: | |
6b8f1020 | 380 | ret = vdev->host_features; |
53c25cea PB |
381 | break; |
382 | case VIRTIO_PCI_GUEST_FEATURES: | |
704a76fc | 383 | ret = vdev->guest_features; |
53c25cea PB |
384 | break; |
385 | case VIRTIO_PCI_QUEUE_PFN: | |
386 | ret = virtio_queue_get_addr(vdev, vdev->queue_sel) | |
387 | >> VIRTIO_PCI_QUEUE_ADDR_SHIFT; | |
388 | break; | |
389 | case VIRTIO_PCI_QUEUE_NUM: | |
390 | ret = virtio_queue_get_num(vdev, vdev->queue_sel); | |
391 | break; | |
392 | case VIRTIO_PCI_QUEUE_SEL: | |
393 | ret = vdev->queue_sel; | |
394 | break; | |
395 | case VIRTIO_PCI_STATUS: | |
396 | ret = vdev->status; | |
397 | break; | |
398 | case VIRTIO_PCI_ISR: | |
399 | /* reading from the ISR also clears it. */ | |
0687c37c | 400 | ret = atomic_xchg(&vdev->isr, 0); |
9e64f8a3 | 401 | pci_irq_deassert(&proxy->pci_dev); |
53c25cea | 402 | break; |
aba800a3 MT |
403 | case VIRTIO_MSI_CONFIG_VECTOR: |
404 | ret = vdev->config_vector; | |
405 | break; | |
406 | case VIRTIO_MSI_QUEUE_VECTOR: | |
407 | ret = virtio_queue_vector(vdev, vdev->queue_sel); | |
408 | break; | |
53c25cea PB |
409 | default: |
410 | break; | |
411 | } | |
412 | ||
413 | return ret; | |
414 | } | |
415 | ||
df6db5b3 AG |
416 | static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr, |
417 | unsigned size) | |
53c25cea PB |
418 | { |
419 | VirtIOPCIProxy *proxy = opaque; | |
a3fc66d9 | 420 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
cbbe4f50 | 421 | uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev); |
df6db5b3 | 422 | uint64_t val = 0; |
aba800a3 | 423 | if (addr < config) { |
df6db5b3 | 424 | return virtio_ioport_read(proxy, addr); |
aba800a3 MT |
425 | } |
426 | addr -= config; | |
53c25cea | 427 | |
df6db5b3 AG |
428 | switch (size) { |
429 | case 1: | |
a3fc66d9 | 430 | val = virtio_config_readb(vdev, addr); |
df6db5b3 AG |
431 | break; |
432 | case 2: | |
a3fc66d9 | 433 | val = virtio_config_readw(vdev, addr); |
616a6552 | 434 | if (virtio_is_big_endian(vdev)) { |
8e4a424b BS |
435 | val = bswap16(val); |
436 | } | |
df6db5b3 AG |
437 | break; |
438 | case 4: | |
a3fc66d9 | 439 | val = virtio_config_readl(vdev, addr); |
616a6552 | 440 | if (virtio_is_big_endian(vdev)) { |
8e4a424b BS |
441 | val = bswap32(val); |
442 | } | |
df6db5b3 | 443 | break; |
82afa586 | 444 | } |
df6db5b3 | 445 | return val; |
53c25cea PB |
446 | } |
447 | ||
df6db5b3 AG |
448 | static void virtio_pci_config_write(void *opaque, hwaddr addr, |
449 | uint64_t val, unsigned size) | |
53c25cea PB |
450 | { |
451 | VirtIOPCIProxy *proxy = opaque; | |
cbbe4f50 | 452 | uint32_t config = VIRTIO_PCI_CONFIG_SIZE(&proxy->pci_dev); |
a3fc66d9 | 453 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
aba800a3 MT |
454 | if (addr < config) { |
455 | virtio_ioport_write(proxy, addr, val); | |
456 | return; | |
457 | } | |
458 | addr -= config; | |
df6db5b3 AG |
459 | /* |
460 | * Virtio-PCI is odd. Ioports are LE but config space is target native | |
461 | * endian. | |
462 | */ | |
463 | switch (size) { | |
464 | case 1: | |
a3fc66d9 | 465 | virtio_config_writeb(vdev, addr, val); |
df6db5b3 AG |
466 | break; |
467 | case 2: | |
616a6552 | 468 | if (virtio_is_big_endian(vdev)) { |
8e4a424b BS |
469 | val = bswap16(val); |
470 | } | |
a3fc66d9 | 471 | virtio_config_writew(vdev, addr, val); |
df6db5b3 AG |
472 | break; |
473 | case 4: | |
616a6552 | 474 | if (virtio_is_big_endian(vdev)) { |
8e4a424b BS |
475 | val = bswap32(val); |
476 | } | |
a3fc66d9 | 477 | virtio_config_writel(vdev, addr, val); |
df6db5b3 | 478 | break; |
82afa586 | 479 | } |
53c25cea PB |
480 | } |
481 | ||
da146d0a | 482 | static const MemoryRegionOps virtio_pci_config_ops = { |
df6db5b3 AG |
483 | .read = virtio_pci_config_read, |
484 | .write = virtio_pci_config_write, | |
485 | .impl = { | |
486 | .min_access_size = 1, | |
487 | .max_access_size = 4, | |
488 | }, | |
8e4a424b | 489 | .endianness = DEVICE_LITTLE_ENDIAN, |
da146d0a | 490 | }; |
aba800a3 | 491 | |
a93c8d82 AK |
492 | static MemoryRegion *virtio_address_space_lookup(VirtIOPCIProxy *proxy, |
493 | hwaddr *off, int len) | |
494 | { | |
495 | int i; | |
496 | VirtIOPCIRegion *reg; | |
497 | ||
498 | for (i = 0; i < ARRAY_SIZE(proxy->regs); ++i) { | |
499 | reg = &proxy->regs[i]; | |
500 | if (*off >= reg->offset && | |
501 | *off + len <= reg->offset + reg->size) { | |
502 | *off -= reg->offset; | |
503 | return ®->mr; | |
504 | } | |
505 | } | |
506 | ||
507 | return NULL; | |
508 | } | |
509 | ||
1e40356c MT |
510 | /* Below are generic functions to do memcpy from/to an address space, |
511 | * without byteswaps, with input validation. | |
512 | * | |
513 | * As regular address_space_* APIs all do some kind of byteswap at least for | |
514 | * some host/target combinations, we are forced to explicitly convert to a | |
515 | * known-endianness integer value. | |
516 | * It doesn't really matter which endian format to go through, so the code | |
517 | * below selects the endian that causes the least amount of work on the given | |
518 | * host. | |
519 | * | |
520 | * Note: host pointer must be aligned. | |
521 | */ | |
522 | static | |
a93c8d82 | 523 | void virtio_address_space_write(VirtIOPCIProxy *proxy, hwaddr addr, |
1e40356c MT |
524 | const uint8_t *buf, int len) |
525 | { | |
a93c8d82 AK |
526 | uint64_t val; |
527 | MemoryRegion *mr; | |
1e40356c MT |
528 | |
529 | /* address_space_* APIs assume an aligned address. | |
530 | * As address is under guest control, handle illegal values. | |
531 | */ | |
532 | addr &= ~(len - 1); | |
533 | ||
a93c8d82 AK |
534 | mr = virtio_address_space_lookup(proxy, &addr, len); |
535 | if (!mr) { | |
536 | return; | |
537 | } | |
538 | ||
1e40356c MT |
539 | /* Make sure caller aligned buf properly */ |
540 | assert(!(((uintptr_t)buf) & (len - 1))); | |
541 | ||
542 | switch (len) { | |
543 | case 1: | |
544 | val = pci_get_byte(buf); | |
1e40356c MT |
545 | break; |
546 | case 2: | |
9bf825bf | 547 | val = pci_get_word(buf); |
1e40356c MT |
548 | break; |
549 | case 4: | |
9bf825bf | 550 | val = pci_get_long(buf); |
1e40356c MT |
551 | break; |
552 | default: | |
553 | /* As length is under guest control, handle illegal values. */ | |
a93c8d82 | 554 | return; |
1e40356c | 555 | } |
d5d680ca | 556 | memory_region_dispatch_write(mr, addr, val, size_memop(len) | MO_LE, |
062c08d1 | 557 | MEMTXATTRS_UNSPECIFIED); |
1e40356c MT |
558 | } |
559 | ||
560 | static void | |
a93c8d82 AK |
561 | virtio_address_space_read(VirtIOPCIProxy *proxy, hwaddr addr, |
562 | uint8_t *buf, int len) | |
1e40356c | 563 | { |
a93c8d82 AK |
564 | uint64_t val; |
565 | MemoryRegion *mr; | |
1e40356c MT |
566 | |
567 | /* address_space_* APIs assume an aligned address. | |
568 | * As address is under guest control, handle illegal values. | |
569 | */ | |
570 | addr &= ~(len - 1); | |
571 | ||
a93c8d82 AK |
572 | mr = virtio_address_space_lookup(proxy, &addr, len); |
573 | if (!mr) { | |
574 | return; | |
575 | } | |
576 | ||
1e40356c MT |
577 | /* Make sure caller aligned buf properly */ |
578 | assert(!(((uintptr_t)buf) & (len - 1))); | |
579 | ||
d5d680ca | 580 | memory_region_dispatch_read(mr, addr, &val, size_memop(len) | MO_LE, |
062c08d1 | 581 | MEMTXATTRS_UNSPECIFIED); |
1e40356c MT |
582 | switch (len) { |
583 | case 1: | |
1e40356c MT |
584 | pci_set_byte(buf, val); |
585 | break; | |
586 | case 2: | |
9bf825bf | 587 | pci_set_word(buf, val); |
1e40356c MT |
588 | break; |
589 | case 4: | |
9bf825bf | 590 | pci_set_long(buf, val); |
1e40356c MT |
591 | break; |
592 | default: | |
593 | /* As length is under guest control, handle illegal values. */ | |
594 | break; | |
595 | } | |
596 | } | |
597 | ||
aba800a3 MT |
598 | static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, |
599 | uint32_t val, int len) | |
600 | { | |
3f262b26 | 601 | VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev); |
a3fc66d9 | 602 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
ada434cd | 603 | struct virtio_pci_cfg_cap *cfg; |
ed757e14 | 604 | |
1129714f MT |
605 | pci_default_write_config(pci_dev, address, val, len); |
606 | ||
eb1556c4 JS |
607 | if (proxy->flags & VIRTIO_PCI_FLAG_INIT_FLR) { |
608 | pcie_cap_flr_write_config(pci_dev, address, val, len); | |
609 | } | |
610 | ||
1129714f | 611 | if (range_covers_byte(address, len, PCI_COMMAND) && |
68a27b20 | 612 | !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { |
1129714f | 613 | virtio_pci_stop_ioeventfd(proxy); |
45363e46 | 614 | virtio_set_status(vdev, vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK); |
ed757e14 | 615 | } |
ada434cd MT |
616 | |
617 | if (proxy->config_cap && | |
618 | ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap, | |
619 | pci_cfg_data), | |
620 | sizeof cfg->pci_cfg_data)) { | |
621 | uint32_t off; | |
622 | uint32_t len; | |
623 | ||
624 | cfg = (void *)(proxy->pci_dev.config + proxy->config_cap); | |
625 | off = le32_to_cpu(cfg->cap.offset); | |
626 | len = le32_to_cpu(cfg->cap.length); | |
627 | ||
2a639123 MT |
628 | if (len == 1 || len == 2 || len == 4) { |
629 | assert(len <= sizeof cfg->pci_cfg_data); | |
a93c8d82 | 630 | virtio_address_space_write(proxy, off, cfg->pci_cfg_data, len); |
ada434cd MT |
631 | } |
632 | } | |
633 | } | |
634 | ||
635 | static uint32_t virtio_read_config(PCIDevice *pci_dev, | |
636 | uint32_t address, int len) | |
637 | { | |
3f262b26 | 638 | VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev); |
ada434cd MT |
639 | struct virtio_pci_cfg_cap *cfg; |
640 | ||
641 | if (proxy->config_cap && | |
642 | ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap, | |
643 | pci_cfg_data), | |
644 | sizeof cfg->pci_cfg_data)) { | |
645 | uint32_t off; | |
646 | uint32_t len; | |
647 | ||
648 | cfg = (void *)(proxy->pci_dev.config + proxy->config_cap); | |
649 | off = le32_to_cpu(cfg->cap.offset); | |
650 | len = le32_to_cpu(cfg->cap.length); | |
651 | ||
2a639123 MT |
652 | if (len == 1 || len == 2 || len == 4) { |
653 | assert(len <= sizeof cfg->pci_cfg_data); | |
a93c8d82 | 654 | virtio_address_space_read(proxy, off, cfg->pci_cfg_data, len); |
ada434cd MT |
655 | } |
656 | } | |
657 | ||
658 | return pci_default_read_config(pci_dev, address, len); | |
53c25cea PB |
659 | } |
660 | ||
7d37d351 JK |
661 | static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy, |
662 | unsigned int queue_no, | |
d1f6af6a | 663 | unsigned int vector) |
7d37d351 | 664 | { |
7d37d351 | 665 | VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; |
15b2bd18 | 666 | int ret; |
7d37d351 JK |
667 | |
668 | if (irqfd->users == 0) { | |
d1f6af6a | 669 | ret = kvm_irqchip_add_msi_route(kvm_state, vector, &proxy->pci_dev); |
7d37d351 JK |
670 | if (ret < 0) { |
671 | return ret; | |
672 | } | |
673 | irqfd->virq = ret; | |
674 | } | |
675 | irqfd->users++; | |
7d37d351 JK |
676 | return 0; |
677 | } | |
678 | ||
679 | static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy, | |
7d37d351 | 680 | unsigned int vector) |
774345f9 MT |
681 | { |
682 | VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; | |
683 | if (--irqfd->users == 0) { | |
684 | kvm_irqchip_release_virq(kvm_state, irqfd->virq); | |
685 | } | |
686 | } | |
687 | ||
f1d0f15a MT |
688 | static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy, |
689 | unsigned int queue_no, | |
690 | unsigned int vector) | |
691 | { | |
692 | VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; | |
a3fc66d9 PB |
693 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
694 | VirtQueue *vq = virtio_get_queue(vdev, queue_no); | |
f1d0f15a | 695 | EventNotifier *n = virtio_queue_get_guest_notifier(vq); |
9be38598 | 696 | return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, n, NULL, irqfd->virq); |
f1d0f15a MT |
697 | } |
698 | ||
699 | static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy, | |
700 | unsigned int queue_no, | |
701 | unsigned int vector) | |
7d37d351 | 702 | { |
a3fc66d9 PB |
703 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
704 | VirtQueue *vq = virtio_get_queue(vdev, queue_no); | |
15b2bd18 | 705 | EventNotifier *n = virtio_queue_get_guest_notifier(vq); |
7d37d351 | 706 | VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; |
15b2bd18 | 707 | int ret; |
7d37d351 | 708 | |
1c9b71a7 | 709 | ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, n, irqfd->virq); |
7d37d351 | 710 | assert(ret == 0); |
f1d0f15a | 711 | } |
7d37d351 | 712 | |
774345f9 MT |
713 | static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs) |
714 | { | |
715 | PCIDevice *dev = &proxy->pci_dev; | |
a3fc66d9 | 716 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
181103cd | 717 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
774345f9 MT |
718 | unsigned int vector; |
719 | int ret, queue_no; | |
774345f9 MT |
720 | |
721 | for (queue_no = 0; queue_no < nvqs; queue_no++) { | |
722 | if (!virtio_queue_get_num(vdev, queue_no)) { | |
723 | break; | |
724 | } | |
725 | vector = virtio_queue_vector(vdev, queue_no); | |
726 | if (vector >= msix_nr_vectors_allocated(dev)) { | |
727 | continue; | |
728 | } | |
d1f6af6a | 729 | ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector); |
774345f9 MT |
730 | if (ret < 0) { |
731 | goto undo; | |
7d37d351 | 732 | } |
f1d0f15a MT |
733 | /* If guest supports masking, set up irqfd now. |
734 | * Otherwise, delay until unmasked in the frontend. | |
735 | */ | |
5669655a | 736 | if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) { |
f1d0f15a MT |
737 | ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector); |
738 | if (ret < 0) { | |
739 | kvm_virtio_pci_vq_vector_release(proxy, vector); | |
740 | goto undo; | |
741 | } | |
742 | } | |
7d37d351 | 743 | } |
7d37d351 | 744 | return 0; |
774345f9 MT |
745 | |
746 | undo: | |
747 | while (--queue_no >= 0) { | |
748 | vector = virtio_queue_vector(vdev, queue_no); | |
749 | if (vector >= msix_nr_vectors_allocated(dev)) { | |
750 | continue; | |
751 | } | |
5669655a | 752 | if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) { |
e387f99e | 753 | kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); |
f1d0f15a | 754 | } |
774345f9 MT |
755 | kvm_virtio_pci_vq_vector_release(proxy, vector); |
756 | } | |
757 | return ret; | |
7d37d351 JK |
758 | } |
759 | ||
774345f9 MT |
760 | static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs) |
761 | { | |
762 | PCIDevice *dev = &proxy->pci_dev; | |
a3fc66d9 | 763 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
774345f9 MT |
764 | unsigned int vector; |
765 | int queue_no; | |
181103cd | 766 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
774345f9 MT |
767 | |
768 | for (queue_no = 0; queue_no < nvqs; queue_no++) { | |
769 | if (!virtio_queue_get_num(vdev, queue_no)) { | |
770 | break; | |
771 | } | |
772 | vector = virtio_queue_vector(vdev, queue_no); | |
773 | if (vector >= msix_nr_vectors_allocated(dev)) { | |
774 | continue; | |
775 | } | |
f1d0f15a MT |
776 | /* If guest supports masking, clean up irqfd now. |
777 | * Otherwise, it was cleaned when masked in the frontend. | |
778 | */ | |
5669655a | 779 | if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) { |
e387f99e | 780 | kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); |
f1d0f15a | 781 | } |
774345f9 MT |
782 | kvm_virtio_pci_vq_vector_release(proxy, vector); |
783 | } | |
784 | } | |
785 | ||
a38b2c49 MT |
786 | static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy, |
787 | unsigned int queue_no, | |
788 | unsigned int vector, | |
789 | MSIMessage msg) | |
774345f9 | 790 | { |
a3fc66d9 PB |
791 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
792 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
793 | VirtQueue *vq = virtio_get_queue(vdev, queue_no); | |
774345f9 | 794 | EventNotifier *n = virtio_queue_get_guest_notifier(vq); |
a38b2c49 | 795 | VirtIOIRQFD *irqfd; |
53510bfc | 796 | int ret = 0; |
774345f9 | 797 | |
a38b2c49 MT |
798 | if (proxy->vector_irqfd) { |
799 | irqfd = &proxy->vector_irqfd[vector]; | |
800 | if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) { | |
dc9f06ca PF |
801 | ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg, |
802 | &proxy->pci_dev); | |
a38b2c49 MT |
803 | if (ret < 0) { |
804 | return ret; | |
805 | } | |
3f1fea0f | 806 | kvm_irqchip_commit_routes(kvm_state); |
774345f9 MT |
807 | } |
808 | } | |
809 | ||
f1d0f15a MT |
810 | /* If guest supports masking, irqfd is already setup, unmask it. |
811 | * Otherwise, set it up now. | |
812 | */ | |
5669655a | 813 | if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) { |
a3fc66d9 | 814 | k->guest_notifier_mask(vdev, queue_no, false); |
f1d0f15a | 815 | /* Test after unmasking to avoid losing events. */ |
181103cd | 816 | if (k->guest_notifier_pending && |
a3fc66d9 | 817 | k->guest_notifier_pending(vdev, queue_no)) { |
f1d0f15a MT |
818 | event_notifier_set(n); |
819 | } | |
820 | } else { | |
821 | ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector); | |
7d37d351 | 822 | } |
774345f9 | 823 | return ret; |
7d37d351 JK |
824 | } |
825 | ||
a38b2c49 | 826 | static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy, |
7d37d351 JK |
827 | unsigned int queue_no, |
828 | unsigned int vector) | |
829 | { | |
a3fc66d9 PB |
830 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
831 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); | |
181103cd | 832 | |
f1d0f15a MT |
833 | /* If guest supports masking, keep irqfd but mask it. |
834 | * Otherwise, clean it up now. | |
835 | */ | |
5669655a | 836 | if (vdev->use_guest_notifier_mask && k->guest_notifier_mask) { |
a3fc66d9 | 837 | k->guest_notifier_mask(vdev, queue_no, true); |
f1d0f15a | 838 | } else { |
e387f99e | 839 | kvm_virtio_pci_irqfd_release(proxy, queue_no, vector); |
f1d0f15a | 840 | } |
7d37d351 JK |
841 | } |
842 | ||
a38b2c49 MT |
843 | static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector, |
844 | MSIMessage msg) | |
7d37d351 JK |
845 | { |
846 | VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); | |
a3fc66d9 | 847 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
851c2a75 JW |
848 | VirtQueue *vq = virtio_vector_first_queue(vdev, vector); |
849 | int ret, index, unmasked = 0; | |
7d37d351 | 850 | |
851c2a75 JW |
851 | while (vq) { |
852 | index = virtio_get_queue_index(vq); | |
853 | if (!virtio_queue_get_num(vdev, index)) { | |
7d37d351 JK |
854 | break; |
855 | } | |
6652d081 JW |
856 | if (index < proxy->nvqs_with_notifiers) { |
857 | ret = virtio_pci_vq_vector_unmask(proxy, index, vector, msg); | |
858 | if (ret < 0) { | |
859 | goto undo; | |
860 | } | |
861 | ++unmasked; | |
7d37d351 | 862 | } |
851c2a75 | 863 | vq = virtio_vector_next_queue(vq); |
7d37d351 | 864 | } |
851c2a75 | 865 | |
7d37d351 JK |
866 | return 0; |
867 | ||
868 | undo: | |
851c2a75 | 869 | vq = virtio_vector_first_queue(vdev, vector); |
6652d081 | 870 | while (vq && unmasked >= 0) { |
851c2a75 | 871 | index = virtio_get_queue_index(vq); |
6652d081 JW |
872 | if (index < proxy->nvqs_with_notifiers) { |
873 | virtio_pci_vq_vector_mask(proxy, index, vector); | |
874 | --unmasked; | |
875 | } | |
851c2a75 | 876 | vq = virtio_vector_next_queue(vq); |
7d37d351 JK |
877 | } |
878 | return ret; | |
879 | } | |
880 | ||
a38b2c49 | 881 | static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector) |
7d37d351 JK |
882 | { |
883 | VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); | |
a3fc66d9 | 884 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
851c2a75 JW |
885 | VirtQueue *vq = virtio_vector_first_queue(vdev, vector); |
886 | int index; | |
7d37d351 | 887 | |
851c2a75 JW |
888 | while (vq) { |
889 | index = virtio_get_queue_index(vq); | |
890 | if (!virtio_queue_get_num(vdev, index)) { | |
7d37d351 JK |
891 | break; |
892 | } | |
6652d081 JW |
893 | if (index < proxy->nvqs_with_notifiers) { |
894 | virtio_pci_vq_vector_mask(proxy, index, vector); | |
895 | } | |
851c2a75 | 896 | vq = virtio_vector_next_queue(vq); |
7d37d351 JK |
897 | } |
898 | } | |
899 | ||
a38b2c49 MT |
900 | static void virtio_pci_vector_poll(PCIDevice *dev, |
901 | unsigned int vector_start, | |
902 | unsigned int vector_end) | |
89d62be9 MT |
903 | { |
904 | VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); | |
a3fc66d9 | 905 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
181103cd | 906 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
89d62be9 MT |
907 | int queue_no; |
908 | unsigned int vector; | |
909 | EventNotifier *notifier; | |
910 | VirtQueue *vq; | |
911 | ||
2d620f59 | 912 | for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) { |
89d62be9 MT |
913 | if (!virtio_queue_get_num(vdev, queue_no)) { |
914 | break; | |
915 | } | |
916 | vector = virtio_queue_vector(vdev, queue_no); | |
917 | if (vector < vector_start || vector >= vector_end || | |
918 | !msix_is_masked(dev, vector)) { | |
919 | continue; | |
920 | } | |
921 | vq = virtio_get_queue(vdev, queue_no); | |
922 | notifier = virtio_queue_get_guest_notifier(vq); | |
181103cd FK |
923 | if (k->guest_notifier_pending) { |
924 | if (k->guest_notifier_pending(vdev, queue_no)) { | |
f1d0f15a MT |
925 | msix_set_pending(dev, vector); |
926 | } | |
927 | } else if (event_notifier_test_and_clear(notifier)) { | |
89d62be9 MT |
928 | msix_set_pending(dev, vector); |
929 | } | |
930 | } | |
931 | } | |
932 | ||
933 | static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign, | |
934 | bool with_irqfd) | |
ade80dc8 | 935 | { |
d2a0ccc6 | 936 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
a3fc66d9 PB |
937 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
938 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); | |
939 | VirtQueue *vq = virtio_get_queue(vdev, n); | |
ade80dc8 MT |
940 | EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); |
941 | ||
942 | if (assign) { | |
943 | int r = event_notifier_init(notifier, 0); | |
944 | if (r < 0) { | |
945 | return r; | |
946 | } | |
89d62be9 | 947 | virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd); |
ade80dc8 | 948 | } else { |
89d62be9 | 949 | virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd); |
ade80dc8 MT |
950 | event_notifier_cleanup(notifier); |
951 | } | |
952 | ||
5669655a VK |
953 | if (!msix_enabled(&proxy->pci_dev) && |
954 | vdev->use_guest_notifier_mask && | |
955 | vdc->guest_notifier_mask) { | |
a3fc66d9 | 956 | vdc->guest_notifier_mask(vdev, n, !assign); |
62c96360 MT |
957 | } |
958 | ||
ade80dc8 MT |
959 | return 0; |
960 | } | |
961 | ||
d2a0ccc6 | 962 | static bool virtio_pci_query_guest_notifiers(DeviceState *d) |
5430a28f | 963 | { |
d2a0ccc6 | 964 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
5430a28f MT |
965 | return msix_enabled(&proxy->pci_dev); |
966 | } | |
967 | ||
2d620f59 | 968 | static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign) |
54dd9321 | 969 | { |
d2a0ccc6 | 970 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
a3fc66d9 | 971 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
181103cd | 972 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
54dd9321 | 973 | int r, n; |
89d62be9 MT |
974 | bool with_irqfd = msix_enabled(&proxy->pci_dev) && |
975 | kvm_msi_via_irqfd_enabled(); | |
54dd9321 | 976 | |
87b3bd1c | 977 | nvqs = MIN(nvqs, VIRTIO_QUEUE_MAX); |
2d620f59 MT |
978 | |
979 | /* When deassigning, pass a consistent nvqs value | |
980 | * to avoid leaking notifiers. | |
981 | */ | |
982 | assert(assign || nvqs == proxy->nvqs_with_notifiers); | |
983 | ||
984 | proxy->nvqs_with_notifiers = nvqs; | |
985 | ||
7d37d351 | 986 | /* Must unset vector notifier while guest notifier is still assigned */ |
181103cd | 987 | if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) { |
7d37d351 | 988 | msix_unset_vector_notifiers(&proxy->pci_dev); |
a38b2c49 MT |
989 | if (proxy->vector_irqfd) { |
990 | kvm_virtio_pci_vector_release(proxy, nvqs); | |
991 | g_free(proxy->vector_irqfd); | |
992 | proxy->vector_irqfd = NULL; | |
993 | } | |
7d37d351 JK |
994 | } |
995 | ||
2d620f59 | 996 | for (n = 0; n < nvqs; n++) { |
54dd9321 MT |
997 | if (!virtio_queue_get_num(vdev, n)) { |
998 | break; | |
999 | } | |
1000 | ||
23fe2b3f | 1001 | r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd); |
54dd9321 MT |
1002 | if (r < 0) { |
1003 | goto assign_error; | |
1004 | } | |
1005 | } | |
1006 | ||
7d37d351 | 1007 | /* Must set vector notifier after guest notifier has been assigned */ |
181103cd | 1008 | if ((with_irqfd || k->guest_notifier_mask) && assign) { |
a38b2c49 MT |
1009 | if (with_irqfd) { |
1010 | proxy->vector_irqfd = | |
1011 | g_malloc0(sizeof(*proxy->vector_irqfd) * | |
1012 | msix_nr_vectors_allocated(&proxy->pci_dev)); | |
1013 | r = kvm_virtio_pci_vector_use(proxy, nvqs); | |
1014 | if (r < 0) { | |
1015 | goto assign_error; | |
1016 | } | |
774345f9 | 1017 | } |
7d37d351 | 1018 | r = msix_set_vector_notifiers(&proxy->pci_dev, |
a38b2c49 MT |
1019 | virtio_pci_vector_unmask, |
1020 | virtio_pci_vector_mask, | |
1021 | virtio_pci_vector_poll); | |
7d37d351 | 1022 | if (r < 0) { |
774345f9 | 1023 | goto notifiers_error; |
7d37d351 JK |
1024 | } |
1025 | } | |
1026 | ||
54dd9321 MT |
1027 | return 0; |
1028 | ||
774345f9 | 1029 | notifiers_error: |
a38b2c49 MT |
1030 | if (with_irqfd) { |
1031 | assert(assign); | |
1032 | kvm_virtio_pci_vector_release(proxy, nvqs); | |
1033 | } | |
774345f9 | 1034 | |
54dd9321 MT |
1035 | assign_error: |
1036 | /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */ | |
7d37d351 | 1037 | assert(assign); |
54dd9321 | 1038 | while (--n >= 0) { |
89d62be9 | 1039 | virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd); |
54dd9321 MT |
1040 | } |
1041 | return r; | |
1042 | } | |
1043 | ||
6f80e617 TB |
1044 | static int virtio_pci_set_host_notifier_mr(DeviceState *d, int n, |
1045 | MemoryRegion *mr, bool assign) | |
1046 | { | |
1047 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); | |
1048 | int offset; | |
1049 | ||
1050 | if (n >= VIRTIO_QUEUE_MAX || !virtio_pci_modern(proxy) || | |
1051 | virtio_pci_queue_mem_mult(proxy) != memory_region_size(mr)) { | |
1052 | return -1; | |
1053 | } | |
1054 | ||
1055 | if (assign) { | |
1056 | offset = virtio_pci_queue_mem_mult(proxy) * n; | |
1057 | memory_region_add_subregion_overlap(&proxy->notify.mr, offset, mr, 1); | |
1058 | } else { | |
1059 | memory_region_del_subregion(&proxy->notify.mr, mr); | |
1060 | } | |
1061 | ||
1062 | return 0; | |
1063 | } | |
1064 | ||
d2a0ccc6 | 1065 | static void virtio_pci_vmstate_change(DeviceState *d, bool running) |
25db9ebe | 1066 | { |
d2a0ccc6 | 1067 | VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); |
a3fc66d9 | 1068 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
25db9ebe SH |
1069 | |
1070 | if (running) { | |
68a27b20 MT |
1071 | /* Old QEMU versions did not set bus master enable on status write. |
1072 | * Detect DRIVER set and enable it. | |
1073 | */ | |
1074 | if ((proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION) && | |
1075 | (vdev->status & VIRTIO_CONFIG_S_DRIVER) && | |
45363e46 | 1076 | !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { |
68a27b20 MT |
1077 | pci_default_write_config(&proxy->pci_dev, PCI_COMMAND, |
1078 | proxy->pci_dev.config[PCI_COMMAND] | | |
1079 | PCI_COMMAND_MASTER, 1); | |
89c473fd | 1080 | } |
25db9ebe | 1081 | virtio_pci_start_ioeventfd(proxy); |
ade80dc8 | 1082 | } else { |
25db9ebe | 1083 | virtio_pci_stop_ioeventfd(proxy); |
ade80dc8 | 1084 | } |
ade80dc8 MT |
1085 | } |
1086 | ||
085bccb7 FK |
1087 | /* |
1088 | * virtio-pci: This is the PCIDevice which has a virtio-pci-bus. | |
1089 | */ | |
1090 | ||
e0d686bf JW |
1091 | static int virtio_pci_query_nvectors(DeviceState *d) |
1092 | { | |
1093 | VirtIOPCIProxy *proxy = VIRTIO_PCI(d); | |
1094 | ||
1095 | return proxy->nvectors; | |
1096 | } | |
1097 | ||
8607f5c3 JW |
1098 | static AddressSpace *virtio_pci_get_dma_as(DeviceState *d) |
1099 | { | |
1100 | VirtIOPCIProxy *proxy = VIRTIO_PCI(d); | |
1101 | PCIDevice *dev = &proxy->pci_dev; | |
1102 | ||
f0edf239 | 1103 | return pci_get_address_space(dev); |
8607f5c3 JW |
1104 | } |
1105 | ||
ada434cd | 1106 | static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy, |
dfb8e184 MT |
1107 | struct virtio_pci_cap *cap) |
1108 | { | |
1109 | PCIDevice *dev = &proxy->pci_dev; | |
1110 | int offset; | |
1111 | ||
9a7c2a59 MZ |
1112 | offset = pci_add_capability(dev, PCI_CAP_ID_VNDR, 0, |
1113 | cap->cap_len, &error_abort); | |
dfb8e184 MT |
1114 | |
1115 | assert(cap->cap_len >= sizeof *cap); | |
1116 | memcpy(dev->config + offset + PCI_CAP_FLAGS, &cap->cap_len, | |
1117 | cap->cap_len - PCI_CAP_FLAGS); | |
ada434cd MT |
1118 | |
1119 | return offset; | |
dfb8e184 MT |
1120 | } |
1121 | ||
dfb8e184 MT |
1122 | static uint64_t virtio_pci_common_read(void *opaque, hwaddr addr, |
1123 | unsigned size) | |
1124 | { | |
1125 | VirtIOPCIProxy *proxy = opaque; | |
1126 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); | |
1127 | uint32_t val = 0; | |
1128 | int i; | |
1129 | ||
1130 | switch (addr) { | |
1131 | case VIRTIO_PCI_COMMON_DFSELECT: | |
1132 | val = proxy->dfselect; | |
1133 | break; | |
1134 | case VIRTIO_PCI_COMMON_DF: | |
1135 | if (proxy->dfselect <= 1) { | |
9b706dbb MT |
1136 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); |
1137 | ||
1138 | val = (vdev->host_features & ~vdc->legacy_features) >> | |
5f456073 | 1139 | (32 * proxy->dfselect); |
dfb8e184 MT |
1140 | } |
1141 | break; | |
1142 | case VIRTIO_PCI_COMMON_GFSELECT: | |
1143 | val = proxy->gfselect; | |
1144 | break; | |
1145 | case VIRTIO_PCI_COMMON_GF: | |
3750dabc | 1146 | if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) { |
dfb8e184 MT |
1147 | val = proxy->guest_features[proxy->gfselect]; |
1148 | } | |
1149 | break; | |
1150 | case VIRTIO_PCI_COMMON_MSIX: | |
1151 | val = vdev->config_vector; | |
1152 | break; | |
1153 | case VIRTIO_PCI_COMMON_NUMQ: | |
1154 | for (i = 0; i < VIRTIO_QUEUE_MAX; ++i) { | |
1155 | if (virtio_queue_get_num(vdev, i)) { | |
1156 | val = i + 1; | |
1157 | } | |
1158 | } | |
1159 | break; | |
1160 | case VIRTIO_PCI_COMMON_STATUS: | |
1161 | val = vdev->status; | |
1162 | break; | |
1163 | case VIRTIO_PCI_COMMON_CFGGENERATION: | |
b8f05908 | 1164 | val = vdev->generation; |
dfb8e184 MT |
1165 | break; |
1166 | case VIRTIO_PCI_COMMON_Q_SELECT: | |
1167 | val = vdev->queue_sel; | |
1168 | break; | |
1169 | case VIRTIO_PCI_COMMON_Q_SIZE: | |
1170 | val = virtio_queue_get_num(vdev, vdev->queue_sel); | |
1171 | break; | |
1172 | case VIRTIO_PCI_COMMON_Q_MSIX: | |
1173 | val = virtio_queue_vector(vdev, vdev->queue_sel); | |
1174 | break; | |
1175 | case VIRTIO_PCI_COMMON_Q_ENABLE: | |
1176 | val = proxy->vqs[vdev->queue_sel].enabled; | |
1177 | break; | |
1178 | case VIRTIO_PCI_COMMON_Q_NOFF: | |
1179 | /* Simply map queues in order */ | |
1180 | val = vdev->queue_sel; | |
1181 | break; | |
1182 | case VIRTIO_PCI_COMMON_Q_DESCLO: | |
1183 | val = proxy->vqs[vdev->queue_sel].desc[0]; | |
1184 | break; | |
1185 | case VIRTIO_PCI_COMMON_Q_DESCHI: | |
1186 | val = proxy->vqs[vdev->queue_sel].desc[1]; | |
1187 | break; | |
1188 | case VIRTIO_PCI_COMMON_Q_AVAILLO: | |
1189 | val = proxy->vqs[vdev->queue_sel].avail[0]; | |
1190 | break; | |
1191 | case VIRTIO_PCI_COMMON_Q_AVAILHI: | |
1192 | val = proxy->vqs[vdev->queue_sel].avail[1]; | |
1193 | break; | |
1194 | case VIRTIO_PCI_COMMON_Q_USEDLO: | |
1195 | val = proxy->vqs[vdev->queue_sel].used[0]; | |
1196 | break; | |
1197 | case VIRTIO_PCI_COMMON_Q_USEDHI: | |
1198 | val = proxy->vqs[vdev->queue_sel].used[1]; | |
1199 | break; | |
1200 | default: | |
1201 | val = 0; | |
1202 | } | |
1203 | ||
1204 | return val; | |
1205 | } | |
1206 | ||
1207 | static void virtio_pci_common_write(void *opaque, hwaddr addr, | |
1208 | uint64_t val, unsigned size) | |
1209 | { | |
1210 | VirtIOPCIProxy *proxy = opaque; | |
1211 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); | |
1212 | ||
1213 | switch (addr) { | |
1214 | case VIRTIO_PCI_COMMON_DFSELECT: | |
1215 | proxy->dfselect = val; | |
1216 | break; | |
1217 | case VIRTIO_PCI_COMMON_GFSELECT: | |
1218 | proxy->gfselect = val; | |
1219 | break; | |
1220 | case VIRTIO_PCI_COMMON_GF: | |
3750dabc | 1221 | if (proxy->gfselect < ARRAY_SIZE(proxy->guest_features)) { |
dfb8e184 MT |
1222 | proxy->guest_features[proxy->gfselect] = val; |
1223 | virtio_set_features(vdev, | |
1224 | (((uint64_t)proxy->guest_features[1]) << 32) | | |
1225 | proxy->guest_features[0]); | |
1226 | } | |
1227 | break; | |
1228 | case VIRTIO_PCI_COMMON_MSIX: | |
1229 | msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); | |
1230 | /* Make it possible for guest to discover an error took place. */ | |
1231 | if (msix_vector_use(&proxy->pci_dev, val) < 0) { | |
1232 | val = VIRTIO_NO_VECTOR; | |
1233 | } | |
1234 | vdev->config_vector = val; | |
1235 | break; | |
1236 | case VIRTIO_PCI_COMMON_STATUS: | |
1237 | if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) { | |
1238 | virtio_pci_stop_ioeventfd(proxy); | |
1239 | } | |
1240 | ||
1241 | virtio_set_status(vdev, val & 0xFF); | |
1242 | ||
1243 | if (val & VIRTIO_CONFIG_S_DRIVER_OK) { | |
1244 | virtio_pci_start_ioeventfd(proxy); | |
1245 | } | |
1246 | ||
1247 | if (vdev->status == 0) { | |
75fd6f13 | 1248 | virtio_pci_reset(DEVICE(proxy)); |
dfb8e184 MT |
1249 | } |
1250 | ||
1251 | break; | |
1252 | case VIRTIO_PCI_COMMON_Q_SELECT: | |
1253 | if (val < VIRTIO_QUEUE_MAX) { | |
1254 | vdev->queue_sel = val; | |
1255 | } | |
1256 | break; | |
1257 | case VIRTIO_PCI_COMMON_Q_SIZE: | |
1258 | proxy->vqs[vdev->queue_sel].num = val; | |
1259 | break; | |
1260 | case VIRTIO_PCI_COMMON_Q_MSIX: | |
1261 | msix_vector_unuse(&proxy->pci_dev, | |
1262 | virtio_queue_vector(vdev, vdev->queue_sel)); | |
1263 | /* Make it possible for guest to discover an error took place. */ | |
1264 | if (msix_vector_use(&proxy->pci_dev, val) < 0) { | |
1265 | val = VIRTIO_NO_VECTOR; | |
1266 | } | |
1267 | virtio_queue_set_vector(vdev, vdev->queue_sel, val); | |
1268 | break; | |
1269 | case VIRTIO_PCI_COMMON_Q_ENABLE: | |
dfb8e184 MT |
1270 | virtio_queue_set_num(vdev, vdev->queue_sel, |
1271 | proxy->vqs[vdev->queue_sel].num); | |
1272 | virtio_queue_set_rings(vdev, vdev->queue_sel, | |
1273 | ((uint64_t)proxy->vqs[vdev->queue_sel].desc[1]) << 32 | | |
1274 | proxy->vqs[vdev->queue_sel].desc[0], | |
1275 | ((uint64_t)proxy->vqs[vdev->queue_sel].avail[1]) << 32 | | |
1276 | proxy->vqs[vdev->queue_sel].avail[0], | |
1277 | ((uint64_t)proxy->vqs[vdev->queue_sel].used[1]) << 32 | | |
1278 | proxy->vqs[vdev->queue_sel].used[0]); | |
393f04d3 | 1279 | proxy->vqs[vdev->queue_sel].enabled = 1; |
dfb8e184 MT |
1280 | break; |
1281 | case VIRTIO_PCI_COMMON_Q_DESCLO: | |
1282 | proxy->vqs[vdev->queue_sel].desc[0] = val; | |
1283 | break; | |
1284 | case VIRTIO_PCI_COMMON_Q_DESCHI: | |
1285 | proxy->vqs[vdev->queue_sel].desc[1] = val; | |
1286 | break; | |
1287 | case VIRTIO_PCI_COMMON_Q_AVAILLO: | |
1288 | proxy->vqs[vdev->queue_sel].avail[0] = val; | |
1289 | break; | |
1290 | case VIRTIO_PCI_COMMON_Q_AVAILHI: | |
1291 | proxy->vqs[vdev->queue_sel].avail[1] = val; | |
1292 | break; | |
1293 | case VIRTIO_PCI_COMMON_Q_USEDLO: | |
1294 | proxy->vqs[vdev->queue_sel].used[0] = val; | |
1295 | break; | |
1296 | case VIRTIO_PCI_COMMON_Q_USEDHI: | |
1297 | proxy->vqs[vdev->queue_sel].used[1] = val; | |
1298 | break; | |
1299 | default: | |
1300 | break; | |
1301 | } | |
1302 | } | |
1303 | ||
1304 | ||
1305 | static uint64_t virtio_pci_notify_read(void *opaque, hwaddr addr, | |
1306 | unsigned size) | |
1307 | { | |
1308 | return 0; | |
1309 | } | |
1310 | ||
1311 | static void virtio_pci_notify_write(void *opaque, hwaddr addr, | |
1312 | uint64_t val, unsigned size) | |
1313 | { | |
1314 | VirtIODevice *vdev = opaque; | |
d9997d89 MA |
1315 | VirtIOPCIProxy *proxy = VIRTIO_PCI(DEVICE(vdev)->parent_bus->parent); |
1316 | unsigned queue = addr / virtio_pci_queue_mem_mult(proxy); | |
dfb8e184 MT |
1317 | |
1318 | if (queue < VIRTIO_QUEUE_MAX) { | |
1319 | virtio_queue_notify(vdev, queue); | |
1320 | } | |
1321 | } | |
1322 | ||
9824d2a3 JW |
1323 | static void virtio_pci_notify_write_pio(void *opaque, hwaddr addr, |
1324 | uint64_t val, unsigned size) | |
1325 | { | |
1326 | VirtIODevice *vdev = opaque; | |
1327 | unsigned queue = val; | |
1328 | ||
1329 | if (queue < VIRTIO_QUEUE_MAX) { | |
1330 | virtio_queue_notify(vdev, queue); | |
1331 | } | |
1332 | } | |
1333 | ||
dfb8e184 MT |
1334 | static uint64_t virtio_pci_isr_read(void *opaque, hwaddr addr, |
1335 | unsigned size) | |
1336 | { | |
1337 | VirtIOPCIProxy *proxy = opaque; | |
1338 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); | |
0687c37c | 1339 | uint64_t val = atomic_xchg(&vdev->isr, 0); |
dfb8e184 MT |
1340 | pci_irq_deassert(&proxy->pci_dev); |
1341 | ||
1342 | return val; | |
1343 | } | |
1344 | ||
1345 | static void virtio_pci_isr_write(void *opaque, hwaddr addr, | |
1346 | uint64_t val, unsigned size) | |
1347 | { | |
1348 | } | |
1349 | ||
1350 | static uint64_t virtio_pci_device_read(void *opaque, hwaddr addr, | |
1351 | unsigned size) | |
1352 | { | |
1353 | VirtIODevice *vdev = opaque; | |
1354 | uint64_t val = 0; | |
1355 | ||
1356 | switch (size) { | |
1357 | case 1: | |
54c720d4 | 1358 | val = virtio_config_modern_readb(vdev, addr); |
dfb8e184 MT |
1359 | break; |
1360 | case 2: | |
54c720d4 | 1361 | val = virtio_config_modern_readw(vdev, addr); |
dfb8e184 MT |
1362 | break; |
1363 | case 4: | |
54c720d4 | 1364 | val = virtio_config_modern_readl(vdev, addr); |
dfb8e184 MT |
1365 | break; |
1366 | } | |
1367 | return val; | |
1368 | } | |
1369 | ||
1370 | static void virtio_pci_device_write(void *opaque, hwaddr addr, | |
1371 | uint64_t val, unsigned size) | |
1372 | { | |
1373 | VirtIODevice *vdev = opaque; | |
1374 | switch (size) { | |
1375 | case 1: | |
54c720d4 | 1376 | virtio_config_modern_writeb(vdev, addr, val); |
dfb8e184 MT |
1377 | break; |
1378 | case 2: | |
54c720d4 | 1379 | virtio_config_modern_writew(vdev, addr, val); |
dfb8e184 MT |
1380 | break; |
1381 | case 4: | |
54c720d4 | 1382 | virtio_config_modern_writel(vdev, addr, val); |
dfb8e184 MT |
1383 | break; |
1384 | } | |
1385 | } | |
1386 | ||
1141ce21 GH |
1387 | static void virtio_pci_modern_regions_init(VirtIOPCIProxy *proxy) |
1388 | { | |
1389 | static const MemoryRegionOps common_ops = { | |
1390 | .read = virtio_pci_common_read, | |
1391 | .write = virtio_pci_common_write, | |
1392 | .impl = { | |
1393 | .min_access_size = 1, | |
1394 | .max_access_size = 4, | |
1395 | }, | |
1396 | .endianness = DEVICE_LITTLE_ENDIAN, | |
1397 | }; | |
1398 | static const MemoryRegionOps isr_ops = { | |
1399 | .read = virtio_pci_isr_read, | |
1400 | .write = virtio_pci_isr_write, | |
1401 | .impl = { | |
1402 | .min_access_size = 1, | |
1403 | .max_access_size = 4, | |
1404 | }, | |
1405 | .endianness = DEVICE_LITTLE_ENDIAN, | |
1406 | }; | |
1407 | static const MemoryRegionOps device_ops = { | |
1408 | .read = virtio_pci_device_read, | |
1409 | .write = virtio_pci_device_write, | |
1410 | .impl = { | |
1411 | .min_access_size = 1, | |
1412 | .max_access_size = 4, | |
1413 | }, | |
1414 | .endianness = DEVICE_LITTLE_ENDIAN, | |
1415 | }; | |
1416 | static const MemoryRegionOps notify_ops = { | |
1417 | .read = virtio_pci_notify_read, | |
1418 | .write = virtio_pci_notify_write, | |
1419 | .impl = { | |
1420 | .min_access_size = 1, | |
1421 | .max_access_size = 4, | |
1422 | }, | |
1423 | .endianness = DEVICE_LITTLE_ENDIAN, | |
1424 | }; | |
9824d2a3 JW |
1425 | static const MemoryRegionOps notify_pio_ops = { |
1426 | .read = virtio_pci_notify_read, | |
1427 | .write = virtio_pci_notify_write_pio, | |
1428 | .impl = { | |
1429 | .min_access_size = 1, | |
1430 | .max_access_size = 4, | |
1431 | }, | |
1432 | .endianness = DEVICE_LITTLE_ENDIAN, | |
1433 | }; | |
1434 | ||
1141ce21 GH |
1435 | |
1436 | memory_region_init_io(&proxy->common.mr, OBJECT(proxy), | |
1437 | &common_ops, | |
1438 | proxy, | |
b6ce27a5 GH |
1439 | "virtio-pci-common", |
1440 | proxy->common.size); | |
a3cc2e81 | 1441 | |
1141ce21 GH |
1442 | memory_region_init_io(&proxy->isr.mr, OBJECT(proxy), |
1443 | &isr_ops, | |
1444 | proxy, | |
b6ce27a5 GH |
1445 | "virtio-pci-isr", |
1446 | proxy->isr.size); | |
a3cc2e81 | 1447 | |
1141ce21 GH |
1448 | memory_region_init_io(&proxy->device.mr, OBJECT(proxy), |
1449 | &device_ops, | |
1450 | virtio_bus_get_device(&proxy->bus), | |
b6ce27a5 GH |
1451 | "virtio-pci-device", |
1452 | proxy->device.size); | |
a3cc2e81 | 1453 | |
1141ce21 GH |
1454 | memory_region_init_io(&proxy->notify.mr, OBJECT(proxy), |
1455 | ¬ify_ops, | |
1456 | virtio_bus_get_device(&proxy->bus), | |
1457 | "virtio-pci-notify", | |
b6ce27a5 | 1458 | proxy->notify.size); |
9824d2a3 JW |
1459 | |
1460 | memory_region_init_io(&proxy->notify_pio.mr, OBJECT(proxy), | |
1461 | ¬ify_pio_ops, | |
1462 | virtio_bus_get_device(&proxy->bus), | |
1463 | "virtio-pci-notify-pio", | |
e3aab6c7 | 1464 | proxy->notify_pio.size); |
a3cc2e81 GH |
1465 | } |
1466 | ||
1467 | static void virtio_pci_modern_region_map(VirtIOPCIProxy *proxy, | |
54790d71 | 1468 | VirtIOPCIRegion *region, |
9824d2a3 JW |
1469 | struct virtio_pci_cap *cap, |
1470 | MemoryRegion *mr, | |
1471 | uint8_t bar) | |
a3cc2e81 | 1472 | { |
9824d2a3 | 1473 | memory_region_add_subregion(mr, region->offset, ®ion->mr); |
54790d71 | 1474 | |
fc004905 | 1475 | cap->cfg_type = region->type; |
9824d2a3 | 1476 | cap->bar = bar; |
54790d71 | 1477 | cap->offset = cpu_to_le32(region->offset); |
b6ce27a5 | 1478 | cap->length = cpu_to_le32(region->size); |
54790d71 | 1479 | virtio_pci_add_mem_cap(proxy, cap); |
9824d2a3 JW |
1480 | |
1481 | } | |
1482 | ||
1483 | static void virtio_pci_modern_mem_region_map(VirtIOPCIProxy *proxy, | |
1484 | VirtIOPCIRegion *region, | |
1485 | struct virtio_pci_cap *cap) | |
1486 | { | |
1487 | virtio_pci_modern_region_map(proxy, region, cap, | |
7a25126d | 1488 | &proxy->modern_bar, proxy->modern_mem_bar_idx); |
1141ce21 | 1489 | } |
dfb8e184 | 1490 | |
9824d2a3 JW |
1491 | static void virtio_pci_modern_io_region_map(VirtIOPCIProxy *proxy, |
1492 | VirtIOPCIRegion *region, | |
1493 | struct virtio_pci_cap *cap) | |
1494 | { | |
1495 | virtio_pci_modern_region_map(proxy, region, cap, | |
7a25126d | 1496 | &proxy->io_bar, proxy->modern_io_bar_idx); |
9824d2a3 JW |
1497 | } |
1498 | ||
1499 | static void virtio_pci_modern_mem_region_unmap(VirtIOPCIProxy *proxy, | |
1500 | VirtIOPCIRegion *region) | |
27462695 MT |
1501 | { |
1502 | memory_region_del_subregion(&proxy->modern_bar, | |
1503 | ®ion->mr); | |
1504 | } | |
1505 | ||
9824d2a3 JW |
1506 | static void virtio_pci_modern_io_region_unmap(VirtIOPCIProxy *proxy, |
1507 | VirtIOPCIRegion *region) | |
1508 | { | |
1509 | memory_region_del_subregion(&proxy->io_bar, | |
1510 | ®ion->mr); | |
1511 | } | |
1512 | ||
d1b4259f MC |
1513 | static void virtio_pci_pre_plugged(DeviceState *d, Error **errp) |
1514 | { | |
1515 | VirtIOPCIProxy *proxy = VIRTIO_PCI(d); | |
1516 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); | |
1517 | ||
1518 | if (virtio_pci_modern(proxy)) { | |
1519 | virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1); | |
1520 | } | |
1521 | ||
1522 | virtio_add_feature(&vdev->host_features, VIRTIO_F_BAD_FEATURE); | |
1523 | } | |
1524 | ||
085bccb7 | 1525 | /* This is called by virtio-bus just after the device is plugged. */ |
e8398045 | 1526 | static void virtio_pci_device_plugged(DeviceState *d, Error **errp) |
085bccb7 FK |
1527 | { |
1528 | VirtIOPCIProxy *proxy = VIRTIO_PCI(d); | |
1529 | VirtioBusState *bus = &proxy->bus; | |
9a4c0e22 | 1530 | bool legacy = virtio_pci_legacy(proxy); |
d1b4259f | 1531 | bool modern; |
9824d2a3 | 1532 | bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY; |
085bccb7 FK |
1533 | uint8_t *config; |
1534 | uint32_t size; | |
6b8f1020 | 1535 | VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); |
085bccb7 | 1536 | |
d1b4259f MC |
1537 | /* |
1538 | * Virtio capabilities present without | |
1539 | * VIRTIO_F_VERSION_1 confuses guests | |
1540 | */ | |
66d1c4c1 MC |
1541 | if (!proxy->ignore_backend_features && |
1542 | !virtio_has_feature(vdev->host_features, VIRTIO_F_VERSION_1)) { | |
d1b4259f MC |
1543 | virtio_pci_disable_modern(proxy); |
1544 | ||
1545 | if (!legacy) { | |
1546 | error_setg(errp, "Device doesn't support modern mode, and legacy" | |
1547 | " mode is disabled"); | |
1548 | error_append_hint(errp, "Set disable-legacy to off\n"); | |
1549 | ||
1550 | return; | |
1551 | } | |
1552 | } | |
1553 | ||
1554 | modern = virtio_pci_modern(proxy); | |
1555 | ||
085bccb7 FK |
1556 | config = proxy->pci_dev.config; |
1557 | if (proxy->class_code) { | |
1558 | pci_config_set_class(config, proxy->class_code); | |
1559 | } | |
e266d421 GH |
1560 | |
1561 | if (legacy) { | |
8607f5c3 JW |
1562 | if (virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM)) { |
1563 | error_setg(errp, "VIRTIO_F_IOMMU_PLATFORM was supported by" | |
2080a29f | 1564 | " neither legacy nor transitional device"); |
8607f5c3 JW |
1565 | return ; |
1566 | } | |
f2bc54de LP |
1567 | /* |
1568 | * Legacy and transitional devices use specific subsystem IDs. | |
1569 | * Note that the subsystem vendor ID (config + PCI_SUBSYSTEM_VENDOR_ID) | |
1570 | * is set to PCI_SUBVENDOR_ID_REDHAT_QUMRANET by default. | |
1571 | */ | |
e266d421 GH |
1572 | pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus)); |
1573 | } else { | |
1574 | /* pure virtio-1.0 */ | |
1575 | pci_set_word(config + PCI_VENDOR_ID, | |
1576 | PCI_VENDOR_ID_REDHAT_QUMRANET); | |
1577 | pci_set_word(config + PCI_DEVICE_ID, | |
1578 | 0x1040 + virtio_bus_get_vdev_id(bus)); | |
1579 | pci_config_set_revision(config, 1); | |
1580 | } | |
085bccb7 FK |
1581 | config[PCI_INTERRUPT_PIN] = 1; |
1582 | ||
dfb8e184 | 1583 | |
e266d421 | 1584 | if (modern) { |
cc52ea90 GH |
1585 | struct virtio_pci_cap cap = { |
1586 | .cap_len = sizeof cap, | |
dfb8e184 MT |
1587 | }; |
1588 | struct virtio_pci_notify_cap notify = { | |
dfb8e184 | 1589 | .cap.cap_len = sizeof notify, |
dfb8e184 | 1590 | .notify_off_multiplier = |
d9997d89 | 1591 | cpu_to_le32(virtio_pci_queue_mem_mult(proxy)), |
dfb8e184 | 1592 | }; |
ada434cd MT |
1593 | struct virtio_pci_cfg_cap cfg = { |
1594 | .cap.cap_len = sizeof cfg, | |
1595 | .cap.cfg_type = VIRTIO_PCI_CAP_PCI_CFG, | |
1596 | }; | |
9824d2a3 JW |
1597 | struct virtio_pci_notify_cap notify_pio = { |
1598 | .cap.cap_len = sizeof notify, | |
1599 | .notify_off_multiplier = cpu_to_le32(0x0), | |
1600 | }; | |
dfb8e184 | 1601 | |
9824d2a3 | 1602 | struct virtio_pci_cfg_cap *cfg_mask; |
dfb8e184 | 1603 | |
1141ce21 | 1604 | virtio_pci_modern_regions_init(proxy); |
9824d2a3 JW |
1605 | |
1606 | virtio_pci_modern_mem_region_map(proxy, &proxy->common, &cap); | |
1607 | virtio_pci_modern_mem_region_map(proxy, &proxy->isr, &cap); | |
1608 | virtio_pci_modern_mem_region_map(proxy, &proxy->device, &cap); | |
1609 | virtio_pci_modern_mem_region_map(proxy, &proxy->notify, ¬ify.cap); | |
1610 | ||
1611 | if (modern_pio) { | |
1612 | memory_region_init(&proxy->io_bar, OBJECT(proxy), | |
1613 | "virtio-pci-io", 0x4); | |
1614 | ||
7a25126d | 1615 | pci_register_bar(&proxy->pci_dev, proxy->modern_io_bar_idx, |
9824d2a3 JW |
1616 | PCI_BASE_ADDRESS_SPACE_IO, &proxy->io_bar); |
1617 | ||
1618 | virtio_pci_modern_io_region_map(proxy, &proxy->notify_pio, | |
1619 | ¬ify_pio.cap); | |
1620 | } | |
ada434cd | 1621 | |
7a25126d | 1622 | pci_register_bar(&proxy->pci_dev, proxy->modern_mem_bar_idx, |
4e93a68e GH |
1623 | PCI_BASE_ADDRESS_SPACE_MEMORY | |
1624 | PCI_BASE_ADDRESS_MEM_PREFETCH | | |
1625 | PCI_BASE_ADDRESS_MEM_TYPE_64, | |
dfb8e184 | 1626 | &proxy->modern_bar); |
ada434cd MT |
1627 | |
1628 | proxy->config_cap = virtio_pci_add_mem_cap(proxy, &cfg.cap); | |
1629 | cfg_mask = (void *)(proxy->pci_dev.wmask + proxy->config_cap); | |
1630 | pci_set_byte(&cfg_mask->cap.bar, ~0x0); | |
1631 | pci_set_long((uint8_t *)&cfg_mask->cap.offset, ~0x0); | |
1632 | pci_set_long((uint8_t *)&cfg_mask->cap.length, ~0x0); | |
1633 | pci_set_long(cfg_mask->pci_cfg_data, ~0x0); | |
dfb8e184 MT |
1634 | } |
1635 | ||
0d583647 RH |
1636 | if (proxy->nvectors) { |
1637 | int err = msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors, | |
ee640c62 | 1638 | proxy->msix_bar_idx, NULL); |
0d583647 | 1639 | if (err) { |
ee640c62 | 1640 | /* Notice when a system that supports MSIx can't initialize it */ |
0d583647 | 1641 | if (err != -ENOTSUP) { |
0765691e MA |
1642 | warn_report("unable to init msix vectors to %" PRIu32, |
1643 | proxy->nvectors); | |
0d583647 RH |
1644 | } |
1645 | proxy->nvectors = 0; | |
1646 | } | |
085bccb7 FK |
1647 | } |
1648 | ||
1649 | proxy->pci_dev.config_write = virtio_write_config; | |
ada434cd | 1650 | proxy->pci_dev.config_read = virtio_read_config; |
085bccb7 | 1651 | |
e266d421 GH |
1652 | if (legacy) { |
1653 | size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) | |
1654 | + virtio_bus_get_vdev_config_len(bus); | |
1d0148fe | 1655 | size = pow2ceil(size); |
085bccb7 | 1656 | |
e266d421 GH |
1657 | memory_region_init_io(&proxy->bar, OBJECT(proxy), |
1658 | &virtio_pci_config_ops, | |
1659 | proxy, "virtio-pci", size); | |
dfb8e184 | 1660 | |
7a25126d | 1661 | pci_register_bar(&proxy->pci_dev, proxy->legacy_io_bar_idx, |
23c5e397 | 1662 | PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar); |
e266d421 | 1663 | } |
085bccb7 FK |
1664 | } |
1665 | ||
06a13073 PB |
1666 | static void virtio_pci_device_unplugged(DeviceState *d) |
1667 | { | |
06a13073 | 1668 | VirtIOPCIProxy *proxy = VIRTIO_PCI(d); |
9a4c0e22 | 1669 | bool modern = virtio_pci_modern(proxy); |
9824d2a3 | 1670 | bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY; |
06a13073 PB |
1671 | |
1672 | virtio_pci_stop_ioeventfd(proxy); | |
27462695 MT |
1673 | |
1674 | if (modern) { | |
9824d2a3 JW |
1675 | virtio_pci_modern_mem_region_unmap(proxy, &proxy->common); |
1676 | virtio_pci_modern_mem_region_unmap(proxy, &proxy->isr); | |
1677 | virtio_pci_modern_mem_region_unmap(proxy, &proxy->device); | |
1678 | virtio_pci_modern_mem_region_unmap(proxy, &proxy->notify); | |
1679 | if (modern_pio) { | |
1680 | virtio_pci_modern_io_region_unmap(proxy, &proxy->notify_pio); | |
1681 | } | |
27462695 | 1682 | } |
06a13073 PB |
1683 | } |
1684 | ||
fc079951 | 1685 | static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp) |
085bccb7 | 1686 | { |
b6ce27a5 | 1687 | VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev); |
085bccb7 | 1688 | VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev); |
fd56e061 DG |
1689 | bool pcie_port = pci_bus_is_express(pci_get_bus(pci_dev)) && |
1690 | !pci_bus_is_root(pci_get_bus(pci_dev)); | |
fc079951 | 1691 | |
c324fd0a | 1692 | if (kvm_enabled() && !kvm_has_many_ioeventfds()) { |
ca2b413c PB |
1693 | proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD; |
1694 | } | |
1695 | ||
b6ce27a5 GH |
1696 | /* |
1697 | * virtio pci bar layout used by default. | |
1698 | * subclasses can re-arrange things if needed. | |
1699 | * | |
1700 | * region 0 -- virtio legacy io bar | |
1701 | * region 1 -- msi-x bar | |
1702 | * region 4+5 -- virtio modern memory (64bit) bar | |
1703 | * | |
1704 | */ | |
7a25126d CF |
1705 | proxy->legacy_io_bar_idx = 0; |
1706 | proxy->msix_bar_idx = 1; | |
1707 | proxy->modern_io_bar_idx = 2; | |
1708 | proxy->modern_mem_bar_idx = 4; | |
b6ce27a5 GH |
1709 | |
1710 | proxy->common.offset = 0x0; | |
1711 | proxy->common.size = 0x1000; | |
1712 | proxy->common.type = VIRTIO_PCI_CAP_COMMON_CFG; | |
1713 | ||
1714 | proxy->isr.offset = 0x1000; | |
1715 | proxy->isr.size = 0x1000; | |
1716 | proxy->isr.type = VIRTIO_PCI_CAP_ISR_CFG; | |
1717 | ||
1718 | proxy->device.offset = 0x2000; | |
1719 | proxy->device.size = 0x1000; | |
1720 | proxy->device.type = VIRTIO_PCI_CAP_DEVICE_CFG; | |
1721 | ||
1722 | proxy->notify.offset = 0x3000; | |
d9997d89 | 1723 | proxy->notify.size = virtio_pci_queue_mem_mult(proxy) * VIRTIO_QUEUE_MAX; |
b6ce27a5 GH |
1724 | proxy->notify.type = VIRTIO_PCI_CAP_NOTIFY_CFG; |
1725 | ||
9824d2a3 JW |
1726 | proxy->notify_pio.offset = 0x0; |
1727 | proxy->notify_pio.size = 0x4; | |
1728 | proxy->notify_pio.type = VIRTIO_PCI_CAP_NOTIFY_CFG; | |
1729 | ||
b6ce27a5 GH |
1730 | /* subclasses can enforce modern, so do this unconditionally */ |
1731 | memory_region_init(&proxy->modern_bar, OBJECT(proxy), "virtio-pci", | |
d9997d89 MA |
1732 | /* PCI BAR regions must be powers of 2 */ |
1733 | pow2ceil(proxy->notify.offset + proxy->notify.size)); | |
b6ce27a5 | 1734 | |
dd56040d DDAG |
1735 | if (proxy->disable_legacy == ON_OFF_AUTO_AUTO) { |
1736 | proxy->disable_legacy = pcie_port ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF; | |
1737 | } | |
1738 | ||
1739 | if (!virtio_pci_modern(proxy) && !virtio_pci_legacy(proxy)) { | |
1740 | error_setg(errp, "device cannot work as neither modern nor legacy mode" | |
1741 | " is enabled"); | |
1742 | error_append_hint(errp, "Set either disable-modern or disable-legacy" | |
1743 | " to off\n"); | |
1744 | return; | |
3eff3769 GK |
1745 | } |
1746 | ||
9a4c0e22 | 1747 | if (pcie_port && pci_is_express(pci_dev)) { |
1811e64c MA |
1748 | int pos; |
1749 | ||
1811e64c MA |
1750 | pos = pcie_endpoint_cap_init(pci_dev, 0); |
1751 | assert(pos > 0); | |
1752 | ||
9a7c2a59 MZ |
1753 | pos = pci_add_capability(pci_dev, PCI_CAP_ID_PM, 0, |
1754 | PCI_PM_SIZEOF, errp); | |
1755 | if (pos < 0) { | |
1756 | return; | |
1757 | } | |
1758 | ||
27ce0f3a | 1759 | pci_dev->exp.pm_cap = pos; |
1811e64c MA |
1760 | |
1761 | /* | |
1762 | * Indicates that this function complies with revision 1.2 of the | |
1763 | * PCI Power Management Interface Specification. | |
1764 | */ | |
1765 | pci_set_word(pci_dev->config + pos + PCI_PM_PMC, 0x3); | |
615c4ed2 | 1766 | |
c2cabb34 MA |
1767 | if (proxy->flags & VIRTIO_PCI_FLAG_INIT_DEVERR) { |
1768 | /* Init error enabling flags */ | |
1769 | pcie_cap_deverr_init(pci_dev); | |
1770 | } | |
1771 | ||
d584f1b9 MA |
1772 | if (proxy->flags & VIRTIO_PCI_FLAG_INIT_LNKCTL) { |
1773 | /* Init Link Control Register */ | |
1774 | pcie_cap_lnkctl_init(pci_dev); | |
1775 | } | |
1776 | ||
27ce0f3a MA |
1777 | if (proxy->flags & VIRTIO_PCI_FLAG_INIT_PM) { |
1778 | /* Init Power Management Control Register */ | |
1779 | pci_set_word(pci_dev->wmask + pos + PCI_PM_CTRL, | |
1780 | PCI_PM_CTRL_STATE_MASK); | |
1781 | } | |
1782 | ||
615c4ed2 JW |
1783 | if (proxy->flags & VIRTIO_PCI_FLAG_ATS) { |
1784 | pcie_ats_init(pci_dev, 256); | |
1785 | } | |
1786 | ||
eb1556c4 JS |
1787 | if (proxy->flags & VIRTIO_PCI_FLAG_INIT_FLR) { |
1788 | /* Set Function Level Reset capability bit */ | |
1789 | pcie_cap_flr_init(pci_dev); | |
1790 | } | |
0560b0e9 SL |
1791 | } else { |
1792 | /* | |
1793 | * make future invocations of pci_is_express() return false | |
1794 | * and pci_config_size() return PCI_CONFIG_SPACE_SIZE. | |
1795 | */ | |
1796 | pci_dev->cap_present &= ~QEMU_PCI_CAP_EXPRESS; | |
1811e64c MA |
1797 | } |
1798 | ||
b6ce27a5 | 1799 | virtio_pci_bus_new(&proxy->bus, sizeof(proxy->bus), proxy); |
fc079951 | 1800 | if (k->realize) { |
b6ce27a5 | 1801 | k->realize(proxy, errp); |
085bccb7 | 1802 | } |
085bccb7 FK |
1803 | } |
1804 | ||
1805 | static void virtio_pci_exit(PCIDevice *pci_dev) | |
1806 | { | |
8b81bb3b | 1807 | msix_uninit_exclusive_bar(pci_dev); |
085bccb7 FK |
1808 | } |
1809 | ||
59ccd20a | 1810 | static void virtio_pci_reset(DeviceState *qdev) |
085bccb7 FK |
1811 | { |
1812 | VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev); | |
1813 | VirtioBusState *bus = VIRTIO_BUS(&proxy->bus); | |
c2cabb34 | 1814 | PCIDevice *dev = PCI_DEVICE(qdev); |
393f04d3 JW |
1815 | int i; |
1816 | ||
085bccb7 FK |
1817 | virtio_pci_stop_ioeventfd(proxy); |
1818 | virtio_bus_reset(bus); | |
1819 | msix_unuse_all_vectors(&proxy->pci_dev); | |
393f04d3 JW |
1820 | |
1821 | for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { | |
1822 | proxy->vqs[i].enabled = 0; | |
60a8d802 JW |
1823 | proxy->vqs[i].num = 0; |
1824 | proxy->vqs[i].desc[0] = proxy->vqs[i].desc[1] = 0; | |
1825 | proxy->vqs[i].avail[0] = proxy->vqs[i].avail[1] = 0; | |
1826 | proxy->vqs[i].used[0] = proxy->vqs[i].used[1] = 0; | |
393f04d3 | 1827 | } |
c2cabb34 MA |
1828 | |
1829 | if (pci_is_express(dev)) { | |
1830 | pcie_cap_deverr_reset(dev); | |
d584f1b9 | 1831 | pcie_cap_lnkctl_reset(dev); |
27ce0f3a MA |
1832 | |
1833 | pci_set_word(dev->config + dev->exp.pm_cap + PCI_PM_CTRL, 0); | |
c2cabb34 | 1834 | } |
085bccb7 FK |
1835 | } |
1836 | ||
85d1277e | 1837 | static Property virtio_pci_properties[] = { |
68a27b20 MT |
1838 | DEFINE_PROP_BIT("virtio-pci-bus-master-bug-migration", VirtIOPCIProxy, flags, |
1839 | VIRTIO_PCI_FLAG_BUS_MASTER_BUG_MIGRATION_BIT, false), | |
a6df8adf JW |
1840 | DEFINE_PROP_BIT("migrate-extra", VirtIOPCIProxy, flags, |
1841 | VIRTIO_PCI_FLAG_MIGRATE_EXTRA_BIT, true), | |
9824d2a3 JW |
1842 | DEFINE_PROP_BIT("modern-pio-notify", VirtIOPCIProxy, flags, |
1843 | VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT, false), | |
1811e64c MA |
1844 | DEFINE_PROP_BIT("x-disable-pcie", VirtIOPCIProxy, flags, |
1845 | VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT, false), | |
d9997d89 MA |
1846 | DEFINE_PROP_BIT("page-per-vq", VirtIOPCIProxy, flags, |
1847 | VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT, false), | |
66d1c4c1 MC |
1848 | DEFINE_PROP_BOOL("x-ignore-backend-features", VirtIOPCIProxy, |
1849 | ignore_backend_features, false), | |
615c4ed2 JW |
1850 | DEFINE_PROP_BIT("ats", VirtIOPCIProxy, flags, |
1851 | VIRTIO_PCI_FLAG_ATS_BIT, false), | |
c2cabb34 MA |
1852 | DEFINE_PROP_BIT("x-pcie-deverr-init", VirtIOPCIProxy, flags, |
1853 | VIRTIO_PCI_FLAG_INIT_DEVERR_BIT, true), | |
d584f1b9 MA |
1854 | DEFINE_PROP_BIT("x-pcie-lnkctl-init", VirtIOPCIProxy, flags, |
1855 | VIRTIO_PCI_FLAG_INIT_LNKCTL_BIT, true), | |
27ce0f3a MA |
1856 | DEFINE_PROP_BIT("x-pcie-pm-init", VirtIOPCIProxy, flags, |
1857 | VIRTIO_PCI_FLAG_INIT_PM_BIT, true), | |
eb1556c4 JS |
1858 | DEFINE_PROP_BIT("x-pcie-flr-init", VirtIOPCIProxy, flags, |
1859 | VIRTIO_PCI_FLAG_INIT_FLR_BIT, true), | |
85d1277e ML |
1860 | DEFINE_PROP_END_OF_LIST(), |
1861 | }; | |
1862 | ||
0560b0e9 SL |
1863 | static void virtio_pci_dc_realize(DeviceState *qdev, Error **errp) |
1864 | { | |
1865 | VirtioPCIClass *vpciklass = VIRTIO_PCI_GET_CLASS(qdev); | |
1866 | VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev); | |
1867 | PCIDevice *pci_dev = &proxy->pci_dev; | |
1868 | ||
1869 | if (!(proxy->flags & VIRTIO_PCI_FLAG_DISABLE_PCIE) && | |
9a4c0e22 | 1870 | virtio_pci_modern(proxy)) { |
0560b0e9 SL |
1871 | pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS; |
1872 | } | |
1873 | ||
1874 | vpciklass->parent_dc_realize(qdev, errp); | |
1875 | } | |
1876 | ||
085bccb7 FK |
1877 | static void virtio_pci_class_init(ObjectClass *klass, void *data) |
1878 | { | |
1879 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1880 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); | |
0560b0e9 | 1881 | VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass); |
085bccb7 | 1882 | |
85d1277e | 1883 | dc->props = virtio_pci_properties; |
fc079951 | 1884 | k->realize = virtio_pci_realize; |
085bccb7 FK |
1885 | k->exit = virtio_pci_exit; |
1886 | k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
1887 | k->revision = VIRTIO_PCI_ABI_VERSION; | |
1888 | k->class_id = PCI_CLASS_OTHERS; | |
bf853881 PMD |
1889 | device_class_set_parent_realize(dc, virtio_pci_dc_realize, |
1890 | &vpciklass->parent_dc_realize); | |
59ccd20a | 1891 | dc->reset = virtio_pci_reset; |
085bccb7 FK |
1892 | } |
1893 | ||
1894 | static const TypeInfo virtio_pci_info = { | |
1895 | .name = TYPE_VIRTIO_PCI, | |
1896 | .parent = TYPE_PCI_DEVICE, | |
1897 | .instance_size = sizeof(VirtIOPCIProxy), | |
1898 | .class_init = virtio_pci_class_init, | |
1899 | .class_size = sizeof(VirtioPCIClass), | |
1900 | .abstract = true, | |
1901 | }; | |
1902 | ||
a4ee4c8b EH |
1903 | static Property virtio_pci_generic_properties[] = { |
1904 | DEFINE_PROP_ON_OFF_AUTO("disable-legacy", VirtIOPCIProxy, disable_legacy, | |
1905 | ON_OFF_AUTO_AUTO), | |
1906 | DEFINE_PROP_BOOL("disable-modern", VirtIOPCIProxy, disable_modern, false), | |
1907 | DEFINE_PROP_END_OF_LIST(), | |
1908 | }; | |
1909 | ||
1910 | static void virtio_pci_base_class_init(ObjectClass *klass, void *data) | |
1911 | { | |
1912 | const VirtioPCIDeviceTypeInfo *t = data; | |
1913 | if (t->class_init) { | |
1914 | t->class_init(klass, NULL); | |
1915 | } | |
1916 | } | |
1917 | ||
1918 | static void virtio_pci_generic_class_init(ObjectClass *klass, void *data) | |
1919 | { | |
1920 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1921 | ||
1922 | dc->props = virtio_pci_generic_properties; | |
1923 | } | |
1924 | ||
a4ee4c8b EH |
1925 | static void virtio_pci_transitional_instance_init(Object *obj) |
1926 | { | |
1927 | VirtIOPCIProxy *proxy = VIRTIO_PCI(obj); | |
1928 | ||
1929 | proxy->disable_legacy = ON_OFF_AUTO_OFF; | |
1930 | proxy->disable_modern = false; | |
1931 | } | |
1932 | ||
1933 | static void virtio_pci_non_transitional_instance_init(Object *obj) | |
1934 | { | |
1935 | VirtIOPCIProxy *proxy = VIRTIO_PCI(obj); | |
1936 | ||
1937 | proxy->disable_legacy = ON_OFF_AUTO_ON; | |
1938 | proxy->disable_modern = false; | |
1939 | } | |
1940 | ||
1941 | void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo *t) | |
1942 | { | |
683c1d89 | 1943 | char *base_name = NULL; |
a4ee4c8b EH |
1944 | TypeInfo base_type_info = { |
1945 | .name = t->base_name, | |
1946 | .parent = t->parent ? t->parent : TYPE_VIRTIO_PCI, | |
1947 | .instance_size = t->instance_size, | |
1948 | .instance_init = t->instance_init, | |
8ea90ee6 | 1949 | .class_size = t->class_size, |
a4ee4c8b | 1950 | .abstract = true, |
1e33b513 | 1951 | .interfaces = t->interfaces, |
a4ee4c8b EH |
1952 | }; |
1953 | TypeInfo generic_type_info = { | |
1954 | .name = t->generic_name, | |
1955 | .parent = base_type_info.name, | |
1956 | .class_init = virtio_pci_generic_class_init, | |
1957 | .interfaces = (InterfaceInfo[]) { | |
1958 | { INTERFACE_PCIE_DEVICE }, | |
1959 | { INTERFACE_CONVENTIONAL_PCI_DEVICE }, | |
1960 | { } | |
1961 | }, | |
1962 | }; | |
1963 | ||
1964 | if (!base_type_info.name) { | |
1965 | /* No base type -> register a single generic device type */ | |
683c1d89 MAL |
1966 | /* use intermediate %s-base-type to add generic device props */ |
1967 | base_name = g_strdup_printf("%s-base-type", t->generic_name); | |
1968 | base_type_info.name = base_name; | |
1969 | base_type_info.class_init = virtio_pci_generic_class_init; | |
1970 | ||
1971 | generic_type_info.parent = base_name; | |
1972 | generic_type_info.class_init = virtio_pci_base_class_init; | |
1973 | generic_type_info.class_data = (void *)t; | |
1974 | ||
a4ee4c8b EH |
1975 | assert(!t->non_transitional_name); |
1976 | assert(!t->transitional_name); | |
683c1d89 MAL |
1977 | } else { |
1978 | base_type_info.class_init = virtio_pci_base_class_init; | |
1979 | base_type_info.class_data = (void *)t; | |
a4ee4c8b EH |
1980 | } |
1981 | ||
1982 | type_register(&base_type_info); | |
1983 | if (generic_type_info.name) { | |
1984 | type_register(&generic_type_info); | |
1985 | } | |
1986 | ||
1987 | if (t->non_transitional_name) { | |
1988 | const TypeInfo non_transitional_type_info = { | |
1989 | .name = t->non_transitional_name, | |
1990 | .parent = base_type_info.name, | |
1991 | .instance_init = virtio_pci_non_transitional_instance_init, | |
1992 | .interfaces = (InterfaceInfo[]) { | |
1993 | { INTERFACE_PCIE_DEVICE }, | |
1994 | { INTERFACE_CONVENTIONAL_PCI_DEVICE }, | |
1995 | { } | |
1996 | }, | |
1997 | }; | |
1998 | type_register(&non_transitional_type_info); | |
1999 | } | |
2000 | ||
2001 | if (t->transitional_name) { | |
2002 | const TypeInfo transitional_type_info = { | |
2003 | .name = t->transitional_name, | |
2004 | .parent = base_type_info.name, | |
2005 | .instance_init = virtio_pci_transitional_instance_init, | |
2006 | .interfaces = (InterfaceInfo[]) { | |
2007 | /* | |
2008 | * Transitional virtio devices work only as Conventional PCI | |
2009 | * devices because they require PIO ports. | |
2010 | */ | |
2011 | { INTERFACE_CONVENTIONAL_PCI_DEVICE }, | |
2012 | { } | |
2013 | }, | |
2014 | }; | |
2015 | type_register(&transitional_type_info); | |
2016 | } | |
683c1d89 | 2017 | g_free(base_name); |
a4ee4c8b EH |
2018 | } |
2019 | ||
0a2acf5e FK |
2020 | /* virtio-pci-bus */ |
2021 | ||
ac7af112 AF |
2022 | static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size, |
2023 | VirtIOPCIProxy *dev) | |
0a2acf5e FK |
2024 | { |
2025 | DeviceState *qdev = DEVICE(dev); | |
f4dd69aa FK |
2026 | char virtio_bus_name[] = "virtio-bus"; |
2027 | ||
fb17dfe0 | 2028 | qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev, |
f4dd69aa | 2029 | virtio_bus_name); |
0a2acf5e FK |
2030 | } |
2031 | ||
2032 | static void virtio_pci_bus_class_init(ObjectClass *klass, void *data) | |
2033 | { | |
2034 | BusClass *bus_class = BUS_CLASS(klass); | |
2035 | VirtioBusClass *k = VIRTIO_BUS_CLASS(klass); | |
2036 | bus_class->max_dev = 1; | |
2037 | k->notify = virtio_pci_notify; | |
2038 | k->save_config = virtio_pci_save_config; | |
2039 | k->load_config = virtio_pci_load_config; | |
2040 | k->save_queue = virtio_pci_save_queue; | |
2041 | k->load_queue = virtio_pci_load_queue; | |
a6df8adf JW |
2042 | k->save_extra_state = virtio_pci_save_extra_state; |
2043 | k->load_extra_state = virtio_pci_load_extra_state; | |
2044 | k->has_extra_state = virtio_pci_has_extra_state; | |
0a2acf5e | 2045 | k->query_guest_notifiers = virtio_pci_query_guest_notifiers; |
0a2acf5e | 2046 | k->set_guest_notifiers = virtio_pci_set_guest_notifiers; |
6f80e617 | 2047 | k->set_host_notifier_mr = virtio_pci_set_host_notifier_mr; |
0a2acf5e | 2048 | k->vmstate_change = virtio_pci_vmstate_change; |
d1b4259f | 2049 | k->pre_plugged = virtio_pci_pre_plugged; |
085bccb7 | 2050 | k->device_plugged = virtio_pci_device_plugged; |
06a13073 | 2051 | k->device_unplugged = virtio_pci_device_unplugged; |
e0d686bf | 2052 | k->query_nvectors = virtio_pci_query_nvectors; |
8e93cef1 | 2053 | k->ioeventfd_enabled = virtio_pci_ioeventfd_enabled; |
9f06e71a | 2054 | k->ioeventfd_assign = virtio_pci_ioeventfd_assign; |
8607f5c3 | 2055 | k->get_dma_as = virtio_pci_get_dma_as; |
0a2acf5e FK |
2056 | } |
2057 | ||
2058 | static const TypeInfo virtio_pci_bus_info = { | |
2059 | .name = TYPE_VIRTIO_PCI_BUS, | |
2060 | .parent = TYPE_VIRTIO_BUS, | |
2061 | .instance_size = sizeof(VirtioPCIBusState), | |
2062 | .class_init = virtio_pci_bus_class_init, | |
2063 | }; | |
2064 | ||
83f7d43a | 2065 | static void virtio_pci_register_types(void) |
53c25cea | 2066 | { |
a4ee4c8b EH |
2067 | /* Base types: */ |
2068 | type_register_static(&virtio_pci_bus_info); | |
2069 | type_register_static(&virtio_pci_info); | |
53c25cea PB |
2070 | } |
2071 | ||
83f7d43a | 2072 | type_init(virtio_pci_register_types) |
271458d7 | 2073 |