Fix regression in option parsing
[qemu/cris-port.git] / hw / virtio-pci.c
blob709d13e4ce160d3ccb2aa670f9baf6faabbb5090
1 /*
2 * Virtio PCI Bindings
4 * Copyright IBM, Corp. 2007
5 * Copyright (c) 2009 CodeSourcery
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paul Brook <paul@codesourcery.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
16 #include <inttypes.h>
18 #include "virtio.h"
19 #include "virtio-blk.h"
20 #include "virtio-net.h"
21 #include "pci.h"
22 #include "sysemu.h"
23 #include "msix.h"
24 #include "net.h"
25 #include "loader.h"
27 /* from Linux's linux/virtio_pci.h */
29 /* A 32-bit r/o bitmask of the features supported by the host */
30 #define VIRTIO_PCI_HOST_FEATURES 0
32 /* A 32-bit r/w bitmask of features activated by the guest */
33 #define VIRTIO_PCI_GUEST_FEATURES 4
35 /* A 32-bit r/w PFN for the currently selected queue */
36 #define VIRTIO_PCI_QUEUE_PFN 8
38 /* A 16-bit r/o queue size for the currently selected queue */
39 #define VIRTIO_PCI_QUEUE_NUM 12
41 /* A 16-bit r/w queue selector */
42 #define VIRTIO_PCI_QUEUE_SEL 14
44 /* A 16-bit r/w queue notifier */
45 #define VIRTIO_PCI_QUEUE_NOTIFY 16
47 /* An 8-bit device status register. */
48 #define VIRTIO_PCI_STATUS 18
50 /* An 8-bit r/o interrupt status register. Reading the value will return the
51 * current contents of the ISR and will also clear it. This is effectively
52 * a read-and-acknowledge. */
53 #define VIRTIO_PCI_ISR 19
55 /* MSI-X registers: only enabled if MSI-X is enabled. */
56 /* A 16-bit vector for configuration changes. */
57 #define VIRTIO_MSI_CONFIG_VECTOR 20
58 /* A 16-bit vector for selected queue notifications. */
59 #define VIRTIO_MSI_QUEUE_VECTOR 22
61 /* Config space size */
62 #define VIRTIO_PCI_CONFIG_NOMSI 20
63 #define VIRTIO_PCI_CONFIG_MSI 24
64 #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
65 VIRTIO_PCI_CONFIG_MSI : \
66 VIRTIO_PCI_CONFIG_NOMSI)
68 /* The remaining space is defined by each driver as the per-driver
69 * configuration space */
70 #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
71 VIRTIO_PCI_CONFIG_MSI : \
72 VIRTIO_PCI_CONFIG_NOMSI)
74 /* Virtio ABI version, if we increment this, we break the guest driver. */
75 #define VIRTIO_PCI_ABI_VERSION 0
77 /* How many bits to shift physical queue address written to QUEUE_PFN.
78 * 12 is historical, and due to x86 page size. */
79 #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
81 /* QEMU doesn't strictly need write barriers since everything runs in
82 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
83 * KVM or if kqemu gets SMP support.
85 #define wmb() do { } while (0)
87 /* PCI bindings. */
89 typedef struct {
90 PCIDevice pci_dev;
91 VirtIODevice *vdev;
92 uint32_t addr;
93 uint32_t class_code;
94 uint32_t nvectors;
95 DriveInfo *dinfo;
96 NICConf nic;
97 uint32_t host_features;
98 /* Max. number of ports we can have for a the virtio-serial device */
99 uint32_t max_virtserial_ports;
100 } VirtIOPCIProxy;
102 /* virtio device */
104 static void virtio_pci_notify(void *opaque, uint16_t vector)
106 VirtIOPCIProxy *proxy = opaque;
107 if (msix_enabled(&proxy->pci_dev))
108 msix_notify(&proxy->pci_dev, vector);
109 else
110 qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1);
113 static void virtio_pci_save_config(void * opaque, QEMUFile *f)
115 VirtIOPCIProxy *proxy = opaque;
116 pci_device_save(&proxy->pci_dev, f);
117 msix_save(&proxy->pci_dev, f);
118 if (msix_present(&proxy->pci_dev))
119 qemu_put_be16(f, proxy->vdev->config_vector);
122 static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f)
124 VirtIOPCIProxy *proxy = opaque;
125 if (msix_present(&proxy->pci_dev))
126 qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n));
129 static int virtio_pci_load_config(void * opaque, QEMUFile *f)
131 VirtIOPCIProxy *proxy = opaque;
132 int ret;
133 ret = pci_device_load(&proxy->pci_dev, f);
134 if (ret) {
135 return ret;
137 msix_load(&proxy->pci_dev, f);
138 if (msix_present(&proxy->pci_dev)) {
139 qemu_get_be16s(f, &proxy->vdev->config_vector);
140 } else {
141 proxy->vdev->config_vector = VIRTIO_NO_VECTOR;
143 if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
144 return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
146 return 0;
149 static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f)
151 VirtIOPCIProxy *proxy = opaque;
152 uint16_t vector;
153 if (msix_present(&proxy->pci_dev)) {
154 qemu_get_be16s(f, &vector);
155 } else {
156 vector = VIRTIO_NO_VECTOR;
158 virtio_queue_set_vector(proxy->vdev, n, vector);
159 if (vector != VIRTIO_NO_VECTOR) {
160 return msix_vector_use(&proxy->pci_dev, vector);
162 return 0;
165 static void virtio_pci_reset(DeviceState *d)
167 VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev);
168 virtio_reset(proxy->vdev);
169 msix_reset(&proxy->pci_dev);
172 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
174 VirtIOPCIProxy *proxy = opaque;
175 VirtIODevice *vdev = proxy->vdev;
176 target_phys_addr_t pa;
178 switch (addr) {
179 case VIRTIO_PCI_GUEST_FEATURES:
180 /* Guest does not negotiate properly? We have to assume nothing. */
181 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
182 if (vdev->bad_features)
183 val = proxy->host_features & vdev->bad_features(vdev);
184 else
185 val = 0;
187 if (vdev->set_features)
188 vdev->set_features(vdev, val);
189 vdev->guest_features = val;
190 break;
191 case VIRTIO_PCI_QUEUE_PFN:
192 pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
193 if (pa == 0) {
194 virtio_reset(proxy->vdev);
195 msix_unuse_all_vectors(&proxy->pci_dev);
197 else
198 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
199 break;
200 case VIRTIO_PCI_QUEUE_SEL:
201 if (val < VIRTIO_PCI_QUEUE_MAX)
202 vdev->queue_sel = val;
203 break;
204 case VIRTIO_PCI_QUEUE_NOTIFY:
205 virtio_queue_notify(vdev, val);
206 break;
207 case VIRTIO_PCI_STATUS:
208 vdev->status = val & 0xFF;
209 if (vdev->status == 0) {
210 virtio_reset(proxy->vdev);
211 msix_unuse_all_vectors(&proxy->pci_dev);
213 break;
214 case VIRTIO_MSI_CONFIG_VECTOR:
215 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
216 /* Make it possible for guest to discover an error took place. */
217 if (msix_vector_use(&proxy->pci_dev, val) < 0)
218 val = VIRTIO_NO_VECTOR;
219 vdev->config_vector = val;
220 break;
221 case VIRTIO_MSI_QUEUE_VECTOR:
222 msix_vector_unuse(&proxy->pci_dev,
223 virtio_queue_vector(vdev, vdev->queue_sel));
224 /* Make it possible for guest to discover an error took place. */
225 if (msix_vector_use(&proxy->pci_dev, val) < 0)
226 val = VIRTIO_NO_VECTOR;
227 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
228 break;
229 default:
230 fprintf(stderr, "%s: unexpected address 0x%x value 0x%x\n",
231 __func__, addr, val);
232 break;
236 static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
238 VirtIODevice *vdev = proxy->vdev;
239 uint32_t ret = 0xFFFFFFFF;
241 switch (addr) {
242 case VIRTIO_PCI_HOST_FEATURES:
243 ret = proxy->host_features;
244 break;
245 case VIRTIO_PCI_GUEST_FEATURES:
246 ret = vdev->guest_features;
247 break;
248 case VIRTIO_PCI_QUEUE_PFN:
249 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
250 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
251 break;
252 case VIRTIO_PCI_QUEUE_NUM:
253 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
254 break;
255 case VIRTIO_PCI_QUEUE_SEL:
256 ret = vdev->queue_sel;
257 break;
258 case VIRTIO_PCI_STATUS:
259 ret = vdev->status;
260 break;
261 case VIRTIO_PCI_ISR:
262 /* reading from the ISR also clears it. */
263 ret = vdev->isr;
264 vdev->isr = 0;
265 qemu_set_irq(proxy->pci_dev.irq[0], 0);
266 break;
267 case VIRTIO_MSI_CONFIG_VECTOR:
268 ret = vdev->config_vector;
269 break;
270 case VIRTIO_MSI_QUEUE_VECTOR:
271 ret = virtio_queue_vector(vdev, vdev->queue_sel);
272 break;
273 default:
274 break;
277 return ret;
280 static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
282 VirtIOPCIProxy *proxy = opaque;
283 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
284 addr -= proxy->addr;
285 if (addr < config)
286 return virtio_ioport_read(proxy, addr);
287 addr -= config;
288 return virtio_config_readb(proxy->vdev, addr);
291 static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
293 VirtIOPCIProxy *proxy = opaque;
294 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
295 addr -= proxy->addr;
296 if (addr < config)
297 return virtio_ioport_read(proxy, addr);
298 addr -= config;
299 return virtio_config_readw(proxy->vdev, addr);
302 static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
304 VirtIOPCIProxy *proxy = opaque;
305 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
306 addr -= proxy->addr;
307 if (addr < config)
308 return virtio_ioport_read(proxy, addr);
309 addr -= config;
310 return virtio_config_readl(proxy->vdev, addr);
313 static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val)
315 VirtIOPCIProxy *proxy = opaque;
316 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
317 addr -= proxy->addr;
318 if (addr < config) {
319 virtio_ioport_write(proxy, addr, val);
320 return;
322 addr -= config;
323 virtio_config_writeb(proxy->vdev, addr, val);
326 static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val)
328 VirtIOPCIProxy *proxy = opaque;
329 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
330 addr -= proxy->addr;
331 if (addr < config) {
332 virtio_ioport_write(proxy, addr, val);
333 return;
335 addr -= config;
336 virtio_config_writew(proxy->vdev, addr, val);
339 static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val)
341 VirtIOPCIProxy *proxy = opaque;
342 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
343 addr -= proxy->addr;
344 if (addr < config) {
345 virtio_ioport_write(proxy, addr, val);
346 return;
348 addr -= config;
349 virtio_config_writel(proxy->vdev, addr, val);
352 static void virtio_map(PCIDevice *pci_dev, int region_num,
353 pcibus_t addr, pcibus_t size, int type)
355 VirtIOPCIProxy *proxy = container_of(pci_dev, VirtIOPCIProxy, pci_dev);
356 VirtIODevice *vdev = proxy->vdev;
357 unsigned config_len = VIRTIO_PCI_REGION_SIZE(pci_dev) + vdev->config_len;
359 proxy->addr = addr;
361 register_ioport_write(addr, config_len, 1, virtio_pci_config_writeb, proxy);
362 register_ioport_write(addr, config_len, 2, virtio_pci_config_writew, proxy);
363 register_ioport_write(addr, config_len, 4, virtio_pci_config_writel, proxy);
364 register_ioport_read(addr, config_len, 1, virtio_pci_config_readb, proxy);
365 register_ioport_read(addr, config_len, 2, virtio_pci_config_readw, proxy);
366 register_ioport_read(addr, config_len, 4, virtio_pci_config_readl, proxy);
368 if (vdev->config_len)
369 vdev->get_config(vdev, vdev->config);
372 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
373 uint32_t val, int len)
375 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
377 if (PCI_COMMAND == address) {
378 if (!(val & PCI_COMMAND_MASTER)) {
379 proxy->vdev->status &= ~VIRTIO_CONFIG_S_DRIVER_OK;
383 pci_default_write_config(pci_dev, address, val, len);
384 msix_write_config(pci_dev, address, val, len);
387 static unsigned virtio_pci_get_features(void *opaque)
389 VirtIOPCIProxy *proxy = opaque;
390 return proxy->host_features;
393 static const VirtIOBindings virtio_pci_bindings = {
394 .notify = virtio_pci_notify,
395 .save_config = virtio_pci_save_config,
396 .load_config = virtio_pci_load_config,
397 .save_queue = virtio_pci_save_queue,
398 .load_queue = virtio_pci_load_queue,
399 .get_features = virtio_pci_get_features,
402 static void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev,
403 uint16_t vendor, uint16_t device,
404 uint16_t class_code, uint8_t pif)
406 uint8_t *config;
407 uint32_t size;
409 proxy->vdev = vdev;
411 config = proxy->pci_dev.config;
412 pci_config_set_vendor_id(config, vendor);
413 pci_config_set_device_id(config, device);
415 config[0x08] = VIRTIO_PCI_ABI_VERSION;
417 config[0x09] = pif;
418 pci_config_set_class(config, class_code);
419 config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL;
421 config[0x2c] = vendor & 0xFF;
422 config[0x2d] = (vendor >> 8) & 0xFF;
423 config[0x2e] = vdev->device_id & 0xFF;
424 config[0x2f] = (vdev->device_id >> 8) & 0xFF;
426 config[0x3d] = 1;
428 if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors, 1, 0)) {
429 pci_register_bar(&proxy->pci_dev, 1,
430 msix_bar_size(&proxy->pci_dev),
431 PCI_BASE_ADDRESS_SPACE_MEMORY,
432 msix_mmio_map);
433 } else
434 vdev->nvectors = 0;
436 proxy->pci_dev.config_write = virtio_write_config;
438 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
439 if (size & (size-1))
440 size = 1 << qemu_fls(size);
442 pci_register_bar(&proxy->pci_dev, 0, size, PCI_BASE_ADDRESS_SPACE_IO,
443 virtio_map);
445 virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
446 proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
447 proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
448 proxy->host_features = vdev->get_features(vdev, proxy->host_features);
451 static int virtio_blk_init_pci(PCIDevice *pci_dev)
453 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
454 VirtIODevice *vdev;
456 if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
457 proxy->class_code != PCI_CLASS_STORAGE_OTHER)
458 proxy->class_code = PCI_CLASS_STORAGE_SCSI;
460 if (!proxy->dinfo) {
461 qemu_error("virtio-blk-pci: drive property not set\n");
462 return -1;
464 vdev = virtio_blk_init(&pci_dev->qdev, proxy->dinfo);
465 vdev->nvectors = proxy->nvectors;
466 virtio_init_pci(proxy, vdev,
467 PCI_VENDOR_ID_REDHAT_QUMRANET,
468 PCI_DEVICE_ID_VIRTIO_BLOCK,
469 proxy->class_code, 0x00);
470 /* make the actual value visible */
471 proxy->nvectors = vdev->nvectors;
472 return 0;
475 static int virtio_exit_pci(PCIDevice *pci_dev)
477 return msix_uninit(pci_dev);
480 static int virtio_blk_exit_pci(PCIDevice *pci_dev)
482 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
484 drive_uninit(proxy->dinfo);
485 return virtio_exit_pci(pci_dev);
488 static int virtio_serial_init_pci(PCIDevice *pci_dev)
490 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
491 VirtIODevice *vdev;
493 if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
494 proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
495 proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */
496 proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER;
498 vdev = virtio_serial_init(&pci_dev->qdev, proxy->max_virtserial_ports);
499 if (!vdev) {
500 return -1;
502 vdev->nvectors = proxy->nvectors ? proxy->nvectors
503 : proxy->max_virtserial_ports + 1;
504 virtio_init_pci(proxy, vdev,
505 PCI_VENDOR_ID_REDHAT_QUMRANET,
506 PCI_DEVICE_ID_VIRTIO_CONSOLE,
507 proxy->class_code, 0x00);
508 proxy->nvectors = vdev->nvectors;
509 return 0;
512 static int virtio_net_init_pci(PCIDevice *pci_dev)
514 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
515 VirtIODevice *vdev;
517 vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic);
519 vdev->nvectors = proxy->nvectors;
520 virtio_init_pci(proxy, vdev,
521 PCI_VENDOR_ID_REDHAT_QUMRANET,
522 PCI_DEVICE_ID_VIRTIO_NET,
523 PCI_CLASS_NETWORK_ETHERNET,
524 0x00);
526 /* make the actual value visible */
527 proxy->nvectors = vdev->nvectors;
528 return 0;
531 static int virtio_net_exit_pci(PCIDevice *pci_dev)
533 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
535 virtio_net_exit(proxy->vdev);
536 return virtio_exit_pci(pci_dev);
539 static int virtio_balloon_init_pci(PCIDevice *pci_dev)
541 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
542 VirtIODevice *vdev;
544 vdev = virtio_balloon_init(&pci_dev->qdev);
545 virtio_init_pci(proxy, vdev,
546 PCI_VENDOR_ID_REDHAT_QUMRANET,
547 PCI_DEVICE_ID_VIRTIO_BALLOON,
548 PCI_CLASS_MEMORY_RAM,
549 0x00);
550 return 0;
553 static PCIDeviceInfo virtio_info[] = {
555 .qdev.name = "virtio-blk-pci",
556 .qdev.size = sizeof(VirtIOPCIProxy),
557 .init = virtio_blk_init_pci,
558 .exit = virtio_blk_exit_pci,
559 .qdev.props = (Property[]) {
560 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
561 DEFINE_PROP_DRIVE("drive", VirtIOPCIProxy, dinfo),
562 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
563 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
564 DEFINE_PROP_END_OF_LIST(),
566 .qdev.reset = virtio_pci_reset,
568 .qdev.name = "virtio-net-pci",
569 .qdev.size = sizeof(VirtIOPCIProxy),
570 .init = virtio_net_init_pci,
571 .exit = virtio_net_exit_pci,
572 .romfile = "pxe-virtio.bin",
573 .qdev.props = (Property[]) {
574 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
575 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
576 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic),
577 DEFINE_PROP_END_OF_LIST(),
579 .qdev.reset = virtio_pci_reset,
581 .qdev.name = "virtio-serial-pci",
582 .qdev.alias = "virtio-serial",
583 .qdev.size = sizeof(VirtIOPCIProxy),
584 .init = virtio_serial_init_pci,
585 .exit = virtio_exit_pci,
586 .qdev.props = (Property[]) {
587 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 0),
588 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
589 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
590 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy, max_virtserial_ports,
591 31),
592 DEFINE_PROP_END_OF_LIST(),
594 .qdev.reset = virtio_pci_reset,
596 .qdev.name = "virtio-balloon-pci",
597 .qdev.size = sizeof(VirtIOPCIProxy),
598 .init = virtio_balloon_init_pci,
599 .exit = virtio_exit_pci,
600 .qdev.props = (Property[]) {
601 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
602 DEFINE_PROP_END_OF_LIST(),
604 .qdev.reset = virtio_pci_reset,
606 /* end of list */
610 static void virtio_pci_register_devices(void)
612 pci_qdev_register_many(virtio_info);
615 device_init(virtio_pci_register_devices)