xref: /openbmc/qemu/hw/virtio/vhost-user-rng-pci.c (revision ed75658a)
1 /*
2  * Vhost-user RNG virtio device PCI glue
3  *
4  * Copyright (c) 2021 Mathieu Poirier <mathieu.poirier@linaro.org>
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "qemu/osdep.h"
10 #include "hw/qdev-properties.h"
11 #include "hw/virtio/vhost-user-rng.h"
12 #include "hw/virtio/virtio-pci.h"
13 
14 struct VHostUserRNGPCI {
15     VirtIOPCIProxy parent_obj;
16     VHostUserRNG vdev;
17 };
18 
19 typedef struct VHostUserRNGPCI VHostUserRNGPCI;
20 
21 #define TYPE_VHOST_USER_RNG_PCI "vhost-user-rng-pci-base"
22 
23 DECLARE_INSTANCE_CHECKER(VHostUserRNGPCI, VHOST_USER_RNG_PCI,
24                          TYPE_VHOST_USER_RNG_PCI)
25 
26 static Property vhost_user_rng_pci_properties[] = {
27     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
28                        DEV_NVECTORS_UNSPECIFIED),
29     DEFINE_PROP_END_OF_LIST(),
30 };
31 
32 static void vhost_user_rng_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
33 {
34     VHostUserRNGPCI *dev = VHOST_USER_RNG_PCI(vpci_dev);
35     DeviceState *vdev = DEVICE(&dev->vdev);
36 
37     if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
38         vpci_dev->nvectors = 1;
39     }
40 
41     qdev_realize(vdev, BUS(&vpci_dev->bus), errp);
42 }
43 
44 static void vhost_user_rng_pci_class_init(ObjectClass *klass, void *data)
45 {
46     DeviceClass *dc = DEVICE_CLASS(klass);
47     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
48     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
49     k->realize = vhost_user_rng_pci_realize;
50     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
51     device_class_set_props(dc, vhost_user_rng_pci_properties);
52     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
53     pcidev_k->device_id = 0; /* Set by virtio-pci based on virtio id */
54     pcidev_k->revision = 0x00;
55     pcidev_k->class_id = PCI_CLASS_OTHERS;
56 }
57 
58 static void vhost_user_rng_pci_instance_init(Object *obj)
59 {
60     VHostUserRNGPCI *dev = VHOST_USER_RNG_PCI(obj);
61 
62     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
63                                 TYPE_VHOST_USER_RNG);
64 }
65 
66 static const VirtioPCIDeviceTypeInfo vhost_user_rng_pci_info = {
67     .base_name = TYPE_VHOST_USER_RNG_PCI,
68     .non_transitional_name = "vhost-user-rng-pci",
69     .instance_size = sizeof(VHostUserRNGPCI),
70     .instance_init = vhost_user_rng_pci_instance_init,
71     .class_init = vhost_user_rng_pci_class_init,
72 };
73 
74 static void vhost_user_rng_pci_register(void)
75 {
76     virtio_pci_types_register(&vhost_user_rng_pci_info);
77 }
78 
79 type_init(vhost_user_rng_pci_register);
80