]>
Commit | Line | Data |
---|---|---|
53c25cea PB |
1 | /* |
2 | * Virtio PCI Bindings | |
3 | * | |
4 | * Copyright IBM, Corp. 2007 | |
5 | * Copyright (c) 2009 CodeSourcery | |
6 | * | |
7 | * Authors: | |
8 | * Anthony Liguori <[email protected]> | |
9 | * Paul Brook <[email protected]> | |
10 | * | |
11 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
12 | * the COPYING file in the top-level directory. | |
13 | * | |
6b620ca3 PB |
14 | * Contributions after 2012-01-13 are licensed under the terms of the |
15 | * GNU GPL, version 2 or (at your option) any later version. | |
53c25cea PB |
16 | */ |
17 | ||
18 | #include <inttypes.h> | |
19 | ||
20 | #include "virtio.h" | |
8172539d MT |
21 | #include "virtio-blk.h" |
22 | #include "virtio-net.h" | |
6b331efb | 23 | #include "virtio-serial.h" |
973abc7f | 24 | #include "virtio-scsi.h" |
53c25cea | 25 | #include "pci.h" |
2f792016 | 26 | #include "qemu-error.h" |
7d37d351 | 27 | #include "msi.h" |
aba800a3 | 28 | #include "msix.h" |
a1e0fea5 | 29 | #include "net.h" |
97b15621 | 30 | #include "loader.h" |
ade80dc8 | 31 | #include "kvm.h" |
2446333c | 32 | #include "blockdev.h" |
9fe1ebeb | 33 | #include "virtio-pci.h" |
1129714f | 34 | #include "range.h" |
53c25cea PB |
35 | |
36 | /* from Linux's linux/virtio_pci.h */ | |
37 | ||
38 | /* A 32-bit r/o bitmask of the features supported by the host */ | |
39 | #define VIRTIO_PCI_HOST_FEATURES 0 | |
40 | ||
41 | /* A 32-bit r/w bitmask of features activated by the guest */ | |
42 | #define VIRTIO_PCI_GUEST_FEATURES 4 | |
43 | ||
44 | /* A 32-bit r/w PFN for the currently selected queue */ | |
45 | #define VIRTIO_PCI_QUEUE_PFN 8 | |
46 | ||
47 | /* A 16-bit r/o queue size for the currently selected queue */ | |
48 | #define VIRTIO_PCI_QUEUE_NUM 12 | |
49 | ||
50 | /* A 16-bit r/w queue selector */ | |
51 | #define VIRTIO_PCI_QUEUE_SEL 14 | |
52 | ||
53 | /* A 16-bit r/w queue notifier */ | |
54 | #define VIRTIO_PCI_QUEUE_NOTIFY 16 | |
55 | ||
56 | /* An 8-bit device status register. */ | |
57 | #define VIRTIO_PCI_STATUS 18 | |
58 | ||
59 | /* An 8-bit r/o interrupt status register. Reading the value will return the | |
60 | * current contents of the ISR and will also clear it. This is effectively | |
61 | * a read-and-acknowledge. */ | |
62 | #define VIRTIO_PCI_ISR 19 | |
63 | ||
aba800a3 MT |
64 | /* MSI-X registers: only enabled if MSI-X is enabled. */ |
65 | /* A 16-bit vector for configuration changes. */ | |
66 | #define VIRTIO_MSI_CONFIG_VECTOR 20 | |
67 | /* A 16-bit vector for selected queue notifications. */ | |
68 | #define VIRTIO_MSI_QUEUE_VECTOR 22 | |
69 | ||
70 | /* Config space size */ | |
71 | #define VIRTIO_PCI_CONFIG_NOMSI 20 | |
72 | #define VIRTIO_PCI_CONFIG_MSI 24 | |
73 | #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \ | |
74 | VIRTIO_PCI_CONFIG_MSI : \ | |
75 | VIRTIO_PCI_CONFIG_NOMSI) | |
76 | ||
77 | /* The remaining space is defined by each driver as the per-driver | |
78 | * configuration space */ | |
79 | #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \ | |
80 | VIRTIO_PCI_CONFIG_MSI : \ | |
81 | VIRTIO_PCI_CONFIG_NOMSI) | |
53c25cea | 82 | |
53c25cea PB |
83 | /* How many bits to shift physical queue address written to QUEUE_PFN. |
84 | * 12 is historical, and due to x86 page size. */ | |
85 | #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12 | |
86 | ||
3dbca8e6 SH |
87 | /* Flags track per-device state like workarounds for quirks in older guests. */ |
88 | #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0) | |
c81131db | 89 | |
53c25cea PB |
90 | /* QEMU doesn't strictly need write barriers since everything runs in |
91 | * lock-step. We'll leave the calls to wmb() in though to make it obvious for | |
92 | * KVM or if kqemu gets SMP support. | |
93 | */ | |
94 | #define wmb() do { } while (0) | |
95 | ||
82afa586 BH |
96 | /* HACK for virtio to determine if it's running a big endian guest */ |
97 | bool virtio_is_big_endian(void); | |
98 | ||
53c25cea PB |
99 | /* virtio device */ |
100 | ||
7055e687 | 101 | static void virtio_pci_notify(void *opaque, uint16_t vector) |
53c25cea PB |
102 | { |
103 | VirtIOPCIProxy *proxy = opaque; | |
aba800a3 MT |
104 | if (msix_enabled(&proxy->pci_dev)) |
105 | msix_notify(&proxy->pci_dev, vector); | |
106 | else | |
107 | qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1); | |
53c25cea PB |
108 | } |
109 | ||
ff24bd58 MT |
110 | static void virtio_pci_save_config(void * opaque, QEMUFile *f) |
111 | { | |
112 | VirtIOPCIProxy *proxy = opaque; | |
113 | pci_device_save(&proxy->pci_dev, f); | |
114 | msix_save(&proxy->pci_dev, f); | |
115 | if (msix_present(&proxy->pci_dev)) | |
116 | qemu_put_be16(f, proxy->vdev->config_vector); | |
117 | } | |
118 | ||
119 | static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f) | |
120 | { | |
121 | VirtIOPCIProxy *proxy = opaque; | |
122 | if (msix_present(&proxy->pci_dev)) | |
123 | qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n)); | |
124 | } | |
125 | ||
126 | static int virtio_pci_load_config(void * opaque, QEMUFile *f) | |
127 | { | |
128 | VirtIOPCIProxy *proxy = opaque; | |
129 | int ret; | |
130 | ret = pci_device_load(&proxy->pci_dev, f); | |
e6da7680 | 131 | if (ret) { |
ff24bd58 | 132 | return ret; |
e6da7680 | 133 | } |
ff24bd58 | 134 | msix_load(&proxy->pci_dev, f); |
e6da7680 | 135 | if (msix_present(&proxy->pci_dev)) { |
ff24bd58 | 136 | qemu_get_be16s(f, &proxy->vdev->config_vector); |
e6da7680 MT |
137 | } else { |
138 | proxy->vdev->config_vector = VIRTIO_NO_VECTOR; | |
139 | } | |
140 | if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) { | |
141 | return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector); | |
142 | } | |
ff24bd58 MT |
143 | return 0; |
144 | } | |
145 | ||
146 | static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f) | |
147 | { | |
148 | VirtIOPCIProxy *proxy = opaque; | |
149 | uint16_t vector; | |
e6da7680 MT |
150 | if (msix_present(&proxy->pci_dev)) { |
151 | qemu_get_be16s(f, &vector); | |
152 | } else { | |
153 | vector = VIRTIO_NO_VECTOR; | |
154 | } | |
ff24bd58 | 155 | virtio_queue_set_vector(proxy->vdev, n, vector); |
e6da7680 MT |
156 | if (vector != VIRTIO_NO_VECTOR) { |
157 | return msix_vector_use(&proxy->pci_dev, vector); | |
158 | } | |
ff24bd58 MT |
159 | return 0; |
160 | } | |
161 | ||
25db9ebe | 162 | static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy, |
26b9b5fe | 163 | int n, bool assign, bool set_handler) |
25db9ebe SH |
164 | { |
165 | VirtQueue *vq = virtio_get_queue(proxy->vdev, n); | |
166 | EventNotifier *notifier = virtio_queue_get_host_notifier(vq); | |
da146d0a AK |
167 | int r = 0; |
168 | ||
25db9ebe SH |
169 | if (assign) { |
170 | r = event_notifier_init(notifier, 1); | |
171 | if (r < 0) { | |
b36e3914 MT |
172 | error_report("%s: unable to init event notifier: %d", |
173 | __func__, r); | |
25db9ebe SH |
174 | return r; |
175 | } | |
26b9b5fe | 176 | virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler); |
da146d0a | 177 | memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2, |
753d5e14 | 178 | true, n, notifier); |
25db9ebe | 179 | } else { |
da146d0a | 180 | memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2, |
753d5e14 | 181 | true, n, notifier); |
26b9b5fe | 182 | virtio_queue_set_host_notifier_fd_handler(vq, false, false); |
25db9ebe SH |
183 | event_notifier_cleanup(notifier); |
184 | } | |
185 | return r; | |
186 | } | |
187 | ||
b36e3914 | 188 | static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy) |
25db9ebe SH |
189 | { |
190 | int n, r; | |
191 | ||
192 | if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) || | |
193 | proxy->ioeventfd_disabled || | |
194 | proxy->ioeventfd_started) { | |
b36e3914 | 195 | return; |
25db9ebe SH |
196 | } |
197 | ||
198 | for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) { | |
199 | if (!virtio_queue_get_num(proxy->vdev, n)) { | |
200 | continue; | |
201 | } | |
202 | ||
26b9b5fe | 203 | r = virtio_pci_set_host_notifier_internal(proxy, n, true, true); |
25db9ebe SH |
204 | if (r < 0) { |
205 | goto assign_error; | |
206 | } | |
25db9ebe SH |
207 | } |
208 | proxy->ioeventfd_started = true; | |
b36e3914 | 209 | return; |
25db9ebe SH |
210 | |
211 | assign_error: | |
212 | while (--n >= 0) { | |
213 | if (!virtio_queue_get_num(proxy->vdev, n)) { | |
214 | continue; | |
215 | } | |
216 | ||
26b9b5fe | 217 | r = virtio_pci_set_host_notifier_internal(proxy, n, false, false); |
b36e3914 | 218 | assert(r >= 0); |
25db9ebe SH |
219 | } |
220 | proxy->ioeventfd_started = false; | |
b36e3914 | 221 | error_report("%s: failed. Fallback to a userspace (slower).", __func__); |
25db9ebe SH |
222 | } |
223 | ||
b36e3914 | 224 | static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy) |
25db9ebe | 225 | { |
b36e3914 | 226 | int r; |
25db9ebe SH |
227 | int n; |
228 | ||
229 | if (!proxy->ioeventfd_started) { | |
b36e3914 | 230 | return; |
25db9ebe SH |
231 | } |
232 | ||
233 | for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) { | |
234 | if (!virtio_queue_get_num(proxy->vdev, n)) { | |
235 | continue; | |
236 | } | |
237 | ||
26b9b5fe | 238 | r = virtio_pci_set_host_notifier_internal(proxy, n, false, false); |
b36e3914 | 239 | assert(r >= 0); |
25db9ebe SH |
240 | } |
241 | proxy->ioeventfd_started = false; | |
25db9ebe SH |
242 | } |
243 | ||
8798d6c9 | 244 | void virtio_pci_reset(DeviceState *d) |
7055e687 | 245 | { |
e489030d | 246 | VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev); |
25db9ebe | 247 | virtio_pci_stop_ioeventfd(proxy); |
7055e687 | 248 | virtio_reset(proxy->vdev); |
25db9ebe | 249 | proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG; |
7055e687 MT |
250 | } |
251 | ||
53c25cea PB |
252 | static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val) |
253 | { | |
254 | VirtIOPCIProxy *proxy = opaque; | |
255 | VirtIODevice *vdev = proxy->vdev; | |
c227f099 | 256 | target_phys_addr_t pa; |
53c25cea | 257 | |
53c25cea PB |
258 | switch (addr) { |
259 | case VIRTIO_PCI_GUEST_FEATURES: | |
260 | /* Guest does not negotiate properly? We have to assume nothing. */ | |
261 | if (val & (1 << VIRTIO_F_BAD_FEATURE)) { | |
ad0c9332 | 262 | val = vdev->bad_features ? vdev->bad_features(vdev) : 0; |
53c25cea | 263 | } |
ad0c9332 | 264 | virtio_set_features(vdev, val); |
53c25cea PB |
265 | break; |
266 | case VIRTIO_PCI_QUEUE_PFN: | |
c227f099 | 267 | pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT; |
1b8e9b27 | 268 | if (pa == 0) { |
25db9ebe | 269 | virtio_pci_stop_ioeventfd(proxy); |
1b8e9b27 MT |
270 | virtio_reset(proxy->vdev); |
271 | msix_unuse_all_vectors(&proxy->pci_dev); | |
272 | } | |
7055e687 MT |
273 | else |
274 | virtio_queue_set_addr(vdev, vdev->queue_sel, pa); | |
53c25cea PB |
275 | break; |
276 | case VIRTIO_PCI_QUEUE_SEL: | |
277 | if (val < VIRTIO_PCI_QUEUE_MAX) | |
278 | vdev->queue_sel = val; | |
279 | break; | |
280 | case VIRTIO_PCI_QUEUE_NOTIFY: | |
7157e2e2 SH |
281 | if (val < VIRTIO_PCI_QUEUE_MAX) { |
282 | virtio_queue_notify(vdev, val); | |
283 | } | |
53c25cea PB |
284 | break; |
285 | case VIRTIO_PCI_STATUS: | |
25db9ebe SH |
286 | if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) { |
287 | virtio_pci_stop_ioeventfd(proxy); | |
288 | } | |
289 | ||
3e607cb5 | 290 | virtio_set_status(vdev, val & 0xFF); |
25db9ebe SH |
291 | |
292 | if (val & VIRTIO_CONFIG_S_DRIVER_OK) { | |
293 | virtio_pci_start_ioeventfd(proxy); | |
294 | } | |
295 | ||
1b8e9b27 MT |
296 | if (vdev->status == 0) { |
297 | virtio_reset(proxy->vdev); | |
298 | msix_unuse_all_vectors(&proxy->pci_dev); | |
299 | } | |
c81131db AG |
300 | |
301 | /* Linux before 2.6.34 sets the device as OK without enabling | |
302 | the PCI device bus master bit. In this case we need to disable | |
303 | some safety checks. */ | |
304 | if ((val & VIRTIO_CONFIG_S_DRIVER_OK) && | |
305 | !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { | |
3dbca8e6 | 306 | proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG; |
c81131db | 307 | } |
53c25cea | 308 | break; |
aba800a3 MT |
309 | case VIRTIO_MSI_CONFIG_VECTOR: |
310 | msix_vector_unuse(&proxy->pci_dev, vdev->config_vector); | |
311 | /* Make it possible for guest to discover an error took place. */ | |
312 | if (msix_vector_use(&proxy->pci_dev, val) < 0) | |
313 | val = VIRTIO_NO_VECTOR; | |
314 | vdev->config_vector = val; | |
315 | break; | |
316 | case VIRTIO_MSI_QUEUE_VECTOR: | |
317 | msix_vector_unuse(&proxy->pci_dev, | |
318 | virtio_queue_vector(vdev, vdev->queue_sel)); | |
319 | /* Make it possible for guest to discover an error took place. */ | |
320 | if (msix_vector_use(&proxy->pci_dev, val) < 0) | |
321 | val = VIRTIO_NO_VECTOR; | |
322 | virtio_queue_set_vector(vdev, vdev->queue_sel, val); | |
323 | break; | |
324 | default: | |
4e02d460 SH |
325 | error_report("%s: unexpected address 0x%x value 0x%x", |
326 | __func__, addr, val); | |
aba800a3 | 327 | break; |
53c25cea PB |
328 | } |
329 | } | |
330 | ||
aba800a3 | 331 | static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr) |
53c25cea | 332 | { |
53c25cea PB |
333 | VirtIODevice *vdev = proxy->vdev; |
334 | uint32_t ret = 0xFFFFFFFF; | |
335 | ||
53c25cea PB |
336 | switch (addr) { |
337 | case VIRTIO_PCI_HOST_FEATURES: | |
8172539d | 338 | ret = proxy->host_features; |
53c25cea PB |
339 | break; |
340 | case VIRTIO_PCI_GUEST_FEATURES: | |
704a76fc | 341 | ret = vdev->guest_features; |
53c25cea PB |
342 | break; |
343 | case VIRTIO_PCI_QUEUE_PFN: | |
344 | ret = virtio_queue_get_addr(vdev, vdev->queue_sel) | |
345 | >> VIRTIO_PCI_QUEUE_ADDR_SHIFT; | |
346 | break; | |
347 | case VIRTIO_PCI_QUEUE_NUM: | |
348 | ret = virtio_queue_get_num(vdev, vdev->queue_sel); | |
349 | break; | |
350 | case VIRTIO_PCI_QUEUE_SEL: | |
351 | ret = vdev->queue_sel; | |
352 | break; | |
353 | case VIRTIO_PCI_STATUS: | |
354 | ret = vdev->status; | |
355 | break; | |
356 | case VIRTIO_PCI_ISR: | |
357 | /* reading from the ISR also clears it. */ | |
358 | ret = vdev->isr; | |
359 | vdev->isr = 0; | |
7055e687 | 360 | qemu_set_irq(proxy->pci_dev.irq[0], 0); |
53c25cea | 361 | break; |
aba800a3 MT |
362 | case VIRTIO_MSI_CONFIG_VECTOR: |
363 | ret = vdev->config_vector; | |
364 | break; | |
365 | case VIRTIO_MSI_QUEUE_VECTOR: | |
366 | ret = virtio_queue_vector(vdev, vdev->queue_sel); | |
367 | break; | |
53c25cea PB |
368 | default: |
369 | break; | |
370 | } | |
371 | ||
372 | return ret; | |
373 | } | |
374 | ||
375 | static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr) | |
376 | { | |
377 | VirtIOPCIProxy *proxy = opaque; | |
aba800a3 | 378 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
aba800a3 MT |
379 | if (addr < config) |
380 | return virtio_ioport_read(proxy, addr); | |
381 | addr -= config; | |
53c25cea PB |
382 | return virtio_config_readb(proxy->vdev, addr); |
383 | } | |
384 | ||
385 | static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr) | |
386 | { | |
387 | VirtIOPCIProxy *proxy = opaque; | |
aba800a3 | 388 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
82afa586 | 389 | uint16_t val; |
aba800a3 MT |
390 | if (addr < config) |
391 | return virtio_ioport_read(proxy, addr); | |
392 | addr -= config; | |
82afa586 BH |
393 | val = virtio_config_readw(proxy->vdev, addr); |
394 | if (virtio_is_big_endian()) { | |
395 | /* | |
396 | * virtio is odd, ioports are LE but config space is target native | |
397 | * endian. However, in qemu, all PIO is LE, so we need to re-swap | |
398 | * on BE targets | |
399 | */ | |
400 | val = bswap16(val); | |
401 | } | |
402 | return val; | |
53c25cea PB |
403 | } |
404 | ||
405 | static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr) | |
406 | { | |
407 | VirtIOPCIProxy *proxy = opaque; | |
aba800a3 | 408 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
82afa586 | 409 | uint32_t val; |
aba800a3 MT |
410 | if (addr < config) |
411 | return virtio_ioport_read(proxy, addr); | |
412 | addr -= config; | |
82afa586 BH |
413 | val = virtio_config_readl(proxy->vdev, addr); |
414 | if (virtio_is_big_endian()) { | |
415 | val = bswap32(val); | |
416 | } | |
417 | return val; | |
53c25cea PB |
418 | } |
419 | ||
420 | static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val) | |
421 | { | |
422 | VirtIOPCIProxy *proxy = opaque; | |
aba800a3 | 423 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
aba800a3 MT |
424 | if (addr < config) { |
425 | virtio_ioport_write(proxy, addr, val); | |
426 | return; | |
427 | } | |
428 | addr -= config; | |
53c25cea PB |
429 | virtio_config_writeb(proxy->vdev, addr, val); |
430 | } | |
431 | ||
432 | static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val) | |
433 | { | |
434 | VirtIOPCIProxy *proxy = opaque; | |
aba800a3 | 435 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
aba800a3 MT |
436 | if (addr < config) { |
437 | virtio_ioport_write(proxy, addr, val); | |
438 | return; | |
439 | } | |
440 | addr -= config; | |
82afa586 BH |
441 | if (virtio_is_big_endian()) { |
442 | val = bswap16(val); | |
443 | } | |
53c25cea PB |
444 | virtio_config_writew(proxy->vdev, addr, val); |
445 | } | |
446 | ||
447 | static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val) | |
448 | { | |
449 | VirtIOPCIProxy *proxy = opaque; | |
aba800a3 | 450 | uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev); |
aba800a3 MT |
451 | if (addr < config) { |
452 | virtio_ioport_write(proxy, addr, val); | |
453 | return; | |
454 | } | |
455 | addr -= config; | |
82afa586 BH |
456 | if (virtio_is_big_endian()) { |
457 | val = bswap32(val); | |
458 | } | |
53c25cea PB |
459 | virtio_config_writel(proxy->vdev, addr, val); |
460 | } | |
461 | ||
4636b9d1 | 462 | static const MemoryRegionPortio virtio_portio[] = { |
da146d0a AK |
463 | { 0, 0x10000, 1, .write = virtio_pci_config_writeb, }, |
464 | { 0, 0x10000, 2, .write = virtio_pci_config_writew, }, | |
465 | { 0, 0x10000, 4, .write = virtio_pci_config_writel, }, | |
466 | { 0, 0x10000, 1, .read = virtio_pci_config_readb, }, | |
467 | { 0, 0x10000, 2, .read = virtio_pci_config_readw, }, | |
468 | { 0, 0x10000, 4, .read = virtio_pci_config_readl, }, | |
469 | PORTIO_END_OF_LIST() | |
470 | }; | |
53c25cea | 471 | |
da146d0a AK |
472 | static const MemoryRegionOps virtio_pci_config_ops = { |
473 | .old_portio = virtio_portio, | |
474 | .endianness = DEVICE_LITTLE_ENDIAN, | |
475 | }; | |
aba800a3 MT |
476 | |
477 | static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, | |
478 | uint32_t val, int len) | |
479 | { | |
ed757e14 YV |
480 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
481 | ||
1129714f MT |
482 | pci_default_write_config(pci_dev, address, val, len); |
483 | ||
484 | if (range_covers_byte(address, len, PCI_COMMAND) && | |
485 | !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) && | |
486 | !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) { | |
487 | virtio_pci_stop_ioeventfd(proxy); | |
488 | virtio_set_status(proxy->vdev, | |
489 | proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK); | |
ed757e14 | 490 | } |
53c25cea PB |
491 | } |
492 | ||
6d74ca5a MT |
493 | static unsigned virtio_pci_get_features(void *opaque) |
494 | { | |
8172539d MT |
495 | VirtIOPCIProxy *proxy = opaque; |
496 | return proxy->host_features; | |
6d74ca5a MT |
497 | } |
498 | ||
7d37d351 JK |
499 | static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy, |
500 | unsigned int queue_no, | |
501 | unsigned int vector, | |
502 | MSIMessage msg) | |
503 | { | |
504 | VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no); | |
15b2bd18 | 505 | EventNotifier *n = virtio_queue_get_guest_notifier(vq); |
7d37d351 | 506 | VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; |
15b2bd18 | 507 | int ret; |
7d37d351 JK |
508 | |
509 | if (irqfd->users == 0) { | |
510 | ret = kvm_irqchip_add_msi_route(kvm_state, msg); | |
511 | if (ret < 0) { | |
512 | return ret; | |
513 | } | |
514 | irqfd->virq = ret; | |
515 | } | |
516 | irqfd->users++; | |
517 | ||
15b2bd18 | 518 | ret = kvm_irqchip_add_irq_notifier(kvm_state, n, irqfd->virq); |
7d37d351 JK |
519 | if (ret < 0) { |
520 | if (--irqfd->users == 0) { | |
521 | kvm_irqchip_release_virq(kvm_state, irqfd->virq); | |
522 | } | |
523 | return ret; | |
524 | } | |
525 | ||
15b2bd18 | 526 | virtio_queue_set_guest_notifier_fd_handler(vq, true, true); |
7d37d351 JK |
527 | return 0; |
528 | } | |
529 | ||
530 | static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy, | |
531 | unsigned int queue_no, | |
532 | unsigned int vector) | |
533 | { | |
534 | VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no); | |
15b2bd18 | 535 | EventNotifier *n = virtio_queue_get_guest_notifier(vq); |
7d37d351 | 536 | VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector]; |
15b2bd18 | 537 | int ret; |
7d37d351 | 538 | |
15b2bd18 | 539 | ret = kvm_irqchip_remove_irq_notifier(kvm_state, n, irqfd->virq); |
7d37d351 JK |
540 | assert(ret == 0); |
541 | ||
542 | if (--irqfd->users == 0) { | |
543 | kvm_irqchip_release_virq(kvm_state, irqfd->virq); | |
544 | } | |
545 | ||
15b2bd18 | 546 | virtio_queue_set_guest_notifier_fd_handler(vq, true, false); |
7d37d351 JK |
547 | } |
548 | ||
549 | static int kvm_virtio_pci_vector_use(PCIDevice *dev, unsigned vector, | |
550 | MSIMessage msg) | |
551 | { | |
552 | VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); | |
553 | VirtIODevice *vdev = proxy->vdev; | |
554 | int ret, queue_no; | |
555 | ||
556 | for (queue_no = 0; queue_no < VIRTIO_PCI_QUEUE_MAX; queue_no++) { | |
557 | if (!virtio_queue_get_num(vdev, queue_no)) { | |
558 | break; | |
559 | } | |
560 | if (virtio_queue_vector(vdev, queue_no) != vector) { | |
561 | continue; | |
562 | } | |
563 | ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg); | |
564 | if (ret < 0) { | |
565 | goto undo; | |
566 | } | |
567 | } | |
568 | return 0; | |
569 | ||
570 | undo: | |
571 | while (--queue_no >= 0) { | |
572 | if (virtio_queue_vector(vdev, queue_no) != vector) { | |
573 | continue; | |
574 | } | |
575 | kvm_virtio_pci_vq_vector_release(proxy, queue_no, vector); | |
576 | } | |
577 | return ret; | |
578 | } | |
579 | ||
580 | static void kvm_virtio_pci_vector_release(PCIDevice *dev, unsigned vector) | |
581 | { | |
582 | VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev); | |
583 | VirtIODevice *vdev = proxy->vdev; | |
584 | int queue_no; | |
585 | ||
586 | for (queue_no = 0; queue_no < VIRTIO_PCI_QUEUE_MAX; queue_no++) { | |
587 | if (!virtio_queue_get_num(vdev, queue_no)) { | |
588 | break; | |
589 | } | |
590 | if (virtio_queue_vector(vdev, queue_no) != vector) { | |
591 | continue; | |
592 | } | |
593 | kvm_virtio_pci_vq_vector_release(proxy, queue_no, vector); | |
594 | } | |
595 | } | |
596 | ||
ade80dc8 MT |
597 | static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign) |
598 | { | |
599 | VirtIOPCIProxy *proxy = opaque; | |
600 | VirtQueue *vq = virtio_get_queue(proxy->vdev, n); | |
601 | EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); | |
602 | ||
603 | if (assign) { | |
604 | int r = event_notifier_init(notifier, 0); | |
605 | if (r < 0) { | |
606 | return r; | |
607 | } | |
15b2bd18 | 608 | virtio_queue_set_guest_notifier_fd_handler(vq, true, false); |
ade80dc8 | 609 | } else { |
15b2bd18 | 610 | virtio_queue_set_guest_notifier_fd_handler(vq, false, false); |
ade80dc8 MT |
611 | event_notifier_cleanup(notifier); |
612 | } | |
613 | ||
614 | return 0; | |
615 | } | |
616 | ||
5430a28f MT |
617 | static bool virtio_pci_query_guest_notifiers(void *opaque) |
618 | { | |
619 | VirtIOPCIProxy *proxy = opaque; | |
620 | return msix_enabled(&proxy->pci_dev); | |
621 | } | |
622 | ||
54dd9321 MT |
623 | static int virtio_pci_set_guest_notifiers(void *opaque, bool assign) |
624 | { | |
625 | VirtIOPCIProxy *proxy = opaque; | |
626 | VirtIODevice *vdev = proxy->vdev; | |
627 | int r, n; | |
628 | ||
7d37d351 | 629 | /* Must unset vector notifier while guest notifier is still assigned */ |
614e41bc | 630 | if (kvm_msi_via_irqfd_enabled() && !assign) { |
7d37d351 JK |
631 | msix_unset_vector_notifiers(&proxy->pci_dev); |
632 | g_free(proxy->vector_irqfd); | |
633 | proxy->vector_irqfd = NULL; | |
634 | } | |
635 | ||
54dd9321 MT |
636 | for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) { |
637 | if (!virtio_queue_get_num(vdev, n)) { | |
638 | break; | |
639 | } | |
640 | ||
641 | r = virtio_pci_set_guest_notifier(opaque, n, assign); | |
642 | if (r < 0) { | |
643 | goto assign_error; | |
644 | } | |
645 | } | |
646 | ||
7d37d351 | 647 | /* Must set vector notifier after guest notifier has been assigned */ |
614e41bc | 648 | if (kvm_msi_via_irqfd_enabled() && assign) { |
7d37d351 JK |
649 | proxy->vector_irqfd = |
650 | g_malloc0(sizeof(*proxy->vector_irqfd) * | |
651 | msix_nr_vectors_allocated(&proxy->pci_dev)); | |
652 | r = msix_set_vector_notifiers(&proxy->pci_dev, | |
653 | kvm_virtio_pci_vector_use, | |
654 | kvm_virtio_pci_vector_release); | |
655 | if (r < 0) { | |
656 | goto assign_error; | |
657 | } | |
658 | } | |
659 | ||
54dd9321 MT |
660 | return 0; |
661 | ||
662 | assign_error: | |
663 | /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */ | |
7d37d351 | 664 | assert(assign); |
54dd9321 MT |
665 | while (--n >= 0) { |
666 | virtio_pci_set_guest_notifier(opaque, n, !assign); | |
667 | } | |
668 | return r; | |
669 | } | |
670 | ||
ade80dc8 MT |
671 | static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign) |
672 | { | |
673 | VirtIOPCIProxy *proxy = opaque; | |
25db9ebe SH |
674 | |
675 | /* Stop using ioeventfd for virtqueue kick if the device starts using host | |
676 | * notifiers. This makes it easy to avoid stepping on each others' toes. | |
677 | */ | |
678 | proxy->ioeventfd_disabled = assign; | |
ade80dc8 | 679 | if (assign) { |
25db9ebe SH |
680 | virtio_pci_stop_ioeventfd(proxy); |
681 | } | |
682 | /* We don't need to start here: it's not needed because backend | |
683 | * currently only stops on status change away from ok, | |
684 | * reset, vmstop and such. If we do add code to start here, | |
685 | * need to check vmstate, device state etc. */ | |
26b9b5fe | 686 | return virtio_pci_set_host_notifier_internal(proxy, n, assign, false); |
25db9ebe SH |
687 | } |
688 | ||
689 | static void virtio_pci_vmstate_change(void *opaque, bool running) | |
690 | { | |
691 | VirtIOPCIProxy *proxy = opaque; | |
692 | ||
693 | if (running) { | |
89c473fd MT |
694 | /* Try to find out if the guest has bus master disabled, but is |
695 | in ready state. Then we have a buggy guest OS. */ | |
696 | if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) && | |
697 | !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { | |
698 | proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG; | |
699 | } | |
25db9ebe | 700 | virtio_pci_start_ioeventfd(proxy); |
ade80dc8 | 701 | } else { |
25db9ebe | 702 | virtio_pci_stop_ioeventfd(proxy); |
ade80dc8 | 703 | } |
ade80dc8 MT |
704 | } |
705 | ||
53c25cea | 706 | static const VirtIOBindings virtio_pci_bindings = { |
ff24bd58 MT |
707 | .notify = virtio_pci_notify, |
708 | .save_config = virtio_pci_save_config, | |
709 | .load_config = virtio_pci_load_config, | |
710 | .save_queue = virtio_pci_save_queue, | |
711 | .load_queue = virtio_pci_load_queue, | |
6d74ca5a | 712 | .get_features = virtio_pci_get_features, |
5430a28f | 713 | .query_guest_notifiers = virtio_pci_query_guest_notifiers, |
ade80dc8 | 714 | .set_host_notifier = virtio_pci_set_host_notifier, |
54dd9321 | 715 | .set_guest_notifiers = virtio_pci_set_guest_notifiers, |
25db9ebe | 716 | .vmstate_change = virtio_pci_vmstate_change, |
53c25cea PB |
717 | }; |
718 | ||
befeac45 | 719 | void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev) |
53c25cea PB |
720 | { |
721 | uint8_t *config; | |
722 | uint32_t size; | |
723 | ||
724 | proxy->vdev = vdev; | |
725 | ||
726 | config = proxy->pci_dev.config; | |
53c25cea | 727 | |
e75ccf2c IY |
728 | if (proxy->class_code) { |
729 | pci_config_set_class(config, proxy->class_code); | |
730 | } | |
ad3d11e6 HKR |
731 | pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID, |
732 | pci_get_word(config + PCI_VENDOR_ID)); | |
733 | pci_set_word(config + PCI_SUBSYSTEM_ID, vdev->device_id); | |
734 | config[PCI_INTERRUPT_PIN] = 1; | |
53c25cea | 735 | |
b2357c48 AW |
736 | if (vdev->nvectors && |
737 | msix_init_exclusive_bar(&proxy->pci_dev, vdev->nvectors, 1)) { | |
aba800a3 | 738 | vdev->nvectors = 0; |
b2357c48 | 739 | } |
aba800a3 | 740 | |
ed757e14 YV |
741 | proxy->pci_dev.config_write = virtio_write_config; |
742 | ||
aba800a3 | 743 | size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len; |
53c25cea PB |
744 | if (size & (size-1)) |
745 | size = 1 << qemu_fls(size); | |
746 | ||
da146d0a AK |
747 | memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy, |
748 | "virtio-pci", size); | |
e824b2cc AK |
749 | pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO, |
750 | &proxy->bar); | |
53c25cea | 751 | |
25db9ebe SH |
752 | if (!kvm_has_many_ioeventfds()) { |
753 | proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD; | |
754 | } | |
755 | ||
53c25cea | 756 | virtio_bind_device(vdev, &virtio_pci_bindings, proxy); |
8172539d MT |
757 | proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY; |
758 | proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE; | |
759 | proxy->host_features = vdev->get_features(vdev, proxy->host_features); | |
53c25cea PB |
760 | } |
761 | ||
81a322d4 | 762 | static int virtio_blk_init_pci(PCIDevice *pci_dev) |
53c25cea PB |
763 | { |
764 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); | |
765 | VirtIODevice *vdev; | |
766 | ||
ab73ff29 GH |
767 | if (proxy->class_code != PCI_CLASS_STORAGE_SCSI && |
768 | proxy->class_code != PCI_CLASS_STORAGE_OTHER) | |
769 | proxy->class_code = PCI_CLASS_STORAGE_SCSI; | |
53c25cea | 770 | |
12c5674b | 771 | vdev = virtio_blk_init(&pci_dev->qdev, &proxy->blk); |
ac0c14d7 MA |
772 | if (!vdev) { |
773 | return -1; | |
774 | } | |
177539e0 | 775 | vdev->nvectors = proxy->nvectors; |
e75ccf2c | 776 | virtio_init_pci(proxy, vdev); |
177539e0 GH |
777 | /* make the actual value visible */ |
778 | proxy->nvectors = vdev->nvectors; | |
81a322d4 | 779 | return 0; |
21d58b57 MM |
780 | } |
781 | ||
f90c2bcd | 782 | static void virtio_exit_pci(PCIDevice *pci_dev) |
0f457d91 | 783 | { |
da146d0a AK |
784 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
785 | ||
786 | memory_region_destroy(&proxy->bar); | |
b2357c48 | 787 | msix_uninit_exclusive_bar(pci_dev); |
0f457d91 MT |
788 | } |
789 | ||
f90c2bcd | 790 | static void virtio_blk_exit_pci(PCIDevice *pci_dev) |
56a14938 GH |
791 | { |
792 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); | |
793 | ||
25db9ebe | 794 | virtio_pci_stop_ioeventfd(proxy); |
9d0d3138 | 795 | virtio_blk_exit(proxy->vdev); |
f90c2bcd | 796 | virtio_exit_pci(pci_dev); |
56a14938 GH |
797 | } |
798 | ||
98b19252 | 799 | static int virtio_serial_init_pci(PCIDevice *pci_dev) |
21d58b57 | 800 | { |
d6beee99 | 801 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); |
85c2c735 MM |
802 | VirtIODevice *vdev; |
803 | ||
d6beee99 GH |
804 | if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER && |
805 | proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */ | |
806 | proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */ | |
807 | proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER; | |
808 | ||
6b331efb | 809 | vdev = virtio_serial_init(&pci_dev->qdev, &proxy->serial); |
25fe3654 AS |
810 | if (!vdev) { |
811 | return -1; | |
812 | } | |
573fb60c | 813 | vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED |
6b331efb | 814 | ? proxy->serial.max_virtserial_ports + 1 |
573fb60c | 815 | : proxy->nvectors; |
e75ccf2c | 816 | virtio_init_pci(proxy, vdev); |
a1829205 | 817 | proxy->nvectors = vdev->nvectors; |
81a322d4 | 818 | return 0; |
53c25cea PB |
819 | } |
820 | ||
f90c2bcd | 821 | static void virtio_serial_exit_pci(PCIDevice *pci_dev) |
8b53a865 AS |
822 | { |
823 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); | |
824 | ||
32059220 | 825 | virtio_pci_stop_ioeventfd(proxy); |
8b53a865 | 826 | virtio_serial_exit(proxy->vdev); |
f90c2bcd | 827 | virtio_exit_pci(pci_dev); |
8b53a865 AS |
828 | } |
829 | ||
81a322d4 | 830 | static int virtio_net_init_pci(PCIDevice *pci_dev) |
53c25cea PB |
831 | { |
832 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); | |
833 | VirtIODevice *vdev; | |
834 | ||
f0c07c7c | 835 | vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net); |
a1e0fea5 | 836 | |
97b15621 | 837 | vdev->nvectors = proxy->nvectors; |
e75ccf2c | 838 | virtio_init_pci(proxy, vdev); |
a1e0fea5 GH |
839 | |
840 | /* make the actual value visible */ | |
841 | proxy->nvectors = vdev->nvectors; | |
81a322d4 | 842 | return 0; |
53c25cea PB |
843 | } |
844 | ||
f90c2bcd | 845 | static void virtio_net_exit_pci(PCIDevice *pci_dev) |
97b15621 GH |
846 | { |
847 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); | |
848 | ||
25db9ebe | 849 | virtio_pci_stop_ioeventfd(proxy); |
97b15621 | 850 | virtio_net_exit(proxy->vdev); |
f90c2bcd | 851 | virtio_exit_pci(pci_dev); |
97b15621 GH |
852 | } |
853 | ||
81a322d4 | 854 | static int virtio_balloon_init_pci(PCIDevice *pci_dev) |
53c25cea PB |
855 | { |
856 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); | |
857 | VirtIODevice *vdev; | |
858 | ||
2ba1d381 DG |
859 | if (proxy->class_code != PCI_CLASS_OTHERS && |
860 | proxy->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */ | |
861 | proxy->class_code = PCI_CLASS_OTHERS; | |
862 | } | |
863 | ||
53c25cea | 864 | vdev = virtio_balloon_init(&pci_dev->qdev); |
f76f6655 AS |
865 | if (!vdev) { |
866 | return -1; | |
867 | } | |
e75ccf2c | 868 | virtio_init_pci(proxy, vdev); |
81a322d4 | 869 | return 0; |
53c25cea PB |
870 | } |
871 | ||
f90c2bcd | 872 | static void virtio_balloon_exit_pci(PCIDevice *pci_dev) |
855d7e25 AS |
873 | { |
874 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); | |
875 | ||
876 | virtio_pci_stop_ioeventfd(proxy); | |
877 | virtio_balloon_exit(proxy->vdev); | |
f90c2bcd | 878 | virtio_exit_pci(pci_dev); |
855d7e25 AS |
879 | } |
880 | ||
40021f08 AL |
881 | static Property virtio_blk_properties[] = { |
882 | DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0), | |
12c5674b | 883 | DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, blk.conf), |
e63e7fde | 884 | DEFINE_BLOCK_CHS_PROPERTIES(VirtIOPCIProxy, blk.conf), |
12c5674b | 885 | DEFINE_PROP_STRING("serial", VirtIOPCIProxy, blk.serial), |
a6c5c84a PB |
886 | #ifdef __linux__ |
887 | DEFINE_PROP_BIT("scsi", VirtIOPCIProxy, blk.scsi, 0, true), | |
888 | #endif | |
eec7f96c | 889 | DEFINE_PROP_BIT("config-wce", VirtIOPCIProxy, blk.config_wce, 0, true), |
40021f08 AL |
890 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), |
891 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), | |
892 | DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features), | |
893 | DEFINE_PROP_END_OF_LIST(), | |
e855761c AL |
894 | }; |
895 | ||
40021f08 AL |
896 | static void virtio_blk_class_init(ObjectClass *klass, void *data) |
897 | { | |
39bffca2 | 898 | DeviceClass *dc = DEVICE_CLASS(klass); |
40021f08 AL |
899 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
900 | ||
901 | k->init = virtio_blk_init_pci; | |
902 | k->exit = virtio_blk_exit_pci; | |
903 | k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
904 | k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK; | |
905 | k->revision = VIRTIO_PCI_ABI_VERSION; | |
906 | k->class_id = PCI_CLASS_STORAGE_SCSI; | |
39bffca2 AL |
907 | dc->reset = virtio_pci_reset; |
908 | dc->props = virtio_blk_properties; | |
40021f08 AL |
909 | } |
910 | ||
39bffca2 AL |
911 | static TypeInfo virtio_blk_info = { |
912 | .name = "virtio-blk-pci", | |
913 | .parent = TYPE_PCI_DEVICE, | |
914 | .instance_size = sizeof(VirtIOPCIProxy), | |
915 | .class_init = virtio_blk_class_init, | |
40021f08 AL |
916 | }; |
917 | ||
918 | static Property virtio_net_properties[] = { | |
919 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false), | |
920 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3), | |
921 | DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features), | |
922 | DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic), | |
923 | DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy, net.txtimer, TX_TIMER_INTERVAL), | |
924 | DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy, net.txburst, TX_BURST), | |
925 | DEFINE_PROP_STRING("tx", VirtIOPCIProxy, net.tx), | |
926 | DEFINE_PROP_END_OF_LIST(), | |
927 | }; | |
928 | ||
929 | static void virtio_net_class_init(ObjectClass *klass, void *data) | |
930 | { | |
39bffca2 | 931 | DeviceClass *dc = DEVICE_CLASS(klass); |
40021f08 AL |
932 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
933 | ||
934 | k->init = virtio_net_init_pci; | |
935 | k->exit = virtio_net_exit_pci; | |
936 | k->romfile = "pxe-virtio.rom"; | |
937 | k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
938 | k->device_id = PCI_DEVICE_ID_VIRTIO_NET; | |
939 | k->revision = VIRTIO_PCI_ABI_VERSION; | |
940 | k->class_id = PCI_CLASS_NETWORK_ETHERNET; | |
39bffca2 AL |
941 | dc->reset = virtio_pci_reset; |
942 | dc->props = virtio_net_properties; | |
40021f08 AL |
943 | } |
944 | ||
39bffca2 AL |
945 | static TypeInfo virtio_net_info = { |
946 | .name = "virtio-net-pci", | |
947 | .parent = TYPE_PCI_DEVICE, | |
948 | .instance_size = sizeof(VirtIOPCIProxy), | |
949 | .class_init = virtio_net_class_init, | |
e855761c AL |
950 | }; |
951 | ||
40021f08 AL |
952 | static Property virtio_serial_properties[] = { |
953 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), | |
954 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, DEV_NVECTORS_UNSPECIFIED), | |
955 | DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0), | |
956 | DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), | |
957 | DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy, serial.max_virtserial_ports, 31), | |
958 | DEFINE_PROP_END_OF_LIST(), | |
e855761c AL |
959 | }; |
960 | ||
40021f08 AL |
961 | static void virtio_serial_class_init(ObjectClass *klass, void *data) |
962 | { | |
39bffca2 | 963 | DeviceClass *dc = DEVICE_CLASS(klass); |
40021f08 AL |
964 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
965 | ||
966 | k->init = virtio_serial_init_pci; | |
967 | k->exit = virtio_serial_exit_pci; | |
968 | k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
969 | k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE; | |
970 | k->revision = VIRTIO_PCI_ABI_VERSION; | |
971 | k->class_id = PCI_CLASS_COMMUNICATION_OTHER; | |
39bffca2 AL |
972 | dc->reset = virtio_pci_reset; |
973 | dc->props = virtio_serial_properties; | |
40021f08 AL |
974 | } |
975 | ||
39bffca2 AL |
976 | static TypeInfo virtio_serial_info = { |
977 | .name = "virtio-serial-pci", | |
978 | .parent = TYPE_PCI_DEVICE, | |
979 | .instance_size = sizeof(VirtIOPCIProxy), | |
980 | .class_init = virtio_serial_class_init, | |
40021f08 AL |
981 | }; |
982 | ||
983 | static Property virtio_balloon_properties[] = { | |
984 | DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), | |
2ba1d381 | 985 | DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0), |
40021f08 AL |
986 | DEFINE_PROP_END_OF_LIST(), |
987 | }; | |
988 | ||
989 | static void virtio_balloon_class_init(ObjectClass *klass, void *data) | |
990 | { | |
39bffca2 | 991 | DeviceClass *dc = DEVICE_CLASS(klass); |
40021f08 AL |
992 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
993 | ||
994 | k->init = virtio_balloon_init_pci; | |
995 | k->exit = virtio_balloon_exit_pci; | |
996 | k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
997 | k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON; | |
998 | k->revision = VIRTIO_PCI_ABI_VERSION; | |
2ba1d381 | 999 | k->class_id = PCI_CLASS_OTHERS; |
39bffca2 AL |
1000 | dc->reset = virtio_pci_reset; |
1001 | dc->props = virtio_balloon_properties; | |
40021f08 AL |
1002 | } |
1003 | ||
39bffca2 AL |
1004 | static TypeInfo virtio_balloon_info = { |
1005 | .name = "virtio-balloon-pci", | |
1006 | .parent = TYPE_PCI_DEVICE, | |
1007 | .instance_size = sizeof(VirtIOPCIProxy), | |
1008 | .class_init = virtio_balloon_class_init, | |
0aab0d3a GH |
1009 | }; |
1010 | ||
973abc7f SH |
1011 | static int virtio_scsi_init_pci(PCIDevice *pci_dev) |
1012 | { | |
1013 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); | |
1014 | VirtIODevice *vdev; | |
1015 | ||
1016 | vdev = virtio_scsi_init(&pci_dev->qdev, &proxy->scsi); | |
1017 | if (!vdev) { | |
1018 | return -EINVAL; | |
1019 | } | |
1020 | ||
4c205d0c PB |
1021 | vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED |
1022 | ? proxy->scsi.num_queues + 3 | |
1023 | : proxy->nvectors; | |
973abc7f SH |
1024 | virtio_init_pci(proxy, vdev); |
1025 | ||
1026 | /* make the actual value visible */ | |
1027 | proxy->nvectors = vdev->nvectors; | |
1028 | return 0; | |
1029 | } | |
1030 | ||
f90c2bcd | 1031 | static void virtio_scsi_exit_pci(PCIDevice *pci_dev) |
973abc7f SH |
1032 | { |
1033 | VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); | |
1034 | ||
1035 | virtio_scsi_exit(proxy->vdev); | |
f90c2bcd | 1036 | virtio_exit_pci(pci_dev); |
973abc7f SH |
1037 | } |
1038 | ||
1039 | static Property virtio_scsi_properties[] = { | |
3f910904 | 1040 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), |
4c205d0c | 1041 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, DEV_NVECTORS_UNSPECIFIED), |
973abc7f SH |
1042 | DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOPCIProxy, host_features, scsi), |
1043 | DEFINE_PROP_END_OF_LIST(), | |
1044 | }; | |
1045 | ||
1046 | static void virtio_scsi_class_init(ObjectClass *klass, void *data) | |
1047 | { | |
1048 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1049 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); | |
1050 | ||
1051 | k->init = virtio_scsi_init_pci; | |
1052 | k->exit = virtio_scsi_exit_pci; | |
1053 | k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; | |
1054 | k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI; | |
1055 | k->revision = 0x00; | |
1056 | k->class_id = PCI_CLASS_STORAGE_SCSI; | |
1057 | dc->reset = virtio_pci_reset; | |
1058 | dc->props = virtio_scsi_properties; | |
1059 | } | |
1060 | ||
1061 | static TypeInfo virtio_scsi_info = { | |
1062 | .name = "virtio-scsi-pci", | |
1063 | .parent = TYPE_PCI_DEVICE, | |
1064 | .instance_size = sizeof(VirtIOPCIProxy), | |
1065 | .class_init = virtio_scsi_class_init, | |
1066 | }; | |
1067 | ||
83f7d43a | 1068 | static void virtio_pci_register_types(void) |
53c25cea | 1069 | { |
39bffca2 | 1070 | type_register_static(&virtio_blk_info); |
39bffca2 | 1071 | type_register_static(&virtio_net_info); |
39bffca2 | 1072 | type_register_static(&virtio_serial_info); |
39bffca2 | 1073 | type_register_static(&virtio_balloon_info); |
973abc7f | 1074 | type_register_static(&virtio_scsi_info); |
53c25cea PB |
1075 | } |
1076 | ||
83f7d43a | 1077 | type_init(virtio_pci_register_types) |