Add VIRTIO sound card
v13 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.
v13 can be found online at:
https://gitlab.com/epilys/qemu/-/tree/virtio-snd-v13
Ref e7fb941cf7636fdff40cbdcdcd660dec5f15ca3c
Main differences with v12 patch series [^v12]:
- Fixed device aliases (thanks Volker Rümelin <[email protected]>)
v11[^v11] -> v12[^v12]
======================
- 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:
[^v12]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^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]/
Manos 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 | 2 + 10 files changed, 1825 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: 384dbdda94c0bba55bf186cccd3714bbb9b737e9
Signature · 1 line
2.39.2
PATCH v13 4/11 virtio-sound: handle VIRTIO_SND_R_PCM_INFO request
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
@@ -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
PATCH v13 1/11 Add virtio-sound device stub
In reply to Manos PitsidianakisAdd 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 ++++++++++++ system/qdev-monitor.c | 1 + 7 files changed, 335 insertions(+) create mode 100644 hw/audio/virtio-snd.c create mode 100644 include/hw/audio/virtio-snd.h
MAINTAINERS +7 -0
@@ -2282,6 +2282,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
@@ -50,3 +50,8 @@ config CS4231 config ASC bool + +config VIRTIO_SND + bool + default y + depends on VIRTIO
hw/audio/meson.build +1 -0
@@ -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
@@ -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
system/qdev-monitor.c +1 -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}, + { "virtio-sound-device", "virtio-sound", QEMU_ARCH_VIRTIO_MMIO }, { "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
PATCH v13 2/11 Add virtio-sound-pci device
In reply to Manos PitsidianakisThis 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
@@ -2287,6 +2287,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
@@ -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
@@ -112,6 +112,7 @@ static const QDevAlias qdev_alias_table[] = { { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_VIRTIO_CCW }, { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_VIRTIO_PCI}, { "virtio-sound-device", "virtio-sound", QEMU_ARCH_VIRTIO_MMIO }, + { "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
PATCH v13 7/11 virtio-sound: handle VIRTIO_SND_R_PCM_PREPARE
In reply to Manos PitsidianakisHandles 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
@@ -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
PATCH v13 5/11 virtio-sound: handle VIRTIO_SND_R_PCM_{START,STOP}
In reply to Manos PitsidianakisHandle 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
@@ -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
PATCH v13 6/11 virtio-sound: handle VIRTIO_SND_R_PCM_SET_PARAMS
In reply to Manos PitsidianakisHandle 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
@@ -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
@@ -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
PATCH v13 8/11 virtio-sound: handle VIRTIO_SND_R_PCM_RELEASE
In reply to Manos PitsidianakisHandle 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
@@ -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
PATCH v13 10/11 virtio-sound: implement audio capture (RX)
In reply to Manos PitsidianakisTo 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
@@ -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
PATCH v13 9/11 virtio-sound: implement audio output (TX)
In reply to Manos PitsidianakisHandle 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
@@ -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
PATCH v13 11/11 docs/system: add basic virtio-snd documentation
In reply to Manos PitsidianakisThis 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
@@ -2289,6 +2289,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
@@ -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
PATCH v13 3/11 virtio-sound: handle control messages and streams
In reply to Manos PitsidianakisReceive 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
@@ -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
Quoted 5 lines · Manos Pitsidianakis
This patch series adds an audio device implementing the recent virtio sound spec (1.2) and a corresponding PCI wrapper device.
v13 can be found online at:
Quoted 7 lines
https://gitlab.com/epilys/qemu/-/tree/virtio-snd-v13
Ref
e7fb941cf7636fdff40cbdcdcd660dec5f15ca3cMain differences with v12 patch series [^v12]:
- Fixed device aliases (thanks Volker Rümelin <[email protected]>)
Gentle ping. Is this ready to be taken through Gerd's audio tree or should it go another route? It would be nice to get it in for 8.2 and the freeze is coming.
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
Quoted 5 lines · Manos Pitsidianakis
This patch series adds an audio device implementing the recent virtio sound spec (1.2) and a corresponding PCI wrapper device.
v13 can be found online at:
Quoted 7 lines
https://gitlab.com/epilys/qemu/-/tree/virtio-snd-v13
Ref
e7fb941cf7636fdff40cbdcdcd660dec5f15ca3cMain differences with v12 patch series [^v12]:
- Fixed device aliases (thanks Volker Rümelin <[email protected]>)
to merge through the audio tree.
Quoted 151 lines
v11[^v11] -> v12[^v12]
======================- 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.
- Setlatency_bytesto 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.
- Addcpu_to_le32(_)andle32_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 whereblock->offsetwas not respected in Playbackv6[^v6] -> v7[^v7]
==================- Removed minor stale/duplicate code.
- Addressed [^v6] review comments.
Notably, the audio driver name is nowvirtioinstead ofvirtio-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.
- Usevirtio_add_feature()instead of XORing constants.
- Use %zu format specifier forsize_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 supportPreviously:
[^v12]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^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]/Manos Pitsidianakis (11):
Add virtio-sound device stub
Add virtio-sound-pci device
virtio-sound: handle control messages and streams
virtio-sound: handleVIRTIO_SND_R_PCM_INFOrequest
virtio-sound: handle VIRTIO_SND_R_PCM_{START,STOP}
virtio-sound: handleVIRTIO_SND_R_PCM_SET_PARAMS
virtio-sound: handleVIRTIO_SND_R_PCM_PREPARE
virtio-sound: handleVIRTIO_SND_R_PCM_RELEASE
virtio-sound: implement audio output (TX)
virtio-sound: implement audio capture (RX) docs/system: add basic virtio-snd documentationMAINTAINERS | 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 | 2 + 10 files changed, 1825 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.hbase-commit:
384dbdda94c0bba55bf186cccd3714bbb9b737e9Signature · 1 line
2.39.2
Quoted 18 lines · Alex Bennée
Quoted 16 lines · Michael S. Tsirkin
Quoted 5 lines · Manos Pitsidianakis
This patch series adds an audio device implementing the recent virtio sound spec (1.2) and a corresponding PCI wrapper device.
v13 can be found online at:
Quoted 7 lines
https://gitlab.com/epilys/qemu/-/tree/virtio-snd-v13
Ref
e7fb941cf7636fdff40cbdcdcd660dec5f15ca3cMain differences with v12 patch series [^v12]:
- Fixed device aliases (thanks Volker Rümelin <[email protected]>)
Quoted 8 lines
Quoted 1 line
to merge through the audio tree.
This hasn't been picked up yet and I'm worried we'll miss the soft-freeze. I guess this is the reason the entire audio stack is currently on "Odd Fixes".
Should we just re-spin with us as maintainers for virtio-sound and merge it through my tree?
Since there's no response I'll pick it up, don't worry.
Quoted 10 lines · Michael S. Tsirkin
Quoted 9 lines · Manos Pitsidianakis
This patch series adds an audio device implementing the recent virtio sound spec (1.2) and a corresponding PCI wrapper device.
v13 can be found online at:
https://gitlab.com/epilys/qemu/-/tree/virtio-snd-v13
Ref
e7fb941cf7636fdff40cbdcdcd660dec5f15ca3c
Quoted 7 lines
Quoted 3 lines
Main differences with v12 patch series [^v12]:
- Fixed device aliases (thanks Volker Rümelin <[email protected]>)
to merge through the audio tree.
This hasn't been picked up yet and I'm worried we'll miss the soft-freeze. I guess this is the reason the entire audio stack is currently on "Odd Fixes".
Should we just re-spin with us as maintainers for virtio-sound and merge it through my tree?
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
Quoted 21 lines · Michael S. Tsirkin
Quoted 20 lines · Alex Bennée
Quoted 17 lines · Michael S. Tsirkin
Quoted 5 lines · Manos Pitsidianakis
This patch series adds an audio device implementing the recent virtio sound spec (1.2) and a corresponding PCI wrapper device.
v13 can be found online at:
Quoted 7 lines
https://gitlab.com/epilys/qemu/-/tree/virtio-snd-v13
Ref
e7fb941cf7636fdff40cbdcdcd660dec5f15ca3cMain differences with v12 patch series [^v12]:
- Fixed device aliases (thanks Volker Rümelin <[email protected]>)
to merge through the audio tree.
Quoted 8 lines
Quoted 6 lines
This hasn't been picked up yet and I'm worried we'll miss the soft-freeze. I guess this is the reason the entire audio stack is currently on "Odd Fixes".
Should we just re-spin with us as maintainers for virtio-sound and merge it through my tree?
Since there's no response I'll pick it up, don't worry.
Thanks :-)
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
Quoted 2 lines · Manos Pitsidianakis
This patch series adds an audio device implementing the recent virtio sound spec (1.2) and a corresponding PCI wrapper device.
Pls send patches on top to fix checkpatch warnings.
https://gitlab.com/mstredhat/qemu/-/jobs/5470533893
Quoted 161 lines
v13 can be found online at:
https://gitlab.com/epilys/qemu/-/tree/virtio-snd-v13
Ref
e7fb941cf7636fdff40cbdcdcd660dec5f15ca3cMain differences with v12 patch series [^v12]:
- Fixed device aliases (thanks Volker Rümelin <[email protected]>)
v11[^v11] -> v12[^v12]
======================- 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.
- Setlatency_bytesto 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.
- Addcpu_to_le32(_)andle32_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 whereblock->offsetwas not respected in Playbackv6[^v6] -> v7[^v7]
==================- Removed minor stale/duplicate code.
- Addressed [^v6] review comments.
Notably, the audio driver name is nowvirtioinstead ofvirtio-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.
- Usevirtio_add_feature()instead of XORing constants.
- Use %zu format specifier forsize_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 supportPreviously:
[^v12]:
https://lore.kernel.org/qemu-devel/[email protected]/
[^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]/Manos Pitsidianakis (11):
Add virtio-sound device stub
Add virtio-sound-pci device
virtio-sound: handle control messages and streams
virtio-sound: handleVIRTIO_SND_R_PCM_INFOrequest
virtio-sound: handle VIRTIO_SND_R_PCM_{START,STOP}
virtio-sound: handleVIRTIO_SND_R_PCM_SET_PARAMS
virtio-sound: handleVIRTIO_SND_R_PCM_PREPARE
virtio-sound: handleVIRTIO_SND_R_PCM_RELEASE
virtio-sound: implement audio output (TX)
virtio-sound: implement audio capture (RX) docs/system: add basic virtio-snd documentationMAINTAINERS | 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 | 2 + 10 files changed, 1825 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.hbase-commit:
384dbdda94c0bba55bf186cccd3714bbb9b737e9Signature · 1 line
2.39.2
Quoted 9 lines · Michael S. Tsirkin
Quoted 2 lines · Manos Pitsidianakis
This patch series adds an audio device implementing the recent virtio sound spec (1.2) and a corresponding PCI wrapper device.
Pls send patches on top to fix checkpatch warnings.
Do you mean this one?
6583087d531f2e37a2e2ba2cef7d123e21f8b0ad:214: ERROR: spaces required around that ':' (ctx:VxE)
total: 1 errors, 0 warnings, 674 lines checked
If yes, it's a false warning. It's a switch statement with a macro and it confuses it with the ternary operator.