1 /*
2  * Vhost-user generic virtio device PCI glue
3  *
4  * Copyright (c) 2023 Linaro Ltd
5  * Author: Alex Bennée <alex.bennee@linaro.org>
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9 
10 #include "qemu/osdep.h"
11 #include "hw/qdev-properties.h"
12 #include "hw/virtio/vhost-user-base.h"
13 #include "hw/virtio/virtio-pci.h"
14 
15 struct VHostUserDevicePCI {
16     VirtIOPCIProxy parent_obj;
17 
18     VHostUserBase vub;
19 };
20 
21 #define TYPE_VHOST_USER_DEVICE_PCI "vhost-user-device-pci-base"
22 
23 OBJECT_DECLARE_SIMPLE_TYPE(VHostUserDevicePCI, VHOST_USER_DEVICE_PCI)
24 
25 static void vhost_user_device_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
26 {
27     VHostUserDevicePCI *dev = VHOST_USER_DEVICE_PCI(vpci_dev);
28     DeviceState *vdev = DEVICE(&dev->vub);
29 
30     vpci_dev->nvectors = 1;
31     qdev_realize(vdev, BUS(&vpci_dev->bus), errp);
32 }
33 
34 static void vhost_user_device_pci_class_init(ObjectClass *klass, void *data)
35 {
36     DeviceClass *dc = DEVICE_CLASS(klass);
37     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
38     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
39 
40     /* Reason: stop users confusing themselves */
41     dc->user_creatable = false;
42 
43     k->realize = vhost_user_device_pci_realize;
44     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
45     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
46     pcidev_k->device_id = 0; /* Set by virtio-pci based on virtio id */
47     pcidev_k->revision = 0x00;
48     pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
49 }
50 
51 static void vhost_user_device_pci_instance_init(Object *obj)
52 {
53     VHostUserDevicePCI *dev = VHOST_USER_DEVICE_PCI(obj);
54 
55     virtio_instance_init_common(obj, &dev->vub, sizeof(dev->vub),
56                                 TYPE_VHOST_USER_DEVICE);
57 }
58 
59 static const VirtioPCIDeviceTypeInfo vhost_user_device_pci_info = {
60     .base_name = TYPE_VHOST_USER_DEVICE_PCI,
61     .non_transitional_name = "vhost-user-device-pci",
62     .instance_size = sizeof(VHostUserDevicePCI),
63     .instance_init = vhost_user_device_pci_instance_init,
64     .class_init = vhost_user_device_pci_class_init,
65 };
66 
67 static void vhost_user_device_pci_register(void)
68 {
69     virtio_pci_types_register(&vhost_user_device_pci_info);
70 }
71 
72 type_init(vhost_user_device_pci_register);
73