xref: /openbmc/qemu/hw/audio/virtio-snd-pci.c (revision 7d87775f)
1 /*
2  * VIRTIO Sound Device PCI Bindings
3  *
4  * Copyright (c) 2023 Emmanouil Pitsidianakis <manos.pitsidianakis@linaro.org>
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or
7  * (at your option) any later version.  See the COPYING file in the
8  * top-level directory.
9  */
10 
11 #include "qemu/osdep.h"
12 #include "qom/object.h"
13 #include "qapi/error.h"
14 #include "hw/audio/soundhw.h"
15 #include "hw/virtio/virtio-pci.h"
16 #include "hw/audio/virtio-snd.h"
17 
18 /*
19  * virtio-snd-pci: This extends VirtioPCIProxy.
20  */
21 #define TYPE_VIRTIO_SND_PCI "virtio-sound-pci"
22 OBJECT_DECLARE_SIMPLE_TYPE(VirtIOSoundPCI, VIRTIO_SND_PCI)
23 
24 struct VirtIOSoundPCI {
25     VirtIOPCIProxy parent_obj;
26 
27     VirtIOSound vdev;
28 };
29 
30 static Property virtio_snd_pci_properties[] = {
31     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
32                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
33     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
34     DEFINE_PROP_END_OF_LIST(),
35 };
36 
37 static void virtio_snd_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
38 {
39     VirtIOSoundPCI *dev = VIRTIO_SND_PCI(vpci_dev);
40     DeviceState *vdev = DEVICE(&dev->vdev);
41 
42     virtio_pci_force_virtio_1(vpci_dev);
43     qdev_realize(vdev, BUS(&vpci_dev->bus), errp);
44 }
45 
46 static void virtio_snd_pci_class_init(ObjectClass *klass, void *data)
47 {
48     DeviceClass *dc = DEVICE_CLASS(klass);
49     VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
50     PCIDeviceClass *pcidevklass = PCI_DEVICE_CLASS(klass);
51 
52     device_class_set_props(dc, virtio_snd_pci_properties);
53     dc->desc = "Virtio Sound";
54     set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
55 
56     vpciklass->realize = virtio_snd_pci_realize;
57     pcidevklass->class_id = PCI_CLASS_MULTIMEDIA_AUDIO;
58 }
59 
60 static void virtio_snd_pci_instance_init(Object *obj)
61 {
62     VirtIOSoundPCI *dev = VIRTIO_SND_PCI(obj);
63 
64     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
65                                 TYPE_VIRTIO_SND);
66 }
67 
68 static const VirtioPCIDeviceTypeInfo virtio_snd_pci_info = {
69     .generic_name  = TYPE_VIRTIO_SND_PCI,
70     .instance_size = sizeof(VirtIOSoundPCI),
71     .instance_init = virtio_snd_pci_instance_init,
72     .class_init    = virtio_snd_pci_class_init,
73 };
74 
75 /* Create a Virtio Sound PCI device, so '-audio driver,model=virtio' works. */
76 static int virtio_snd_pci_init(PCIBus *bus, const char *audiodev)
77 {
78     DeviceState *vdev = NULL;
79     VirtIOSoundPCI *dev = NULL;
80 
81     vdev = qdev_new(TYPE_VIRTIO_SND_PCI);
82     assert(vdev);
83     dev = VIRTIO_SND_PCI(vdev);
84     qdev_prop_set_string(DEVICE(&dev->vdev), "audiodev", audiodev);
85     qdev_realize_and_unref(vdev, BUS(bus), &error_fatal);
86     return 0;
87 }
88 
89 static void virtio_snd_pci_register(void)
90 {
91     virtio_pci_types_register(&virtio_snd_pci_info);
92     pci_register_soundhw("virtio", "Virtio Sound", virtio_snd_pci_init);
93 }
94 
95 type_init(virtio_snd_pci_register);
96