Skip to contentSkip to conversation

Add VIRTIO sound card

PATCH Superseded Emmanouil Pitsidianakis 4 participants 22 messages Aug 15, 2023 – Aug 22, 2023 Arm/Amlogic Meson SoC support

v6 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v6 cover Add VIRTIO sound card

This patch series adds an audio device implementing the recent virtio sound spec (1.2) and a corresponding PCI wrapper device.

https://github.com/epilys/qemu-virtio-snd/tree/virtio-snd-v6

Main differences with v5 patch series [^v5] <[email protected]>:

- Free any existing PCM stream resources before allocating a new one.
- Add docs.

[^v5]:
https://lore.kernel.org/qemu-devel/[email protected]/

Previously:

[^v4]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^v3]:
https://lore.kernel.org/qemu-devel/[email protected]/

Emmanouil Pitsidianakis (12):
Add virtio-sound device stub
Add virtio-sound-pci device
virtio-sound: handle control messages and streams
virtio-sound: set PCM stream parameters
virtio-sound: handle VIRTIO_SND_R_PCM_INFO request
virtio-sound: handle VIRTIO_SND_R_PCM_{START,STOP}
virtio-sound: handle VIRTIO_SND_PCM_SET_PARAMS
virtio-sound: handle VIRTIO_SND_R_PCM_PREPARE
virtio-sound: handle VIRTIO_SND_PCM_RELEASE
virtio-sound: implement audio output (TX)
virtio-sound: implement audio capture (RX) docs/system: add basic virtio-snd documentation

 MAINTAINERS                        |    6 +
 docs/system/device-emulation.rst   |    1 +
 docs/system/devices/virtio-snd.rst |   36 +
 hw/virtio/Kconfig                  |    5 +
 hw/virtio/meson.build              |    2 +
 hw/virtio/trace-events             |   20 +
 hw/virtio/virtio-snd-pci.c         |   91 ++
 hw/virtio/virtio-snd.c             | 1308 ++++++++++++++++++++++++++++
 include/hw/pci/pci.h               |    1 +
 include/hw/virtio/virtio-snd.h     |  158 ++++
 softmmu/qdev-monitor.c             |    1 +
 11 files changed, 1629 insertions(+)
 create mode 100644 docs/system/devices/virtio-snd.rst
 create mode 100644 hw/virtio/virtio-snd-pci.c
 create mode 100644 hw/virtio/virtio-snd.c
 create mode 100644 include/hw/virtio/virtio-snd.h

Range-diff against v5:

 1:  899b9a06bb =  1:  86f98e0b7d Add virtio-sound device stub
 2:  035be510f8 =  2:  8f996f9aed Add virtio-sound-pci device
 3:  e6a3624f89 !  3:  66eac6c1e0 virtio-sound: handle control messages and streams
    @@ hw/virtio/virtio-snd.c: static void virtio_snd_realize(DeviceState *dev, Error *
      }
      
     +/*
    -+ * Close the sound card.
    ++ * Close the stream and free its resources.
     + *
     + * @stream: VirtIOSoundPCMStream *stream
     + */
     +static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream)
     +{
    -+    virtio_snd_process_cmdq(stream->s);
     +}
     +
      static void virtio_snd_unrealize(DeviceState *dev)
    @@ hw/virtio/virtio-snd.c: static void virtio_snd_realize(DeviceState *dev, Error *
      
          trace_virtio_snd_unrealize(vsnd);
      
    -+    for (uint32_t i = 0; i < vsnd->snd_conf.streams; i++) {
    -+        stream = vsnd->pcm->streams[i];
    -+        virtio_snd_pcm_close(stream);
    -+        g_free(stream);
    ++    if (vsnd->pcm) {
    ++        if (vsnd->pcm->streams) {
    ++            for (uint32_t i = 0; i < vsnd->snd_conf.streams; i++) {
    ++                stream = vsnd->pcm->streams[i];
    ++                if (stream) {
    ++                    virtio_snd_process_cmdq(stream->s);
    ++                    virtio_snd_pcm_close(stream);
    ++                    g_free(stream);
    ++                }
    ++            }
    ++            g_free(vsnd->pcm->streams);
    ++        }
    ++        g_free(vsnd->pcm);
    ++        vsnd->pcm = NULL;
     +    }
          AUD_remove_card(&vsnd->card);
    -+    g_free(vsnd->pcm);
          virtio_cleanup(vdev);
      }

4: 82c80e2ae4 ! 4: fbb22214f2 virtio-sound: set PCM stream parameters

    @@ hw/virtio/virtio-snd.c: virtio_snd_set_config(VirtIODevice *vdev, const uint8_t
     +
     +    return VIRTIO_SND_S_OK;
     +}
    ++
    ++/*
    ++ * Get a QEMU Audiosystem compatible format value from a VIRTIO_SND_PCM_FMT_*
    ++ */
    ++static AudioFormat virtio_snd_get_qemu_format(uint32_t format)
    ++{
    ++    #define CASE(FMT)               \
    ++    case VIRTIO_SND_PCM_FMT_##FMT:  \
    ++        return AUDIO_FORMAT_##FMT;
    ++
    ++    switch (format) {
    ++    CASE(U8)
    ++    CASE(S8)
    ++    CASE(U16)
    ++    CASE(S16)
    ++    CASE(U32)
    ++    CASE(S32)
    ++    case VIRTIO_SND_PCM_FMT_FLOAT:
    ++        return AUDIO_FORMAT_F32;
    ++    default:
    ++        g_assert_not_reached();
    ++    }
    ++
    ++    #undef CASE
    ++}
    ++
    ++/*
    ++ * Get a QEMU Audiosystem compatible frequency value from a
    ++ * VIRTIO_SND_PCM_RATE_*
    ++ */
    ++static uint32_t virtio_snd_get_qemu_freq(uint32_t rate)
    ++{
    ++    #define CASE(RATE)               \
    ++    case VIRTIO_SND_PCM_RATE_##RATE: \
    ++        return RATE;
    ++
    ++    switch (rate) {
    ++    CASE(5512)
    ++    CASE(8000)
    ++    CASE(11025)
    ++    CASE(16000)
    ++    CASE(22050)
    ++    CASE(32000)
    ++    CASE(44100)
    ++    CASE(48000)
    ++    CASE(64000)
    ++    CASE(88200)
    ++    CASE(96000)
    ++    CASE(176400)
    ++    CASE(192000)
    ++    CASE(384000)
    ++    default:
    ++        g_assert_not_reached();
    ++    }
    ++
    ++    #undef CASE
    ++}
    ++
    ++/*
    ++ * Get QEMU Audiosystem compatible audsettings from virtio based pcm stream
    ++ * params.
    ++ */
    ++static void virtio_snd_get_qemu_audsettings(audsettings *as,
    ++                                            VirtIOSoundPCMParams *params)
    ++{
    ++    as->nchannels = MIN(AUDIO_MAX_CHANNELS, params->channels);
    ++    as->fmt = virtio_snd_get_qemu_format(params->format);
    ++    as->freq = virtio_snd_get_qemu_freq(params->rate);
    ++    as->endianness = AUDIO_HOST_ENDIANNESS;
    ++}
    ++
    ++/*
    ++ * Close a stream and free all its resources.
    ++ *
    ++ * @stream: VirtIOSoundPCMStream *stream
    ++ */
    ++static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream)
    ++{
    ++    if (stream) {
    ++        qemu_mutex_destroy(&stream->queue_mutex);
    ++        g_free(stream);
    ++    }
    ++}
    ++
    ++/*
    ++ * Prepares a VirtIOSound card stream.
    ++ * Returns the response status code. (VIRTIO_SND_S_*).
    ++ *
    ++ * @s: VirtIOSound device
    ++ * @stream_id: stream id
    ++ */
    ++static uint32_t virtio_snd_pcm_prepare_impl(VirtIOSound *s, uint32_t stream_id)
    ++{
    ++    audsettings as;
    ++    VirtIOSoundPCMParams *params;
    ++    VirtIOSoundPCMStream *stream;
    ++
    ++    if (!s->pcm->streams ||
    ++        !s->pcm->pcm_params ||
    ++        !s->pcm->pcm_params[stream_id]) {
    ++        return VIRTIO_SND_S_BAD_MSG;
    ++    }
    ++
    ++    params = virtio_snd_pcm_get_params(s, stream_id);
    ++    if (!params) {
    ++        return VIRTIO_SND_S_BAD_MSG;
    ++    }
    ++
    ++    virtio_snd_get_qemu_audsettings(&as, params);
    ++
    ++    virtio_snd_pcm_close(s->pcm->streams[stream_id]);
    ++
    ++    stream = g_new0(VirtIOSoundPCMStream, 1);
    ++
    ++    stream->id = stream_id;
    ++    stream->pcm = s->pcm;
    ++    stream->direction = stream_id < s->snd_conf.streams / 2 +
    ++        (s->snd_conf.streams & 1) ? VIRTIO_SND_D_OUTPUT : VIRTIO_SND_D_INPUT;
    ++    stream->info.hdr.hda_fn_nid = VIRTIO_SOUND_HDA_FN_NID;
    ++    stream->features = 0;
    ++    stream->channels_min = 1;
    ++    stream->channels_max = as.nchannels;
    ++    stream->formats = supported_formats;
    ++    stream->rates = supported_rates;
    ++    stream->s = s;
    ++
    ++    stream->buffer_bytes = params->buffer_bytes;
    ++    stream->period_bytes = params->period_bytes;
    ++
    ++    stream->positions[0] = VIRTIO_SND_CHMAP_FL;
    ++    stream->positions[1] = VIRTIO_SND_CHMAP_FR;
    ++
    ++    stream->as = as;
    ++    stream->desired_as = stream->as;
    ++    qemu_mutex_init(&stream->queue_mutex);
    ++    QSIMPLEQ_INIT(&stream->queue);
    ++
    ++    s->pcm->streams[stream_id] = stream;
    ++
    ++    return VIRTIO_SND_S_OK;
    ++}
     +
      /*
       * The actual processing done in virtio_snd_process_cmdq().
    @@ hw/virtio/virtio-snd.c: static void virtio_snd_common_realize(DeviceState *dev,
     +                       print_code(status));
     +            return;
     +        }
    ++        status = virtio_snd_pcm_prepare_impl(vsnd, i);
    ++        if (status != VIRTIO_SND_S_OK) {
    ++            error_setg(errp,
    ++                       "Can't prepare streams, device responded with %s.",
    ++                       print_code(status));
    ++            return;
    ++        }
     +    }
      }
      
      static void
    +@@ hw/virtio/virtio-snd.c: static void virtio_snd_realize(DeviceState *dev, Error **errp)
    +                               errp);
    + }
    + 
    +-/*
    +- * Close the stream and free its resources.
    +- *
    +- * @stream: VirtIOSoundPCMStream *stream
    +- */
    +-static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream)
    +-{
    +-}
    +-
    + static void virtio_snd_unrealize(DeviceState *dev)
    + {
    +     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
    +@@ hw/virtio/virtio-snd.c: static void virtio_snd_unrealize(DeviceState *dev)
    +         vsnd->pcm = NULL;
    +     }
    +     AUD_remove_card(&vsnd->card);
    ++    qemu_mutex_destroy(&vsnd->cmdq_mutex);
    +     virtio_cleanup(vdev);
    + }
    + 
 5:  19d95e8411 <  -:  ---------- virtio-sound: prepare PCM streams
 6:  78777fb46f =  5:  06f676e360 virtio-sound: handle VIRTIO_SND_R_PCM_INFO request
 7:  7641e21ee8 =  6:  d3102a0850 virtio-sound: handle VIRTIO_SND_R_PCM_{START,STOP}
 8:  48f9b776f5 =  7:  0cb3e8a3b5 virtio-sound: handle VIRTIO_SND_PCM_SET_PARAMS
 9:  cdb8ab3ee2 =  8:  894b52532b virtio-sound: handle VIRTIO_SND_R_PCM_PREPARE
10:  bbc11d0348 !  9:  4aebc54222 virtio-sound: handle VIRTIO_SND_PCM_RELEASE

@@ hw/virtio/virtio-snd.c: static void virtio_snd_handle_pcm_start_stop(VirtIOSound
}

+/*
-+ * Releases the buffer resources allocated to a stream. Seperated from the -+ * handler so that the code can be reused in the unrealize function. Returns -+ * the response status code. (VIRTIO_SND_S_*).
-+ *
-+ * @stream: VirtIOSoundPCMStream stream
-+ * @stream_id: stream id
-+ */
-+static uint32_t virtio_snd_pcm_release_impl(VirtIOSoundPCMStream *stream,
-+ uint32_t stream_id)
-+{
-+ return VIRTIO_SND_S_OK;
-+}
-+
-+/*
-+ * Handles VIRTIO_SND_R_PCM_RELEASE.
++ * Handles VIRTIO_SND_R_PCM_RELEASE. Releases the buffer resources allocated to ++ * a stream.
+ *
+ * @s: VirtIOSound device
+ * @cmd: The request command queue element from VirtIOSound cmdq field
@@ hw/virtio/virtio-snd.c: static void virtio_snd_handle_pcm_start_stop(VirtIOSound
+ cmd->resp.code = VIRTIO_SND_S_BAD_MSG;
+ return;
+ }
-+ cmd->resp.code = virtio_snd_pcm_release_impl(stream, stream_id);
++ cmd->resp.code = VIRTIO_SND_S_OK;
+}
+
/*
11: d5fb4b058c ! 10: ab95cdd4ae virtio-sound: implement audio output (TX)

    @@ hw/virtio/virtio-snd.c
      static uint32_t supported_formats = BIT(VIRTIO_SND_PCM_FMT_S8)
                                        | BIT(VIRTIO_SND_PCM_FMT_U8)
                                        | BIT(VIRTIO_SND_PCM_FMT_S16)
    +@@ hw/virtio/virtio-snd.c: static void virtio_snd_get_qemu_audsettings(audsettings *as,
    +  */
    + static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream)
    + {
    ++    VirtIOSoundPCMBlock *block, *next;
    ++
    +     if (stream) {
    ++        WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
    ++            QSIMPLEQ_FOREACH_SAFE(block, &stream->queue, entry, next) {
    ++                virtqueue_push(block->vq,
    ++                        block->elem,
    ++                        sizeof(block->elem));
    ++                virtio_notify(VIRTIO_DEVICE(stream->s),
    ++                        block->vq);
    ++                QSIMPLEQ_REMOVE_HEAD(&stream->queue, entry);
    ++                g_free(block);
    ++            }
    ++        }
    ++        if (stream->direction == VIRTIO_SND_D_OUTPUT) {
    ++            AUD_close_out(&stream->pcm->snd->card, stream->voice.out);
    ++            stream->voice.out = NULL;
    ++        }
    +         qemu_mutex_destroy(&stream->queue_mutex);
    +         g_free(stream);
    +     }
     @@ hw/virtio/virtio-snd.c: static uint32_t virtio_snd_pcm_prepare_impl(VirtIOSound *s, uint32_t stream_id)
          stream->positions[0] = VIRTIO_SND_CHMAP_FL;
          stream->positions[1] = VIRTIO_SND_CHMAP_FR;
    @@ hw/virtio/virtio-snd.c: static void virtio_snd_handle_pcm_start_stop(VirtIOSound
          }
      }
      
    -+/*
    + /*
    +- * Handles VIRTIO_SND_R_PCM_RELEASE. Releases the buffer resources allocated to
    +- * a stream.
     + * Returns the number of bytes that have not been passed to AUD_write yet.
     + *
     + * @stream: VirtIOSoundPCMStream
    @@ hw/virtio/virtio-snd.c: static void virtio_snd_handle_pcm_start_stop(VirtIOSound
     +    return size;
     +}
     +
    - /*
    -  * Releases the buffer resources allocated to a stream. Seperated from the
    -  * handler so that the code can be reused in the unrealize function. Returns
    -@@ hw/virtio/virtio-snd.c: static void virtio_snd_handle_pcm_start_stop(VirtIOSound *s,
    - static uint32_t virtio_snd_pcm_release_impl(VirtIOSoundPCMStream *stream,
    -                                             uint32_t stream_id)
    - {
    ++/*
    ++ * Handles VIRTIO_SND_R_PCM_RELEASE.
    +  *
    +  * @s: VirtIOSound device
    +  * @cmd: The request command queue element from VirtIOSound cmdq field
    +@@ hw/virtio/virtio-snd.c: static void virtio_snd_handle_pcm_release(VirtIOSound *s,
    +         cmd->resp.code = VIRTIO_SND_S_BAD_MSG;
    +         return;
    +     }
    ++
     +    if (virtio_snd_pcm_get_pending_bytes(stream)) {
     +        /*
     +         * virtio-v1.2-csd01, 5.14.6.6.5.1,
    @@ hw/virtio/virtio-snd.c: static void virtio_snd_handle_pcm_start_stop(VirtIOSound
     +        virtio_snd_pcm_flush(stream);
     +    }
     +
    -     return VIRTIO_SND_S_OK;
    +     cmd->resp.code = VIRTIO_SND_S_OK;
      }
      
     @@ hw/virtio/virtio-snd.c: static void virtio_snd_handle_event(VirtIODevice *vdev, VirtQueue *vq)
    @@ hw/virtio/virtio-snd.c: static void virtio_snd_realize(DeviceState *dev, Error *
     +    }
     +}
     +
    - /*
    -  * Close the sound card.
    -  *
    -@@ hw/virtio/virtio-snd.c: static void virtio_snd_realize(DeviceState *dev, Error **errp)
    - static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream)
    - {
    -     virtio_snd_process_cmdq(stream->s);
    -+    if (stream->direction == VIRTIO_SND_D_OUTPUT) {
    -+        AUD_close_out(&stream->pcm->snd->card, stream->voice.out);
    -+        stream->voice.out = NULL;
    -+    }
    - }
    - 
      static void virtio_snd_unrealize(DeviceState *dev)
    + {
    +     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     @@ hw/virtio/virtio-snd.c: static void virtio_snd_unrealize(DeviceState *dev)
    - 
    -     trace_virtio_snd_unrealize(vsnd);
    - 
    --    for (uint32_t i = 0; i < vsnd->snd_conf.streams; i++) {
    --        stream = vsnd->pcm->streams[i];
    --        virtio_snd_pcm_close(stream);
    --        g_free(stream);
    -+    if (vsnd->pcm) {
    -+        if (vsnd->pcm->streams) {
    -+            for (uint32_t i = 0; i < vsnd->snd_conf.streams; i++) {
    -+                stream = vsnd->pcm->streams[i];
    -+                if (stream) {
    -+                    virtio_snd_pcm_close(stream);
    -+                    g_free(stream);
    -+                }
    -+            }
    -+            g_free(vsnd->pcm->streams);
    -+            g_free(vsnd->pcm->pcm_params);
    -+            g_free(vsnd->pcm->jacks);
    -+        }
    -+
    -+        g_free(vsnd->pcm);
    +             }
    +             g_free(vsnd->pcm->streams);
    +         }
    ++        g_free(vsnd->pcm->pcm_params);
    ++        g_free(vsnd->pcm->jacks);
    +         g_free(vsnd->pcm);
    +         vsnd->pcm = NULL;
          }
    -+
    -+    vsnd->pcm = NULL;
    -     AUD_remove_card(&vsnd->card);
    --    g_free(vsnd->pcm);
    -     virtio_cleanup(vdev);
    +@@ hw/virtio/virtio-snd.c: static void virtio_snd_unrealize(DeviceState *dev)
      }

12: e2c30e2a21 ! 11: 5e8f423499 virtio-sound: implement audio capture (RX)
@@ hw/virtio/virtio-snd.c

static uint32_t supported_formats = BIT(VIRTIO_SND_PCM_FMT_S8)
| BIT(VIRTIO_SND_PCM_FMT_U8)
+@@ hw/virtio/virtio-snd.c: static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream)
+ if (stream->direction == VIRTIO_SND_D_OUTPUT) {
+ AUD_close_out(&stream->pcm->snd->card, stream->voice.out);
+ stream->voice.out = NULL;
++ } else if (stream->direction == VIRTIO_SND_D_INPUT) {
++ AUD_close_in(&stream->pcm->snd->card, stream->voice.in);
++ stream->voice.in = NULL;
+ }
+ qemu_mutex_destroy(&stream->queue_mutex);
+ g_free(stream);
@@ hw/virtio/virtio-snd.c: static uint32_t virtio_snd_pcm_prepare_impl(VirtIOSound *s, uint32_t stream_id)
virtio_snd_pcm_out_cb,
&as);
@@ hw/virtio/virtio-snd.c: static void virtio_snd_handle_pcm_start_stop(VirtIOSound
}
} else {
error_report("Invalid stream id: %"PRIu32, req.stream_id);
-@@ hw/virtio/virtio-snd.c: static uint32_t virtio_snd_pcm_release_impl(VirtIOSoundPCMStream *stream, +@@ hw/virtio/virtio-snd.c: static void virtio_snd_handle_pcm_release(VirtIOSound *s,
*/
virtio_snd_process_cmdq(stream->s); trace_virtio_snd_pcm_stream_flush(stream_id);
@@ hw/virtio/virtio-snd.c: static uint32_t virtio_snd_pcm_release_impl(VirtIOSoundP
+ }
}

- return VIRTIO_SND_S_OK;
+ cmd->resp.code = VIRTIO_SND_S_OK;
@@ hw/virtio/virtio-snd.c: static void virtio_snd_handle_event(VirtIODevice *vdev, VirtQueue *vq)
* @vdev: VirtIOSound device
* @vq: tx virtqueue
@@ hw/virtio/virtio-snd.c: static void virtio_snd_pcm_out_cb(void *data, int availa
+ );
+}
+
- /*
- * Close the sound card.
- *
+ static void virtio_snd_unrealize(DeviceState *dev)
+ {
+ VirtIODevice *vdev = VIRTIO_DEVICE(dev);
@@ hw/virtio/virtio-snd.c: static void virtio_snd_unrealize(DeviceState *dev)

-: ---------- > 12: a3f2576f6a docs/system: add basic virtio-snd documentation

base-commit: 408af44d04476c633065bfb1eca6865ea93f2984

Signature · 1 line
2.39.2
#1 lore raw

PATCH v6 1/12 Add virtio-sound device stub

Add a new VIRTIO device for the virtio sound device id. Functionality will be added in the following commits.

---
 MAINTAINERS                    |   6 +
 hw/virtio/Kconfig              |   5 +
 hw/virtio/meson.build          |   1 +
 hw/virtio/trace-events         |   9 ++
 hw/virtio/virtio-snd.c         | 231 +++++++++++++++++++++++++++++++++
 include/hw/virtio/virtio-snd.h |  80 ++++++++++++
 6 files changed, 332 insertions(+)
 create mode 100644 hw/virtio/virtio-snd.c
 create mode 100644 include/hw/virtio/virtio-snd.h
MAINTAINERS +6 -0
index 6111b6b4d9..ba365d621c 100644
@@ -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 <[email protected]>
+S: Supported
+F: hw/virtio/virtio-snd*.c
+F: include/hw/virtio/virtio-snd.h
+
 nvme
 M: Keith Busch <[email protected]>
 M: Klaus Jensen <[email protected]>
hw/virtio/Kconfig +5 -0
index 92c9cf6c96..d6f20657b3 100644
@@ -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
hw/virtio/meson.build +1 -0
index 13e7c6c272..120d4bfa0a 100644
@@ -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'))
hw/virtio/trace-events +9 -0
index 7109cf1a3b..3ed7da35f2 100644
@@ -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"
hw/virtio/virtio-snd.c new file +231 -0

231 lines not on this page · raw message

include/hw/virtio/virtio-snd.h new file +80 -0

80 lines not on this page · raw message

Signature · 1 line
2.39.2
#9 lore raw

PATCH v6 4/12 virtio-sound: set PCM stream parameters

In reply to Emmanouil Pitsidianakis

This commit sets the virtio-snd device's default PCM parameters in virtio_snd_pcm_set_params_impl(). The same function will be used to set parameters from the guest with VIRTIO_SND_R_PCM_SET_PARAMS in a follow-up commit.

PCM parameters describe the sound card parameters that the guest's kernel sees as an ALSA device.

---
 hw/virtio/virtio-snd.c | 267 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 258 insertions(+), 9 deletions(-)
hw/virtio/virtio-snd.c +258 -9
index b6a510a416..2be656d64b 100644
@@ -30,6 +30,29 @@
 #define VIRTIO_SOUND_CHMAP_DEFAULT 0
 #define VIRTIO_SOUND_HDA_FN_NID 0
 
+static uint32_t supported_formats = BIT(VIRTIO_SND_PCM_FMT_S8)
+                                  | BIT(VIRTIO_SND_PCM_FMT_U8)
+                                  | BIT(VIRTIO_SND_PCM_FMT_S16)
+                                  | BIT(VIRTIO_SND_PCM_FMT_U16)
+                                  | BIT(VIRTIO_SND_PCM_FMT_S32)
+                                  | BIT(VIRTIO_SND_PCM_FMT_U32)
+                                  | BIT(VIRTIO_SND_PCM_FMT_FLOAT);
+
+static uint32_t supported_rates = BIT(VIRTIO_SND_PCM_RATE_5512)
+                                | BIT(VIRTIO_SND_PCM_RATE_8000)
+                                | BIT(VIRTIO_SND_PCM_RATE_11025)
+                                | BIT(VIRTIO_SND_PCM_RATE_16000)
+                                | BIT(VIRTIO_SND_PCM_RATE_22050)
+                                | BIT(VIRTIO_SND_PCM_RATE_32000)
+                                | BIT(VIRTIO_SND_PCM_RATE_44100)
+                                | BIT(VIRTIO_SND_PCM_RATE_48000)
+                                | BIT(VIRTIO_SND_PCM_RATE_64000)
+                                | BIT(VIRTIO_SND_PCM_RATE_88200)
+                                | BIT(VIRTIO_SND_PCM_RATE_96000)
+                                | BIT(VIRTIO_SND_PCM_RATE_176400)
+                                | BIT(VIRTIO_SND_PCM_RATE_192000)
+                                | BIT(VIRTIO_SND_PCM_RATE_384000);
+
 static const char *print_code(uint32_t code)
 {
     #define CASE(CODE)            \
@@ -111,6 +134,213 @@ virtio_snd_set_config(VirtIODevice *vdev, const uint8_t *config)
     memcpy(&s->snd_conf, sndconfig, sizeof(s->snd_conf));
 }
 
+/*
+ * Get params for a specific stream.
+ *
+ * @s: VirtIOSound device
+ * @stream_id: stream id
+ */
+static VirtIOSoundPCMParams *virtio_snd_pcm_get_params(VirtIOSound *s,
+                                                       uint32_t stream_id)
+{
+    return stream_id >= s->snd_conf.streams ? NULL
+        : s->pcm->pcm_params[stream_id];
+}
+
+/*
+ * Set the given stream params.
+ * Called by both virtio_snd_handle_pcm_set_params and during device
+ * initialization.
+ * Returns the response status code. (VIRTIO_SND_S_*).
+ *
+ * @s: VirtIOSound device
+ * @params: The PCM params as defined in the virtio specification
+ */
+static
+uint32_t virtio_snd_pcm_set_params_impl(VirtIOSound *s,
+                                        virtio_snd_pcm_set_params *params)
+{
+    VirtIOSoundPCMParams *st_params;
+    uint32_t stream_id = params->hdr.stream_id;
+
+    if (stream_id > s->snd_conf.streams || !(s->pcm->pcm_params)) {
+        virtio_error(VIRTIO_DEVICE(s), "Streams have not been initialized.\n");
+        return VIRTIO_SND_S_BAD_MSG;
+    }
+
+    if (!s->pcm->pcm_params[stream_id]) {
+        s->pcm->pcm_params[stream_id] = g_new0(VirtIOSoundPCMParams, 1);
+    }
+    st_params = virtio_snd_pcm_get_params(s, stream_id);
+
+    st_params->features = params->features;
+    st_params->buffer_bytes = params->buffer_bytes;
+    st_params->period_bytes = params->period_bytes;
+
+    if (params->channels < 1 || params->channels > AUDIO_MAX_CHANNELS) {
+        error_report("Number of channels is not supported.");
+        return VIRTIO_SND_S_NOT_SUPP;
+    }
+    st_params->channels = params->channels;
+
+    if (!(supported_formats & BIT(params->format))) {
+        error_report("Stream format is not supported.");
+        return VIRTIO_SND_S_NOT_SUPP;
+    }
+    st_params->format = params->format;
+
+    if (!(supported_rates & BIT(params->rate))) {
+        error_report("Stream rate is not supported.");
+        return VIRTIO_SND_S_NOT_SUPP;
+    }
+    st_params->rate = params->rate;
+    st_params->period_bytes = params->period_bytes;
+    st_params->buffer_bytes = params->buffer_bytes;
+
+    return VIRTIO_SND_S_OK;
+}
+
+/*
+ * Get a QEMU Audiosystem compatible format value from a VIRTIO_SND_PCM_FMT_*
+ */
+static AudioFormat virtio_snd_get_qemu_format(uint32_t format)
+{
+    #define CASE(FMT)               \
+    case VIRTIO_SND_PCM_FMT_##FMT:  \
+        return AUDIO_FORMAT_##FMT;
+
+    switch (format) {
+    CASE(U8)
+    CASE(S8)
+    CASE(U16)
+    CASE(S16)
+    CASE(U32)
+    CASE(S32)
+    case VIRTIO_SND_PCM_FMT_FLOAT:
+        return AUDIO_FORMAT_F32;
+    default:
+        g_assert_not_reached();
+    }
+
+    #undef CASE
+}
+
+/*
+ * Get a QEMU Audiosystem compatible frequency value from a
+ * VIRTIO_SND_PCM_RATE_*
+ */
+static uint32_t virtio_snd_get_qemu_freq(uint32_t rate)
+{
+    #define CASE(RATE)               \
+    case VIRTIO_SND_PCM_RATE_##RATE: \
+        return RATE;
+
+    switch (rate) {
+    CASE(5512)
+    CASE(8000)
+    CASE(11025)
+    CASE(16000)
+    CASE(22050)
+    CASE(32000)
+    CASE(44100)
+    CASE(48000)
+    CASE(64000)
+    CASE(88200)
+    CASE(96000)
+    CASE(176400)
+    CASE(192000)
+    CASE(384000)
+    default:
+        g_assert_not_reached();
+    }
+
+    #undef CASE
+}
+
+/*
+ * Get QEMU Audiosystem compatible audsettings from virtio based pcm stream
+ * params.
+ */
+static void virtio_snd_get_qemu_audsettings(audsettings *as,
+                                            VirtIOSoundPCMParams *params)
+{
+    as->nchannels = MIN(AUDIO_MAX_CHANNELS, params->channels);
+    as->fmt = virtio_snd_get_qemu_format(params->format);
+    as->freq = virtio_snd_get_qemu_freq(params->rate);
+    as->endianness = AUDIO_HOST_ENDIANNESS;
+}
+
+/*
+ * Close a stream and free all its resources.
+ *
+ * @stream: VirtIOSoundPCMStream *stream
+ */
+static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream)
+{
+    if (stream) {
+        qemu_mutex_destroy(&stream->queue_mutex);
+        g_free(stream);
+    }
+}
+
+/*
+ * Prepares a VirtIOSound card stream.
+ * Returns the response status code. (VIRTIO_SND_S_*).
+ *
+ * @s: VirtIOSound device
+ * @stream_id: stream id
+ */
+static uint32_t virtio_snd_pcm_prepare_impl(VirtIOSound *s, uint32_t stream_id)
+{
+    audsettings as;
+    VirtIOSoundPCMParams *params;
+    VirtIOSoundPCMStream *stream;
+
+    if (!s->pcm->streams ||
+        !s->pcm->pcm_params ||
+        !s->pcm->pcm_params[stream_id]) {
+        return VIRTIO_SND_S_BAD_MSG;
+    }
+
+    params = virtio_snd_pcm_get_params(s, stream_id);
+    if (!params) {
+        return VIRTIO_SND_S_BAD_MSG;
+    }
+
+    virtio_snd_get_qemu_audsettings(&as, params);
+
+    virtio_snd_pcm_close(s->pcm->streams[stream_id]);
+
+    stream = g_new0(VirtIOSoundPCMStream, 1);
+
+    stream->id = stream_id;
+    stream->pcm = s->pcm;
+    stream->direction = stream_id < s->snd_conf.streams / 2 +
+        (s->snd_conf.streams & 1) ? VIRTIO_SND_D_OUTPUT : VIRTIO_SND_D_INPUT;
+    stream->info.hdr.hda_fn_nid = VIRTIO_SOUND_HDA_FN_NID;
+    stream->features = 0;
+    stream->channels_min = 1;
+    stream->channels_max = as.nchannels;
+    stream->formats = supported_formats;
+    stream->rates = supported_rates;
+    stream->s = s;
+
+    stream->buffer_bytes = params->buffer_bytes;
+    stream->period_bytes = params->period_bytes;
+
+    stream->positions[0] = VIRTIO_SND_CHMAP_FL;
+    stream->positions[1] = VIRTIO_SND_CHMAP_FR;
+
+    stream->as = as;
+    stream->desired_as = stream->as;
+    qemu_mutex_init(&stream->queue_mutex);
+    QSIMPLEQ_INIT(&stream->queue);
+
+    s->pcm->streams[stream_id] = stream;
+
+    return VIRTIO_SND_S_OK;
+}
+
 /*
  * The actual processing done in virtio_snd_process_cmdq().
  *
@@ -294,6 +524,8 @@ static void virtio_snd_common_realize(DeviceState *dev,
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtIOSound *vsnd = VIRTIO_SND(dev);
+    virtio_snd_pcm_set_params default_params;
+    uint32_t status;
 
     virtio_snd_set_pcm(vsnd);
 
@@ -323,12 +555,37 @@ static void virtio_snd_common_realize(DeviceState *dev,
 
     AUD_register_card("virtio-sound", &vsnd->card);
 
+    /* set default params for all streams */
+    default_params.features = 0;
+    default_params.buffer_bytes = 8192;
+    default_params.period_bytes = 4096;
+    default_params.channels = 2;
+    default_params.format = VIRTIO_SND_PCM_FMT_S16;
+    default_params.rate = VIRTIO_SND_PCM_RATE_44100;
     vsnd->queues[VIRTIO_SND_VQ_CONTROL] = virtio_add_queue(vdev, 64, ctrl);
     vsnd->queues[VIRTIO_SND_VQ_EVENT] = virtio_add_queue(vdev, 64, evt);
     vsnd->queues[VIRTIO_SND_VQ_TX] = virtio_add_queue(vdev, 64, txq);
     vsnd->queues[VIRTIO_SND_VQ_RX] = virtio_add_queue(vdev, 64, rxq);
     qemu_mutex_init(&vsnd->cmdq_mutex);
     QTAILQ_INIT(&vsnd->cmdq);
+
+    for (uint32_t i = 0; i < vsnd->snd_conf.streams; i++) {
+        default_params.hdr.stream_id = i;
+        status = virtio_snd_pcm_set_params_impl(vsnd, &default_params);
+        if (status != VIRTIO_SND_S_OK) {
+            error_setg(errp,
+                       "Can't initalize stream params, device responded with %s.",
+                       print_code(status));
+            return;
+        }
+        status = virtio_snd_pcm_prepare_impl(vsnd, i);
+        if (status != VIRTIO_SND_S_OK) {
+            error_setg(errp,
+                       "Can't prepare streams, device responded with %s.",
+                       print_code(status));
+            return;
+        }
+    }
 }
 
 static void
@@ -360,15 +617,6 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp)
                               errp);
 }
 
-/*
- * Close the stream and free its resources.
- *
- * @stream: VirtIOSoundPCMStream *stream
- */
-static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream)
-{
-}
-
 static void virtio_snd_unrealize(DeviceState *dev)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
@@ -396,6 +644,7 @@ static void virtio_snd_unrealize(DeviceState *dev)
         vsnd->pcm = NULL;
     }
     AUD_remove_card(&vsnd->card);
+    qemu_mutex_destroy(&vsnd->cmdq_mutex);
     virtio_cleanup(vdev);
 }
 
Signature · 1 line
2.39.2
#15 lore raw

PATCH v6 10/12 virtio-sound: implement audio output (TX)

In reply to Emmanouil Pitsidianakis

Handle output IO messages in the transmit (TX) virtqueue.

It allocates a VirtIOSoundPCMBlock for each IO message and copies the data buffer to it. When the IO buffer is written to the host's sound card, the guest will be notified that it has been consumed.

The lifetime of an IO message is:

1. Guest sends IO message to TX virtqueue.
2. QEMU adds it to the appropriate stream's IO buffer queue.
3. Sometime later, the host audio backend calls the output callback, virtio_snd_pcm_out_cb(), which is defined with an AUD_open_out() call. The callback gets an available number of bytes the backend can receive. Then it writes data from the IO buffer queue to the backend. If at any time a buffer is exhausted, it is returned to the guest as completed.
4. If the guest releases the stream, its buffer queue is flushed by attempting to write any leftover data to the audio backend and releasing all IO messages back to the guest. This is how according to the spec the guest knows the release was successful.

---
 hw/virtio/trace-events         |   2 +
 hw/virtio/virtio-snd.c         | 251 ++++++++++++++++++++++++++++++++-
 include/hw/virtio/virtio-snd.h |  11 ++
 3 files changed, 260 insertions(+), 4 deletions(-)
hw/virtio/trace-events +2 -0
index 60ab62a80d..3b95e745c2 100644
@@ -171,3 +171,5 @@ virtio_snd_handle_pcm_release(uint32_t stream) "VIRTIO_SND_PCM_RELEASE called fo
 virtio_snd_handle_code(uint32_t val, const char *code) "ctrl code msg val = %"PRIu32" == %s"
 virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called"
 virtio_snd_handle_event(void) "event queue callback called"
+virtio_snd_pcm_stream_flush(uint32_t stream) "flushing stream %"PRIu32
+virtio_snd_handle_xfer(void) "tx/rx queue callback called"
hw/virtio/virtio-snd.c +247 -4

316 lines not on this page · raw message

include/hw/virtio/virtio-snd.h +11 -0
index 7cc5061fa8..6665c8a703 100644
@@ -81,6 +81,8 @@ typedef struct VirtIOSoundPCMParams VirtIOSoundPCMParams;
 
 typedef struct VirtIOSoundPCM VirtIOSoundPCM;
 
+typedef struct VirtIOSoundPCMBlock VirtIOSoundPCMBlock;
+
 /* Stream params */
 struct VirtIOSoundPCMParams {
     uint32_t features;
@@ -91,6 +93,15 @@ struct VirtIOSoundPCMParams {
     uint8_t rate;
 };
 
+struct VirtIOSoundPCMBlock {
+    QSIMPLEQ_ENTRY(VirtIOSoundPCMBlock) entry;
+    VirtQueueElement *elem;
+    VirtQueue *vq;
+    size_t size;
+    uint64_t offset;
+    uint8_t data[];
+};
+
 struct VirtIOSoundPCM {
     VirtIOSound *snd;
     VirtIOSoundPCMParams **pcm_params;
Signature · 1 line
2.39.2
#12 lore raw

PATCH v6 7/12 virtio-sound: handle VIRTIO_SND_PCM_SET_PARAMS

In reply to Emmanouil Pitsidianakis

Handle the set parameters control request. It reconfigures a stream based on a guest's preference if the values are valid and supported.

---
 hw/virtio/trace-events |  1 +
 hw/virtio/virtio-snd.c | 26 ++++++++++++++++++++++++++
 2 files changed, 27 insertions(+)
hw/virtio/trace-events +1 -0
index 8eae1bf881..f70cde4f01 100644
@@ -163,6 +163,7 @@ 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"
+virtio_snd_handle_pcm_set_params(uint32_t stream) "VIRTIO_SND_PCM_SET_PARAMS called for stream %"PRIu32
 virtio_snd_handle_ctrl(void *vdev, void *vq) "snd %p: handle ctrl event for queue %p"
 virtio_snd_handle_pcm_info(uint32_t stream) "VIRTIO_SND_R_PCM_INFO called for stream %"PRIu32
 virtio_snd_handle_pcm_start_stop(const char *code, uint32_t stream) "%s called for stream %"PRIu32
hw/virtio/virtio-snd.c +26 -0
index 8fbad8f07f..c9909a29af 100644
@@ -276,6 +276,30 @@ uint32_t virtio_snd_pcm_set_params_impl(VirtIOSound *s,
     return VIRTIO_SND_S_OK;
 }
 
+/*
+ * Handles the VIRTIO_SND_R_PCM_SET_PARAMS request.
+ *
+ * @s: VirtIOSound device
+ * @cmd: The request command queue element from VirtIOSound cmdq field
+ */
+static void virtio_snd_handle_pcm_set_params(VirtIOSound *s,
+                                             virtio_snd_ctrl_command *cmd)
+{
+    virtio_snd_pcm_set_params req;
+    size_t sz = iov_to_buf(cmd->elem->out_sg,
+                           cmd->elem->out_num,
+                           0,
+                           &req,
+                           sizeof(req));
+    if (sz != sizeof(virtio_snd_pcm_set_params)) {
+        cmd->resp.code = VIRTIO_SND_S_BAD_MSG;
+        return;
+    }
+
+    trace_virtio_snd_handle_pcm_set_params(req.hdr.stream_id);
+    cmd->resp.code = virtio_snd_pcm_set_params_impl(s, &req);
+}
+
 /*
  * Get a QEMU Audiosystem compatible format value from a VIRTIO_SND_PCM_FMT_*
  */
@@ -492,6 +516,8 @@ process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cmd)
         virtio_snd_handle_pcm_start_stop(s, cmd, false);
         break;
     case VIRTIO_SND_R_PCM_SET_PARAMS:
+        virtio_snd_handle_pcm_set_params(s, cmd);
+        break;
     case VIRTIO_SND_R_PCM_PREPARE:
     case VIRTIO_SND_R_PCM_RELEASE:
         cmd->resp.code = VIRTIO_SND_S_NOT_SUPP;
Signature · 1 line
2.39.2
#11 lore raw

PATCH v6 6/12 virtio-sound: handle VIRTIO_SND_R_PCM_{START,STOP}

In reply to Emmanouil Pitsidianakis

Handle the start and stop control messages for a stream_id. This request does nothing at the moment except for replying to it. Audio playback or capture will be started/stopped here in follow-up commits.

---
 hw/virtio/trace-events |  1 +
 hw/virtio/virtio-snd.c | 42 ++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 41 insertions(+), 2 deletions(-)
hw/virtio/trace-events +1 -0
index 3e619f778b..8eae1bf881 100644
@@ -165,6 +165,7 @@ virtio_snd_realize(void *snd) "snd %p: realize"
 virtio_snd_unrealize(void *snd) "snd %p: unrealize"
 virtio_snd_handle_ctrl(void *vdev, void *vq) "snd %p: handle ctrl event for queue %p"
 virtio_snd_handle_pcm_info(uint32_t stream) "VIRTIO_SND_R_PCM_INFO called for stream %"PRIu32
+virtio_snd_handle_pcm_start_stop(const char *code, uint32_t stream) "%s called for stream %"PRIu32
 virtio_snd_handle_code(uint32_t val, const char *code) "ctrl code msg val = %"PRIu32" == %s"
 virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called"
 virtio_snd_handle_event(void) "event queue callback called"
hw/virtio/virtio-snd.c +40 -2

56 lines not on this page · raw message

Signature · 1 line
2.39.2
#6 lore raw

PATCH v6 2/12 Add virtio-sound-pci device

In reply to Emmanouil Pitsidianakis

This patch adds a PCI wrapper device for the virtio-sound device. It is necessary to instantiate a virtio-snd device in a guest. All sound logic will be added to the virtio-snd device in the following commits.

To add this device with a guest, 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
or
-audio driver=pa,model=virtio-sound,server=/run/user/1000/pulse/native
sdl:
-audio driver=sdl,model=virtio-sound
coreaudio (macos/darwin):
-audio driver=coreaudio,model=virtio-sound
etc.

---
 hw/virtio/meson.build      |  1 +
 hw/virtio/virtio-snd-pci.c | 91 ++++++++++++++++++++++++++++++++++++++
 include/hw/pci/pci.h       |  1 +
 softmmu/qdev-monitor.c     |  1 +
 4 files changed, 94 insertions(+)
 create mode 100644 hw/virtio/virtio-snd-pci.c
hw/virtio/meson.build +1 -0
index 120d4bfa0a..5e5a83a4ee 100644
@@ -63,6 +63,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'))
 virtio_pci_ss.add(when: 'CONFIG_VIRTIO_MD', if_true: files('virtio-md-pci.c'))
 
hw/virtio/virtio-snd-pci.c new file +91 -0

91 lines not on this page · raw message

include/hw/pci/pci.h +1 -0
index abdc1ef103..3cd5712035 100644
@@ -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,
softmmu/qdev-monitor.c +1 -0
index 74f4e41338..2e9835ad88 100644
@@ -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 },
Signature · 1 line
2.39.2
#13 lore raw

PATCH v6 8/12 virtio-sound: handle VIRTIO_SND_R_PCM_PREPARE

In reply to Emmanouil Pitsidianakis

Handles the PCM prepare control request. It initializes a PCM stream when the guests asks for it.

---
 hw/virtio/virtio-snd.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
hw/virtio/virtio-snd.c +23 -0
index c9909a29af..f314b6270f 100644
@@ -441,6 +441,27 @@ static uint32_t virtio_snd_pcm_prepare_impl(VirtIOSound *s, uint32_t stream_id)
     return VIRTIO_SND_S_OK;
 }
 
+/*
+ * Handles VIRTIO_SND_R_PCM_PREPARE.
+ *
+ * @s: VirtIOSound device
+ * @cmd: The request command queue element from VirtIOSound cmdq field
+ */
+static void virtio_snd_handle_pcm_prepare(VirtIOSound *s,
+                                          virtio_snd_ctrl_command *cmd)
+{
+    uint32_t stream_id;
+    size_t sz = iov_to_buf(cmd->elem->out_sg,
+                           cmd->elem->out_num,
+                           sizeof(virtio_snd_hdr),
+                           &stream_id,
+                           sizeof(stream_id));
+
+    cmd->resp.code = sz == sizeof(uint32_t)
+                   ? virtio_snd_pcm_prepare_impl(s, stream_id)
+                   : VIRTIO_SND_S_BAD_MSG;
+}
+
 /*
  * Handles VIRTIO_SND_R_PCM_START.
  *
@@ -519,6 +540,8 @@ process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cmd)
         virtio_snd_handle_pcm_set_params(s, cmd);
         break;
     case VIRTIO_SND_R_PCM_PREPARE:
+        virtio_snd_handle_pcm_prepare(s, cmd);
+        break;
     case VIRTIO_SND_R_PCM_RELEASE:
         cmd->resp.code = VIRTIO_SND_S_NOT_SUPP;
         break;
Signature · 1 line
2.39.2
#8 lore raw

PATCH v6 3/12 virtio-sound: handle control messages and streams

In reply to Emmanouil Pitsidianakis

Receive guest requests in the control (CTRL) queue of the virtio sound device and reply with a NOT SUPPORTED error to all control commands.

The receiving handler is virtio_snd_handle_ctrl(). It stores all control messages in the queue in the device's command queue. Then it calls virtio_snd_process_cmdq() to handle each message.

The handler is process_cmd() which replies with VIRTIO_SND_S_NOT_SUPP.

---
 hw/virtio/trace-events         |   4 +
 hw/virtio/virtio-snd.c         | 229 ++++++++++++++++++++++++++++++++-
 include/hw/virtio/virtio-snd.h |  71 +++++++++-
 3 files changed, 295 insertions(+), 9 deletions(-)
hw/virtio/trace-events +4 -0
index 3ed7da35f2..8a223e36e9 100644
@@ -163,3 +163,7 @@ 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"
+virtio_snd_handle_ctrl(void *vdev, void *vq) "snd %p: handle ctrl event for queue %p"
+virtio_snd_handle_code(uint32_t val, const char *code) "ctrl code msg val = %"PRIu32" == %s"
+virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called"
+virtio_snd_handle_event(void) "event queue callback called"
hw/virtio/virtio-snd.c +222 -7

293 lines not on this page · raw message

include/hw/virtio/virtio-snd.h +69 -2

81 lines not on this page · raw message

Signature · 1 line
2.39.2
#10 lore raw

PATCH v6 5/12 virtio-sound: handle VIRTIO_SND_R_PCM_INFO request

In reply to Emmanouil Pitsidianakis

Respond to the VIRTIO_SND_R_PCM_INFO control request with the parameters of each requested PCM stream.

---
 hw/virtio/trace-events |  1 +
 hw/virtio/virtio-snd.c | 78 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 79 insertions(+)
hw/virtio/trace-events +1 -0
index 8a223e36e9..3e619f778b 100644
@@ -164,6 +164,7 @@ 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"
 virtio_snd_handle_ctrl(void *vdev, void *vq) "snd %p: handle ctrl event for queue %p"
+virtio_snd_handle_pcm_info(uint32_t stream) "VIRTIO_SND_R_PCM_INFO called for stream %"PRIu32
 virtio_snd_handle_code(uint32_t val, const char *code) "ctrl code msg val = %"PRIu32" == %s"
 virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called"
 virtio_snd_handle_event(void) "event queue callback called"
hw/virtio/virtio-snd.c +78 -0

96 lines not on this page · raw message

Signature · 1 line
2.39.2
#18 lore raw

PATCH v6 12/12 docs/system: add basic virtio-snd documentation

In reply to Emmanouil Pitsidianakis

This commit adds basic documentation for using virtio-snd.

---
 docs/system/device-emulation.rst   |  1 +
 docs/system/devices/virtio-snd.rst | 36 ++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
 create mode 100644 docs/system/devices/virtio-snd.rst
docs/system/device-emulation.rst +1 -0
index 4491c4cbf7..dae19446e5 100644
@@ -92,6 +92,7 @@ Emulated Devices
    devices/usb.rst
    devices/vhost-user.rst
    devices/virtio-pmem.rst
+   devices/virtio-snd.rst
    devices/vhost-user-rng.rst
    devices/canokey.rst
    devices/usb-u2f.rst
docs/system/devices/virtio-snd.rst new file +36 -0
new file mode 100644
index 0000000000..b73de6b42d
@@ -0,0 +1,36 @@
+virtio-snd
+==========
+
+This document explains the setup and usage of the virtio-snd device.
+The virtio-snd device is a paravirtualized sound card device.
+
+Linux kernel support
+--------------------
+
+virtio-snd requires a guest Linux kernel built with the
+``CONFIG_SND_VIRTIO`` option.
+
+Description
+-----------
+
+virtio-snd implements capture and playback from inside a guest using the
+configured audio backend of the host machine.
+
+Examples
+--------
+
+Add a PCI device:
+
+::
+
+  -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``
+   or ``-audio driver=pa,model=virtio-sound,server=/run/user/1000/pulse/native``
+ * sdl: ``-audio driver=sdl,model=virtio-sound``
+ * coreaudio: ``-audio driver=coreaudio,model=virtio-sound``
+
+etc.
Signature · 1 line
2.39.2
#14 lore raw

PATCH v6 9/12 virtio-sound: handle VIRTIO_SND_PCM_RELEASE

In reply to Emmanouil Pitsidianakis

Handle the PCM release control request, which is necessary for flushing pending sound IO. No IO is handled yet so currently it only replies to the request.

---
 hw/virtio/trace-events |  1 +
 hw/virtio/virtio-snd.c | 38 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 38 insertions(+), 1 deletion(-)
hw/virtio/trace-events +1 -0
index f70cde4f01..60ab62a80d 100644
@@ -167,6 +167,7 @@ virtio_snd_handle_pcm_set_params(uint32_t stream) "VIRTIO_SND_PCM_SET_PARAMS cal
 virtio_snd_handle_ctrl(void *vdev, void *vq) "snd %p: handle ctrl event for queue %p"
 virtio_snd_handle_pcm_info(uint32_t stream) "VIRTIO_SND_R_PCM_INFO called for stream %"PRIu32
 virtio_snd_handle_pcm_start_stop(const char *code, uint32_t stream) "%s called for stream %"PRIu32
+virtio_snd_handle_pcm_release(uint32_t stream) "VIRTIO_SND_PCM_RELEASE called for stream %"PRIu32
 virtio_snd_handle_code(uint32_t val, const char *code) "ctrl code msg val = %"PRIu32" == %s"
 virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called"
 virtio_snd_handle_event(void) "event queue callback called"
hw/virtio/virtio-snd.c +37 -1
index f314b6270f..61e6b8ceea 100644
@@ -496,6 +496,42 @@ static void virtio_snd_handle_pcm_start_stop(VirtIOSound *s,
     }
 }
 
+/*
+ * Handles VIRTIO_SND_R_PCM_RELEASE. Releases the buffer resources allocated to
+ * a stream.
+ *
+ * @s: VirtIOSound device
+ * @cmd: The request command queue element from VirtIOSound cmdq field
+ */
+static void virtio_snd_handle_pcm_release(VirtIOSound *s,
+                                          virtio_snd_ctrl_command *cmd)
+{
+    uint32_t stream_id;
+    VirtIOSoundPCMStream *stream;
+    size_t sz = iov_to_buf(cmd->elem->out_sg,
+                           cmd->elem->out_num,
+                           sizeof(virtio_snd_hdr),
+                           &stream_id,
+                           sizeof(stream_id));
+    if (sz != sizeof(uint32_t)) {
+        cmd->resp.code = VIRTIO_SND_S_BAD_MSG;
+        return;
+    }
+
+    trace_virtio_snd_handle_pcm_release(stream_id);
+
+    stream = virtio_snd_pcm_get_stream(s, stream_id);
+    if (!stream) {
+        error_report("already released stream %"PRIu32, stream_id);
+        virtio_error(VIRTIO_DEVICE(s),
+                     "already released stream %"PRIu32,
+                     stream_id);
+        cmd->resp.code = VIRTIO_SND_S_BAD_MSG;
+        return;
+    }
+    cmd->resp.code = VIRTIO_SND_S_OK;
+}
+
 /*
  * The actual processing done in virtio_snd_process_cmdq().
  *
@@ -543,7 +579,7 @@ process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cmd)
         virtio_snd_handle_pcm_prepare(s, cmd);
         break;
     case VIRTIO_SND_R_PCM_RELEASE:
-        cmd->resp.code = VIRTIO_SND_S_NOT_SUPP;
+        virtio_snd_handle_pcm_release(s, cmd);
         break;
     case VIRTIO_SND_R_CHMAP_INFO:
         qemu_log_mask(LOG_UNIMP,
Signature · 1 line
2.39.2
#16 lore raw

PATCH v6 11/12 virtio-sound: implement audio capture (RX)

In reply to Emmanouil Pitsidianakis

To perform audio capture we duplicate the TX logic of the previous commit with the following difference: we receive data from the QEMU audio backend and write it in the virt queue IO buffers the guest sends to QEMU. When they are full (i.e. they have period_bytes amount of data) or when recording stops in QEMU's audio backend, the buffer is returned to the guest by notifying it.

---
 hw/virtio/trace-events |   3 +-
 hw/virtio/virtio-snd.c | 233 +++++++++++++++++++++++++++++++++++------
 2 files changed, 203 insertions(+), 33 deletions(-)
hw/virtio/trace-events +2 -1
index 3b95e745c2..a5829b112c 100644
@@ -172,4 +172,5 @@ virtio_snd_handle_code(uint32_t val, const char *code) "ctrl code msg val = %"PR
 virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called"
 virtio_snd_handle_event(void) "event queue callback called"
 virtio_snd_pcm_stream_flush(uint32_t stream) "flushing stream %"PRIu32
-virtio_snd_handle_xfer(void) "tx/rx queue callback called"
+virtio_snd_handle_tx_xfer(void) "tx queue callback called"
+virtio_snd_handle_rx_xfer(void) "tx queue callback called"
hw/virtio/virtio-snd.c +201 -32

332 lines not on this page · raw message

Signature · 1 line
2.39.2
#17 lore raw

Re: [PATCH v6 11/12] virtio-sound: implement audio capture (RX)

typo error? this should be rx queue callback called ?

+virtio_snd_handle_rx_xfer(void) "tx queue callback called"

Quoted 380 lines
From: "Emmanouil Pitsidianakis"<[email protected]>
Date:  Tue, Aug 15, 2023, 17:10
Subject:  [PATCH v6 11/12] virtio-sound: implement audio capture (RX)
To: <[email protected]>

Cc: "Emmanouil Pitsidianakis"<[email protected]>, "Igor Skalkin"<[email protected]>, "Anton Yakovlev"<[email protected]>, "Paolo Bonzini"<[email protected]>, "Gerd Hoffmann"<[email protected]>, "Michael S. Tsirkin"<[email protected]>, "Marcel Apfelbaum"<[email protected]>, "Daniel P. Berrangé"<[email protected]>, "Eduardo Habkost"<[email protected]>, "Marc-André Lureau"<[email protected]>, "Volker Rümelin"<[email protected]>, "Kővágó, Zoltán"<[email protected]>, "Alex Bennée"<[email protected]>, "Philippe Mathieu-Daudé"<[email protected]> To perform audio capture we duplicate the TX logic of the previous
commit with the following difference: we receive data from the QEMU
audio backend and write it in the virt queue IO buffers the guest sends
to QEMU. When they are full (i.e. they have period_bytes amount of
data) or when recording stops in QEMU's audio backend, the buffer is
returned to the guest by notifying it.

---
 hw/virtio/trace-events |   3 +-
 hw/virtio/virtio-snd.c | 233 +++++++++++++++++++++++++++++++++++------
 2 files changed, 203 insertions(+), 33 deletions(-)
hw/virtio/trace-events +2 -1
index 3b95e745c2..a5829b112c 100644
@@ -172,4 +172,5 @@ virtio_snd_handle_code(uint32_t val, const char *code) "ctrl code msg val = %"PR
 virtio_snd_handle_chmap_info(void) "VIRTIO_SND_CHMAP_INFO called"
 virtio_snd_handle_event(void) "event queue callback called"
 virtio_snd_pcm_stream_flush(uint32_t stream) "flushing stream %"PRIu32
-virtio_snd_handle_xfer(void) "tx/rx queue callback called"
+virtio_snd_handle_tx_xfer(void) "tx queue callback called"
+virtio_snd_handle_rx_xfer(void) "tx queue callback called"
hw/virtio/virtio-snd.c +201 -32
index 926f6bbf62..b3e175a03c 100644
@@ -26,18 +26,21 @@
 
 #define VIRTIO_SOUND_VM_VERSION 1
 #define VIRTIO_SOUND_JACK_DEFAULT 0
-#define VIRTIO_SOUND_STREAM_DEFAULT 1
+#define VIRTIO_SOUND_STREAM_DEFAULT 2
 #define VIRTIO_SOUND_CHMAP_DEFAULT 0
 #define VIRTIO_SOUND_HDA_FN_NID 0
 
 static void virtio_snd_pcm_out_cb(void *data, int available);
 static void virtio_snd_process_cmdq(VirtIOSound *s);
-static void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream);
-static uint32_t
-virtio_snd_pcm_read_write(VirtIOSoundPCMStream *stream,
-                          VirtQueue *vq,
-                          VirtQueueElement *element,
-                          bool read);
+static void virtio_snd_pcm_out_flush(VirtIOSoundPCMStream *stream);
+static void virtio_snd_pcm_in_flush(VirtIOSoundPCMStream *stream);
+static void virtio_snd_pcm_in_cb(void *data, int available);
+static uint32_t virtio_snd_pcm_write(VirtIOSoundPCMStream *stream,
+                                     VirtQueue *vq,
+                                     VirtQueueElement *element);
+static uint32_t virtio_snd_pcm_read(VirtIOSoundPCMStream *stream,
+                                    VirtQueue *vq,
+                                    VirtQueueElement *element);
 
 static uint32_t supported_formats = BIT(VIRTIO_SND_PCM_FMT_S8)
                                   | BIT(VIRTIO_SND_PCM_FMT_U8)
@@ -403,6 +406,9 @@ static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream)
         if (stream->direction == VIRTIO_SND_D_OUTPUT) {
             AUD_close_out(&stream->pcm->snd->card, stream->voice.out);
             stream->voice.out = NULL;
+        } else if (stream->direction == VIRTIO_SND_D_INPUT) {
+            AUD_close_in(&stream->pcm->snd->card, stream->voice.in);
+            stream->voice.in = NULL;
         }
         qemu_mutex_destroy(&stream->queue_mutex);
         g_free(stream);
@@ -465,7 +471,12 @@ static uint32_t virtio_snd_pcm_prepare_impl(VirtIOSound *s, uint32_t stream_id)
                                          virtio_snd_pcm_out_cb,
                                          &as);
     } else {
-        qemu_log_mask(LOG_UNIMP, "virtio_snd: input/capture is unimplemented.");
+        stream->voice.in = AUD_open_in(&s->card,
+                                        stream->voice.in,
+                                        "virtio_snd_card",
+                                        stream,
+                                        virtio_snd_pcm_in_cb,
+                                        &as);
     }
 
     stream->as = as;
@@ -530,6 +541,8 @@ static void virtio_snd_handle_pcm_start_stop(VirtIOSound *s,
     if (stream) {
         if (stream->direction == VIRTIO_SND_D_OUTPUT) {
             AUD_set_active_out(stream->voice.out, start);
+        } else {
+            AUD_set_active_in(stream->voice.in, start);
         }
     } else {
         error_report("Invalid stream id: %"PRIu32, req.stream_id);
@@ -601,7 +614,11 @@ static void virtio_snd_handle_pcm_release(VirtIOSound *s,
          */
         virtio_snd_process_cmdq(stream->s);
         trace_virtio_snd_pcm_stream_flush(stream_id);
-        virtio_snd_pcm_flush(stream);
+        if (stream->direction == VIRTIO_SND_D_OUTPUT) {
+            virtio_snd_pcm_out_flush(stream);
+        } else {
+            virtio_snd_pcm_in_flush(stream);
+        }
     }
 
     cmd->resp.code = VIRTIO_SND_S_OK;
@@ -760,7 +777,7 @@ static void virtio_snd_handle_event(VirtIODevice *vdev, VirtQueue *vq)
  * @vdev: VirtIOSound device
  * @vq: tx virtqueue
  */
-static void virtio_snd_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
+static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq)
 {
     VirtIOSound *s = VIRTIO_SND(vdev);
     VirtIOSoundPCMStream *stream = NULL;
@@ -769,7 +786,7 @@ static void virtio_snd_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
     virtio_snd_pcm_xfer hdr;
     virtio_snd_pcm_status resp = { 0 };
 
-    trace_virtio_snd_handle_xfer();
+    trace_virtio_snd_handle_tx_xfer();
 
     for (;;) {
         elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
@@ -793,11 +810,11 @@ static void virtio_snd_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
             goto tx_err;
         }
 
+        assert(hdr.stream_id != VIRTIO_SND_D_INPUT);
         WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
-            virtio_snd_pcm_read_write(stream,
+            virtio_snd_pcm_write(stream,
                     vq,
-                    elem,
-                    hdr.stream_id == VIRTIO_SND_D_INPUT);
+                    elem);
 
             resp.status = VIRTIO_SND_S_OK;
             iov_from_buf(elem->in_sg,
@@ -827,12 +844,54 @@ tx_err:
 }
 
 /*
- * Stub buffer virtqueue handler.
+ * The rx virtqueue handler. Makes the buffers available to their respective
+ * streams for consumption.
  *
  * @vdev: VirtIOSound device
- * @vq: virtqueue
+ * @vq: tx virtqueue
  */
-static void virtio_snd_handle_xfer(VirtIODevice *vdev, VirtQueue *vq) {}
+static void virtio_snd_handle_rx_xfer(VirtIODevice *vdev, VirtQueue *vq)
+{
+    VirtIOSound *s = VIRTIO_SND(vdev);
+    VirtIOSoundPCMStream *stream = NULL;
+    VirtQueueElement *elem;
+    size_t sz;
+    virtio_snd_pcm_xfer hdr;
+
+    trace_virtio_snd_handle_rx_xfer();
+
+    for (;;) {
+        elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
+        if (!elem) {
+            break;
+        }
+        /* get the message hdr object */
+        sz = iov_to_buf(elem->out_sg,
+                        elem->out_num,
+                        0,
+                        &hdr,
+                        sizeof(hdr));
+        if (sz != sizeof(hdr)
+            || hdr.stream_id >= s->snd_conf.streams
+            || !s->pcm->streams[hdr.stream_id]) {
+            continue;
+        }
+
+        stream = s->pcm->streams[hdr.stream_id];
+        if (stream->direction == VIRTIO_SND_D_OUTPUT) {
+            continue;
+        }
+        WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
+            virtio_snd_pcm_read(stream, vq, elem);
+        }
+    }
+
+    /*
+     * Notify vq about virtio_snd_pcm_status responses.
+     * Buffer responses must be notified separately later.
+     */
+    virtio_notify(VIRTIO_DEVICE(s), vq);
+}
 
 static uint64_t get_features(VirtIODevice *vdev, uint64_t features,
                              Error **errp)
@@ -961,8 +1020,8 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp)
     virtio_snd_common_realize(dev,
                               virtio_snd_handle_ctrl,
                               virtio_snd_handle_event,
-                              virtio_snd_handle_tx,
-                              virtio_snd_handle_xfer,
+                              virtio_snd_handle_tx_xfer,
+                              virtio_snd_handle_rx_xfer,
                               errp);
 }
 
@@ -1013,26 +1072,119 @@ static void virtio_snd_pcm_out_cb(void *data, int available)
 }
 
 /*
- * Flush all buffer data from this stream's queue into the driver's virtual
- * queue.
+ * AUD_* input callback.
  *
- * @stream: VirtIOSoundPCMStream *stream
+ * @data: VirtIOSoundPCMStream stream
+ * @available: number of bytes that can be read with AUD_read()
  */
-static void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream)
+static void virtio_snd_pcm_in_cb(void *data, int available)
 {
+    VirtIOSoundPCMStream *stream = data;
     VirtIOSoundPCMBlock *block;
-    VirtIOSoundPCMBlock *next;
+    uint32_t sz;
+    virtio_snd_pcm_status resp = { 0 };
+    size_t size;
 
     WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {
-        QSIMPLEQ_FOREACH_SAFE(block, &stream->queue, entry, next) {
-            AUD_write(stream->voice.out, block->data, block->size);
-            virtqueue_push(block->vq, block->elem, sizeof(block->elem));
-            virtio_notify(VIRTIO_DEVICE(stream->s), block->vq);
-            QSIMPLEQ_REMOVE(&stream->queue, block, VirtIOSoundPCMBlock, entry);
+        while (!QSIMPLEQ_EMPTY(&stream->queue)) {
+            block = QSIMPLEQ_FIRST(&stream->queue);
+
+            for (;;) {
+                size = AUD_read(stream->voice.in,
+                        block->data + block->offset,
+                        MIN(stream->period_bytes - block->offset, available));
+                block->offset += size;
+                block->size += size;
+                if (size == 0 || block->size >= stream->period_bytes) {
+                    resp.status = VIRTIO_SND_S_OK;
+                     sz = iov_from_buf(block->elem->in_sg,
+                                  block->elem->in_num,
+                                  0,
+                                  &resp,
+                                  sizeof(resp));
+
+                    /* Copy data -if any- to guest */
+                    if (block->size) {
+                        iov_from_buf(block->elem->in_sg,
+                                     block->elem->in_num,
+                                     sz,
+                                     &block->data,
+                                     MIN(stream->period_bytes, block->size));
+                    }
+                    virtqueue_push(block->vq,
+                            block->elem,
+                            sizeof(block->elem));
+                    virtio_notify(VIRTIO_DEVICE(stream->s),
+                            block->vq);
+                    QSIMPLEQ_REMOVE_HEAD(&stream->queue, entry);
+                    g_free(block);
+                    available -= size;
+                    break;
+                }
+
+                available -= size;
+                if (!available) {
+                    break;
+                }
+            }
+            if (!available) {
+                break;
+            }
         }
     }
 }
 
+#define virtio_snd_pcm_flush(AUD_CB)                                    \
+    VirtIOSoundPCMBlock *block;                                         \
+    VirtIOSoundPCMBlock *next;                                          \
+    WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) {                        \
+        QSIMPLEQ_FOREACH_SAFE(block, &stream->queue, entry, next) {     \
+            do {                                                        \
+                AUD_CB;                                                 \
+            } while (0)                                                 \
+            ;                                                           \
+            virtqueue_push(block->vq, block->elem, sizeof(block->elem));\
+            virtio_notify(VIRTIO_DEVICE(stream->s), block->vq);         \
+            QSIMPLEQ_REMOVE(&stream->queue,                             \
+                            block,                                      \
+                            VirtIOSoundPCMBlock,                        \
+                            entry);                                     \
+        }                                                               \
+    }                                                                   \
+
+
+/*
+ * Flush all buffer data from this output stream's queue into the driver's
+ * virtual queue.
+ *
+ * @stream: VirtIOSoundPCMStream *stream
+ */
+static void virtio_snd_pcm_out_flush(VirtIOSoundPCMStream *stream)
+{
+    virtio_snd_pcm_flush(
+            AUD_write(stream->voice.out,
+                              block->data,
+                              block->size);
+            );
+}
+
+/*
+ * Flush all buffer data from this input stream's queue into the driver's
+ * virtual queue.
+ *
+ * @stream: VirtIOSoundPCMStream *stream
+ */
+static void virtio_snd_pcm_in_flush(VirtIOSoundPCMStream *stream)
+{
+    virtio_snd_pcm_flush(
+            iov_from_buf(block->elem->in_sg,
+                         block->elem->in_num,
+                         sizeof(virtio_snd_pcm_info),
+                         block->data,
+                         block->offset);
+            );
+}
+
 static void virtio_snd_unrealize(DeviceState *dev)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
@@ -1068,10 +1220,9 @@ static void virtio_snd_unrealize(DeviceState *dev)
 
 
 static uint32_t
-virtio_snd_pcm_read_write(VirtIOSoundPCMStream *stream,
+virtio_snd_pcm_write(VirtIOSoundPCMStream *stream,
                           VirtQueue *vq,
-                          VirtQueueElement *element,
-                          bool read)
+                          VirtQueueElement *element)
 {
     VirtIOSoundPCMBlock *fragment;
     size_t size = iov_size(element->out_sg, element->out_num) -
@@ -1093,6 +1244,24 @@ virtio_snd_pcm_read_write(VirtIOSoundPCMStream *stream,
     return fragment->size;
 }
 
+static uint32_t
+virtio_snd_pcm_read(VirtIOSoundPCMStream *stream,
+                          VirtQueue *vq,
+                          VirtQueueElement *element)
+{
+    VirtIOSoundPCMBlock *fragment;
+
+    fragment = g_malloc0(sizeof(VirtIOSoundPCMBlock) + stream->period_bytes);
+    fragment->elem = element;
+    fragment->vq = vq;
+    fragment->size = 0;
+    fragment->offset = 0;
+
+    QSIMPLEQ_INSERT_TAIL(&stream->queue, fragment, entry);
+
+    return fragment->size;
+}
+
 static void virtio_snd_reset(VirtIODevice *vdev)
 {
     VirtIOSound *s = VIRTIO_SND(vdev);
Signature · 1 line
2.39.2
#7 lore raw

Re: [PATCH v6 02/12] Add virtio-sound-pci device

In reply to Emmanouil Pitsidianakis

Hi Manos,

I see a core dump with this code on my host. I don't use the -audio command line argument and the audiodev_id variable is NULL. You can't use a global audiodev_id variable because each guest can have more than one virtio-sound-pci device and each virtio-sound-pci device may be connected to a different audio backend device.

(gdb) r
Starting program: /home/ruemelin/rpmbuild/BUILD/qemu-8.0.50-build/qemu-system-x86_64 -machine q35 -device virtio-sound-pci,audiodev=audio0 -audiodev pipewire,out.frequency=48000,in.frequency=48000,id=audio0

Thread 1 "qemu-system-x86" received signal SIGSEGV, Segmentation fault. 0x00007ffff1987dd1 in __strlen_avx2 () from /lib64/libc.so.6

(gdb) bt
#0  0x00007ffff1987dd1 in __strlen_avx2 () at /lib64/libc.so.6
#1  0x0000555555e19afd in qstring_from_str (str=0x0) at ../qemu-master/qobject/qstring.c:50
#2  0x0000555555cb254c in object_property_set_str
    (obj=obj@entry=0x555557d3db30, name=name@entry=0x555555fea6b9 "audiodev", value=<optimized out>, errp=0x5555567b4580 <error_abort>) at ../qemu-master/qom/object.c:1427
#3  0x0000555555caa84c in qdev_prop_set_string
    (dev=dev@entry=0x555557d3db30, name=name@entry=0x555555fea6b9 "audiodev", value=<optimized out>)
    at ../qemu-master/hw/core/qdev-properties.c:729
#4  0x0000555555c3e8d1 in virtio_snd_pci_realize (vpci_dev=0x555557d35760, errp=0x7fffffffd750)
    at ../qemu-master/hw/virtio/virtio-snd-pci.c:48
#5  0x00005555559d0549 in pci_qdev_realize (qdev=<optimized out>, errp=<optimized out>) at ../qemu-master/hw/pci/pci.c:2124
#6  0x0000555555cabad3 in device_set_realized (obj=<optimized out>, value=<optimized out>, errp=0x7fffffffd980)
    at ../qemu-master/hw/core/qdev.c:510
#7  0x0000555555caf4f6 in property_set_bool
    (obj=0x555557d35760, v=<optimized out>, name=<optimized out>, opaque=0x55555690f5f0, errp=0x7fffffffd980)
    at ../qemu-master/qom/object.c:2285
#8  0x0000555555cb2124 in object_property_set
     (obj=obj@entry=0x555557d35760, name=name@entry=0x555555f9d929 "realized", v=v@entry=0x555557d3fdc0, errp=errp@entry=0x7fffffffd980) at ../qemu-master/qom/object.c:1420
#9  0x0000555555cb5370 in object_property_set_qobject
    (obj=obj@entry=0x555557d35760, name=name@entry=0x555555f9d929 "realized", value=value@entry=0x555557d3ec50, errp=errp@entry=0x7fffffffd980) at ../qemu-master/qom/qom-qobject.c:28
#10 0x0000555555cb2705 in object_property_set_bool
    (obj=obj@entry=0x555557d35760, name=name@entry=0x555555f9d929 "realized", value=value@entry=true, errp=errp@entry=0x7fffffffd980--) at ../qemu-master/qom/object.c:1489
#11 0x0000555555cac42e in qdev_realize (dev=dev@entry=0x555557d35760, bus=bus@entry=0x555556d96610, errp=errp@entry=0x7fffffffd980) at ../qemu-master/hw/core/qdev.c:292
#12 0x0000555555a89917 in qdev_device_add_from_qdict (opts=opts@entry=0x555557d34740, from_json=from_json@entry=false, errp=0x7fffffffd980, errp@entry=0x5555567b4578 <error_fatal>) at ../qemu-master/softmmu/qdev-monitor.c:715 #13 0x0000555555a89a42 in qdev_device_add (opts=0x55555690bb80, errp=errp@entry=0x5555567b4578 <error_fatal>) at ../qemu-master/softmmu/qdev-monitor.c:734
#14 0x0000555555a8e0ef in device_init_func (opaque=<optimized out>, opts=<optimized out>, errp=0x5555567b4578 <error_fatal>) at ../qemu-master/softmmu/vl.c:1153
#15 0x0000555555e2d6f2 in qemu_opts_foreach (list=<optimized out>, func=func@entry=0x555555a8e0e0 <device_init_func>, opaque=opaque@entry=0x0, errp=errp@entry=0x5555567b4578 <error_fatal>) at ../qemu-master/util/qemu-option.c:1135 #16 0x0000555555a9087a in qemu_create_cli_devices () at ../qemu-master/softmmu/vl.c:2577
#17 qmp_x_exit_preconfig (errp=<optimized out>) at ../qemu-master/softmmu/vl.c:2645
#18 0x0000555555a9433b in qmp_x_exit_preconfig (errp=<optimized out>) at ../qemu-master/softmmu/vl.c:2639
#19 qemu_init (argc=<optimized out>, argv=<optimized out>) at ../qemu-master/softmmu/vl.c:3663
#20 0x0000555555888959 in main (argc=<optimized out>, argv=<optimized out>) at ../qemu-master/softmmu/main.c:47

Quoted 5 lines

This patch adds a PCI wrapper device for the virtio-sound device. It is necessary to instantiate a virtio-snd device in a guest. All sound logic will be added to the virtio-snd device in the following commits.

Quoted 7 lines

To add this device with a guest, 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

virtio-legacy should be disabled in the virtio-snd-pci.c code.

Quoted 44 lines

And an audio backend listed with -audio driver=help that works on your host machine, e.g.:

Pulseaudio:
-audio driver=pa,model=virtio-sound
or
-audio driver=pa,model=virtio-sound,server=/run/user/1000/pulse/native
sdl:
-audio driver=sdl,model=virtio-sound
coreaudio (macos/darwin):
-audio driver=coreaudio,model=virtio-sound
etc.

---
  hw/virtio/meson.build      |  1 +
  hw/virtio/virtio-snd-pci.c | 91 ++++++++++++++++++++++++++++++++++++++
  include/hw/pci/pci.h       |  1 +
  softmmu/qdev-monitor.c     |  1 +
  4 files changed, 94 insertions(+)

create mode 100644 hw/virtio/virtio-snd-pci.c

hw/virtio/meson.build +1 -0
index 120d4bfa0a..5e5a83a4ee 100644
@@ -63,6 +63,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'))
  virtio_pci_ss.add(when: 'CONFIG_VIRTIO_MD', if_true: files('virtio-md-pci.c'))
 
hw/virtio/virtio-snd-pci.c new file +4 -0
new file mode 100644
index 0000000000..53070b85f6
@@ -0,0 +1,91 @@
+/*
+ * VIRTIO Sound Device PCI Bindings
+ *
+ * Copyright (c) 2023 Emmanouil Pitsidianakis<[email protected]>

hw/virtio/virtio-snd-pci.c line 14

Quoted 10 lines
hw/virtio/virtio-snd-pci.c new file +10 -0
+ *
+ * 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"

+#include "qapi/error.h"
+#include "qom/object.h"

hw/virtio/virtio-snd-pci.c line 19

Quoted 5 lines
hw/virtio/virtio-snd-pci.c new file +5 -0
+
+typedef struct VirtIOSoundPCI VirtIOSoundPCI;
+
+/*
+ * virtio-snd-pci: This extends VirtioPCIProxy.

- * virtio-snd-pci: This extends VirtioPCIProxy.
+ * virtio-sound-pci: This extends VirtioPCIProxy.

hw/virtio/virtio-snd-pci.c line 23

Quoted 4 lines
hw/virtio/virtio-snd-pci.c new file +4 -0
+ */
+#define TYPE_VIRTIO_SND_PCI "virtio-sound-pci-base"
+DECLARE_INSTANCE_CHECKER(VirtIOSoundPCI, VIRTIO_SOUND_PCI,
+                         TYPE_VIRTIO_SND_PCI)

-#define TYPE_VIRTIO_SND_PCI "virtio-sound-pci-base" -DECLARE_INSTANCE_CHECKER(VirtIOSoundPCI, VIRTIO_SOUND_PCI,
- TYPE_VIRTIO_SND_PCI)
+#define TYPE_VIRTIO_SND_PCI "virtio-sound-pci" +DECLARE_INSTANCE_CHECKER(VirtIOSoundPCI, VIRTIO_SND_PCI, TYPE_VIRTIO_SND_PCI)

The virtio-pci code creates a base type automatically.

hw/virtio/virtio-snd-pci.c line 30

Quoted 7 lines
hw/virtio/virtio-snd-pci.c new file +7 -0
+
+struct VirtIOSoundPCI {
+    VirtIOPCIProxy parent;
+    VirtIOSound vdev;
+};
+
+static Property virtio_snd_pci_properties[] = {

+ DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
+ VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
+ DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),

You should advertize MSI vectors. I think MSI is the recommended way for PCI to raise interrupts. The Linux virtio sound device code uses 2 MSIs.

hw/virtio/virtio-snd-pci.c line 41

Quoted 11 lines
hw/virtio/virtio-snd-pci.c new file +11 -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 const char *audiodev_id;
-
-static int virtio_snd_init_pci(PCIBus *init_bus, const char *audiodev) -{
- audiodev_id = audiodev;
- return 0;
-}
-

hw/virtio/virtio-snd-pci.c line 44

Quoted 3 lines
hw/virtio/virtio-snd-pci.c new file +3 -0
+static void virtio_snd_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
+{
+    VirtIOSoundPCI *dev = VIRTIO_SOUND_PCI(vpci_dev);

- VirtIOSoundPCI *dev = VIRTIO_SOUND_PCI(vpci_dev);
+ VirtIOSoundPCI *dev = VIRTIO_SND_PCI(vpci_dev);

hw/virtio/virtio-snd-pci.c line 49

Quoted 5 lines
hw/virtio/virtio-snd-pci.c new file +5 -0
+    DeviceState *vdev = DEVICE(&dev->vdev);
+
+    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);
-    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);
+    virtio_pci_force_virtio_1(vpci_dev);
+    qdev_realize(vdev, BUS(&vpci_dev->bus), errp);

The virtio-sound-pci device is a modern virtio-pci device.

hw/virtio/virtio-snd-pci.c line 55

Quoted 6 lines
hw/virtio/virtio-snd-pci.c new file +6 -0
+}
+
+static void virtio_snd_pci_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);

- VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
+ VirtioPCIClass *vpci_k = VIRTIO_PCI_CLASS(klass);

hw/virtio/virtio-snd-pci.c line 58

Quoted 3 lines
hw/virtio/virtio-snd-pci.c new file +3 -0
+    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
+
+    vpciklass->realize = virtio_snd_pci_realize;
-    vpciklass->realize = virtio_snd_pci_realize;
+    device_class_set_props(dc, virtio_snd_pci_properties);
+    dc->desc = "Virtio Sound";

hw/virtio/virtio-snd-pci.c line 63

Quoted 5 lines
hw/virtio/virtio-snd-pci.c new file +5 -0
+    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->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
-    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SND;
-    pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
+    vpci_k->realize = virtio_snd_pci_realize;

The removed code is unnecessary virtio-legacy code.

hw/virtio/virtio-snd-pci.c line 65

Quoted 2 lines
hw/virtio/virtio-snd-pci.c new file +2 -0
+    pcidev_k->class_id = PCI_CLASS_MULTIMEDIA_AUDIO;
+    device_class_set_props(dc, virtio_snd_pci_properties);

- device_class_set_props(dc, virtio_snd_pci_properties);

hw/virtio/virtio-snd-pci.c line 70

Quoted 5 lines
hw/virtio/virtio-snd-pci.c new file +5 -0
+}
+
+static void virtio_snd_pci_instance_init(Object *obj)
+{
+    VirtIOSoundPCI *dev = VIRTIO_SOUND_PCI(obj);

- VirtIOSoundPCI *dev = VIRTIO_SOUND_PCI(obj);
+ VirtIOSoundPCI *dev = VIRTIO_SND_PCI(obj);

hw/virtio/virtio-snd-pci.c line 78

Quoted 8 lines
hw/virtio/virtio-snd-pci.c new file +8 -0
+
+    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",
-    .base_name     = TYPE_VIRTIO_SND_PCI,
-    .generic_name  = "virtio-sound-pci",
+    .generic_name  = TYPE_VIRTIO_SND_PCI,

hw/virtio/virtio-snd-pci.c line 83

Quoted 5 lines
hw/virtio/virtio-snd-pci.c new file +5 -0
+    .instance_size = sizeof(VirtIOSoundPCI),
+    .instance_init = virtio_snd_pci_instance_init,
+    .class_init    = virtio_snd_pci_class_init,
+};
+

+/* Create a Virtio Sound PCI device, so '-audio driver,model=virtio' works. */ +static int virtio_snd_pci_init(PCIBus *bus, const char *audiodev) +{
+ DeviceState *dev;
+

+    dev = qdev_new(TYPE_VIRTIO_SND_PCI);
+    qdev_prop_set_string(dev, "audiodev", audiodev);
+    qdev_realize_and_unref(dev, BUS(bus), &error_fatal);
+    return 0;

+}
+

hw/virtio/virtio-snd-pci.c line 88

Quoted 5 lines
hw/virtio/virtio-snd-pci.c new file +5 -0
+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);

- pci_register_soundhw("virtio-sound", "Virtio Sound Device",
- virtio_snd_init_pci);
+ pci_register_soundhw("virtio", "Virtio Sound", virtio_snd_pci_init);

The change above is not strictly necessary. But I think the -audio parameter implies an audio device and the -sound suffix in virtio-sound is redundant.

include/hw/pci/pci.h extern bool pci_available; · line 88

Quoted 12 lines
hw/virtio/virtio-snd-pci.c new file +3 -0
+}
+
+type_init(virtio_snd_pci_register);
include/hw/pci/pci.h +1 -0
index abdc1ef103..3cd5712035 100644
@@ -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

The virtio-sound-pci device is a modern virtio device. Adding the PCI_DEVICE_ID_VIRTIO_SND definition is not necessary. See the complete comment below in include/hw/pci/pci.h.

Quoted 14 lines
include/hw/pci/pci.h +0 -0
  /*
   * modern virtio-pci devices get their id assigned automatically,
softmmu/qdev-monitor.c +1 -0
index 74f4e41338..2e9835ad88 100644
@@ -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 },
#2 lore raw

Re: [PATCH v6 01/12] Add virtio-sound device stub

In reply to Emmanouil Pitsidianakis

Hi Manos,

Quoted 2 lines

Add a new VIRTIO device for the virtio sound device id. Functionality will be added in the following commits.

I think the virtio-snd.c code, the trace events and the Kconfig VIRTIO_SND should be moved to hw/audio. The code for nearly all audio devices is in this directory. This would be similar to other virtio devices. E.g. the virtio-scsi code is in hw/scsi and the virtio-net code is in hw/net.

With best regards,
Volker

Quoted 13 lines
---
  MAINTAINERS                    |   6 +
  hw/virtio/Kconfig              |   5 +
  hw/virtio/meson.build          |   1 +
  hw/virtio/trace-events         |   9 ++
  hw/virtio/virtio-snd.c         | 231 +++++++++++++++++++++++++++++++++
  include/hw/virtio/virtio-snd.h |  80 ++++++++++++
  6 files changed, 332 insertions(+)

create mode 100644 hw/virtio/virtio-snd.c create mode 100644 include/hw/virtio/virtio-snd.h

#19 lore raw
In reply to Emmanouil Pitsidianakis

Hi Manos,

I tested the virtio-sound-pci device. It seems the device works unreliably. Audio playback has a lot of dropouts. I can actually hear my mouse moving around. Audio recording with audacity doesn't work. Either recording stops with an error or the recorded stream is silent.

I'll see if I can change the code so audio playback works reliably. I don't think it makes sense to review the current code as it is. I will of course report any issues I find.

With best regards,
Volker

Quoted 53 lines

This patch series adds an audio device implementing the recent virtio sound spec (1.2) and a corresponding PCI wrapper device.

https://github.com/epilys/qemu-virtio-snd/tree/virtio-snd-v6

Main differences with v5 patch series [^v5] <[email protected]>:

- Free any existing PCM stream resources before allocating a new one.
- Add docs.

[^v5]:
https://lore.kernel.org/qemu-devel/[email protected]/

Previously:

[^v4]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^v3]:
https://lore.kernel.org/qemu-devel/[email protected]/

Emmanouil Pitsidianakis (12):
Add virtio-sound device stub
Add virtio-sound-pci device
virtio-sound: handle control messages and streams
virtio-sound: set PCM stream parameters
virtio-sound: handle VIRTIO_SND_R_PCM_INFO request
virtio-sound: handle VIRTIO_SND_R_PCM_{START,STOP}
virtio-sound: handle VIRTIO_SND_PCM_SET_PARAMS
virtio-sound: handle VIRTIO_SND_R_PCM_PREPARE
virtio-sound: handle VIRTIO_SND_PCM_RELEASE
virtio-sound: implement audio output (TX)
virtio-sound: implement audio capture (RX) docs/system: add basic virtio-snd documentation

  MAINTAINERS                        |    6 +
  docs/system/device-emulation.rst   |    1 +
  docs/system/devices/virtio-snd.rst |   36 +
  hw/virtio/Kconfig                  |    5 +
  hw/virtio/meson.build              |    2 +
  hw/virtio/trace-events             |   20 +
  hw/virtio/virtio-snd-pci.c         |   91 ++
  hw/virtio/virtio-snd.c             | 1308 ++++++++++++++++++++++++++++
  include/hw/pci/pci.h               |    1 +
  include/hw/virtio/virtio-snd.h     |  158 ++++
  softmmu/qdev-monitor.c             |    1 +
  11 files changed, 1629 insertions(+)

create mode 100644 docs/system/devices/virtio-snd.rst create mode 100644 hw/virtio/virtio-snd-pci.c create mode 100644 hw/virtio/virtio-snd.c create mode 100644 include/hw/virtio/virtio-snd.h

#3 lore raw

Re: [PATCH v6 01/12] Add virtio-sound device stub

In reply to Volker Rümelin

Hello Volker!

Quoted 1 line · Volker Rümelin

I think the virtio-snd.c code, the trace events and the Kconfig VIRTIO_SND should be moved to hw/audio. The code for nearly all audio devices is in this directory. This would be similar to other virtio devices. E.g. the virtio-scsi code is in hw/scsi and the virtio-net code is in hw/net.

This was where it was initially but in previous patchset versions it was recommended to move them to hw/virtio. I don't mind either approach though.

Manos

#20 lore raw
In reply to Volker Rümelin

Hello Volker,

Quoted 3 lines · Volker Rümelin

I tested the virtio-sound-pci device. It seems the device works unreliably. Audio playback has a lot of dropouts. I can actually hear my mouse moving around. Audio recording with audacity doesn't work. Either recording stops with an error or the recorded stream is silent.

I'll see if I can change the code so audio playback works reliably. I don't think it makes sense to review the current code as it is. I will of course report any issues I find.

have you been having this bad performance with pulseaudio/pipewire? Are you using alsa for playback/recording in the guest?

I am asking because this was my setup and I was wondering if it affected the code I ended up with. For me I had normal playback, except for a short delay at first (maybe something to do with alsa buffer lengths, I am not familiar with ALSA much).

If you can share your guest and host setup you used for this I can try replicating it.

Manos

#4 lore raw

Re: [PATCH v6 01/12] Add virtio-sound device stub

In reply to Manos Pitsidianakis

Am 21.08.23 um 08:06 schrieb Manos Pitsidianakis:

Quoted 6 lines

Hello Volker!

Quoted 1 line · Volker Rümelin

I think the virtio-snd.c code, the trace events and the Kconfig VIRTIO_SND should be moved to hw/audio. The code for nearly all audio devices is in this directory. This would be similar to other virtio devices. E.g. the virtio-scsi code is in hw/scsi and the virtio-net code is in hw/net.

This was where it was initially but in previous patchset versions it was recommended to move them to hw/virtio. I don't mind either approach though.

Hi Manos,

Ok, then don't change the directory. I guess I will have to discuss this with Alex first.

With best regards,
Volker

#21 lore raw
In reply to Manos Pitsidianakis

Am 21.08.23 um 08:10 schrieb Manos Pitsidianakis:

Quoted 5 lines

Hello Volker,

Quoted 2 lines · Volker Rümelin

I tested the virtio-sound-pci device. It seems the device works unreliably. Audio playback has a lot of dropouts. I can actually hear my mouse moving around. Audio recording with audacity doesn't work. Either recording stops with an error or the recorded stream is silent.

Quoted 8 lines
Quoted 1 line

I'll see if I can change the code so audio playback works reliably. I don't think it makes sense to review the current code as it is. I will of course report any issues I find.

have you been having this bad performance with pulseaudio/pipewire? Are you using alsa for playback/recording in the guest?

I am asking because this was my setup and I was wondering if it affected the code I ended up with. For me I had normal playback, except for a short delay at first (maybe something to do with alsa buffer lengths, I am not familiar with ALSA much).

If you can share your guest and host setup you used for this I can try replicating it.

Hi Manos,

on the host I use pipewire. The audio device used for playback and recording is a Intel HDA device. I also tested recording from the playback monitor of the HDA device. The important command line arguments are: ./qemu-system-x86_64 -machine q35 -device virtio-vga-gl,xres=1280,yres=768,bus=pcie.0 -display gtk,zoom-to-fit=off,gl=on -machine pcspk-audiodev=audio0 -device virtio-sound-pci,bus=pcie.0,audiodev=audio0 -audiodev pipewire,out.frequency=48000,in.frequency=48000,id=audio0

The guest is Linux OpenSUSE 15.5 system. The guest uses PulseAudio. This means audacity ALSA audio recording was routed through PulseAudio. Audacity doesn't really start recording but after a few seconds it reports 'Wait timed out' and 'Error opening sound device. Try changing the audio host, recording device and the project sample rate'.

When I start QEMU with -audiodev pipewire,out.mixing-engine=off,in.mixing-engine=off,id=audio0 audacity only records silence.

For playback I use Rhythmbox or Audacity. If you don't immediately hear dropouts try activating and deactivating the QEMU gtk window in quick succession. A slightly increased processor load like moving the mouse around also increases the dropout rate.

With best regards,
Volker

#5 lore raw

Re: [PATCH v6 01/12] Add virtio-sound device stub

In reply to Volker Rümelin
Quoted 10 lines · Volker Rümelin

Am 21.08.23 um 08:06 schrieb Manos Pitsidianakis:

Hello Volker!

On Sun, 20 Aug 2023 12:33, Volker Rümelin <[email protected]> wrote:

I think the virtio-snd.c code, the trace events and the Kconfig VIRTIO_SND should be moved to hw/audio. The code for nearly all audio devices is in this directory. This would be similar to other virtio devices. E.g. the virtio-scsi code is in hw/scsi and the virtio-net code is in hw/net.

Quoted 7 lines

This was where it was initially but in previous patchset versions it was recommended to move them to hw/virtio. I don't mind either approach though.

Hi Manos,

Ok, then don't change the directory. I guess I will have to discuss this with Alex first.

Yeah we are not super consistent about this in the code base.

There are a whole class of VirtIO devices which are either stubs for vhost-user or not the usual block/net devices where hw/virtio makes the most sense to keep things together.

Things like virtio-block and -net often share or integrate more closely with their sub-systems so that certainly makes an argument for including them in the respective subdirs. You also get arch specific virtio devices (e.g. s390x ccw devices) which live in the hw/$ARCH directories.

I would certainly argue they should all share the include/hw/virtio directory for their type and structure definitions. Whether virtio-sound deserves to be with the rest of the sound HW I guess depends on if it makes things easier to integrate? There will be a vhost-user-sound device at some point and that won't require integration with QEMU's internal sound APIs.

mst what do you think?

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro