Merge tag 'pull-request-2023-07-10v2' of https://gitlab.com/thuth/qemu into staging
[qemu/armbru.git] / hw / virtio / vdpa-dev-pci.c
blob5446e6b393ef3427d19a2f4cd9014c41fd21c11b
1 /*
2 * Vhost Vdpa Device PCI Bindings
4 * Copyright (c) Huawei Technologies Co., Ltd. 2022. All Rights Reserved.
6 * Authors:
7 * Longpeng <longpeng2@huawei.com>
9 * Largely based on the "vhost-user-blk-pci.c" and "vhost-user-blk.c"
10 * implemented by:
11 * Changpeng Liu <changpeng.liu@intel.com>
13 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
14 * See the COPYING.LIB file in the top-level directory.
16 #include "qemu/osdep.h"
17 #include <sys/ioctl.h>
18 #include <linux/vhost.h>
19 #include "hw/virtio/virtio.h"
20 #include "hw/virtio/vdpa-dev.h"
21 #include "hw/pci/pci.h"
22 #include "hw/qdev-properties.h"
23 #include "qapi/error.h"
24 #include "qemu/error-report.h"
25 #include "qemu/module.h"
26 #include "hw/virtio/virtio-pci.h"
27 #include "qom/object.h"
30 typedef struct VhostVdpaDevicePCI VhostVdpaDevicePCI;
32 #define TYPE_VHOST_VDPA_DEVICE_PCI "vhost-vdpa-device-pci-base"
33 DECLARE_INSTANCE_CHECKER(VhostVdpaDevicePCI, VHOST_VDPA_DEVICE_PCI,
34 TYPE_VHOST_VDPA_DEVICE_PCI)
36 struct VhostVdpaDevicePCI {
37 VirtIOPCIProxy parent_obj;
38 VhostVdpaDevice vdev;
41 static void vhost_vdpa_device_pci_instance_init(Object *obj)
43 VhostVdpaDevicePCI *dev = VHOST_VDPA_DEVICE_PCI(obj);
45 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
46 TYPE_VHOST_VDPA_DEVICE);
47 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
48 "bootindex");
51 static Property vhost_vdpa_device_pci_properties[] = {
52 DEFINE_PROP_END_OF_LIST(),
55 static int vhost_vdpa_device_pci_post_init(VhostVdpaDevice *v, Error **errp)
57 VhostVdpaDevicePCI *dev = container_of(v, VhostVdpaDevicePCI, vdev);
58 VirtIOPCIProxy *vpci_dev = &dev->parent_obj;
60 vpci_dev->class_code = virtio_pci_get_class_id(v->vdev_id);
61 vpci_dev->trans_devid = virtio_pci_get_trans_devid(v->vdev_id);
62 /* one for config vector */
63 vpci_dev->nvectors = v->num_queues + 1;
65 return 0;
68 static void
69 vhost_vdpa_device_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
71 VhostVdpaDevicePCI *dev = VHOST_VDPA_DEVICE_PCI(vpci_dev);
73 dev->vdev.post_init = vhost_vdpa_device_pci_post_init;
74 qdev_realize(DEVICE(&dev->vdev), BUS(&vpci_dev->bus), errp);
77 static void vhost_vdpa_device_pci_class_init(ObjectClass *klass, void *data)
79 DeviceClass *dc = DEVICE_CLASS(klass);
80 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
82 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
83 device_class_set_props(dc, vhost_vdpa_device_pci_properties);
84 k->realize = vhost_vdpa_device_pci_realize;
87 static const VirtioPCIDeviceTypeInfo vhost_vdpa_device_pci_info = {
88 .base_name = TYPE_VHOST_VDPA_DEVICE_PCI,
89 .generic_name = "vhost-vdpa-device-pci",
90 .transitional_name = "vhost-vdpa-device-pci-transitional",
91 .non_transitional_name = "vhost-vdpa-device-pci-non-transitional",
92 .instance_size = sizeof(VhostVdpaDevicePCI),
93 .instance_init = vhost_vdpa_device_pci_instance_init,
94 .class_init = vhost_vdpa_device_pci_class_init,
97 static void vhost_vdpa_device_pci_register(void)
99 virtio_pci_types_register(&vhost_vdpa_device_pci_info);
102 type_init(vhost_vdpa_device_pci_register);