Skip to contentSkip to conversation

Add VIRTIO sound card

PATCH Superseded Emmanouil Pitsidianakis 3 participants 17 messages Oct 19, 2023 – Oct 23, 2023 Arm/Amlogic Meson SoC support

v12 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v12 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.

v12 can be found online at:

https://gitlab.com/epilys/qemu/-/tree/virtio-snd-v12

Ref c660c8427853d90784a89b6f3b0c31e19cc3be95

Main differences with v11 patch series [^v11]:

- Moved devices from hw/virtio under hw/audio.

v10[^v10] -> v11[^v11]
======================

- Rebased against upstream, which has minor changes to the AUD_* API.
- Fixed noise in playback because of invalid bounds when accessing the audio data in the VirtQueueElement.
- Refactor invalid I/O message queue flushing into separate function.
- Removed attempt to write unwritten bytes to QEMU sound backend when
flushing: it should only happen when the stream STARTs.
- Set latency_bytes to buffer size when returning TX I/O message because it happens immediately after writing the last bytes to the QEMU backend, therefore there might be up to <buffer size> bytes to be played before all the buffer data has finished playing.
- Addressed [^v10] review comments:
- Refactored VirtIOSoundPCMBuffer return code into a function instead of using goto labels in output/input audio callbacks. (Suggested by <[email protected]>)

v9[^v9] -> v10[^v10]
====================

- Addressed [^v9] review comments.
- Copy buffer data just before playing it on the host instead of when the IO message arrives. This in most cases takes care of the buffer not being populated with data from the guest application when it firsts arrives.

v8[^v8] -> v9[^v9]
==================

- Addressed [^v8] review comments.
- Add cpu_to_le32(_) and le32_to_cpu(_) conversions for messages from/to the guest according to the virtio spec.
- Inlined some functions and types to reduce review complexity.
- Corrected the replies to IO messages; now both Playback and Capture work correctly for me. (If you hear cracks in pulseaudio+guest, try pipewire+guest).

v7[^v7] -> v8[^v8]
==================

- Addressed [^v7] review comments.
Functions that were called from more than one place for code re-use are not created until they are actually needed.
- Fixed cases where block->offset was not respected in Playback

v6[^v6] -> v7[^v7]
==================

- Removed minor stale/duplicate code.
- Addressed [^v6] review comments.
Notably, the audio driver name is now virtio instead of virtio-sound.
- Fixed some invalid pointer logic.
- Fixed minor typos and updated documentation.

v5[^v5] -> v6[^v6]
==================

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

v4[^v4] -> v5[^v5]
==================

- Use ERRP_GUARD() to propagate errors.
- Use virtio_add_feature() instead of XORing constants.
- Use %zu format specifier for size_t.

v3[^v3] -> v4[^v4]
==================

- Addressed review style comments.
- Split patches for easier review.

v2[^v2] -> v3[^v3]
==================

- Addressed review comments.

v1[^v1] -> v2[^v2]
==================

- Split virtio-snd and virtio-snd-pci devices to two commits
- Added audio capture support

Previously:

[^v11]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^v10]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^v9]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^v8]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^v7]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^v6]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^v5]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^v4]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^v3]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^v2]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^v1]:
https://lore.kernel.org/qemu-devel/[email protected]/

Emmanouil Pitsidianakis (11):
Add virtio-sound device stub
Add virtio-sound-pci device
virtio-sound: handle control messages and streams
virtio-sound: handle VIRTIO_SND_R_PCM_INFO request
virtio-sound: handle VIRTIO_SND_R_PCM_{START,STOP}
virtio-sound: handle VIRTIO_SND_R_PCM_SET_PARAMS
virtio-sound: handle VIRTIO_SND_R_PCM_PREPARE
virtio-sound: handle VIRTIO_SND_R_PCM_RELEASE
virtio-sound: implement audio output (TX)
virtio-sound: implement audio capture (RX) docs/system: add basic virtio-snd documentation

 MAINTAINERS                        |    9 +
 docs/system/device-emulation.rst   |    1 +
 docs/system/devices/virtio-snd.rst |   49 +
 hw/audio/Kconfig                   |    5 +
 hw/audio/meson.build               |    2 +
 hw/audio/trace-events              |   20 +
 hw/audio/virtio-snd-pci.c          |   93 ++
 hw/audio/virtio-snd.c              | 1409 ++++++++++++++++++++++++++++
 include/hw/audio/virtio-snd.h      |  235 +++++
 system/qdev-monitor.c              |    1 +
 10 files changed, 1824 insertions(+)
 create mode 100644 docs/system/devices/virtio-snd.rst
 create mode 100644 hw/audio/virtio-snd-pci.c
 create mode 100644 hw/audio/virtio-snd.c
 create mode 100644 include/hw/audio/virtio-snd.h

base-commit: deaca3fd30d3a8829160f8d3705d65ad83176800

Signature · 1 line
2.39.2
#1 lore raw

PATCH v12 1/11 Add virtio-sound device stub

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

Based-on: https://github.com/OpenSynergy/qemu/commit/5a2f350eec5d157b90d9c7b40a8e603f4da92471

---
 MAINTAINERS                   |   7 +
 hw/audio/Kconfig              |   5 +
 hw/audio/meson.build          |   1 +
 hw/audio/trace-events         |   9 ++
 hw/audio/virtio-snd.c         | 233 ++++++++++++++++++++++++++++++++++
 include/hw/audio/virtio-snd.h |  79 ++++++++++++
 6 files changed, 334 insertions(+)
 create mode 100644 hw/audio/virtio-snd.c
 create mode 100644 include/hw/audio/virtio-snd.h
MAINTAINERS +7 -0
index 9bd4fe378d..3b7c467280 100644
@@ -2267,6 +2267,13 @@ F: hw/virtio/virtio-mem-pci.h
 F: hw/virtio/virtio-mem-pci.c
 F: include/hw/virtio/virtio-mem.h
 
+virtio-snd
+M: Gerd Hoffmann <[email protected]>
+R: Manos Pitsidianakis <[email protected]>
+S: Supported
+F: hw/audio/virtio-snd.c
+F: include/hw/audio/virtio-snd.h
+
 nvme
 M: Keith Busch <[email protected]>
 M: Klaus Jensen <[email protected]>
hw/audio/Kconfig +5 -0
index d0993514a1..daf060e1be 100644
@@ -50,3 +50,8 @@ config CS4231
 
 config ASC
     bool
+
+config VIRTIO_SND
+    bool
+    default y
+    depends on VIRTIO
hw/audio/meson.build +1 -0
index 8805322f5c..7a503be1fd 100644
@@ -13,3 +13,4 @@ system_ss.add(when: 'CONFIG_PL041', if_true: files('pl041.c', 'lm4549.c'))
 system_ss.add(when: 'CONFIG_SB16', if_true: files('sb16.c'))
 system_ss.add(when: 'CONFIG_VT82C686', if_true: files('via-ac97.c'))
 system_ss.add(when: 'CONFIG_WM8750', if_true: files('wm8750.c'))
+system_ss.add(when: ['CONFIG_VIRTIO_SND', 'CONFIG_VIRTIO'], if_true: files('virtio-snd.c'))
hw/audio/trace-events +9 -0
index 059ce451f5..525ced2b34 100644
@@ -38,3 +38,12 @@ asc_write_fifo(const char fifo, int reg, unsigned size, int wrptr, int cnt, uint
 asc_write_reg(int reg, unsigned size, uint64_t value) "reg=0x%03x size=%u value=0x%"PRIx64
 asc_write_extreg(const char fifo, int reg, unsigned size, uint64_t value) "fifo %c reg=0x%03x size=%u value=0x%"PRIx64
 asc_update_irq(int irq, int a, int b) "set IRQ to %d (A: 0x%x B: 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/audio/virtio-snd.c new file +233 -0

233 lines not on this page · raw message

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

79 lines not on this page · raw message

Signature · 1 line
2.39.2
#2 lore raw

PATCH v12 2/11 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:

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

Based-on: https://github.com/OpenSynergy/qemu/commit/5a2f350eec5d157b90d9c7b40a8e603f4da92471

---
 MAINTAINERS               |  1 +
 hw/audio/meson.build      |  1 +
 hw/audio/virtio-snd-pci.c | 93 +++++++++++++++++++++++++++++++++++++++
 system/qdev-monitor.c     |  1 +
 4 files changed, 96 insertions(+)
 create mode 100644 hw/audio/virtio-snd-pci.c
MAINTAINERS +1 -0
index 3b7c467280..cd2663ada8 100644
@@ -2272,6 +2272,7 @@ M: Gerd Hoffmann <[email protected]>
 R: Manos Pitsidianakis <[email protected]>
 S: Supported
 F: hw/audio/virtio-snd.c
+F: hw/audio/virtio-snd-pci.c
 F: include/hw/audio/virtio-snd.h
 
 nvme
hw/audio/meson.build +1 -0
index 7a503be1fd..2990974449 100644
@@ -14,3 +14,4 @@ system_ss.add(when: 'CONFIG_SB16', if_true: files('sb16.c'))
 system_ss.add(when: 'CONFIG_VT82C686', if_true: files('via-ac97.c'))
 system_ss.add(when: 'CONFIG_WM8750', if_true: files('wm8750.c'))
 system_ss.add(when: ['CONFIG_VIRTIO_SND', 'CONFIG_VIRTIO'], if_true: files('virtio-snd.c'))
+system_ss.add(when: ['CONFIG_VIRTIO_SND', 'CONFIG_VIRTIO', 'CONFIG_VIRTIO_PCI'], if_true: files('virtio-snd-pci.c'))
hw/audio/virtio-snd-pci.c new file +93 -0

93 lines not on this page · raw message

system/qdev-monitor.c +1 -0
index 1b8005ae55..f007ee6953 100644
@@ -111,6 +111,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
#3 lore raw

Re: [PATCH v12 02/11] Add virtio-sound-pci device

Am 19.10.23 um 12:03 schrieb Emmanouil Pitsidianakis:

Quoted 40 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.

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:

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

Based-on: https://github.com/OpenSynergy/qemu/commit/5a2f350eec5d157b90d9c7b40a8e603f4da92471

---
 MAINTAINERS               |  1 +
 hw/audio/meson.build      |  1 +
 hw/audio/virtio-snd-pci.c | 93 +++++++++++++++++++++++++++++++++++++++
 system/qdev-monitor.c     |  1 +
 4 files changed, 96 insertions(+)
 create mode 100644 hw/audio/virtio-snd-pci.c
system/qdev-monitor.c +0 -0
index 1b8005ae55..f007ee6953 100644

system/qdev-monitor.c static const QDevAlias qdev_alias_table[] = { · line 113

Quoted 3 lines
system/qdev-monitor.c +0 -0
@@ -111,6 +111,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},

Hi Manos,

the virtio-sound device alias doesn't work.

Quoted 1 line

./qemu-system-x86_64 -machine q35 -device virtio-sound,audiodev=audio0

-audiodev pipewire,id=audio0
qemu-system-x86_64: -device virtio-sound,audiodev=audio0: No 'virtio-bus' bus found for device 'virtio-sound'

This can be fixed if you change

include/hw/audio/virtio-snd.h +0 -0
@@ -21,7 +21,7 @@

 #include "standard-headers/linux/virtio_ids.h"
 #include "standard-headers/linux/virtio_snd.h"

-#define TYPE_VIRTIO_SND "virtio-sound"
+#define TYPE_VIRTIO_SND "virtio-sound-device"
 #define VIRTIO_SND(obj) \
         OBJECT_CHECK(VirtIOSound, (obj), TYPE_VIRTIO_SND)

in patch 01/11.  The other virtio devices also have a device suffix in their type name. Additionally I would insert this line

+    { "virtio-sound-device", "virtio-sound", QEMU_ARCH_VIRTIO_MMIO},

here.

With best regards,
Volker

Quoted 4 lines

+ { "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 },

#4 lore raw

Re: [PATCH v12 02/11] Add virtio-sound-pci device

Hello Volker,

Quoted 1 line · Volker Rümelin

the virtio-sound device alias doesn't work.

Yes, there's no virtio-sound alias hence why it does not work.

I will wait a bit before spinning a new version of the series because the goal post keeps moving forward everytime there's a new version while there is still stuff to address. If this is the only change I'd like to hear from Gerd if this is ok to merge otherwise, and perhaps I can send a new version of this patch only?

Manos

#5 lore raw

Re: [PATCH v12 02/11] Add virtio-sound-pci device

Am 22.10.23 um 11:58 schrieb Manos Pitsidianakis:

Quoted 6 lines

Hello Volker,

Quoted 1 line · Volker Rümelin

the virtio-sound device alias doesn't work.

Yes, there's no virtio-sound alias hence why it does not work.

Sorry, I don't understand your last sentence. You add this alias with the following line in patch 02/11, so there is a virtio-sound alias. This alias doesn't work because there is a device with the same name.

+    { "virtio-sound-pci", "virtio-sound", QEMU_ARCH_VIRTIO_PCI},

With best regards,
Volker

Quoted 8 lines

I will wait a bit before spinning a new version of the series because the goal post keeps moving forward everytime there's a new version while there is still stuff to address. If this is the only change I'd like to hear from Gerd if this is ok to merge otherwise, and perhaps I can send a new version of this patch only?

Manos

#6 lore raw

Re: [PATCH v12 02/11] Add virtio-sound-pci device

Good morning Volker,

Quoted 3 lines · Volker Rümelin

Sorry, I don't understand your last sentence. You add this alias with the following line in patch 02/11, so there is a virtio-sound alias. This alias doesn't work because there is a device with the same name.

My bad, I thought you were talking about a non-pci alias and I also did not express myself clearly. Sorry for the noise and thanks for spotting this! :) Will send a new version.

Manos

#7 lore raw

PATCH v12 3/11 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.

Based-on: https://github.com/OpenSynergy/qemu/commit/5a2f350eec5d157b90d9c7b40a8e603f4da92471

---
 hw/audio/trace-events         |   4 +
 hw/audio/virtio-snd.c         | 487 +++++++++++++++++++++++++++++++++-
 include/hw/audio/virtio-snd.h | 113 +++++++-
 3 files changed, 595 insertions(+), 9 deletions(-)
hw/audio/trace-events +4 -0
index 525ced2b34..122d1403ef 100644
@@ -47,3 +47,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/audio/virtio-snd.c +480 -7

543 lines not on this page · raw message

include/hw/audio/virtio-snd.h +111 -2

124 lines not on this page · raw message

Signature · 1 line
2.39.2
#8 lore raw

PATCH v12 4/11 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.

Based-on: https://github.com/OpenSynergy/qemu/commit/5a2f350eec5d157b90d9c7b40a8e603f4da92471

---
 hw/audio/trace-events |  1 +
 hw/audio/virtio-snd.c | 82 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 83 insertions(+)
hw/audio/trace-events +1 -0
index 122d1403ef..6def414f96 100644
@@ -48,6 +48,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/audio/virtio-snd.c +82 -0

94 lines not on this page · raw message

Signature · 1 line
2.39.2
#9 lore raw

PATCH v12 5/11 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.

Based-on: https://github.com/OpenSynergy/qemu/commit/5a2f350eec5d157b90d9c7b40a8e603f4da92471

---
 hw/audio/trace-events |  1 +
 hw/audio/virtio-snd.c | 49 +++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 48 insertions(+), 2 deletions(-)
hw/audio/trace-events +1 -0
index 6def414f96..db48ff04fe 100644
@@ -49,6 +49,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/audio/virtio-snd.c +47 -2

63 lines not on this page · raw message

Signature · 1 line
2.39.2
#10 lore raw

PATCH v12 6/11 virtio-sound: handle VIRTIO_SND_R_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.

Based-on: https://github.com/OpenSynergy/qemu/commit/5a2f350eec5d157b90d9c7b40a8e603f4da92471

---
 hw/audio/trace-events |  1 +
 hw/audio/virtio-snd.c | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)
hw/audio/trace-events +1 -0
index db48ff04fe..3badcab2e8 100644
@@ -47,6 +47,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/audio/virtio-snd.c +34 -0
index fc7269f263..8c8d26fd3c 100644
@@ -287,6 +287,38 @@ uint32_t virtio_snd_set_pcm_params(VirtIOSound *s,
     return cpu_to_le32(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 = { 0 };
+    uint32_t stream_id;
+    size_t msg_sz = iov_to_buf(cmd->elem->out_sg,
+                               cmd->elem->out_num,
+                               0,
+                               &req,
+                               sizeof(virtio_snd_pcm_set_params));
+
+    if (msg_sz != sizeof(virtio_snd_pcm_set_params)) {
+        /*
+         * TODO: do we need to set DEVICE_NEEDS_RESET?
+         */
+        qemu_log_mask(LOG_GUEST_ERROR,
+                "%s: virtio-snd command size incorrect %zu vs \
+                %zu\n", __func__, msg_sz, sizeof(virtio_snd_pcm_set_params));
+        cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
+        return;
+    }
+    stream_id = le32_to_cpu(req.hdr.stream_id);
+    trace_virtio_snd_handle_pcm_set_params(stream_id);
+    cmd->resp.code = virtio_snd_set_pcm_params(s, stream_id, &req);
+}
+
 /*
  * Get a QEMU Audiosystem compatible format value from a VIRTIO_SND_PCM_FMT_*
  */
@@ -534,6 +566,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 = cpu_to_le32(VIRTIO_SND_S_NOT_SUPP);
Signature · 1 line
2.39.2
#11 lore raw

PATCH v12 7/11 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.

Based-on: https://github.com/OpenSynergy/qemu/commit/5a2f350eec5d157b90d9c7b40a8e603f4da92471

---
 hw/audio/virtio-snd.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
hw/audio/virtio-snd.c +24 -0
index 8c8d26fd3c..aa4a193508 100644
@@ -478,6 +478,28 @@ static const char *print_code(uint32_t code)
     #undef CASE
 };
 
+/*
+ * 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 msg_sz = iov_to_buf(cmd->elem->out_sg,
+                               cmd->elem->out_num,
+                               sizeof(virtio_snd_hdr),
+                               &stream_id,
+                               sizeof(stream_id));
+
+    stream_id = le32_to_cpu(stream_id);
+    cmd->resp.code = msg_sz == sizeof(stream_id)
+                   ? virtio_snd_pcm_prepare(s, stream_id)
+                   : cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
+}
+
 /*
  * Handles VIRTIO_SND_R_PCM_START.
  *
@@ -569,6 +591,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 = cpu_to_le32(VIRTIO_SND_S_NOT_SUPP);
         break;
Signature · 1 line
2.39.2
#12 lore raw

PATCH v12 8/11 virtio-sound: handle VIRTIO_SND_R_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.

Based-on: https://github.com/OpenSynergy/qemu/commit/5a2f350eec5d157b90d9c7b40a8e603f4da92471

---
 hw/audio/trace-events |  1 +
 hw/audio/virtio-snd.c | 48 ++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 48 insertions(+), 1 deletion(-)
hw/audio/trace-events +1 -0
index 3badcab2e8..33e24d0011 100644
@@ -51,6 +51,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/audio/virtio-snd.c +47 -1

60 lines not on this page · raw message

Signature · 1 line
2.39.2
#13 lore raw

PATCH v12 9/11 virtio-sound: implement audio output (TX)

In reply to Emmanouil Pitsidianakis

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

It allocates a VirtIOSoundPCMBuffer 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.

Based-on: https://github.com/OpenSynergy/qemu/commit/5a2f350eec5d157b90d9c7b40a8e603f4da92471

---
 hw/audio/trace-events         |   2 +
 hw/audio/virtio-snd.c         | 288 +++++++++++++++++++++++++++++++++-
 include/hw/audio/virtio-snd.h |  47 ++++++
 3 files changed, 332 insertions(+), 5 deletions(-)
hw/audio/trace-events +2 -0
index 33e24d0011..884108129b 100644
@@ -55,3 +55,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/audio/virtio-snd.c +283 -5

361 lines not on this page · raw message

include/hw/audio/virtio-snd.h +47 -0

60 lines not on this page · raw message

Signature · 1 line
2.39.2
#14 lore raw

PATCH v12 10/11 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/audio/trace-events |   3 +-
 hw/audio/virtio-snd.c | 262 ++++++++++++++++++++++++++++++++++++------
 2 files changed, 230 insertions(+), 35 deletions(-)
hw/audio/trace-events +2 -1
index 884108129b..b1870ff224 100644
@@ -56,4 +56,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) "rx queue callback called"
hw/audio/virtio-snd.c +228 -34

355 lines not on this page · raw message

Signature · 1 line
2.39.2
#15 lore raw

PATCH v12 11/11 docs/system: add basic virtio-snd documentation

In reply to Emmanouil Pitsidianakis

This commit adds basic documentation for using virtio-snd.

---
 MAINTAINERS                        |  1 +
 docs/system/device-emulation.rst   |  1 +
 docs/system/devices/virtio-snd.rst | 49 ++++++++++++++++++++++++++++++
 3 files changed, 51 insertions(+)
 create mode 100644 docs/system/devices/virtio-snd.rst
MAINTAINERS +1 -0
index cd2663ada8..ca273ddb0e 100644
@@ -2274,6 +2274,7 @@ S: Supported
 F: hw/audio/virtio-snd.c
 F: hw/audio/virtio-snd-pci.c
 F: include/hw/audio/virtio-snd.h
+F: docs/system/devices/virtio-snd.rst
 
 nvme
 M: Keith Busch <[email protected]>
docs/system/device-emulation.rst +1 -0
index 1167f3a9f2..d1f3277cb0 100644
@@ -93,6 +93,7 @@ Emulated Devices
    devices/vhost-user.rst
    devices/virtio-gpu.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 +49 -0

49 lines not on this page · raw message

Signature · 1 line
2.39.2
Tested-by on the v12 series
#16 lore raw
In reply to Emmanouil Pitsidianakis
Quoted 5 lines · Emmanouil Pitsidianakis

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

v12 can be found online at:

Quoted 7 lines

https://gitlab.com/epilys/qemu/-/tree/virtio-snd-v12

Ref c660c8427853d90784a89b6f3b0c31e19cc3be95

Main differences with v11 patch series [^v11]:

- Moved devices from hw/virtio under hw/audio.

OK I've tested record and playback and everything seems to be working fine so have a:

for the series.

Gerd,

Are you happy to merge?

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