Hi On Thu, Jul 20, 2023 at 4:58 PM Emmanouil Pitsidianakis < manos.pitsidiana...@linaro.org> wrote:
> Add a new VIRTIO device for the virtio sound device id. Functionality > will be added in the following commits. > > Signed-off-by: Emmanouil Pitsidianakis <manos.pitsidiana...@linaro.org> > --- > MAINTAINERS | 6 + > hw/virtio/Kconfig | 5 + > hw/virtio/meson.build | 1 + > hw/virtio/trace-events | 9 ++ > hw/virtio/virtio-snd.c | 230 +++++++++++++++++++++++++++++++++ > include/hw/virtio/virtio-snd.h | 79 +++++++++++ > 6 files changed, 330 insertions(+) > create mode 100644 hw/virtio/virtio-snd.c > create mode 100644 include/hw/virtio/virtio-snd.h > > diff --git a/MAINTAINERS b/MAINTAINERS > index 12e59b6b27..2bed60f9c1 100644 > --- a/MAINTAINERS > +++ b/MAINTAINERS > @@ -2245,6 +2245,12 @@ F: hw/virtio/virtio-mem-pci.h > F: hw/virtio/virtio-mem-pci.c > F: include/hw/virtio/virtio-mem.h > > +virtio-snd > +M: Manos Pitsidianakis <manos.pitsidiana...@linaro.org> > +S: Supported > +F: hw/virtio/virtio-snd*.c > +F: include/hw/virtio/virtio-snd.h > + > nvme > M: Keith Busch <kbu...@kernel.org> > M: Klaus Jensen <i...@irrelevant.dk> > diff --git a/hw/virtio/Kconfig b/hw/virtio/Kconfig > index 92c9cf6c96..d6f20657b3 100644 > --- a/hw/virtio/Kconfig > +++ b/hw/virtio/Kconfig > @@ -17,6 +17,11 @@ config VIRTIO_PCI > depends on PCI > select VIRTIO > > +config VIRTIO_SND > + bool > + default y > + depends on VIRTIO > + > config VIRTIO_MMIO > bool > select VIRTIO > diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build > index 13e7c6c272..120d4bfa0a 100644 > --- a/hw/virtio/meson.build > +++ b/hw/virtio/meson.build > @@ -31,6 +31,7 @@ specific_virtio_ss.add(when: 'CONFIG_VHOST_VSOCK', > if_true: files('vhost-vsock.c > specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_VSOCK', if_true: > files('vhost-user-vsock.c')) > specific_virtio_ss.add(when: 'CONFIG_VIRTIO_RNG', if_true: > files('virtio-rng.c')) > specific_virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: > files('virtio-mem.c')) > +specific_virtio_ss.add(when: 'CONFIG_VIRTIO_SND', if_true: > files('virtio-snd.c')) > specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_I2C', if_true: > files('vhost-user-i2c.c')) > specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_RNG', if_true: > files('vhost-user-rng.c')) > specific_virtio_ss.add(when: 'CONFIG_VHOST_USER_GPIO', if_true: > files('vhost-user-gpio.c')) > diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events > index 7109cf1a3b..3ed7da35f2 100644 > --- a/hw/virtio/trace-events > +++ b/hw/virtio/trace-events > @@ -154,3 +154,12 @@ virtio_pmem_flush_done(int type) "fsync return=%d" > virtio_gpio_start(void) "start" > virtio_gpio_stop(void) "stop" > virtio_gpio_set_status(uint8_t status) "0x%x" > + > +#virtio-snd.c > +virtio_snd_get_config(void *vdev, uint32_t jacks, uint32_t streams, > uint32_t chmaps) "snd %p: get_config jacks=%"PRIu32" streams=%"PRIu32" > chmaps=%"PRIu32"" > +virtio_snd_set_config(void *vdev, uint32_t jacks, uint32_t new_jacks, > uint32_t streams, uint32_t new_streams, uint32_t chmaps, uint32_t > new_chmaps) "snd %p: set_config jacks from %"PRIu32"->%"PRIu32", streams > from %"PRIu32"->%"PRIu32", chmaps from %"PRIu32"->%"PRIu32 > +virtio_snd_get_features(void *vdev, uint64_t features) "snd %p: > get_features 0x%"PRIx64 > +virtio_snd_vm_state_running(void) "vm state running" > +virtio_snd_vm_state_stopped(void) "vm state stopped" > +virtio_snd_realize(void *snd) "snd %p: realize" > +virtio_snd_unrealize(void *snd) "snd %p: unrealize" > diff --git a/hw/virtio/virtio-snd.c b/hw/virtio/virtio-snd.c > new file mode 100644 > index 0000000000..e3f2156e5e > --- /dev/null > +++ b/hw/virtio/virtio-snd.c > @@ -0,0 +1,230 @@ > +/* > + * VIRTIO Sound Device conforming to > + * > + * "Virtual I/O Device (VIRTIO) Version 1.2 > + * Committee Specification Draft 01 > + * 09 May 2022" > + * > + * < > https://docs.oasis-open.org/virtio/virtio/v1.2/csd01/virtio-v1.2-csd01.html#x1-52900014 > > > + * > + * Copyright (c) 2023 Emmanouil Pitsidianakis < > manos.pitsidiana...@linaro.org> > + * Copyright (C) 2019 OpenSynergy GmbH > + * > + * 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 "qemu/iov.h" > +#include "qemu/log.h" > +#include "include/qemu/lockable.h" > +#include "sysemu/runstate.h" > +#include "trace.h" > +#include "qapi/error.h" > +#include "hw/virtio/virtio-snd.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 0 > + > +static const VMStateDescription vmstate_virtio_snd_device = { > + .name = TYPE_VIRTIO_SND, > + .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 void > +virtio_snd_get_config(VirtIODevice *vdev, uint8_t *config) > +{ > + VirtIOSound *s = VIRTIO_SND(vdev); > + trace_virtio_snd_get_config(vdev, > + s->snd_conf.jacks, > + s->snd_conf.streams, > + s->snd_conf.chmaps); > + > + memcpy(config, &s->snd_conf, sizeof(s->snd_conf)); > +} > + > +static void > +virtio_snd_set_config(VirtIODevice *vdev, const uint8_t *config) > +{ > + VirtIOSound *s = VIRTIO_SND(vdev); > + const virtio_snd_config *sndconfig = > + (const virtio_snd_config *)config; > + > + > + trace_virtio_snd_set_config(vdev, > + s->snd_conf.jacks, > + sndconfig->jacks, > + s->snd_conf.streams, > + sndconfig->streams, > + s->snd_conf.chmaps, > + sndconfig->chmaps); > + > + memcpy(&s->snd_conf, sndconfig, sizeof(s->snd_conf)); > +} > + > +/* > + * Queue handler stub. > + * > + * @vdev: VirtIOSound device > + * @vq: virtqueue > + */ > +static void virtio_snd_handle_queue(VirtIODevice *vdev, VirtQueue *vq) {} > + > +static uint64_t get_features(VirtIODevice *vdev, uint64_t features, > + Error **errp) > +{ > + /* > + * virtio-v1.2-csd01, 5.14.3, > + * Feature Bits > + * None currently defined. > + */ > + trace_virtio_snd_get_features(vdev, features); > + return features | 1UL << VIRTIO_F_VERSION_1 | 1UL << > VIRTIO_F_IN_ORDER; > ../hw/virtio/virtio-snd.c: In function 'get_features': ../hw/virtio/virtio-snd.c:883:27: error: left shift count >= width of type [-Werror=shift-count-overflow] 883 | return features | 1UL << VIRTIO_F_VERSION_1 | 1UL << VIRTIO_F_IN_ORDER; | ^~ ../hw/virtio/virtio-snd.c:883:55: error: left shift count >= width of type [-Werror=shift-count-overflow] 883 | return features | 1UL << VIRTIO_F_VERSION_1 | 1UL << VIRTIO_F_IN_ORDER; | ^~ -- Marc-André Lureau