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