Message ID | db527bc9a1fdb2199a264ffd0d2526ce709a745c.1686238728.git.manos.pitsidianakis@linaro.org (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | Add VIRTIO sound card | expand |
Manos Pitsidianakis <manos.pitsidianakis@linaro.org> writes: > This patch adds a PCI wrapper device for the virtio-sound device. > > To test this, you'll need a >=5.13 kernel compiled with > CONFIG_SND_VIRTIO=y, which at the time of writing most distros have off > by default. > > Use with following flags in the invocation: > > -device virtio-sound-pci,disable-legacy=on > > And an audio backend listed with `-audio driver=help` that works on > your host machine, e.g.: > > Pulseaudio: > -audio driver=pa,model=virtio-sound > sdl: > -audio driver=sdl,model=virtio-sound > coreaudio (macos/darwin): > -audio driver=coreaudio,model=virtio-sound > etc. > > You can use speaker-test from alsa-tools to play noise, sines, or > WAV files. > > Signed-off-by: Emmanouil Pitsidianakis <manos.pitsidianakis@linaro.org> > --- > --- > hw/virtio/meson.build | 1 + > hw/virtio/virtio-snd-pci.c | 102 +++++++++++++++++++++++++++++++++++++ > include/hw/pci/pci.h | 1 + > softmmu/qdev-monitor.c | 1 + > 4 files changed, 105 insertions(+) > create mode 100644 hw/virtio/virtio-snd-pci.c > > diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build > index 8cf49af618..55d9426415 100644 > --- a/hw/virtio/meson.build > +++ b/hw/virtio/meson.build > @@ -58,6 +58,7 @@ virtio_pci_ss.add(when: 'CONFIG_VIRTIO_SERIAL', if_true: files('virtio-serial-pc > virtio_pci_ss.add(when: 'CONFIG_VIRTIO_PMEM', if_true: files('virtio-pmem-pci.c')) > virtio_pci_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files('virtio-iommu-pci.c')) > virtio_pci_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem-pci.c')) > +virtio_pci_ss.add(when: 'CONFIG_VIRTIO_SND', if_true: files('virtio-snd-pci.c')) > virtio_pci_ss.add(when: 'CONFIG_VHOST_VDPA_DEV', if_true: files('vdpa-dev-pci.c')) > > specific_virtio_ss.add_all(when: 'CONFIG_VIRTIO_PCI', if_true: virtio_pci_ss) > diff --git a/hw/virtio/virtio-snd-pci.c b/hw/virtio/virtio-snd-pci.c > new file mode 100644 > index 0000000000..1a41b53e07 > --- /dev/null > +++ b/hw/virtio/virtio-snd-pci.c > @@ -0,0 +1,102 @@ > +/* > + * VIRTIO Sound Device PCI Bindings > + * > + * Copyright (c) 2023 Emmanouil Pitsidianakis <manos.pitsidianakis@linaro.org> > + * > + * This work is licensed under the terms of the GNU GPL, version 2 or > + * (at your option) any later version. See the COPYING file in the > + * top-level directory. > + */ > + > +#include "qemu/osdep.h" > +#include "hw/audio/soundhw.h" > +#include "hw/virtio/virtio-pci.h" > +#include "hw/virtio/virtio-snd.h" > + > +typedef struct VirtIOSoundPCI VirtIOSoundPCI; > + > +/* > + * virtio-snd-pci: This extends VirtioPCIProxy. > + */ > +#define TYPE_VIRTIO_SND_PCI "virtio-sound-pci-base" > +DECLARE_INSTANCE_CHECKER(VirtIOSoundPCI, VIRTIO_SOUND_PCI, > + TYPE_VIRTIO_SND_PCI) > + > +struct VirtIOSoundPCI { > + VirtIOPCIProxy parent; > + VirtIOSound vdev; > +}; > + > +static Property virtio_snd_pci_properties[] = { > + DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), What does this control? In VirtIOPCIProxy class_code seems to be for transitional devices. It's used inconsistently across the code base AFAICT. > + DEFINE_PROP_END_OF_LIST(), > +}; > + > +static const char *audiodev_id; > + > +static int virtio_snd_init_pci(PCIBus *init_bus, const char *audiodev) > +{ > + audiodev_id = audiodev; > + return 0; > +} > + > +static void virtio_snd_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) > +{ > + VirtIOSoundPCI *dev = VIRTIO_SOUND_PCI(vpci_dev); > + DeviceState *vdev = DEVICE(&dev->vdev); > + VirtIOSound *vsnd = VIRTIO_SND(&dev->vdev); > + > + /* > + * According to spec, non-legacy virtio PCI devices are always little > + * endian > + */ > + vsnd->virtio_access_is_big_endian = false; > + > + > + qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus), errp); > + > + qdev_prop_set_string(vdev, "audiodev", audiodev_id); > + > + object_property_set_bool(OBJECT(vdev), "realized", true, errp); > +} > + > +static void virtio_snd_pci_class_init(ObjectClass *klass, void *data) > +{ > + DeviceClass *dc = DEVICE_CLASS(klass); > + VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass); > + PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); > + > + vpciklass->realize = virtio_snd_pci_realize; > + set_bit(DEVICE_CATEGORY_SOUND, dc->categories); > + > + pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; > + pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SND; > + pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; > + pcidev_k->class_id = PCI_CLASS_MULTIMEDIA_AUDIO; > + device_class_set_props(dc, virtio_snd_pci_properties); > +} > + > +static void virtio_snd_pci_instance_init(Object *obj) > +{ > + VirtIOSoundPCI *dev = VIRTIO_SOUND_PCI(obj); > + > + virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), > + TYPE_VIRTIO_SND); > +} > + > +static const VirtioPCIDeviceTypeInfo virtio_snd_pci_info = { > + .base_name = TYPE_VIRTIO_SND_PCI, > + .generic_name = "virtio-sound-pci", > + .instance_size = sizeof(VirtIOSoundPCI), > + .instance_init = virtio_snd_pci_instance_init, > + .class_init = virtio_snd_pci_class_init, > +}; > + > +static void virtio_snd_pci_register(void) > +{ > + virtio_pci_types_register(&virtio_snd_pci_info); > + pci_register_soundhw("virtio-sound", "Virtio Sound Device", > + virtio_snd_init_pci); > +} > + > +type_init(virtio_snd_pci_register); > diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h > index e6d0574a29..cf4216df81 100644 > --- a/include/hw/pci/pci.h > +++ b/include/hw/pci/pci.h > @@ -85,6 +85,7 @@ extern bool pci_available; > #define PCI_DEVICE_ID_VIRTIO_RNG 0x1005 > #define PCI_DEVICE_ID_VIRTIO_9P 0x1009 > #define PCI_DEVICE_ID_VIRTIO_VSOCK 0x1012 > +#define PCI_DEVICE_ID_VIRTIO_SND 0x1019 > > /* > * modern virtio-pci devices get their id assigned automatically, > diff --git a/softmmu/qdev-monitor.c b/softmmu/qdev-monitor.c > index b8d2c4dadd..49d68495a3 100644 > --- a/softmmu/qdev-monitor.c > +++ b/softmmu/qdev-monitor.c > @@ -108,6 +108,7 @@ static const QDevAlias qdev_alias_table[] = { > { "virtio-serial-device", "virtio-serial", QEMU_ARCH_VIRTIO_MMIO }, > { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_VIRTIO_CCW }, > { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_VIRTIO_PCI}, > + { "virtio-sound-pci", "virtio-sound", QEMU_ARCH_VIRTIO_PCI}, > { "virtio-tablet-device", "virtio-tablet", QEMU_ARCH_VIRTIO_MMIO }, > { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_VIRTIO_CCW }, > { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_VIRTIO_PCI },
diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build index 8cf49af618..55d9426415 100644 --- a/hw/virtio/meson.build +++ b/hw/virtio/meson.build @@ -58,6 +58,7 @@ virtio_pci_ss.add(when: 'CONFIG_VIRTIO_SERIAL', if_true: files('virtio-serial-pc virtio_pci_ss.add(when: 'CONFIG_VIRTIO_PMEM', if_true: files('virtio-pmem-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files('virtio-iommu-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem-pci.c')) +virtio_pci_ss.add(when: 'CONFIG_VIRTIO_SND', if_true: files('virtio-snd-pci.c')) virtio_pci_ss.add(when: 'CONFIG_VHOST_VDPA_DEV', if_true: files('vdpa-dev-pci.c')) specific_virtio_ss.add_all(when: 'CONFIG_VIRTIO_PCI', if_true: virtio_pci_ss) diff --git a/hw/virtio/virtio-snd-pci.c b/hw/virtio/virtio-snd-pci.c new file mode 100644 index 0000000000..1a41b53e07 --- /dev/null +++ b/hw/virtio/virtio-snd-pci.c @@ -0,0 +1,102 @@ +/* + * VIRTIO Sound Device PCI Bindings + * + * Copyright (c) 2023 Emmanouil Pitsidianakis <manos.pitsidianakis@linaro.org> + * + * This work is licensed under the terms of the GNU GPL, version 2 or + * (at your option) any later version. See the COPYING file in the + * top-level directory. + */ + +#include "qemu/osdep.h" +#include "hw/audio/soundhw.h" +#include "hw/virtio/virtio-pci.h" +#include "hw/virtio/virtio-snd.h" + +typedef struct VirtIOSoundPCI VirtIOSoundPCI; + +/* + * virtio-snd-pci: This extends VirtioPCIProxy. + */ +#define TYPE_VIRTIO_SND_PCI "virtio-sound-pci-base" +DECLARE_INSTANCE_CHECKER(VirtIOSoundPCI, VIRTIO_SOUND_PCI, + TYPE_VIRTIO_SND_PCI) + +struct VirtIOSoundPCI { + VirtIOPCIProxy parent; + VirtIOSound vdev; +}; + +static Property virtio_snd_pci_properties[] = { + DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), + DEFINE_PROP_END_OF_LIST(), +}; + +static const char *audiodev_id; + +static int virtio_snd_init_pci(PCIBus *init_bus, const char *audiodev) +{ + audiodev_id = audiodev; + return 0; +} + +static void virtio_snd_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) +{ + VirtIOSoundPCI *dev = VIRTIO_SOUND_PCI(vpci_dev); + DeviceState *vdev = DEVICE(&dev->vdev); + VirtIOSound *vsnd = VIRTIO_SND(&dev->vdev); + + /* + * According to spec, non-legacy virtio PCI devices are always little + * endian + */ + vsnd->virtio_access_is_big_endian = false; + + + qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus), errp); + + qdev_prop_set_string(vdev, "audiodev", audiodev_id); + + object_property_set_bool(OBJECT(vdev), "realized", true, errp); +} + +static void virtio_snd_pci_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass); + PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); + + vpciklass->realize = virtio_snd_pci_realize; + set_bit(DEVICE_CATEGORY_SOUND, dc->categories); + + pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; + pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SND; + pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; + pcidev_k->class_id = PCI_CLASS_MULTIMEDIA_AUDIO; + device_class_set_props(dc, virtio_snd_pci_properties); +} + +static void virtio_snd_pci_instance_init(Object *obj) +{ + VirtIOSoundPCI *dev = VIRTIO_SOUND_PCI(obj); + + virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), + TYPE_VIRTIO_SND); +} + +static const VirtioPCIDeviceTypeInfo virtio_snd_pci_info = { + .base_name = TYPE_VIRTIO_SND_PCI, + .generic_name = "virtio-sound-pci", + .instance_size = sizeof(VirtIOSoundPCI), + .instance_init = virtio_snd_pci_instance_init, + .class_init = virtio_snd_pci_class_init, +}; + +static void virtio_snd_pci_register(void) +{ + virtio_pci_types_register(&virtio_snd_pci_info); + pci_register_soundhw("virtio-sound", "Virtio Sound Device", + virtio_snd_init_pci); +} + +type_init(virtio_snd_pci_register); diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h index e6d0574a29..cf4216df81 100644 --- a/include/hw/pci/pci.h +++ b/include/hw/pci/pci.h @@ -85,6 +85,7 @@ extern bool pci_available; #define PCI_DEVICE_ID_VIRTIO_RNG 0x1005 #define PCI_DEVICE_ID_VIRTIO_9P 0x1009 #define PCI_DEVICE_ID_VIRTIO_VSOCK 0x1012 +#define PCI_DEVICE_ID_VIRTIO_SND 0x1019 /* * modern virtio-pci devices get their id assigned automatically, diff --git a/softmmu/qdev-monitor.c b/softmmu/qdev-monitor.c index b8d2c4dadd..49d68495a3 100644 --- a/softmmu/qdev-monitor.c +++ b/softmmu/qdev-monitor.c @@ -108,6 +108,7 @@ static const QDevAlias qdev_alias_table[] = { { "virtio-serial-device", "virtio-serial", QEMU_ARCH_VIRTIO_MMIO }, { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_VIRTIO_CCW }, { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_VIRTIO_PCI}, + { "virtio-sound-pci", "virtio-sound", QEMU_ARCH_VIRTIO_PCI}, { "virtio-tablet-device", "virtio-tablet", QEMU_ARCH_VIRTIO_MMIO }, { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_VIRTIO_CCW }, { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_VIRTIO_PCI },
This patch adds a PCI wrapper device for the virtio-sound device. To test this, you'll need a >=5.13 kernel compiled with CONFIG_SND_VIRTIO=y, which at the time of writing most distros have off by default. Use with following flags in the invocation: -device virtio-sound-pci,disable-legacy=on And an audio backend listed with `-audio driver=help` that works on your host machine, e.g.: Pulseaudio: -audio driver=pa,model=virtio-sound sdl: -audio driver=sdl,model=virtio-sound coreaudio (macos/darwin): -audio driver=coreaudio,model=virtio-sound etc. You can use speaker-test from alsa-tools to play noise, sines, or WAV files. Signed-off-by: Emmanouil Pitsidianakis <manos.pitsidianakis@linaro.org> --- --- hw/virtio/meson.build | 1 + hw/virtio/virtio-snd-pci.c | 102 +++++++++++++++++++++++++++++++++++++ include/hw/pci/pci.h | 1 + softmmu/qdev-monitor.c | 1 + 4 files changed, 105 insertions(+) create mode 100644 hw/virtio/virtio-snd-pci.c