| 1 | /* |
| 2 | * Virtio rng PCI Bindings |
| 3 | * |
| 4 | * Copyright 2012 Red Hat, Inc. |
| 5 | * Copyright 2012 Amit Shah <amit.shah@redhat.com> |
| 6 | * |
| 7 | * This work is licensed under the terms of the GNU GPL, version 2 or |
| 8 | * (at your option) any later version. See the COPYING file in the |
| 9 | * top-level directory. |
| 10 | */ |
| 11 | |
| 12 | #include "qemu/osdep.h" |
| 13 | |
| 14 | #include "hw/virtio/virtio-pci.h" |
| 15 | #include "hw/virtio/virtio-rng.h" |
| 16 | #include "hw/core/qdev-properties.h" |
| 17 | #include "qapi/error.h" |
| 18 | #include "qemu/module.h" |
| 19 | #include "qom/object.h" |
| 20 | |
| 21 | typedef struct VirtIORngPCI VirtIORngPCI; |
| 22 | |
| 23 | /* |
| 24 | * virtio-rng-pci: This extends VirtioPCIProxy. |
| 25 | */ |
| 26 | #define TYPE_VIRTIO_RNG_PCI "virtio-rng-pci-base" |
| 27 | DECLARE_INSTANCE_CHECKER(VirtIORngPCI, VIRTIO_RNG_PCI, |
| 28 | TYPE_VIRTIO_RNG_PCI) |
| 29 | |
| 30 | struct VirtIORngPCI { |
| 31 | VirtIOPCIProxy parent_obj; |
| 32 | VirtIORNG vdev; |
| 33 | }; |
| 34 | |
| 35 | static const Property virtio_rng_properties[] = { |
| 36 | DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, |
| 37 | VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), |
| 38 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, |
| 39 | DEV_NVECTORS_UNSPECIFIED), |
| 40 | }; |
| 41 | |
| 42 | static void virtio_rng_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) |
| 43 | { |
| 44 | VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev); |
| 45 | DeviceState *vdev = DEVICE(&vrng->vdev); |
| 46 | |
| 47 | if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { |
| 48 | vpci_dev->nvectors = 2; |
| 49 | } |
| 50 | |
| 51 | if (!qdev_realize(vdev, BUS(&vpci_dev->bus), errp)) { |
| 52 | return; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static void virtio_rng_pci_class_init(ObjectClass *klass, const void *data) |
| 57 | { |
| 58 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 59 | VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); |
| 60 | PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); |
| 61 | |
| 62 | k->realize = virtio_rng_pci_realize; |
| 63 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
| 64 | |
| 65 | pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; |
| 66 | pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG; |
| 67 | pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; |
| 68 | pcidev_k->class_id = PCI_CLASS_OTHERS; |
| 69 | device_class_set_props(dc, virtio_rng_properties); |
| 70 | } |
| 71 | |
| 72 | static void virtio_rng_initfn(Object *obj) |
| 73 | { |
| 74 | VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj); |
| 75 | |
| 76 | virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), |
| 77 | TYPE_VIRTIO_RNG); |
| 78 | } |
| 79 | |
| 80 | static const VirtioPCIDeviceTypeInfo virtio_rng_pci_info = { |
| 81 | .base_name = TYPE_VIRTIO_RNG_PCI, |
| 82 | .generic_name = "virtio-rng-pci", |
| 83 | .transitional_name = "virtio-rng-pci-transitional", |
| 84 | .non_transitional_name = "virtio-rng-pci-non-transitional", |
| 85 | .instance_size = sizeof(VirtIORngPCI), |
| 86 | .instance_init = virtio_rng_initfn, |
| 87 | .class_init = virtio_rng_pci_class_init, |
| 88 | }; |
| 89 | |
| 90 | static void virtio_rng_pci_register(void) |
| 91 | { |
| 92 | virtio_pci_types_register(&virtio_rng_pci_info); |
| 93 | } |
| 94 | |
| 95 | type_init(virtio_rng_pci_register) |