[RFC PATCH 07/27] virtio-snd: Add properties for class init

Shreyansh Chouhan posted 27 patches 4 years, 9 months ago
Maintainers: Gerd Hoffmann <kraxel@redhat.com>, "Michael S. Tsirkin" <mst@redhat.com>
[RFC PATCH 07/27] virtio-snd: Add properties for class init
Posted by Shreyansh Chouhan 4 years, 9 months ago
Added properties and function stubs for virtio sound device class
init.

Signed-off-by: Shreyansh Chouhan <chouhan.shreyansh2702@gmail.com>
---
 hw/audio/Kconfig      |   5 ++
 hw/audio/meson.build  |   1 +
 hw/audio/virtio-snd.c | 126 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 132 insertions(+)
 create mode 100644 hw/audio/virtio-snd.c

diff --git a/hw/audio/Kconfig b/hw/audio/Kconfig
index e9c6fed826..00b4e1ca88 100644
--- a/hw/audio/Kconfig
+++ b/hw/audio/Kconfig
@@ -50,3 +50,8 @@ config CS4231
 
 config MARVELL_88W8618
     bool
+
+config VIRTIO_SND
+    bool
+    default y if PCI_DEVICES
+    depends on PCI
diff --git a/hw/audio/meson.build b/hw/audio/meson.build
index 32c42bdebe..8c7b8a1e46 100644
--- a/hw/audio/meson.build
+++ b/hw/audio/meson.build
@@ -13,3 +13,4 @@ softmmu_ss.add(when: 'CONFIG_PL041', if_true: files('pl041.c', 'lm4549.c'))
 softmmu_ss.add(when: 'CONFIG_SB16', if_true: files('sb16.c'))
 softmmu_ss.add(when: 'CONFIG_VT82C686', if_true: files('via-ac97.c'))
 softmmu_ss.add(when: 'CONFIG_WM8750', if_true: files('wm8750.c'))
+softmmu_ss.add(when: 'CONFIG_VIRTIO_SND', if_true: files('virtio-snd.c'))
diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c
new file mode 100644
index 0000000000..ae438aa7ec
--- /dev/null
+++ b/hw/audio/virtio-snd.c
@@ -0,0 +1,126 @@
+/*
+ * Virtio Sound device
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/atomic.h"
+#include "qemu/iov.h"
+#include "qemu/main-loop.h"
+#include "qemu/module.h"
+#include "hw/virtio/virtio.h"
+#include "audio/audio.h"
+#include "qemu/error-report.h"
+#include "qemu/timer.h"
+#include "qemu/option.h"
+#include "qemu/option_int.h"
+#include "qemu/config-file.h"
+#include "qapi/qmp/qdict.h"
+#include "hw/virtio/virtio-snd.h"
+#include "hw/virtio/virtio-bus.h"
+#include "qapi/error.h"
+#include "qapi/qapi-events-audio.h"
+#include "hw/qdev-properties.h"
+#include "qapi/qapi-types-migration.h"
+#include "qapi/qapi-events-migration.h"
+#include "migration/misc.h"
+#include "standard-headers/linux/ethtool.h"
+#include "sysemu/sysemu.h"
+#include "trace.h"
+#include "monitor/qdev.h"
+#include "hw/pci/pci.h"
+#include "intel-hda-defs.h"
+
+#define VIRTIO_SOUND_VM_VERSION 1
+
+#define VIRTIO_SOUND_JACK_DEFAULT 0
+#define VIRTIO_SOUND_STREAM_DEFAULT 1
+#define VIRTIO_SOUND_CHMAP_DEFAULT 0
+
+#define VIRTIO_SOUND_HDA_FN_NID_OUT 0
+#define VIRTIO_SOUND_HDA_FN_NID_IN 1
+
+static void virtio_snd_get_config(VirtIODevice *vdev, uint8_t *config)
+{
+}
+
+static void virtio_snd_set_config(VirtIODevice *vdev, const uint8_t *config)
+{
+}
+
+static const VMStateDescription vmstate_virtio_snd_device = {
+    .name = "virtio-snd-device",
+    .version_id = VIRTIO_SOUND_VM_VERSION,
+    .minimum_version_id = VIRTIO_SOUND_VM_VERSION,
+};
+
+static const VMStateDescription vmstate_virtio_snd = {
+    .name = "virtio-sound",
+    .minimum_version_id = VIRTIO_SOUND_VM_VERSION,
+    .version_id = VIRTIO_SOUND_VM_VERSION,
+    .fields = (VMStateField[]) {
+        VMSTATE_VIRTIO_DEVICE,
+        VMSTATE_END_OF_LIST()
+    },
+};
+
+static Property virtio_snd_properties[] = {
+    DEFINE_AUDIO_PROPERTIES(VirtIOSound, card),
+    DEFINE_PROP_UINT32("jacks", VirtIOSound, snd_conf.jacks,
+                       VIRTIO_SOUND_JACK_DEFAULT),
+    DEFINE_PROP_UINT32("streams", VirtIOSound, snd_conf.streams,
+                       VIRTIO_SOUND_STREAM_DEFAULT),
+    DEFINE_PROP_UINT32("chmaps", VirtIOSound, snd_conf.chmaps,
+                       VIRTIO_SOUND_CHMAP_DEFAULT),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static uint64_t virtio_snd_get_features(VirtIODevice *vdev, uint64_t features,
+                                        Error **errp)
+{
+    return vdev->host_features;
+}
+
+static void virtio_snd_device_realize(DeviceState *dev, Error **errp)
+{
+}
+
+static void virtio_snd_device_unrealize(DeviceState *dev)
+{
+}
+
+static void virtio_snd_reset(VirtIODevice *vdev)
+{
+}
+
+static void virtio_snd_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
+
+    device_class_set_props(dc, virtio_snd_properties);
+    dc->vmsd = &vmstate_virtio_snd;
+    set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
+    vdc->realize = virtio_snd_device_realize;
+    vdc->unrealize = virtio_snd_device_unrealize;
+    vdc->get_config = virtio_snd_get_config;
+    vdc->set_config = virtio_snd_set_config;
+    vdc->get_features = virtio_snd_get_features;
+    vdc->reset = virtio_snd_reset;
+    vdc->legacy_features = 0;
+    vdc->vmsd = &vmstate_virtio_snd_device;
+}
+
+
+static const TypeInfo virtio_snd_dev_info = {
+    .name = TYPE_VIRTIO_SOUND,
+    .parent = TYPE_VIRTIO_DEVICE,
+    .instance_size = sizeof(VirtIOSound),
+    .class_init = virtio_snd_class_init,
+};
+
+static void virtio_register_types(void)
+{
+    type_register_static(&virtio_snd_dev_info);
+}
+
+type_init(virtio_register_types)
-- 
2.25.1


Re: [RFC PATCH 07/27] virtio-snd: Add properties for class init
Posted by Laurent Vivier 4 years, 9 months ago
Le 29/04/2021 à 14:04, Shreyansh Chouhan a écrit :
> Added properties and function stubs for virtio sound device class
> init.
>
> Signed-off-by: Shreyansh Chouhan <chouhan.shreyansh2702@gmail.com>
> ---
>  hw/audio/Kconfig      |   5 ++
>  hw/audio/meson.build  |   1 +
>  hw/audio/virtio-snd.c | 126 ++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 132 insertions(+)
>  create mode 100644 hw/audio/virtio-snd.c
>
> diff --git a/hw/audio/Kconfig b/hw/audio/Kconfig
> index e9c6fed826..00b4e1ca88 100644
> --- a/hw/audio/Kconfig
> +++ b/hw/audio/Kconfig
> @@ -50,3 +50,8 @@ config CS4231
>  
>  config MARVELL_88W8618
>      bool
> +
> +config VIRTIO_SND
> +    bool
> +    default y if PCI_DEVICES
> +    depends on PCI

There is nothing specific to PCI in that code, why do you prevent the use of virtio-snd as a MMIO
device?

Thanks,

Laurent


Re: [RFC PATCH 07/27] virtio-snd: Add properties for class init
Posted by Shreyansh Chouhan 4 years, 9 months ago
On Tue, 4 May 2021 at 19:02, Laurent Vivier <laurent@vivier.eu> wrote:

> There is nothing specific to PCI in that code, why do you prevent the use
> of virtio-snd as a MMIO
> device?
>
> I am sorry I do not understand your question completely. If by preventing
the use of virtio-snd, you mean
why did I add the PCI dependencies to the Kconfig file, then I think I must
have been a bit confused
while writing it. VIRTIO_PCI already includes those dependencies, I will
change the dependency to
VIRTIO. (Which is what it is for other virtio devices too.)

However if you mean why did I not add an MMIO binding for this device, then
there is no
specific reason. I simply followed what QEMU had been doing for the other
virtio devices.
Will there be any advantages to implementing the device as a MMIO device?

> Thanks,
>
> Laurent
>
> --
Regards
Shreyansh
Re: [RFC PATCH 07/27] virtio-snd: Add properties for class init
Posted by Laurent Vivier 4 years, 9 months ago
Hi Shreyansh,

First of all, thank you for your work, I was expecting a virtio sound device for some time...

Le 04/05/2021 à 21:35, Shreyansh Chouhan a écrit :
> On Tue, 4 May 2021 at 19:02, Laurent Vivier <laurent@vivier.eu <mailto:laurent@vivier.eu>> wrote:
> 
>     There is nothing specific to PCI in that code, why do you prevent the use of virtio-snd as a MMIO
>     device?
> 
> I am sorry I do not understand your question completely. If by preventing the use of virtio-snd, you
> mean
> why did I add the PCI dependencies to the Kconfig file, then I think I must have been a bit confused
> while writing it. VIRTIO_PCI already includes those dependencies, I will change the dependency to
> VIRTIO. (Which is what it is for other virtio devices too.)
> 
> However if you mean why did I not add an MMIO binding for this device, then there is no
> specific reason. I simply followed what QEMU had been doing for the other virtio devices.
> Will there be any advantages to implementing the device as a MMIO device? 

No, the question was only about the dependencies, generally a a virtio device is binded to a virtio
bus, and virtio PCI is a PCI card providing a virtio bus with the virtio device attached to it.

For instance, for virtio-net-pci:

HOST

  -> PCI Host controller

    -> PCI virtio net device (TYPE_VIRTIO_NET_PCI)

      -> virtio Bus (TYPE_VIRTIO_BUS)

        -> virtio net device (TYPE_VIRTIO_NET)

TYPE_VIRTIO_NET_PCI is created by hw/virtio/virtio-net-pci.c and TYPE_VIRTIO_NET by hw/net/vrtio-net.c:

hw/virtio/meson.build:

virtio_pci_ss.add(when: 'CONFIG_VIRTIO_NET', if_true: files('virtio-net-pci.c'))
virtio_ss.add_all(when: 'CONFIG_VIRTIO_PCI', if_true: virtio_pci_ss)

hw/net/meson.build
specific_ss.add(when: 'CONFIG_VIRTIO_NET', if_true: files('virtio-net.c'))

hw/net/Kconfig:

config VIRTIO_NET
    bool
    default y
    depends on VIRTIO

So:

the virtio-net device is built when VIRTIO_NET is set,
the virtio-net-pci device is build when VIRTIO_NET and VIRTIO_PCI are set.

So what I expect for virtio-snd:

hw/virtio/meson.build:

virtio_pci_ss.add(when: 'CONFIG_VIRTIO_SND', if_true: files('virtio-snd-pci.c'))

hw/audio/meson.build:

softmmu_ss.add(when: 'CONFIG_VIRTIO_SND', if_true: files('virtio-snd.c'))

hw/audio/Kconfig

config VIRTIO_SND
    bool
    default y
    depends on VIRTIO

With that kind of config, a machine without PCI bus will be able to create a virtio bus to add your
virtio device (like s390x with virtio-ccw of any other MMIO machine like the virt machines).

In short: update your hw/audio/config, and all will be fine.

Thanks,
Laurent

Re: [RFC PATCH 07/27] virtio-snd: Add properties for class init
Posted by Shreyansh Chouhan 4 years, 9 months ago
On Wed, 5 May 2021 at 02:00, Laurent Vivier <laurent@vivier.eu> wrote:

> Hi Shreyansh,
>
> First of all, thank you for your work, I was expecting a virtio sound
> device for some time...
>
> You're welcome :)

> Le 04/05/2021 à 21:35, Shreyansh Chouhan a écrit :
> > On Tue, 4 May 2021 at 19:02, Laurent Vivier <laurent@vivier.eu <mailto:
> laurent@vivier.eu>> wrote:
> >
> >     There is nothing specific to PCI in that code, why do you prevent
> the use of virtio-snd as a MMIO
> >     device?
> >
> > I am sorry I do not understand your question completely. If by
> preventing the use of virtio-snd, you
> > mean
> > why did I add the PCI dependencies to the Kconfig file, then I think I
> must have been a bit confused
> > while writing it. VIRTIO_PCI already includes those dependencies, I will
> change the dependency to
> > VIRTIO. (Which is what it is for other virtio devices too.)
> >
> > However if you mean why did I not add an MMIO binding for this device,
> then there is no
> > specific reason. I simply followed what QEMU had been doing for the
> other virtio devices.
> > Will there be any advantages to implementing the device as a MMIO
> device?
>
> No, the question was only about the dependencies, generally a a virtio
> device is binded to a virtio
> bus, and virtio PCI is a PCI card providing a virtio bus with the virtio
> device attached to it.
>
> For instance, for virtio-net-pci:
>
> HOST
>
>   -> PCI Host controller
>
>     -> PCI virtio net device (TYPE_VIRTIO_NET_PCI)
>
>       -> virtio Bus (TYPE_VIRTIO_BUS)
>
>         -> virtio net device (TYPE_VIRTIO_NET)
>
> TYPE_VIRTIO_NET_PCI is created by hw/virtio/virtio-net-pci.c and
> TYPE_VIRTIO_NET by hw/net/vrtio-net.c:
>
> hw/virtio/meson.build:
>
> virtio_pci_ss.add(when: 'CONFIG_VIRTIO_NET', if_true:
> files('virtio-net-pci.c'))
> virtio_ss.add_all(when: 'CONFIG_VIRTIO_PCI', if_true: virtio_pci_ss)
>
> hw/net/meson.build
> specific_ss.add(when: 'CONFIG_VIRTIO_NET', if_true: files('virtio-net.c'))
>
> hw/net/Kconfig:
>
> config VIRTIO_NET
>     bool
>     default y
>     depends on VIRTIO
>
> So:
>
> the virtio-net device is built when VIRTIO_NET is set,
> the virtio-net-pci device is build when VIRTIO_NET and VIRTIO_PCI are set.
>
> So what I expect for virtio-snd:
>
> hw/virtio/meson.build:
>
> virtio_pci_ss.add(when: 'CONFIG_VIRTIO_SND', if_true:
> files('virtio-snd-pci.c'))
>
> hw/audio/meson.build:
>
> softmmu_ss.add(when: 'CONFIG_VIRTIO_SND', if_true: files('virtio-snd.c'))
>
> hw/audio/Kconfig
>
> config VIRTIO_SND
>     bool
>     default y
>     depends on VIRTIO
>
> With that kind of config, a machine without PCI bus will be able to create
> a virtio bus to add your
> virtio device (like s390x with virtio-ccw of any other MMIO machine like
> the virt machines).
>
> Thanks a lot for the detailed explanation. It clarifies everything
regarding the separate
pci and device source files and how they are built, which was confusing me
a little bit.
I will fix the meson.build and Kconfig files in the upcoming versions of
these patches.

In short: update your hw/audio/config, and all will be fine.


>
Thanks,
> Laurent
>
--
Thanks
Shreyansh