hw/audio/virtio-sound: basic migration support
v2 cover hw/audio/virtio-sound: basic migration support
From: Alexander Mikhalitsyn <[email protected]>
Dear friends,
This patchset is aimed to support virtio-sound live migration and based on
pre-existing Volker Rümelin's patches from qemu-devel lists [1]. I tried
to ask (https://lore.kernel.org/qemu-devel/[email protected]/)
if Volker is going to continue his work on this, but received no reply so I
just decided to go forward: take his patches, rebase and resend. Of course,
I kept all authorship/tags so I hope that it is fine.
Testing.
I manually validated this with Ubuntu 24.04 Desktop VM and found no issues. Also, it was validated with CI (https://gitlab.com/mihalicyn/qemu/-/pipelines/2626702088).
Original patchset:
https://lore.kernel.org/qemu-devel/[email protected] [1]
Git tree:
https://gitlab.com/mihalicyn/qemu/-/commits/virtio-sound-migration
Changelog for version 2:
- no code changes (only commit messages)
followed suggestions from Daniel P. Berrangé (thanks, Daniel!)
https://lore.kernel.org/qemu-devel/[email protected]/
and added some extra details about what I did with the original patches during rebase (for some of them it was simpler and less error prone to just drop changes and reimplement them by following the idea of the patch, cause base code has changed significantly).
Changelog relative to the original Volker Rümelin's submission:
- rebased:
- "hw/audio/virtio-sound: return correct command response size" was dropped as it was already merged
- "hw/audio/virtio-sound: fix segmentation fault in tx/rx xfer handler" was dropped as it seems to be replaced with another fix
https://github.com/qemu/qemu/commit/731655f87f319fd06f27282c6cafbc2467ac8045
- I had to drop all pre-existing Reviewed-by tags cause it was 2 years since this patchset was sent and reviewed and I did a quite significant code changes during rebase
Kind regards,
Alex
Volker Rümelin (9):
hw/audio/virtio-sound: remove command and stream mutexes hw/audio/virtio-sound: allocate an array of streams hw/audio/virtio-sound: free all stream buffers on reset hw/audio/virtio-sound: split out virtio_snd_pcm_start_stop() hw/audio/virtio-sound: add stream state variable hw/audio/virtio-sound: introduce virtio_snd_pcm_open() hw/audio/virtio-sound: introduce virtio_snd_set_active() hw/audio/virtio-sound: add missing vmstate fields hw/audio/virtio-sound: add placeholder for buffer write position
hw/audio/trace-events | 3 +- hw/audio/virtio-snd.c | 685 ++++++++++++++++++++-------------- include/hw/audio/virtio-snd.h | 25 +- 3 files changed, 407 insertions(+), 306 deletions(-)
Signature · 1 line
2.47.3
PATCH v2 1/9 hw/audio/virtio-sound: remove command and stream mutexes
From: Volker Rümelin <[email protected]>
All code in virtio-snd.c runs with the BQL held. Remove the command queue mutex and the stream queue mutexes. The qatomic functions are also not needed.
[AM: there were too many conflicts, I did git checkout --ours -- <.>
and then reimplemented the patch idea
/AM]
--- hw/audio/virtio-snd.c | 248 +++++++++++++++------------------- include/hw/audio/virtio-snd.h | 3 - 2 files changed, 110 insertions(+), 141 deletions(-)
hw/audio/virtio-snd.c +110 -138
@@ -19,7 +19,6 @@#include "qemu/iov.h" #include "qemu/log.h" #include "qemu/error-report.h" -#include "qemu/lockable.h" #include "system/runstate.h" #include "trace.h" #include "qapi/error.h"
@@ -442,7 +441,6 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id) stream->id = stream_id; stream->s = s; stream->latency_bytes =0 ; -qemu_mutex_init (&stream->queue_mutex);QSIMPLEQ_INIT (&stream->queue);/*
@@ -568,9 +566,7 @@ static void virtio_snd_handle_pcm_start_stop(VirtIOSound *s, stream =virtio_snd_pcm_get_stream (s, stream_id);if (stream) { - WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { - stream->active = start; - } + stream->active = start;if (stream->info.direction ==VIRTIO_SND_D_OUTPUT ) {audio_be_set_active_out (s->audio_be, stream->voice.out, start); }else {
@@ -594,10 +590,8 @@ static size_t virtio_snd_pcm_get_io_msgs_count(VirtIOSoundPCMStream *stream)VirtIOSoundPCMBuffer *buffer, *next;size_t count =0 ; - WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { - QSIMPLEQ_FOREACH_SAFE(buffer, &stream->queue, entry, next) { - count +=1 ; - } + QSIMPLEQ_FOREACH_SAFE(buffer, &stream->queue, entry, next) { + count +=1 ; }return count; }
@@ -739,23 +733,15 @@ static void virtio_snd_process_cmdq(VirtIOSound *s) {virtio_snd_ctrl_command *cmd; -if (unlikely (qatomic_read (&s->processing_cmdq))) { -return ; - } - - WITH_QEMU_LOCK_GUARD(&s->cmdq_mutex) { -qatomic_set (&s->processing_cmdq, true); -while (!QTAILQ_EMPTY (&s->cmdq)) { - cmd =QTAILQ_FIRST (&s->cmdq); +while (!QTAILQ_EMPTY (&s->cmdq)) { + cmd =QTAILQ_FIRST (&s->cmdq); -/* process command */ -process_cmd (s, cmd); +/* process command */ +process_cmd (s, cmd); -QTAILQ_REMOVE (&s->cmdq, cmd, next); +QTAILQ_REMOVE (&s->cmdq, cmd, next); -virtio_snd_ctrl_cmd_free (cmd); - } -qatomic_set (&s->processing_cmdq, false); +virtio_snd_ctrl_cmd_free (cmd); } }
@@ -892,19 +878,17 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq) goto tx_err; } - WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { - size =iov_size (elem->out_sg, elem->out_num) - msg_sz; + size =iov_size (elem->out_sg, elem->out_num) - msg_sz; - buffer =g_malloc0 (sizeof (VirtIOSoundPCMBuffer) + size); - buffer->elem = elem; - buffer->populated = false; - buffer->vq = vq; - buffer->size = size; - buffer->offset =0 ; - stream->latency_bytes += size; + buffer =g_malloc0 (sizeof (VirtIOSoundPCMBuffer) + size); + buffer->elem = elem; + buffer->populated = false; + buffer->vq = vq; + buffer->size = size; + buffer->offset =0 ; + stream->latency_bytes += size; -QSIMPLEQ_INSERT_TAIL (&stream->queue, buffer, entry); - } +QSIMPLEQ_INSERT_TAIL (&stream->queue, buffer, entry);continue ; tx_err:
@@ -973,16 +957,15 @@ static void virtio_snd_handle_rx_xfer(VirtIODevice *vdev, VirtQueue *vq)if (stream ==NULL || stream->info.direction !=VIRTIO_SND_D_INPUT ) { goto rx_err; } - WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { - size =iov_size (elem->in_sg, elem->in_num) - -sizeof (virtio_snd_pcm_status); - buffer =g_malloc0 (sizeof (VirtIOSoundPCMBuffer) + size); - buffer->elem = elem; - buffer->vq = vq; - buffer->size =0 ; - buffer->offset =0 ; -QSIMPLEQ_INSERT_TAIL (&stream->queue, buffer, entry); - } + + size =iov_size (elem->in_sg, elem->in_num) - +sizeof (virtio_snd_pcm_status); + buffer =g_malloc0 (sizeof (VirtIOSoundPCMBuffer) + size); + buffer->elem = elem; + buffer->vq = vq; + buffer->size =0 ; + buffer->offset =0 ; +QSIMPLEQ_INSERT_TAIL (&stream->queue, buffer, entry);continue ; rx_err:
@@ -1086,7 +1069,6 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp)virtio_add_queue (vdev,64 , virtio_snd_handle_tx_xfer); vsnd->queues[VIRTIO_SND_VQ_RX ] =virtio_add_queue (vdev,64 , virtio_snd_handle_rx_xfer); -qemu_mutex_init (&vsnd->cmdq_mutex);QTAILQ_INIT (&vsnd->cmdq);QSIMPLEQ_INIT (&vsnd->invalid);
@@ -1154,52 +1136,50 @@ static void virtio_snd_pcm_out_cb(void *data, int available)VirtIOSoundPCMBuffer *buffer;size_t size; - WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { -while (!QSIMPLEQ_EMPTY (&stream->queue)) { - buffer =QSIMPLEQ_FIRST (&stream->queue); -if (!virtio_queue_ready (buffer->vq)) { -return ; +while (!QSIMPLEQ_EMPTY (&stream->queue)) { + buffer =QSIMPLEQ_FIRST (&stream->queue); +if (!virtio_queue_ready (buffer->vq)) { +return ; + } +if (!stream->active) { +/* Stream has stopped, so do not perform audio_be_write. */ +return_tx_buffer (stream, buffer); +continue ; + } +if (!buffer->populated) { +iov_to_buf (buffer->elem->out_sg, + buffer->elem->out_num, +sizeof (virtio_snd_pcm_xfer), + buffer->data, + buffer->size); + buffer->populated = true; + } +for (;;) { + size =audio_be_write (stream->s->audio_be, + stream->voice.out, + buffer->data + buffer->offset, +MIN (buffer->size, available)); +assert (size <=MIN (buffer->size, available)); +if (size ==0 ) { +/* break out of both loops */ + available =0 ; +break ; } -if (!stream->active) { -/* Stream has stopped, so do not perform audio_be_write. */ + buffer->size -= size; + buffer->offset += size; + available -= size; +update_latency (stream, size); +if (buffer->size <1 ) {return_tx_buffer (stream, buffer); -continue ; - } -if (!buffer->populated) { -iov_to_buf (buffer->elem->out_sg, - buffer->elem->out_num, -sizeof (virtio_snd_pcm_xfer), - buffer->data, - buffer->size); - buffer->populated = true; - } -for (;;) { - size =audio_be_write (stream->s->audio_be, - stream->voice.out, - buffer->data + buffer->offset, -MIN (buffer->size, available)); -assert (size <=MIN (buffer->size, available)); -if (size ==0 ) { -/* break out of both loops */ - available =0 ; -break ; - } - buffer->size -= size; - buffer->offset += size; - available -= size; -update_latency (stream, size); -if (buffer->size <1 ) { -return_tx_buffer (stream, buffer); -break ; - } -if (!available) { -break ; - } +break ; }if (!available) {break ; } } +if (!available) { +break ; + } } }
@@ -1250,55 +1230,53 @@ static void virtio_snd_pcm_in_cb(void *data, int available)VirtIOSoundPCMBuffer *buffer;size_t size, max_size, to_read; - WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { -while (!QSIMPLEQ_EMPTY (&stream->queue)) { - buffer =QSIMPLEQ_FIRST (&stream->queue); -if (!virtio_queue_ready (buffer->vq)) { -return ; - } -if (!stream->active) { -/* Stream has stopped, so do not perform audio_be_read. */ -return_rx_buffer (stream, buffer); -continue ; - } +while (!QSIMPLEQ_EMPTY (&stream->queue)) { + buffer =QSIMPLEQ_FIRST (&stream->queue); +if (!virtio_queue_ready (buffer->vq)) { +return ; + } +if (!stream->active) { +/* Stream has stopped, so do not perform audio_be_read. */ +return_rx_buffer (stream, buffer); +continue ; + } + + max_size =iov_size (buffer->elem->in_sg, buffer->elem->in_num); +if (max_size <=sizeof (virtio_snd_pcm_status)) { +return_rx_buffer (stream, buffer); +continue ; + } + max_size -=sizeof (virtio_snd_pcm_status); - max_size =iov_size (buffer->elem->in_sg, buffer->elem->in_num); -if (max_size <=sizeof (virtio_snd_pcm_status)) { +for (;;) { +if (buffer->size >= max_size) {return_rx_buffer (stream, buffer); -continue ; +break ; } - max_size -=sizeof (virtio_snd_pcm_status); - -for (;;) { -if (buffer->size >= max_size) { -return_rx_buffer (stream, buffer); -break ; - } - to_read = stream->params.period_bytes - buffer->size; - to_read =MIN (to_read, available); - to_read =MIN (to_read, max_size - buffer->size); - size =audio_be_read (stream->s->audio_be, - stream->voice.in, - buffer->data + buffer->size, - to_read); -if (!size) { - available =0 ; -break ; - } - buffer->size += size; - available -= size; -if (buffer->size >= stream->params.period_bytes) { -return_rx_buffer (stream, buffer); -break ; - } -if (!available) { -break ; - } + to_read = stream->params.period_bytes - buffer->size; + to_read =MIN (to_read, available); + to_read =MIN (to_read, max_size - buffer->size); + size =audio_be_read (stream->s->audio_be, + stream->voice.in, + buffer->data + buffer->size, + to_read); +if (!size) { + available =0 ; +break ; + } + buffer->size += size; + available -= size; +if (buffer->size >= stream->params.period_bytes) { +return_rx_buffer (stream, buffer); +break ; }if (!available) {break ; } } +if (!available) { +break ; + } } }
@@ -1315,11 +1293,9 @@ static inline void virtio_snd_pcm_flush(VirtIOSoundPCMStream *stream) (stream->info.direction ==VIRTIO_SND_D_OUTPUT ) ? return_tx_buffer : return_rx_buffer; - WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { -while (!QSIMPLEQ_EMPTY (&stream->queue)) { - buffer =QSIMPLEQ_FIRST (&stream->queue); -cb (stream, buffer); - } +while (!QSIMPLEQ_EMPTY (&stream->queue)) { + buffer =QSIMPLEQ_FIRST (&stream->queue); +cb (stream, buffer); } }
@@ -1338,14 +1314,12 @@ static void virtio_snd_unrealize(DeviceState *dev)if (stream) {virtio_snd_process_cmdq (stream->s);virtio_snd_pcm_close (stream); -qemu_mutex_destroy (&stream->queue_mutex);g_free (stream); } }g_free (vsnd->pcm.streams); }g_free (vsnd->pcm.pcm_params); -qemu_mutex_destroy (&vsnd->cmdq_mutex);virtio_delete_queue (vsnd->queues[VIRTIO_SND_VQ_CONTROL ]);virtio_delete_queue (vsnd->queues[VIRTIO_SND_VQ_EVENT ]);virtio_delete_queue (vsnd->queues[VIRTIO_SND_VQ_TX ]);
@@ -1366,12 +1340,10 @@ static void virtio_snd_reset(VirtIODevice *vdev)*/ g_assert (QSIMPLEQ_EMPTY (&vsnd->invalid)); - WITH_QEMU_LOCK_GUARD(&vsnd->cmdq_mutex) { -while (!QTAILQ_EMPTY (&vsnd->cmdq)) { - cmd =QTAILQ_FIRST (&vsnd->cmdq); -QTAILQ_REMOVE (&vsnd->cmdq, cmd, next); -virtio_snd_ctrl_cmd_free (cmd); - } +while (!QTAILQ_EMPTY (&vsnd->cmdq)) { + cmd =QTAILQ_FIRST (&vsnd->cmdq); +QTAILQ_REMOVE (&vsnd->cmdq, cmd, next); +virtio_snd_ctrl_cmd_free (cmd); } }
include/hw/audio/virtio-snd.h +0 -3
@@ -146,7 +146,6 @@ struct VirtIOSoundPCMStream {SWVoiceIn *in;SWVoiceOut *out; } voice; -QemuMutex queue_mutex;bool active;uint32_t latency_bytes; QSIMPLEQ_HEAD(, VirtIOSoundPCMBuffer) queue;
@@ -218,9 +217,7 @@ struct VirtIOSound {AudioBackend *audio_be;VMChangeStateEntry *vmstate;virtio_snd_config snd_conf; -QemuMutex cmdq_mutex; QTAILQ_HEAD(, virtio_snd_ctrl_command) cmdq; -bool processing_cmdq;/* * Convenience queue to keep track of invalid tx/rx queue messages inside * the tx/rx callbacks.
Signature · 1 line
2.47.3
Re: [PATCH v2 1/9] hw/audio/virtio-sound: remove command and stream mutexes
Quoted 9 lines · Alexander Mikhalitsyn
All code in
virtio-snd.cruns with the BQL held. Remove the command queue mutex and the stream queue mutexes. The qatomic functions are also not needed.[AM: there were too many conflicts, I did
git checkout --ours -- <.>
and then reimplemented the patch idea
/AM][...]
Signature · 1 line
Marc-André Lureau <[email protected]>
PATCH v2 2/9 hw/audio/virtio-sound: allocate an array of streams
In reply to Alexander MikhalitsynFrom: Volker Rümelin <[email protected]>
It is much easier to migrate an array of structs than individual structs that are accessed via a pointer to a pointer to an array of pointers to struct.
For this reason, allocate an array of streams in virtio_snd_realize() and initialise all stream variables that are constant at runtime immediately after allocation.
This makes it easier to remove the virtio_snd_set_pcm_params() and virtio_snd_pcm_prepare() calls in the realisation phase and to migrate the audio streams of the virtio sound device after the next few patches.
[AM: there were too many conflicts, I did git checkout --ours -- <.>
and then reimplemented the patch idea
/AM]
--- hw/audio/virtio-snd.c | 35 ++++++++++++++++++++++------------- include/hw/audio/virtio-snd.h | 1 + 2 files changed, 23 insertions(+), 13 deletions(-)
hw/audio/virtio-snd.c +22 -13
@@ -436,12 +436,9 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id) stream =virtio_snd_pcm_get_stream (s, stream_id);if (stream ==NULL ) { - stream =g_new0 (VirtIOSoundPCMStream,1 ); + stream = &s->streams[stream_id]; stream->active = false; - stream->id = stream_id; - stream->s = s; stream->latency_bytes =0 ; -QSIMPLEQ_INIT (&stream->queue);/* * stream_id >= s->snd_conf.streams was checked before so this is
@@ -451,14 +448,6 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id) }virtio_snd_get_qemu_audsettings (&as, params); - stream->info.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->info.features =0 ; - stream->info.channels_min =1 ; - stream->info.channels_max = as.nchannels; - stream->info.formats = supported_formats; - stream->info.rates = supported_rates; stream->params = *params; stream->positions[0 ] =VIRTIO_SND_CHMAP_FL ;
@@ -1046,6 +1035,25 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp) vsnd->vmstate =qemu_add_vm_change_state_handler (virtio_snd_vm_state_change, vsnd); + vsnd->streams =g_new0 (VirtIOSoundPCMStream, vsnd->snd_conf.streams); + +for (uint32_t i =0 ; i < vsnd->snd_conf.streams; i++) { +VirtIOSoundPCMStream *stream = &vsnd->streams[i]; + + stream->id = i; + stream->s = vsnd; +QSIMPLEQ_INIT (&stream->queue); + stream->info.hdr.hda_fn_nid =VIRTIO_SOUND_HDA_FN_NID ; + stream->info.features =0 ; + stream->info.formats = supported_formats; + stream->info.rates = supported_rates; + stream->info.direction = + i < vsnd->snd_conf.streams /2 + (vsnd->snd_conf.streams &1 ) + ?VIRTIO_SND_D_OUTPUT :VIRTIO_SND_D_INPUT ; + stream->info.channels_min =1 ; + stream->info.channels_max =2 ; + } + vsnd->pcm.streams = g_new0(VirtIOSoundPCMStream *, vsnd->snd_conf.streams); vsnd->pcm.pcm_params =
@@ -1314,12 +1322,13 @@ static void virtio_snd_unrealize(DeviceState *dev)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.pcm_params); +g_free (vsnd->streams); + vsnd->streams =NULL ;virtio_delete_queue (vsnd->queues[VIRTIO_SND_VQ_CONTROL ]);virtio_delete_queue (vsnd->queues[VIRTIO_SND_VQ_EVENT ]);virtio_delete_queue (vsnd->queues[VIRTIO_SND_VQ_TX ]);
include/hw/audio/virtio-snd.h +1 -0
@@ -214,6 +214,7 @@ struct VirtIOSound {VirtQueue *queues[VIRTIO_SND_VQ_MAX ];uint64_t features;VirtIOSoundPCM pcm; +VirtIOSoundPCMStream *streams;AudioBackend *audio_be;VMChangeStateEntry *vmstate;virtio_snd_config snd_conf;
Signature · 1 line
2.47.3
Re: [PATCH v2 2/9] hw/audio/virtio-sound: allocate an array of streams
Quoted 9 lines · Alexander Mikhalitsyn
It is much easier to migrate an array of structs than individual structs that are accessed via a pointer to a pointer to an array of pointers to struct.
For this reason, allocate an array of streams in
virtio_snd_realize()and initialise all stream variables that are constant at runtime immediately after allocation.[...]
Signature · 1 line
Marc-André Lureau <[email protected]>
PATCH v2 3/9 hw/audio/virtio-sound: free all stream buffers on reset
In reply to Alexander MikhalitsynFrom: Volker Rümelin <[email protected]>
All remaining stream buffers in the stream queues must be freed after a reset. This is the initial state of the virtio-sound device.
--- hw/audio/virtio-snd.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
hw/audio/virtio-snd.c +11 -0
@@ -1341,6 +1341,7 @@ static void virtio_snd_reset(VirtIODevice *vdev) {VirtIOSound *vsnd =VIRTIO_SND (vdev);virtio_snd_ctrl_command *cmd; +uint32_t i;/* * Sanity check that the invalid buffer message queue is emptied at the end
@@ -1354,6 +1355,16 @@ static void virtio_snd_reset(VirtIODevice *vdev)QTAILQ_REMOVE (&vsnd->cmdq, cmd, next);virtio_snd_ctrl_cmd_free (cmd); } + +for (i =0 ; i < vsnd->snd_conf.streams; i++) { +VirtIOSoundPCMStream *stream = &vsnd->streams[i]; +VirtIOSoundPCMBuffer *buffer; + +while ((buffer =QSIMPLEQ_FIRST (&stream->queue))) { +QSIMPLEQ_REMOVE_HEAD (&stream->queue, entry); +virtio_snd_pcm_buffer_free (buffer); + } + } }static void virtio_snd_class_init (ObjectClass *klass,const void *data)
Signature · 1 line
2.47.3
Re: [PATCH v2 3/9] hw/audio/virtio-sound: free all stream buffers on reset
Quoted 3 lines · Alexander Mikhalitsyn
All remaining stream buffers in the stream queues must be freed after a reset. This is the initial state of the virtio-sound device.
Signature · 1 line
Marc-André Lureau <[email protected]>
PATCH v2 4/9 hw/audio/virtio-sound: split out virtio_snd_pcm_start_stop()
In reply to Alexander MikhalitsynFrom: Volker Rümelin <[email protected]>
Split out virtio_snd_pcm_start_stop(). This is a preparation for the next patch so that it doesn't become too big.
[AM: there were too many conflicts, I did git checkout --ours -- <.>
and then reimplemented the patch idea
/AM]
--- hw/audio/trace-events | 3 ++- hw/audio/virtio-snd.c | 57 ++++++++++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 21 deletions(-)
hw/audio/trace-events +2 -1
@@ -53,7 +53,8 @@ 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 +virtio_snd_handle_pcm_start(uint32_t stream) "VIRTIO_SND_R_PCM_START called for stream %"PRIu32 +virtio_snd_handle_pcm_stop(uint32_t stream) "VIRTIO_SND_R_PCM_STOP 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"
hw/audio/virtio-snd.c +37 -20
75 lines not on this page · raw message
Signature · 1 line
2.47.3
Re: [PATCH v2 4/9] hw/audio/virtio-sound: split out virtio_snd_pcm_start_stop()
Quoted 24 lines
Split out
virtio_snd_pcm_start_stop(). This is a preparation for the next patch so that it doesn't become too big.Signed-off-by Volker Rümelin <[email protected]>[AM: there were too many conflicts, I did
git checkout --ours -- <.>
and then reimplemented the patch idea
/AM]Signed-off-by Alexander Mikhalitsyn <[email protected]>Message-ID: <[email protected]>
hw/audio/virtio-snd.c +5
-1@@ -521,7 +521,42 @@ static void virtio_snd_handle_pcm_prepare(VirtIOSound *s, }/* -* Handles VIRTIO_SND_R_PCM_START. +* Starts/Stops a VirtIOSound card stream. +* Returns the response status code. (VIRTIO_SND_S_*). +* +* @s: VirtIOSound device +* @stream_id: stream id
hw/audio/virtio-snd.c static void virtio_snd_handle_pcm_prepare(VirtIOSound *s, · line 538
Quoted 10 lines
hw/audio/virtio-snd.c +10
-0+* @start: whether to start or stop the stream +*/ +static uint32_t virtio_snd_pcm_start_stop (VirtIOSound *s, +uint32_t stream_id, +bool start) +{ +VirtIOSoundPCMStream *stream; + + stream =virtio_snd_pcm_get_stream (s, stream_id); +if (!stream) {
I think we should keep the error report here. Or rather make it a qemu_log_mask(LOG_GUEST_ERROR, ..)
other than that:
Signature · 1 line
Marc-André Lureau <[email protected]>
Re: [PATCH v2 4/9] hw/audio/virtio-sound: split out virtio_snd_pcm_start_stop()
Am Do., 25. Juni 2026 um 17:00 Uhr schrieb <[email protected]>:
Quoted 27 lines
Quoted 27 lines
Split out
virtio_snd_pcm_start_stop(). This is a preparation for the next patch so that it doesn't become too big.Signed-off-by Volker Rümelin <[email protected]>[AM: there were too many conflicts, I did
git checkout --ours -- <.>
and then reimplemented the patch idea
/AM]Signed-off-by Alexander Mikhalitsyn <[email protected]>Message-ID: <[email protected]>
hw/audio/virtio-snd.c +7
-1@@ -521,7 +521,42 @@ static void virtio_snd_handle_pcm_prepare(VirtIOSound *s, }/* -* Handles VIRTIO_SND_R_PCM_START. +* Starts/Stops a VirtIOSound card stream. +* Returns the response status code. (VIRTIO_SND_S_*). +* +* @s: VirtIOSound device +* @stream_id: stream id +* @start: whether to start or stop the stream +*/
Quoted 8 lines
hw/audio/virtio-snd.c static void virtio_snd_handle_pcm_prepare(VirtIOSound *s, · line 538
Quoted 8 lines
hw/audio/virtio-snd.c +8
-0+static uint32_t virtio_snd_pcm_start_stop (VirtIOSound *s, +uint32_t stream_id, +bool start) +{ +VirtIOSoundPCMStream *stream; + + stream =virtio_snd_pcm_get_stream (s, stream_id); +if (!stream) {
Dear Marc-André,
Quoted 6 lines
I think we should keep the error report here. Or rather make it a
qemu_log_mask(LOG_GUEST_ERROR, ..)other than that:
Reviewed-by Marc-André Lureau <[email protected]>
thank you! I've resurrected the error report, so it will be in the next patchset version.
Kind regards,
Alex
Quoted 4 lines
--
Marc-André Lureau <[email protected]>
PATCH v2 5/9 hw/audio/virtio-sound: add stream state variable
In reply to Alexander MikhalitsynFrom: Volker Rümelin <[email protected]>
So far, only rudimentary checks have been made to ensure that the guest only performs state transitions permitted in virtio-v1.2-csd01 5.14.6.6.1 PCM Command Lifecycle. Add a state variable per audio stream and check all state transitions.
Because only permitted state transitions are possible, only one copy of the audio stream parameters is required and these do not need to be initialised with default values.
The state variable will also make it easier to restore the audio stream after migration.
[AM: there were too many conflicts, I did git checkout --ours -- <.>
and then reimplemented the patch idea
/AM]
--- hw/audio/virtio-snd.c | 196 +++++++++++++++++++--------------- include/hw/audio/virtio-snd.h | 18 +--- 2 files changed, 109 insertions(+), 105 deletions(-)
hw/audio/virtio-snd.c +108 -88
@@ -30,11 +30,30 @@#define VIRTIO_SOUND_CHMAP_DEFAULT 0#define VIRTIO_SOUND_HDA_FN_NID 0 +#define VSND_PCMSTREAM_STATE_F_PARAMS_SET 0x10000 +#define VSND_PCMSTREAM_STATE_F_PREPARED 0x20000 +#define VSND_PCMSTREAM_STATE_F_ACTIVE 0x40000 + +#define VSND_PCMSTREAM_STATE_UNINITIALIZED 0 +#define VSND_PCMSTREAM_STATE_PARAMS_SET (1 \ + | VSND_PCMSTREAM_STATE_F_PARAMS_SET) +#define VSND_PCMSTREAM_STATE_PREPARED (2 \ + | VSND_PCMSTREAM_STATE_F_PARAMS_SET \ + | VSND_PCMSTREAM_STATE_F_PREPARED) +#define VSND_PCMSTREAM_STATE_STARTED (4 \ + | VSND_PCMSTREAM_STATE_F_PARAMS_SET \ + | VSND_PCMSTREAM_STATE_F_PREPARED \ + | VSND_PCMSTREAM_STATE_F_ACTIVE) +#define VSND_PCMSTREAM_STATE_STOPPED (6 \ + | VSND_PCMSTREAM_STATE_F_PARAMS_SET \ + | VSND_PCMSTREAM_STATE_F_PREPARED) +#define VSND_PCMSTREAM_STATE_RELEASED (7 \ + | VSND_PCMSTREAM_STATE_F_PARAMS_SET) +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 void virtio_snd_pcm_in_cb (void *data,int available); -static void virtio_snd_unrealize (DeviceState *dev);static uint32_t supported_formats =BIT (VIRTIO_SND_PCM_FMT_S8 ) |BIT (VIRTIO_SND_PCM_FMT_U8 )
@@ -129,7 +148,7 @@ static VirtIOSoundPCMStream *virtio_snd_pcm_get_stream(VirtIOSound *s, uint32_t stream_id) {return stream_id >= s->snd_conf.streams ?NULL : - s->pcm.streams[stream_id]; + &s->streams[stream_id]; }/*
@@ -141,8 +160,8 @@ static VirtIOSoundPCMStream *virtio_snd_pcm_get_stream(VirtIOSound *s,static virtio_snd_pcm_set_params *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]; +return stream_id >= s->snd_conf.streams ?NULL : + &s->streams[stream_id].params; }/*
@@ -245,11 +264,10 @@ static void virtio_snd_handle_pcm_info(VirtIOSound *s,/* * 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 +* @stream_id: stream id * @params: The PCM params as defined in the virtio specification */ static
@@ -257,14 +275,25 @@ uint32_t virtio_snd_set_pcm_params(VirtIOSound *s,uint32_t stream_id, virtio_snd_pcm_set_params *params) { + VirtIOSoundPCMStream *stream;virtio_snd_pcm_set_params *st_params; -if (stream_id >= s->snd_conf.streams || s->pcm.pcm_params ==NULL ) { +if (stream_id >= s->snd_conf.streams) {virtio_error (VIRTIO_DEVICE (s),"Streams have not been initialized.\n" );return cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); } - st_params =virtio_snd_pcm_get_params (s, stream_id); + stream =virtio_snd_pcm_get_stream (s, stream_id); + +switch (stream->state) { +case VSND_PCMSTREAM_STATE_UNINITIALIZED : +case VSND_PCMSTREAM_STATE_PARAMS_SET : +case VSND_PCMSTREAM_STATE_PREPARED : +case VSND_PCMSTREAM_STATE_RELEASED : +break ; +default : +return cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); + }if (params->channels <1 || params->channels >AUDIO_MAX_CHANNELS ) {error_report ("Number of channels is not supported." );
@@ -281,6 +310,8 @@ uint32_t virtio_snd_set_pcm_params(VirtIOSound *s,return cpu_to_le32 (VIRTIO_SND_S_NOT_SUPP ); } + st_params =virtio_snd_pcm_get_params (s, stream_id); + st_params->buffer_bytes =le32_to_cpu (params->buffer_bytes); st_params->period_bytes =le32_to_cpu (params->period_bytes); st_params->features =le32_to_cpu (params->features);
@@ -289,6 +320,13 @@ uint32_t virtio_snd_set_pcm_params(VirtIOSound *s, st_params->format = params->format; st_params->rate = params->rate; +if (stream->state &VSND_PCMSTREAM_STATE_F_PREPARED ) { +/* implicit VIRTIO_SND_R_PCM_RELEASE */ +virtio_snd_pcm_flush (stream); + } + + stream->state =VSND_PCMSTREAM_STATE_PARAMS_SET ; +return cpu_to_le32 (VIRTIO_SND_S_OK ); }
@@ -398,15 +436,13 @@ static void virtio_snd_get_qemu_audsettings(audsettings *as,*/ static void virtio_snd_pcm_close (VirtIOSoundPCMStream *stream) { -if (stream) { -virtio_snd_pcm_flush (stream); -if (stream->info.direction ==VIRTIO_SND_D_OUTPUT ) { -audio_be_close_out (stream->s->audio_be, stream->voice.out); - stream->voice.out =NULL ; - }else if (stream->info.direction ==VIRTIO_SND_D_INPUT ) { -audio_be_close_in (stream->s->audio_be, stream->voice.in); - stream->voice.in =NULL ; - } +virtio_snd_pcm_flush (stream); +if (stream->info.direction ==VIRTIO_SND_D_OUTPUT ) { +audio_be_close_out (stream->s->audio_be, stream->voice.out); + stream->voice.out =NULL ; + }else if (stream->info.direction ==VIRTIO_SND_D_INPUT ) { +audio_be_close_in (stream->s->audio_be, stream->voice.in); + stream->voice.in =NULL ; } }
@@ -423,32 +459,23 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)virtio_snd_pcm_set_params *params;VirtIOSoundPCMStream *stream; -if (s->pcm.streams ==NULL || - s->pcm.pcm_params ==NULL || - stream_id >= s->snd_conf.streams) { + stream =virtio_snd_pcm_get_stream (s, stream_id); +if (!stream) {return cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); } - params =virtio_snd_pcm_get_params (s, stream_id); -if (params ==NULL ) { +switch (stream->state) { +case VSND_PCMSTREAM_STATE_PARAMS_SET : +case VSND_PCMSTREAM_STATE_PREPARED : +case VSND_PCMSTREAM_STATE_RELEASED : +break ; +default :return cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); } - stream =virtio_snd_pcm_get_stream (s, stream_id); -if (stream ==NULL ) { - stream = &s->streams[stream_id]; - stream->active = false; - stream->latency_bytes =0 ; - -/* -* stream_id >= s->snd_conf.streams was checked before so this is -* in-bounds -*/ - s->pcm.streams[stream_id] = stream; - } + params =virtio_snd_pcm_get_params (s, stream_id);virtio_snd_get_qemu_audsettings (&as, params); - stream->params = *params; stream->positions[0 ] =VIRTIO_SND_CHMAP_FL ; stream->positions[1 ] =VIRTIO_SND_CHMAP_FR ;
@@ -472,6 +499,8 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)audio_be_set_volume_in_lr (s->audio_be, stream->voice.in,0 ,255 ,255 ); } + stream->state =VSND_PCMSTREAM_STATE_PREPARED ; +return cpu_to_le32 (VIRTIO_SND_S_OK ); }
@@ -540,12 +569,28 @@ static uint32_t virtio_snd_pcm_start_stop(VirtIOSound *s, }if (start) { +switch (stream->state) { +case VSND_PCMSTREAM_STATE_PREPARED : +case VSND_PCMSTREAM_STATE_STOPPED : +break ; +default : +return cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); + } +trace_virtio_snd_handle_pcm_start (stream_id); + stream->state =VSND_PCMSTREAM_STATE_STARTED ; }else { +switch (stream->state) { +case VSND_PCMSTREAM_STATE_STARTED : +break ; +default : +return cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); + } +trace_virtio_snd_handle_pcm_stop (stream_id); + stream->state =VSND_PCMSTREAM_STATE_STOPPED ; } - stream->active = start;if (stream->info.direction ==VIRTIO_SND_D_OUTPUT ) {audio_be_set_active_out (s->audio_be, stream->voice.out, start); }else {
@@ -639,6 +684,15 @@ static void virtio_snd_handle_pcm_release(VirtIOSound *s,return ; } +switch (stream->state) { +case VSND_PCMSTREAM_STATE_PREPARED : +case VSND_PCMSTREAM_STATE_STOPPED : +break ; +default : + cmd->resp.code =cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); +return ; + } +if (virtio_snd_pcm_get_io_msgs_count (stream)) {/* * virtio-v1.2-csd01, 5.14.6.6.5.1,
@@ -653,6 +707,8 @@ static void virtio_snd_handle_pcm_release(VirtIOSound *s,virtio_snd_pcm_flush (stream); } + stream->state =VSND_PCMSTREAM_STATE_RELEASED ; + cmd->resp.code =cpu_to_le32 (VIRTIO_SND_S_OK ); }
@@ -874,12 +930,11 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq) } stream_id =le32_to_cpu (hdr.stream_id); -if (stream_id >= vsnd->snd_conf.streams - || vsnd->pcm.streams[stream_id] ==NULL ) { +if (stream_id >= vsnd->snd_conf.streams) { goto tx_err; } - stream = vsnd->pcm.streams[stream_id]; + stream = &vsnd->streams[stream_id];if (stream->info.direction !=VIRTIO_SND_D_OUTPUT ) { goto tx_err; }
@@ -954,13 +1009,12 @@ static void virtio_snd_handle_rx_xfer(VirtIODevice *vdev, VirtQueue *vq) } stream_id =le32_to_cpu (hdr.stream_id); -if (stream_id >= vsnd->snd_conf.streams - || !vsnd->pcm.streams[stream_id]) { +if (stream_id >= vsnd->snd_conf.streams) { goto rx_err; } - stream = vsnd->pcm.streams[stream_id]; -if (stream ==NULL || stream->info.direction !=VIRTIO_SND_D_INPUT ) { + stream = &vsnd->streams[stream_id]; +if (stream->info.direction !=VIRTIO_SND_D_INPUT ) { goto rx_err; }
@@ -1019,8 +1073,6 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp)ERRP_GUARD ();VirtIOSound *vsnd =VIRTIO_SND (dev);VirtIODevice *vdev =VIRTIO_DEVICE (dev); -virtio_snd_pcm_set_params default_params = {0 }; -uint32_t status;trace_virtio_snd_realize (vsnd);
@@ -1058,6 +1110,7 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp)VirtIOSoundPCMStream *stream = &vsnd->streams[i]; stream->id = i; + stream->state =VSND_PCMSTREAM_STATE_UNINITIALIZED ; stream->s = vsnd;QSIMPLEQ_INIT (&stream->queue); stream->info.hdr.hda_fn_nid =VIRTIO_SOUND_HDA_FN_NID ;
@@ -1071,21 +1124,9 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp) stream->info.channels_max =2 ; } - vsnd->pcm.streams = - g_new0(VirtIOSoundPCMStream *, vsnd->snd_conf.streams); - vsnd->pcm.pcm_params = -g_new0 (virtio_snd_pcm_set_params, vsnd->snd_conf.streams); -virtio_init (vdev,VIRTIO_ID_SOUND ,sizeof (virtio_snd_config));virtio_add_feature (&vsnd->features,VIRTIO_F_VERSION_1 ); -/* set default params for all streams */ - default_params.features =0 ; - default_params.buffer_bytes =cpu_to_le32 (8192 ); - default_params.period_bytes =cpu_to_le32 (2048 ); - default_params.channels =2 ; - default_params.format =VIRTIO_SND_PCM_FMT_S16 ; - default_params.rate =VIRTIO_SND_PCM_RATE_48000 ; vsnd->queues[VIRTIO_SND_VQ_CONTROL ] =virtio_add_queue (vdev,64 , virtio_snd_handle_ctrl); vsnd->queues[VIRTIO_SND_VQ_EVENT] =
@@ -1096,28 +1137,6 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp)virtio_add_queue (vdev,64 , virtio_snd_handle_rx_xfer);QTAILQ_INIT (&vsnd->cmdq);QSIMPLEQ_INIT (&vsnd->invalid); - -for (uint32_t i =0 ; i < vsnd->snd_conf.streams; i++) { - status =virtio_snd_set_pcm_params (vsnd, i, &default_params); -if (status !=cpu_to_le32 (VIRTIO_SND_S_OK )) { -error_setg (errp, -"Can't initialize stream params, device responded with %s." , -print_code (status)); - goto error_cleanup; - } - status =virtio_snd_pcm_prepare (vsnd, i); -if (status !=cpu_to_le32 (VIRTIO_SND_S_OK )) { -error_setg (errp, -"Can't prepare streams, device responded with %s." , -print_code (status)); - goto error_cleanup; - } - } - -return ; - -error_cleanup: -virtio_snd_unrealize (dev); }static inline void update_latency (VirtIOSoundPCMStream *s,size_t used)
@@ -1166,7 +1185,7 @@ static void virtio_snd_pcm_out_cb(void *data, int available)if (!virtio_queue_ready (buffer->vq)) {return ; } -if (!stream->active) { +if (!(stream->state &VSND_PCMSTREAM_STATE_F_ACTIVE )) {/* Stream has stopped, so do not perform audio_be_write. */ return_tx_buffer (stream, buffer);continue ;
@@ -1260,7 +1279,7 @@ static void virtio_snd_pcm_in_cb(void *data, int available)if (!virtio_queue_ready (buffer->vq)) {return ; } -if (!stream->active) { +if (!(stream->state &VSND_PCMSTREAM_STATE_F_ACTIVE )) {/* Stream has stopped, so do not perform audio_be_read. */ return_rx_buffer (stream, buffer);continue ;
@@ -1333,17 +1352,15 @@ static void virtio_snd_unrealize(DeviceState *dev)qemu_del_vm_change_state_handler (vsnd->vmstate);trace_virtio_snd_unrealize (vsnd); -if (vsnd->pcm.streams) { +if (vsnd->streams) { +virtio_snd_process_cmdq (vsnd);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); + stream = &vsnd->streams[i]; +if (stream->state &VSND_PCMSTREAM_STATE_F_PREPARED ) { +virtio_snd_pcm_flush (stream); } } -g_free (vsnd->pcm.streams); } -g_free (vsnd->pcm.pcm_params);g_free (vsnd->streams); vsnd->streams =NULL ;virtio_delete_queue (vsnd->queues[VIRTIO_SND_VQ_CONTROL ]);
@@ -1377,6 +1394,9 @@ static void virtio_snd_reset(VirtIODevice *vdev)VirtIOSoundPCMStream *stream = &vsnd->streams[i];VirtIOSoundPCMBuffer *buffer; +virtio_snd_pcm_close (stream); + stream->state =VSND_PCMSTREAM_STATE_UNINITIALIZED ; +while ((buffer =QSIMPLEQ_FIRST (&stream->queue))) {QSIMPLEQ_REMOVE_HEAD (&stream->queue, entry);virtio_snd_pcm_buffer_free (buffer);
include/hw/audio/virtio-snd.h +1 -17
@@ -75,8 +75,6 @@ typedef struct VirtIOSoundPCMStream VirtIOSoundPCMStream;typedef struct virtio_snd_ctrl_command virtio_snd_ctrl_command ; -typedef struct VirtIOSoundPCM VirtIOSoundPCM ; -typedef struct VirtIOSoundPCMBuffer VirtIOSoundPCMBuffer ;/*
@@ -121,32 +119,19 @@ struct VirtIOSoundPCMBuffer {uint8_t data[]; }; -struct VirtIOSoundPCM { -/* -* PCM parameters are a separate field instead of a VirtIOSoundPCMStream -* field, because the operation of PCM control requests is first -* VIRTIO_SND_R_PCM_SET_PARAMS and then VIRTIO_SND_R_PCM_PREPARE; this -* means that some times we get parameters without having an allocated -* stream yet. -*/ -virtio_snd_pcm_set_params *pcm_params; -VirtIOSoundPCMStream **streams; -}; -struct VirtIOSoundPCMStream {virtio_snd_pcm_info info;virtio_snd_pcm_set_params params;uint32_t id; +uint32_t state;/* channel position values (VIRTIO_SND_CHMAP_XXX) */ uint8_t positions[VIRTIO_SND_CHMAP_MAX_SIZE ];VirtIOSound *s; -bool flushing;audsettings as;union {SWVoiceIn *in;SWVoiceOut *out; } voice; -bool active;uint32_t latency_bytes; QSIMPLEQ_HEAD(, VirtIOSoundPCMBuffer) queue; };
@@ -213,7 +198,6 @@ struct VirtIOSound {VirtQueue *queues[VIRTIO_SND_VQ_MAX ];uint64_t features; -VirtIOSoundPCM pcm;VirtIOSoundPCMStream *streams;AudioBackend *audio_be;VMChangeStateEntry *vmstate;
Signature · 1 line
2.47.3
Re: [PATCH v2 5/9] hw/audio/virtio-sound: add stream state variable
Quoted 446 lines
So far, only rudimentary checks have been made to ensure that the guest only performs state transitions permitted in virtio-v1.2-csd01 5.14.6.6.1 PCM Command Lifecycle. Add a state variable per audio stream and check all state transitions.
Because only permitted state transitions are possible, only one copy of the audio stream parameters is required and these do not need to be initialised with default values.
The state variable will also make it easier to restore the audio stream after migration.
Signed-off-by Volker Rümelin <[email protected]>[AM: there were too many conflicts, I did
git checkout --ours -- <.>
and then reimplemented the patch idea
/AM]Signed-off-by Alexander Mikhalitsyn <[email protected]>Message-ID: <[email protected]>
include/hw/audio/virtio-snd.h +1
-17@@ -75,8 +75,6 @@ typedef struct VirtIOSoundPCMStream VirtIOSoundPCMStream;typedef struct virtio_snd_ctrl_command virtio_snd_ctrl_command ; -typedef struct VirtIOSoundPCM VirtIOSoundPCM ; -typedef struct VirtIOSoundPCMBuffer VirtIOSoundPCMBuffer ;/* @@ -121,32 +119,19 @@ struct VirtIOSoundPCMBuffer {uint8_t data[]; }; -struct VirtIOSoundPCM { -/* -* PCM parameters are a separate field instead of a VirtIOSoundPCMStream -* field, because the operation of PCM control requests is first -* VIRTIO_SND_R_PCM_SET_PARAMS and then VIRTIO_SND_R_PCM_PREPARE; this -* means that some times we get parameters without having an allocated -* stream yet. -*/ -virtio_snd_pcm_set_params *pcm_params; -VirtIOSoundPCMStream **streams; -}; -struct VirtIOSoundPCMStream {virtio_snd_pcm_info info;virtio_snd_pcm_set_params params;uint32_t id; +uint32_t state;/* channel position values (VIRTIO_SND_CHMAP_XXX) */ uint8_t positions[VIRTIO_SND_CHMAP_MAX_SIZE ];VirtIOSound *s; -bool flushing;audsettings as;union {SWVoiceIn *in;SWVoiceOut *out; } voice; -bool active;uint32_t latency_bytes; QSIMPLEQ_HEAD(, VirtIOSoundPCMBuffer) queue; };@@ -213,7 +198,6 @@ struct VirtIOSound {VirtQueue *queues[VIRTIO_SND_VQ_MAX ];uint64_t features; -VirtIOSoundPCM pcm;VirtIOSoundPCMStream *streams;AudioBackend *audio_be;VMChangeStateEntry *vmstate;hw/audio/virtio-snd.c +100
-82@@ -30,11 +30,30 @@#define VIRTIO_SOUND_CHMAP_DEFAULT 0#define VIRTIO_SOUND_HDA_FN_NID 0 +#define VSND_PCMSTREAM_STATE_F_PARAMS_SET 0x10000 +#define VSND_PCMSTREAM_STATE_F_PREPARED 0x20000 +#define VSND_PCMSTREAM_STATE_F_ACTIVE 0x40000 + +#define VSND_PCMSTREAM_STATE_UNINITIALIZED 0 +#define VSND_PCMSTREAM_STATE_PARAMS_SET (1 \ + | VSND_PCMSTREAM_STATE_F_PARAMS_SET) +#define VSND_PCMSTREAM_STATE_PREPARED (2 \ + | VSND_PCMSTREAM_STATE_F_PARAMS_SET \ + | VSND_PCMSTREAM_STATE_F_PREPARED) +#define VSND_PCMSTREAM_STATE_STARTED (4 \ + | VSND_PCMSTREAM_STATE_F_PARAMS_SET \ + | VSND_PCMSTREAM_STATE_F_PREPARED \ + | VSND_PCMSTREAM_STATE_F_ACTIVE) +#define VSND_PCMSTREAM_STATE_STOPPED (6 \ + | VSND_PCMSTREAM_STATE_F_PARAMS_SET \ + | VSND_PCMSTREAM_STATE_F_PREPARED) +#define VSND_PCMSTREAM_STATE_RELEASED (7 \ + | VSND_PCMSTREAM_STATE_F_PARAMS_SET) +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 void virtio_snd_pcm_in_cb (void *data,int available); -static void virtio_snd_unrealize (DeviceState *dev);static uint32_t supported_formats =BIT (VIRTIO_SND_PCM_FMT_S8 ) |BIT (VIRTIO_SND_PCM_FMT_U8 )@@ -129,7 +148,7 @@ static VirtIOSoundPCMStream *virtio_snd_pcm_get_stream(VirtIOSound *s, uint32_t stream_id) {return stream_id >= s->snd_conf.streams ?NULL : - s->pcm.streams[stream_id]; + &s->streams[stream_id]; }/* @@ -141,8 +160,8 @@ static VirtIOSoundPCMStream *virtio_snd_pcm_get_stream(VirtIOSound *s,static virtio_snd_pcm_set_params *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]; +return stream_id >= s->snd_conf.streams ?NULL : + &s->streams[stream_id].params; }/* @@ -245,11 +264,10 @@ static void virtio_snd_handle_pcm_info(VirtIOSound *s,/* * 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 +* @stream_id: stream id * @params: The PCM params as defined in the virtio specification */ static@@ -257,14 +275,25 @@ uint32_t virtio_snd_set_pcm_params(VirtIOSound *s,uint32_t stream_id, virtio_snd_pcm_set_params *params) { + VirtIOSoundPCMStream *stream;virtio_snd_pcm_set_params *st_params; -if (stream_id >= s->snd_conf.streams || s->pcm.pcm_params ==NULL ) { +if (stream_id >= s->snd_conf.streams) {virtio_error (VIRTIO_DEVICE (s),"Streams have not been initialized.\n" );return cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); } - st_params =virtio_snd_pcm_get_params (s, stream_id); + stream =virtio_snd_pcm_get_stream (s, stream_id); + +switch (stream->state) { +case VSND_PCMSTREAM_STATE_UNINITIALIZED : +case VSND_PCMSTREAM_STATE_PARAMS_SET : +case VSND_PCMSTREAM_STATE_PREPARED : +case VSND_PCMSTREAM_STATE_RELEASED : +break ; +default : +return cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); + }if (params->channels <1 || params->channels >AUDIO_MAX_CHANNELS ) {error_report ("Number of channels is not supported." );@@ -281,6 +310,8 @@ uint32_t virtio_snd_set_pcm_params(VirtIOSound *s,return cpu_to_le32 (VIRTIO_SND_S_NOT_SUPP ); } + st_params =virtio_snd_pcm_get_params (s, stream_id); + st_params->buffer_bytes =le32_to_cpu (params->buffer_bytes); st_params->period_bytes =le32_to_cpu (params->period_bytes); st_params->features =le32_to_cpu (params->features);@@ -289,6 +320,13 @@ uint32_t virtio_snd_set_pcm_params(VirtIOSound *s, st_params->format = params->format; st_params->rate = params->rate; +if (stream->state &VSND_PCMSTREAM_STATE_F_PREPARED ) { +/* implicit VIRTIO_SND_R_PCM_RELEASE */ +virtio_snd_pcm_flush (stream); + } + + stream->state =VSND_PCMSTREAM_STATE_PARAMS_SET ; +return cpu_to_le32 (VIRTIO_SND_S_OK ); }@@ -398,15 +436,13 @@ static void virtio_snd_get_qemu_audsettings(audsettings *as,*/ static void virtio_snd_pcm_close (VirtIOSoundPCMStream *stream) { -if (stream) { -virtio_snd_pcm_flush (stream); -if (stream->info.direction ==VIRTIO_SND_D_OUTPUT ) { -audio_be_close_out (stream->s->audio_be, stream->voice.out); - stream->voice.out =NULL ; - }else if (stream->info.direction ==VIRTIO_SND_D_INPUT ) { -audio_be_close_in (stream->s->audio_be, stream->voice.in); - stream->voice.in =NULL ; - } +virtio_snd_pcm_flush (stream); +if (stream->info.direction ==VIRTIO_SND_D_OUTPUT ) { +audio_be_close_out (stream->s->audio_be, stream->voice.out); + stream->voice.out =NULL ; + }else if (stream->info.direction ==VIRTIO_SND_D_INPUT ) { +audio_be_close_in (stream->s->audio_be, stream->voice.in); + stream->voice.in =NULL ; } }@@ -423,32 +459,23 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)virtio_snd_pcm_set_params *params;VirtIOSoundPCMStream *stream; -if (s->pcm.streams ==NULL || - s->pcm.pcm_params ==NULL || - stream_id >= s->snd_conf.streams) { + stream =virtio_snd_pcm_get_stream (s, stream_id); +if (!stream) {return cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); } - params =virtio_snd_pcm_get_params (s, stream_id); -if (params ==NULL ) { +switch (stream->state) { +case VSND_PCMSTREAM_STATE_PARAMS_SET : +case VSND_PCMSTREAM_STATE_PREPARED : +case VSND_PCMSTREAM_STATE_RELEASED : +break ; +default :return cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); } - stream =virtio_snd_pcm_get_stream (s, stream_id); -if (stream ==NULL ) { - stream = &s->streams[stream_id]; - stream->active = false; - stream->latency_bytes =0 ; - -/* -* stream_id >= s->snd_conf.streams was checked before so this is -* in-bounds -*/ - s->pcm.streams[stream_id] = stream; - } + params =virtio_snd_pcm_get_params (s, stream_id);virtio_snd_get_qemu_audsettings (&as, params); - stream->params = *params; stream->positions[0 ] =VIRTIO_SND_CHMAP_FL ; stream->positions[1 ] =VIRTIO_SND_CHMAP_FR ;@@ -472,6 +499,8 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)audio_be_set_volume_in_lr (s->audio_be, stream->voice.in,0 ,255 ,255 ); } + stream->state =VSND_PCMSTREAM_STATE_PREPARED ; +return cpu_to_le32 (VIRTIO_SND_S_OK ); }@@ -540,12 +569,28 @@ static uint32_t virtio_snd_pcm_start_stop(VirtIOSound *s, }if (start) { +switch (stream->state) { +case VSND_PCMSTREAM_STATE_PREPARED : +case VSND_PCMSTREAM_STATE_STOPPED : +break ; +default : +return cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); + } +trace_virtio_snd_handle_pcm_start (stream_id); + stream->state =VSND_PCMSTREAM_STATE_STARTED ; }else { +switch (stream->state) { +case VSND_PCMSTREAM_STATE_STARTED : +break ; +default : +return cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); + } +trace_virtio_snd_handle_pcm_stop (stream_id); + stream->state =VSND_PCMSTREAM_STATE_STOPPED ; } - stream->active = start;if (stream->info.direction ==VIRTIO_SND_D_OUTPUT ) {audio_be_set_active_out (s->audio_be, stream->voice.out, start); }else {@@ -639,6 +684,15 @@ static void virtio_snd_handle_pcm_release(VirtIOSound *s,return ; } +switch (stream->state) { +case VSND_PCMSTREAM_STATE_PREPARED : +case VSND_PCMSTREAM_STATE_STOPPED : +break ; +default : + cmd->resp.code =cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); +return ; + } +if (virtio_snd_pcm_get_io_msgs_count (stream)) {/* * virtio-v1.2-csd01, 5.14.6.6.5.1, @@ -653,6 +707,8 @@ static void virtio_snd_handle_pcm_release(VirtIOSound *s,virtio_snd_pcm_flush (stream); } + stream->state =VSND_PCMSTREAM_STATE_RELEASED ; + cmd->resp.code =cpu_to_le32 (VIRTIO_SND_S_OK ); }@@ -874,12 +930,11 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq) } stream_id =le32_to_cpu (hdr.stream_id); -if (stream_id >= vsnd->snd_conf.streams - || vsnd->pcm.streams[stream_id] ==NULL ) { +if (stream_id >= vsnd->snd_conf.streams) { goto tx_err; } - stream = vsnd->pcm.streams[stream_id]; + stream = &vsnd->streams[stream_id];if (stream->info.direction !=VIRTIO_SND_D_OUTPUT ) { goto tx_err; }@@ -954,13 +1009,12 @@ static void virtio_snd_handle_rx_xfer(VirtIODevice *vdev, VirtQueue *vq) } stream_id =le32_to_cpu (hdr.stream_id); -if (stream_id >= vsnd->snd_conf.streams - || !vsnd->pcm.streams[stream_id]) { +if (stream_id >= vsnd->snd_conf.streams) { goto rx_err; } - stream = vsnd->pcm.streams[stream_id]; -if (stream ==NULL || stream->info.direction !=VIRTIO_SND_D_INPUT ) { + stream = &vsnd->streams[stream_id]; +if (stream->info.direction !=VIRTIO_SND_D_INPUT ) { goto rx_err; }@@ -1019,8 +1073,6 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp)ERRP_GUARD ();VirtIOSound *vsnd =VIRTIO_SND (dev);VirtIODevice *vdev =VIRTIO_DEVICE (dev); -virtio_snd_pcm_set_params default_params = {0 }; -uint32_t status;trace_virtio_snd_realize (vsnd);@@ -1058,6 +1110,7 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp)VirtIOSoundPCMStream *stream = &vsnd->streams[i]; stream->id = i; + stream->state =VSND_PCMSTREAM_STATE_UNINITIALIZED ; stream->s = vsnd;QSIMPLEQ_INIT (&stream->queue); stream->info.hdr.hda_fn_nid =VIRTIO_SOUND_HDA_FN_NID ;@@ -1071,21 +1124,9 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp) stream->info.channels_max =2 ; } - vsnd->pcm.streams = - g_new0(VirtIOSoundPCMStream *, vsnd->snd_conf.streams); - vsnd->pcm.pcm_params = -g_new0 (virtio_snd_pcm_set_params, vsnd->snd_conf.streams); -virtio_init (vdev,VIRTIO_ID_SOUND ,sizeof (virtio_snd_config));virtio_add_feature (&vsnd->features,VIRTIO_F_VERSION_1 ); -/* set default params for all streams */ - default_params.features =0 ; - default_params.buffer_bytes =cpu_to_le32 (8192 ); - default_params.period_bytes =cpu_to_le32 (2048 ); - default_params.channels =2 ; - default_params.format =VIRTIO_SND_PCM_FMT_S16 ; - default_params.rate =VIRTIO_SND_PCM_RATE_48000 ; vsnd->queues[VIRTIO_SND_VQ_CONTROL ] =virtio_add_queue (vdev,64 , virtio_snd_handle_ctrl); vsnd->queues[VIRTIO_SND_VQ_EVENT] =@@ -1096,28 +1137,6 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp)virtio_add_queue (vdev,64 , virtio_snd_handle_rx_xfer);QTAILQ_INIT (&vsnd->cmdq);QSIMPLEQ_INIT (&vsnd->invalid); - -for (uint32_t i =0 ; i < vsnd->snd_conf.streams; i++) { - status =virtio_snd_set_pcm_params (vsnd, i, &default_params); -if (status !=cpu_to_le32 (VIRTIO_SND_S_OK )) { -error_setg (errp, -"Can't initialize stream params, device responded with %s." , -print_code (status)); - goto error_cleanup; - } - status =virtio_snd_pcm_prepare (vsnd, i); -if (status !=cpu_to_le32 (VIRTIO_SND_S_OK )) { -error_setg (errp, -"Can't prepare streams, device responded with %s." , -print_code (status)); - goto error_cleanup; - } - } - -return ; - -error_cleanup: -virtio_snd_unrealize (dev); }static inline void update_latency (VirtIOSoundPCMStream *s,size_t used)@@ -1166,7 +1185,7 @@ static void virtio_snd_pcm_out_cb(void *data, int available)if (!virtio_queue_ready (buffer->vq)) {return ; } -if (!stream->active) { +if (!(stream->state &VSND_PCMSTREAM_STATE_F_ACTIVE )) {/* Stream has stopped, so do not perform audio_be_write. */ return_tx_buffer (stream, buffer);continue ;@@ -1260,7 +1279,7 @@ static void virtio_snd_pcm_in_cb(void *data, int available)if (!virtio_queue_ready (buffer->vq)) {return ; } -if (!stream->active) { +if (!(stream->state &VSND_PCMSTREAM_STATE_F_ACTIVE )) {/* Stream has stopped, so do not perform audio_be_read. */ return_rx_buffer (stream, buffer);continue ;@@ -1333,17 +1352,15 @@ static void virtio_snd_unrealize(DeviceState *dev)qemu_del_vm_change_state_handler (vsnd->vmstate);trace_virtio_snd_unrealize (vsnd); -if (vsnd->pcm.streams) {
hw/audio/virtio-snd.c static void virtio_snd_unrealize(DeviceState *dev) · line 1360
Quoted 10 lines
hw/audio/virtio-snd.c +5
-4+if (vsnd->streams) { +virtio_snd_process_cmdq (vsnd);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); + stream = &vsnd->streams[i]; +if (stream->state &VSND_PCMSTREAM_STATE_F_PREPARED ) { +virtio_snd_pcm_flush (stream);
I am not sure we should let the device do IO during unrealize, but this seems to be just buffer work and the notification is hopefully ignored.
However, it should rather call virtio_snd_pcm_close() to release the associated SWVoices.
Signature · 1 line
Marc-André Lureau <[email protected]>
Re: [PATCH v2 5/9] hw/audio/virtio-sound: add stream state variable
Am Do., 25. Juni 2026 um 17:00 Uhr schrieb <[email protected]>:
Quoted 446 lines
Quoted 446 lines
So far, only rudimentary checks have been made to ensure that the guest only performs state transitions permitted in virtio-v1.2-csd01 5.14.6.6.1 PCM Command Lifecycle. Add a state variable per audio stream and check all state transitions.
Because only permitted state transitions are possible, only one copy of the audio stream parameters is required and these do not need to be initialised with default values.
The state variable will also make it easier to restore the audio stream after migration.
Signed-off-by Volker Rümelin <[email protected]>[AM: there were too many conflicts, I did
git checkout --ours -- <.>
and then reimplemented the patch idea
/AM]Signed-off-by Alexander Mikhalitsyn <[email protected]>Message-ID: <[email protected]>
include/hw/audio/virtio-snd.h +1
-17@@ -75,8 +75,6 @@ typedef struct VirtIOSoundPCMStream VirtIOSoundPCMStream;typedef struct virtio_snd_ctrl_command virtio_snd_ctrl_command ; -typedef struct VirtIOSoundPCM VirtIOSoundPCM ; -typedef struct VirtIOSoundPCMBuffer VirtIOSoundPCMBuffer ;/* @@ -121,32 +119,19 @@ struct VirtIOSoundPCMBuffer {uint8_t data[]; }; -struct VirtIOSoundPCM { -/* -* PCM parameters are a separate field instead of a VirtIOSoundPCMStream -* field, because the operation of PCM control requests is first -* VIRTIO_SND_R_PCM_SET_PARAMS and then VIRTIO_SND_R_PCM_PREPARE; this -* means that some times we get parameters without having an allocated -* stream yet. -*/ -virtio_snd_pcm_set_params *pcm_params; -VirtIOSoundPCMStream **streams; -}; -struct VirtIOSoundPCMStream {virtio_snd_pcm_info info;virtio_snd_pcm_set_params params;uint32_t id; +uint32_t state;/* channel position values (VIRTIO_SND_CHMAP_XXX) */ uint8_t positions[VIRTIO_SND_CHMAP_MAX_SIZE ];VirtIOSound *s; -bool flushing;audsettings as;union {SWVoiceIn *in;SWVoiceOut *out; } voice; -bool active;uint32_t latency_bytes; QSIMPLEQ_HEAD(, VirtIOSoundPCMBuffer) queue; };@@ -213,7 +198,6 @@ struct VirtIOSound {VirtQueue *queues[VIRTIO_SND_VQ_MAX ];uint64_t features; -VirtIOSoundPCM pcm;VirtIOSoundPCMStream *streams;AudioBackend *audio_be;VMChangeStateEntry *vmstate;hw/audio/virtio-snd.c +100
-81@@ -30,11 +30,30 @@#define VIRTIO_SOUND_CHMAP_DEFAULT 0#define VIRTIO_SOUND_HDA_FN_NID 0 +#define VSND_PCMSTREAM_STATE_F_PARAMS_SET 0x10000 +#define VSND_PCMSTREAM_STATE_F_PREPARED 0x20000 +#define VSND_PCMSTREAM_STATE_F_ACTIVE 0x40000 + +#define VSND_PCMSTREAM_STATE_UNINITIALIZED 0 +#define VSND_PCMSTREAM_STATE_PARAMS_SET (1 \ + | VSND_PCMSTREAM_STATE_F_PARAMS_SET) +#define VSND_PCMSTREAM_STATE_PREPARED (2 \ + | VSND_PCMSTREAM_STATE_F_PARAMS_SET \ + | VSND_PCMSTREAM_STATE_F_PREPARED) +#define VSND_PCMSTREAM_STATE_STARTED (4 \ + | VSND_PCMSTREAM_STATE_F_PARAMS_SET \ + | VSND_PCMSTREAM_STATE_F_PREPARED \ + | VSND_PCMSTREAM_STATE_F_ACTIVE) +#define VSND_PCMSTREAM_STATE_STOPPED (6 \ + | VSND_PCMSTREAM_STATE_F_PARAMS_SET \ + | VSND_PCMSTREAM_STATE_F_PREPARED) +#define VSND_PCMSTREAM_STATE_RELEASED (7 \ + | VSND_PCMSTREAM_STATE_F_PARAMS_SET) +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 void virtio_snd_pcm_in_cb (void *data,int available); -static void virtio_snd_unrealize (DeviceState *dev);static uint32_t supported_formats =BIT (VIRTIO_SND_PCM_FMT_S8 ) |BIT (VIRTIO_SND_PCM_FMT_U8 )@@ -129,7 +148,7 @@ static VirtIOSoundPCMStream *virtio_snd_pcm_get_stream(VirtIOSound *s, uint32_t stream_id) {return stream_id >= s->snd_conf.streams ?NULL : - s->pcm.streams[stream_id]; + &s->streams[stream_id]; }/* @@ -141,8 +160,8 @@ static VirtIOSoundPCMStream *virtio_snd_pcm_get_stream(VirtIOSound *s,static virtio_snd_pcm_set_params *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]; +return stream_id >= s->snd_conf.streams ?NULL : + &s->streams[stream_id].params; }/* @@ -245,11 +264,10 @@ static void virtio_snd_handle_pcm_info(VirtIOSound *s,/* * 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 +* @stream_id: stream id * @params: The PCM params as defined in the virtio specification */ static@@ -257,14 +275,25 @@ uint32_t virtio_snd_set_pcm_params(VirtIOSound *s,uint32_t stream_id, virtio_snd_pcm_set_params *params) { + VirtIOSoundPCMStream *stream;virtio_snd_pcm_set_params *st_params; -if (stream_id >= s->snd_conf.streams || s->pcm.pcm_params ==NULL ) { +if (stream_id >= s->snd_conf.streams) {virtio_error (VIRTIO_DEVICE (s),"Streams have not been initialized.\n" );return cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); } - st_params =virtio_snd_pcm_get_params (s, stream_id); + stream =virtio_snd_pcm_get_stream (s, stream_id); + +switch (stream->state) { +case VSND_PCMSTREAM_STATE_UNINITIALIZED : +case VSND_PCMSTREAM_STATE_PARAMS_SET : +case VSND_PCMSTREAM_STATE_PREPARED : +case VSND_PCMSTREAM_STATE_RELEASED : +break ; +default : +return cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); + }if (params->channels <1 || params->channels >AUDIO_MAX_CHANNELS ) {error_report ("Number of channels is not supported." );@@ -281,6 +310,8 @@ uint32_t virtio_snd_set_pcm_params(VirtIOSound *s,return cpu_to_le32 (VIRTIO_SND_S_NOT_SUPP ); } + st_params =virtio_snd_pcm_get_params (s, stream_id); + st_params->buffer_bytes =le32_to_cpu (params->buffer_bytes); st_params->period_bytes =le32_to_cpu (params->period_bytes); st_params->features =le32_to_cpu (params->features);@@ -289,6 +320,13 @@ uint32_t virtio_snd_set_pcm_params(VirtIOSound *s, st_params->format = params->format; st_params->rate = params->rate; +if (stream->state &VSND_PCMSTREAM_STATE_F_PREPARED ) { +/* implicit VIRTIO_SND_R_PCM_RELEASE */ +virtio_snd_pcm_flush (stream); + } + + stream->state =VSND_PCMSTREAM_STATE_PARAMS_SET ; +return cpu_to_le32 (VIRTIO_SND_S_OK ); }@@ -398,15 +436,13 @@ static void virtio_snd_get_qemu_audsettings(audsettings *as,*/ static void virtio_snd_pcm_close (VirtIOSoundPCMStream *stream) { -if (stream) { -virtio_snd_pcm_flush (stream); -if (stream->info.direction ==VIRTIO_SND_D_OUTPUT ) { -audio_be_close_out (stream->s->audio_be, stream->voice.out); - stream->voice.out =NULL ; - }else if (stream->info.direction ==VIRTIO_SND_D_INPUT ) { -audio_be_close_in (stream->s->audio_be, stream->voice.in); - stream->voice.in =NULL ; - } +virtio_snd_pcm_flush (stream); +if (stream->info.direction ==VIRTIO_SND_D_OUTPUT ) { +audio_be_close_out (stream->s->audio_be, stream->voice.out); + stream->voice.out =NULL ; + }else if (stream->info.direction ==VIRTIO_SND_D_INPUT ) { +audio_be_close_in (stream->s->audio_be, stream->voice.in); + stream->voice.in =NULL ; } }@@ -423,32 +459,23 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)virtio_snd_pcm_set_params *params;VirtIOSoundPCMStream *stream; -if (s->pcm.streams ==NULL || - s->pcm.pcm_params ==NULL || - stream_id >= s->snd_conf.streams) { + stream =virtio_snd_pcm_get_stream (s, stream_id); +if (!stream) {return cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); } - params =virtio_snd_pcm_get_params (s, stream_id); -if (params ==NULL ) { +switch (stream->state) { +case VSND_PCMSTREAM_STATE_PARAMS_SET : +case VSND_PCMSTREAM_STATE_PREPARED : +case VSND_PCMSTREAM_STATE_RELEASED : +break ; +default :return cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); } - stream =virtio_snd_pcm_get_stream (s, stream_id); -if (stream ==NULL ) { - stream = &s->streams[stream_id]; - stream->active = false; - stream->latency_bytes =0 ; - -/* -* stream_id >= s->snd_conf.streams was checked before so this is -* in-bounds -*/ - s->pcm.streams[stream_id] = stream; - } + params =virtio_snd_pcm_get_params (s, stream_id);virtio_snd_get_qemu_audsettings (&as, params); - stream->params = *params; stream->positions[0 ] =VIRTIO_SND_CHMAP_FL ; stream->positions[1 ] =VIRTIO_SND_CHMAP_FR ;@@ -472,6 +499,8 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)audio_be_set_volume_in_lr (s->audio_be, stream->voice.in,0 ,255 ,255 ); } + stream->state =VSND_PCMSTREAM_STATE_PREPARED ; +return cpu_to_le32 (VIRTIO_SND_S_OK ); }@@ -540,12 +569,28 @@ static uint32_t virtio_snd_pcm_start_stop(VirtIOSound *s, }if (start) { +switch (stream->state) { +case VSND_PCMSTREAM_STATE_PREPARED : +case VSND_PCMSTREAM_STATE_STOPPED : +break ; +default : +return cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); + } +trace_virtio_snd_handle_pcm_start (stream_id); + stream->state =VSND_PCMSTREAM_STATE_STARTED ; }else { +switch (stream->state) { +case VSND_PCMSTREAM_STATE_STARTED : +break ; +default : +return cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); + } +trace_virtio_snd_handle_pcm_stop (stream_id); + stream->state =VSND_PCMSTREAM_STATE_STOPPED ; } - stream->active = start;if (stream->info.direction ==VIRTIO_SND_D_OUTPUT ) {audio_be_set_active_out (s->audio_be, stream->voice.out, start); }else {@@ -639,6 +684,15 @@ static void virtio_snd_handle_pcm_release(VirtIOSound *s,return ; } +switch (stream->state) { +case VSND_PCMSTREAM_STATE_PREPARED : +case VSND_PCMSTREAM_STATE_STOPPED : +break ; +default : + cmd->resp.code =cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); +return ; + } +if (virtio_snd_pcm_get_io_msgs_count (stream)) {/* * virtio-v1.2-csd01, 5.14.6.6.5.1, @@ -653,6 +707,8 @@ static void virtio_snd_handle_pcm_release(VirtIOSound *s,virtio_snd_pcm_flush (stream); } + stream->state =VSND_PCMSTREAM_STATE_RELEASED ; + cmd->resp.code =cpu_to_le32 (VIRTIO_SND_S_OK ); }@@ -874,12 +930,11 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq) } stream_id =le32_to_cpu (hdr.stream_id); -if (stream_id >= vsnd->snd_conf.streams - || vsnd->pcm.streams[stream_id] ==NULL ) { +if (stream_id >= vsnd->snd_conf.streams) { goto tx_err; } - stream = vsnd->pcm.streams[stream_id]; + stream = &vsnd->streams[stream_id];if (stream->info.direction !=VIRTIO_SND_D_OUTPUT ) { goto tx_err; }@@ -954,13 +1009,12 @@ static void virtio_snd_handle_rx_xfer(VirtIODevice *vdev, VirtQueue *vq) } stream_id =le32_to_cpu (hdr.stream_id); -if (stream_id >= vsnd->snd_conf.streams - || !vsnd->pcm.streams[stream_id]) { +if (stream_id >= vsnd->snd_conf.streams) { goto rx_err; } - stream = vsnd->pcm.streams[stream_id]; -if (stream ==NULL || stream->info.direction !=VIRTIO_SND_D_INPUT ) { + stream = &vsnd->streams[stream_id]; +if (stream->info.direction !=VIRTIO_SND_D_INPUT ) { goto rx_err; }@@ -1019,8 +1073,6 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp)ERRP_GUARD ();VirtIOSound *vsnd =VIRTIO_SND (dev);VirtIODevice *vdev =VIRTIO_DEVICE (dev); -virtio_snd_pcm_set_params default_params = {0 }; -uint32_t status;trace_virtio_snd_realize (vsnd);@@ -1058,6 +1110,7 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp)VirtIOSoundPCMStream *stream = &vsnd->streams[i]; stream->id = i; + stream->state =VSND_PCMSTREAM_STATE_UNINITIALIZED ; stream->s = vsnd;QSIMPLEQ_INIT (&stream->queue); stream->info.hdr.hda_fn_nid =VIRTIO_SOUND_HDA_FN_NID ;@@ -1071,21 +1124,9 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp) stream->info.channels_max =2 ; } - vsnd->pcm.streams = - g_new0(VirtIOSoundPCMStream *, vsnd->snd_conf.streams); - vsnd->pcm.pcm_params = -g_new0 (virtio_snd_pcm_set_params, vsnd->snd_conf.streams); -virtio_init (vdev,VIRTIO_ID_SOUND ,sizeof (virtio_snd_config));virtio_add_feature (&vsnd->features,VIRTIO_F_VERSION_1 ); -/* set default params for all streams */ - default_params.features =0 ; - default_params.buffer_bytes =cpu_to_le32 (8192 ); - default_params.period_bytes =cpu_to_le32 (2048 ); - default_params.channels =2 ; - default_params.format =VIRTIO_SND_PCM_FMT_S16 ; - default_params.rate =VIRTIO_SND_PCM_RATE_48000 ; vsnd->queues[VIRTIO_SND_VQ_CONTROL ] =virtio_add_queue (vdev,64 , virtio_snd_handle_ctrl); vsnd->queues[VIRTIO_SND_VQ_EVENT] =@@ -1096,28 +1137,6 @@ static void virtio_snd_realize(DeviceState *dev, Error **errp)virtio_add_queue (vdev,64 , virtio_snd_handle_rx_xfer);QTAILQ_INIT (&vsnd->cmdq);QSIMPLEQ_INIT (&vsnd->invalid); - -for (uint32_t i =0 ; i < vsnd->snd_conf.streams; i++) { - status =virtio_snd_set_pcm_params (vsnd, i, &default_params); -if (status !=cpu_to_le32 (VIRTIO_SND_S_OK )) { -error_setg (errp, -"Can't initialize stream params, device responded with %s." , -print_code (status)); - goto error_cleanup; - } - status =virtio_snd_pcm_prepare (vsnd, i); -if (status !=cpu_to_le32 (VIRTIO_SND_S_OK )) { -error_setg (errp, -"Can't prepare streams, device responded with %s." , -print_code (status)); - goto error_cleanup; - } - } - -return ; - -error_cleanup: -virtio_snd_unrealize (dev); }static inline void update_latency (VirtIOSoundPCMStream *s,size_t used)@@ -1166,7 +1185,7 @@ static void virtio_snd_pcm_out_cb(void *data, int available)if (!virtio_queue_ready (buffer->vq)) {return ; } -if (!stream->active) { +if (!(stream->state &VSND_PCMSTREAM_STATE_F_ACTIVE )) {/* Stream has stopped, so do not perform audio_be_write. */ return_tx_buffer (stream, buffer);continue ;@@ -1260,7 +1279,7 @@ static void virtio_snd_pcm_in_cb(void *data, int available)if (!virtio_queue_ready (buffer->vq)) {return ; } -if (!stream->active) { +if (!(stream->state &VSND_PCMSTREAM_STATE_F_ACTIVE )) {/* Stream has stopped, so do not perform audio_be_read. */ return_rx_buffer (stream, buffer);continue ;@@ -1333,17 +1352,15 @@ static void virtio_snd_unrealize(DeviceState *dev)qemu_del_vm_change_state_handler (vsnd->vmstate);trace_virtio_snd_unrealize (vsnd);
Quoted 14 lines
hw/audio/virtio-snd.c static void virtio_snd_unrealize(DeviceState *dev) · line 1360
Quoted 11 lines
hw/audio/virtio-snd.c +5
-5-if (vsnd->pcm.streams) { +if (vsnd->streams) { +virtio_snd_process_cmdq (vsnd);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); + stream = &vsnd->streams[i]; +if (stream->state &VSND_PCMSTREAM_STATE_F_PREPARED ) { +virtio_snd_pcm_flush (stream);I am not sure we should let the device do IO during unrealize, but this seems to be just buffer work and the notification is hopefully ignored.
Hmm.. yeah, that's a good point. I went to recheck everything once again and it looks like
even in current QEMU code (without these patches), we have the following callstack:virtio_snd_unrealize() -> virtio_snd_pcm_close() -> virtio_snd_pcm_flush() -> return_{r,t}x_buffer() -> virtio_notify().
So, I agree that doing IO during unrealize() is a bit weird, cause AFAIU in once we call unrealize guest
has no communication with the device (i.e. device is detached from a bus and so on).
WDYT?
Quoted 3 lines
However, it should rather call
virtio_snd_pcm_close()to release the associated SWVoices.
Exactly, I believe I should do something like:
hw/audio/virtio-snd.c +1 -1
@@ -436,7 +436,6 @@ static void virtio_snd_get_qemu_audsettings(audsettings *as,*/ static void virtio_snd_pcm_close (VirtIOSoundPCMStream *stream) { -virtio_snd_pcm_flush (stream);if (stream->info.direction ==VIRTIO_SND_D_OUTPUT ) {audio_be_close_out (stream->s->audio_be, stream->voice.out); stream->voice.out =NULL ;
@@ -1361,6 +1360,7 @@ static void virtio_snd_unrealize(DeviceState *dev)if (stream->state &VSND_PCMSTREAM_STATE_F_PREPARED ) {virtio_snd_pcm_flush (stream); } +virtio_snd_pcm_close (stream); } }g_free (vsnd->streams);
this is my mistake, in the original patch a call to virtio_snd_pcm_flush() was removed from virtio_snd_pcm_close(), then virtio_snd_pcm_flush and virtio_snd_pcm_close we explicitly called from virtio_snd_unrealize(). I will fix this.
You've given me an idea and I looked once again on all codepaths where we do audio_be_{open,close}_{in,out}(),
and I realized that for some reason in virtio_snd_handle_pcm_release() we only do flush, but not close, while
according to 5.14.6.6.1 PCM Command Lifecycle:
Quoted 3 lines
* -
RELEASE
* The device releases the stream (frees resources, etc).
* Possible valid transitions:SET PARAMETERS,PREPARE.
So, once we reached RELEASE, we can only do:
RELEASE -> PREPARE
RELEASE -> SET PARAMETERS -> PREPARE
[ note: we can also do SET PARAMETERS twice or do loops to, ofc ]
my point is that anyways, in any guest driver that makes something reasonable, after RELEASE
we, at some point, go to PREPARE and it does audio_be_open_{in,out}(). So the question is why
we don't have a call to virtio_snd_pcm_close() in virtio_snd_handle_pcm_release()?..
Kind regards,
Alex
Quoted 4 lines
--
Marc-André Lureau <[email protected]>
PATCH v2 6/9 hw/audio/virtio-sound: introduce virtio_snd_pcm_open()
In reply to Alexander MikhalitsynFrom: Volker Rümelin <[email protected]>
Split out the function virtio_snd_pcm_open() from virtio_snd_pcm_prepare(). A later patch also needs the new function. There is no functional change.
--- hw/audio/virtio-snd.c | 60 ++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 27 deletions(-)
hw/audio/virtio-snd.c +33 -27
@@ -429,6 +429,38 @@ static void virtio_snd_get_qemu_audsettings(audsettings *as, as->big_endian = false;/* Conforming to VIRTIO 1.0: always little endian. */ } +/* +* Open a stream. +* +* @stream: VirtIOSoundPCMStream *stream +*/ +static void virtio_snd_pcm_open (VirtIOSoundPCMStream *stream) +{ +virtio_snd_get_qemu_audsettings (&stream->as, &stream->params); + stream->positions[0 ] =VIRTIO_SND_CHMAP_FL ; + stream->positions[1 ] =VIRTIO_SND_CHMAP_FR ; + +if (stream->info.direction ==VIRTIO_SND_D_OUTPUT ) { + stream->voice.out =audio_be_open_out (stream->s->audio_be, + stream->voice.out, +"virtio-sound.out" , + stream, + virtio_snd_pcm_out_cb, + &stream->as); +audio_be_set_volume_out_lr (stream->s->audio_be, + stream->voice.out,0 ,255 ,255 ); + }else { + stream->voice.in =audio_be_open_in (stream->s->audio_be, + stream->voice.in, +"virtio-sound.in" , + stream, + virtio_snd_pcm_in_cb, + &stream->as); +audio_be_set_volume_in_lr (stream->s->audio_be, + stream->voice.in,0 ,255 ,255 ); + } +} +/* * Close a stream and free all its resources. *
@@ -455,8 +487,6 @@ static void virtio_snd_pcm_close(VirtIOSoundPCMStream *stream)*/ static uint32_t virtio_snd_pcm_prepare (VirtIOSound *s,uint32_t stream_id) { -audsettings as; -virtio_snd_pcm_set_params *params;VirtIOSoundPCMStream *stream; stream =virtio_snd_pcm_get_stream (s, stream_id);
@@ -473,31 +503,7 @@ static uint32_t virtio_snd_pcm_prepare(VirtIOSound *s, uint32_t stream_id)return cpu_to_le32 (VIRTIO_SND_S_BAD_MSG ); } - params =virtio_snd_pcm_get_params (s, stream_id); - -virtio_snd_get_qemu_audsettings (&as, params); - - stream->positions[0 ] =VIRTIO_SND_CHMAP_FL ; - stream->positions[1 ] =VIRTIO_SND_CHMAP_FR ; - stream->as = as; - -if (stream->info.direction ==VIRTIO_SND_D_OUTPUT ) { - stream->voice.out =audio_be_open_out (s->audio_be, - stream->voice.out, -"virtio-sound.out" , - stream, - virtio_snd_pcm_out_cb, - &as); -audio_be_set_volume_out_lr (s->audio_be, stream->voice.out,0 ,255 ,255 ); - }else { - stream->voice.in =audio_be_open_in (s->audio_be, - stream->voice.in, -"virtio-sound.in" , - stream, - virtio_snd_pcm_in_cb, - &as); -audio_be_set_volume_in_lr (s->audio_be, stream->voice.in,0 ,255 ,255 ); - } +virtio_snd_pcm_open (stream); stream->state =VSND_PCMSTREAM_STATE_PREPARED ;
Signature · 1 line
2.47.3
Re: [PATCH v2 6/9] hw/audio/virtio-sound: introduce virtio_snd_pcm_open()
Quoted 3 lines · Alexander Mikhalitsyn
Split out the function
virtio_snd_pcm_open()fromvirtio_snd_pcm_prepare(). A later patch also needs the new function. There is no functional change.
Signature · 1 line
Marc-André Lureau <[email protected]>
PATCH v2 7/9 hw/audio/virtio-sound: introduce virtio_snd_set_active()
In reply to Alexander MikhalitsynFrom: Volker Rümelin <[email protected]>
Split out the function virtio_snd_pcm_set_active() from virtio_snd_pcm_start_stop(). A later patch also needs this new funcion. There is no functional change.
--- hw/audio/virtio-snd.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-)
hw/audio/virtio-snd.c +16 -5
@@ -461,6 +461,21 @@ static void virtio_snd_pcm_open(VirtIOSoundPCMStream *stream) } } +/* +* Activate/deactivate a stream. +* +* @stream: VirtIOSoundPCMStream *stream +* @active: whether to activate or deactivate the stream +*/ +static void virtio_snd_pcm_set_active (VirtIOSoundPCMStream *stream,bool active) +{ +if (stream->info.direction ==VIRTIO_SND_D_OUTPUT ) { +audio_be_set_active_out (stream->s->audio_be, stream->voice.out, active); + }else { +audio_be_set_active_in (stream->s->audio_be, stream->voice.in, active); + } +} +/* * Close a stream and free all its resources. *
@@ -597,11 +612,7 @@ static uint32_t virtio_snd_pcm_start_stop(VirtIOSound *s, stream->state =VSND_PCMSTREAM_STATE_STOPPED ; } -if (stream->info.direction ==VIRTIO_SND_D_OUTPUT ) { -audio_be_set_active_out (s->audio_be, stream->voice.out, start); - }else { -audio_be_set_active_in (s->audio_be, stream->voice.in, start); - } +virtio_snd_pcm_set_active (stream, start);return cpu_to_le32 (VIRTIO_SND_S_OK ); }
Signature · 1 line
2.47.3
Re: [PATCH v2 7/9] hw/audio/virtio-sound: introduce virtio_snd_set_active()
Quoted 3 lines · Alexander Mikhalitsyn
Split out the function
virtio_snd_pcm_set_active()fromvirtio_snd_pcm_start_stop(). A later patch also needs this new funcion. There is no functional change.
Signature · 1 line
Marc-André Lureau <[email protected]>
PATCH v2 8/9 hw/audio/virtio-sound: add missing vmstate fields
In reply to Alexander MikhalitsynFrom: Volker Rümelin <[email protected]>
The virtio-sound device is currently not migratable. Add the missing VMSTATE fields, enable migration and reconnect the audio streams after migration.
The queue_inuse[] array variables mimic the inuse variable in struct VirtQueue which is private. They are needed to restart the virtio queues after migration.
--- hw/audio/virtio-snd.c | 83 +++++++++++++++++++++++++++++++---- include/hw/audio/virtio-snd.h | 1 + 2 files changed, 76 insertions(+), 8 deletions(-)
hw/audio/virtio-snd.c +75 -8
@@ -24,7 +24,6 @@#include "qapi/error.h" #include "hw/audio/virtio-snd.h" -#define VIRTIO_SOUND_VM_VERSION 1#define VIRTIO_SOUND_JACK_DEFAULT 0#define VIRTIO_SOUND_STREAM_DEFAULT 2#define VIRTIO_SOUND_CHMAP_DEFAULT 0
@@ -78,17 +77,40 @@ static uint32_t supported_rates = BIT(VIRTIO_SND_PCM_RATE_5512) | BIT(VIRTIO_SND_PCM_RATE_192000) |BIT (VIRTIO_SND_PCM_RATE_384000 ); +static const VMStateDescription vmstate_virtio_snd_stream = { + .name ="virtio-sound-stream" , + .fields = (const VMStateField []) { +VMSTATE_UINT32 (state, VirtIOSoundPCMStream), +VMSTATE_UINT32 (info.hdr.hda_fn_nid, VirtIOSoundPCMStream), +VMSTATE_UINT32 (info.features, VirtIOSoundPCMStream), +VMSTATE_UINT64 (info.formats, VirtIOSoundPCMStream), +VMSTATE_UINT64 (info.rates, VirtIOSoundPCMStream), +VMSTATE_UINT8 (info.direction, VirtIOSoundPCMStream), +VMSTATE_UINT8 (info.channels_min, VirtIOSoundPCMStream), +VMSTATE_UINT8 (info.channels_max, VirtIOSoundPCMStream), +VMSTATE_UINT32 (params.buffer_bytes, VirtIOSoundPCMStream), +VMSTATE_UINT32 (params.period_bytes, VirtIOSoundPCMStream), +VMSTATE_UINT32 (params.features, VirtIOSoundPCMStream), +VMSTATE_UINT8 (params.channels, VirtIOSoundPCMStream), +VMSTATE_UINT8 (params.format, VirtIOSoundPCMStream), +VMSTATE_UINT8 (params.rate, VirtIOSoundPCMStream), +VMSTATE_END_OF_LIST () + }, +}; +static const VMStateDescription vmstate_virtio_snd_device = { - .name =TYPE_VIRTIO_SND , - .version_id =VIRTIO_SOUND_VM_VERSION , - .minimum_version_id =VIRTIO_SOUND_VM_VERSION , + .name ="virtio-sound-device" , + .fields = (const VMStateField []) { +VMSTATE_UINT32_ARRAY (queue_inuse, VirtIOSound,VIRTIO_SND_VQ_MAX ), +VMSTATE_STRUCT_VARRAY_POINTER_UINT32 (streams, VirtIOSound, + snd_conf.streams, + vmstate_virtio_snd_stream, VirtIOSoundPCMStream), +VMSTATE_END_OF_LIST () + }, };static const VMStateDescription vmstate_virtio_snd = { - .name =TYPE_VIRTIO_SND , - .unmigratable =1 , - .minimum_version_id =VIRTIO_SOUND_VM_VERSION , - .version_id =VIRTIO_SOUND_VM_VERSION , + .name ="virtio-sound" , .fields = (const VMStateField []) {VMSTATE_VIRTIO_DEVICE ,VMSTATE_END_OF_LIST ()
@@ -800,6 +822,7 @@ process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cmd) sizeof(virtio_snd_hdr));virtqueue_push (cmd->vq, cmd->elem,sizeof (virtio_snd_hdr) + cmd->payload_size); + s->queue_inuse[VIRTIO_SND_VQ_CONTROL ] -=1 ;virtio_notify (VIRTIO_DEVICE (s), cmd->vq); }
@@ -846,6 +869,7 @@ static void virtio_snd_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) elem =virtqueue_pop (vq,sizeof (VirtQueueElement));while (elem) { + s->queue_inuse[VIRTIO_SND_VQ_CONTROL ] +=1 ; cmd =g_new0 (virtio_snd_ctrl_command,1 ); cmd->elem = elem; cmd->vq = vq;
@@ -956,6 +980,7 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq) goto tx_err; } + vsnd->queue_inuse[VIRTIO_SND_VQ_TX ] +=1 ; size =iov_size (elem->out_sg, elem->out_num) - msg_sz; buffer =g_malloc0 (sizeof (VirtIOSoundPCMBuffer) + size);
@@ -1035,6 +1060,7 @@ static void virtio_snd_handle_rx_xfer(VirtIODevice *vdev, VirtQueue *vq) goto rx_err; } + vsnd->queue_inuse[VIRTIO_SND_VQ_RX ] +=1 ; size =iov_size (elem->in_sg, elem->in_num) -sizeof (virtio_snd_pcm_status); buffer =g_malloc0 (sizeof (VirtIOSoundPCMBuffer) + size);
@@ -1177,6 +1203,7 @@ static inline void return_tx_buffer(VirtIOSoundPCMStream *stream,virtqueue_push (buffer->vq, buffer->elem,sizeof (virtio_snd_pcm_status)); + stream->s->queue_inuse[VIRTIO_SND_VQ_TX ] -=1 ;virtio_notify (VIRTIO_DEVICE (stream->s), buffer->vq); QSIMPLEQ_REMOVE(&stream->queue, buffer,
@@ -1270,6 +1297,7 @@ static inline void return_rx_buffer(VirtIOSoundPCMStream *stream,virtqueue_push (buffer->vq, buffer->elem,sizeof (virtio_snd_pcm_status) + buffer->size); + stream->s->queue_inuse[VIRTIO_SND_VQ_RX ] -=1 ;virtio_notify (VIRTIO_DEVICE (stream->s), buffer->vq); QSIMPLEQ_REMOVE(&stream->queue, buffer,
@@ -1387,6 +1415,40 @@ static void virtio_snd_unrealize(DeviceState *dev)virtio_cleanup (vdev); } +static int virtio_snd_post_load (VirtIODevice *vdev) +{ +VirtIOSound *s =VIRTIO_SND (vdev); +uint32_t i; + +for (i =0 ; i < s->snd_conf.streams; i++) { +struct VirtIOSoundPCMStream *stream; + + stream =virtio_snd_pcm_get_stream (s, i); +if (stream->state &VSND_PCMSTREAM_STATE_F_PREPARED ) { +virtio_snd_pcm_open (stream); + +if (stream->state &VSND_PCMSTREAM_STATE_F_ACTIVE ) { +virtio_snd_pcm_set_active (stream, true); + } + } + } + +for (i =0 ; i <VIRTIO_SND_VQ_MAX ; i++) { +if (s->queue_inuse[i]) { +bool rc; + + rc =virtqueue_rewind (s->queues[i], s->queue_inuse[i]); +if (!rc) { +error_report ( +"virtio-sound: could not rewind %u elements in queue %u" , + s->queue_inuse[i], i); + } + s->queue_inuse[i] =0 ; + } + } + +return 0 ; +}static void virtio_snd_reset (VirtIODevice *vdev) {
@@ -1419,6 +1481,10 @@ static void virtio_snd_reset(VirtIODevice *vdev)virtio_snd_pcm_buffer_free (buffer); } } + +for (i =0 ; i <VIRTIO_SND_VQ_MAX ; i++) { + vsnd->queue_inuse[i] =0 ; + } }static void virtio_snd_class_init (ObjectClass *klass,const void *data)
@@ -1432,6 +1498,7 @@ static void virtio_snd_class_init(ObjectClass *klass, const void *data) dc->vmsd = &vmstate_virtio_snd; vdc->vmsd = &vmstate_virtio_snd_device; + vdc->post_load = virtio_snd_post_load; vdc->realize = virtio_snd_realize; vdc->unrealize = virtio_snd_unrealize; vdc->get_config = virtio_snd_get_config;
include/hw/audio/virtio-snd.h +1 -0
@@ -197,6 +197,7 @@ struct VirtIOSound {VirtIODevice parent_obj;VirtQueue *queues[VIRTIO_SND_VQ_MAX ]; +uint32_t queue_inuse[VIRTIO_SND_VQ_MAX ];uint64_t features;VirtIOSoundPCMStream *streams;AudioBackend *audio_be;
Signature · 1 line
2.47.3
Re: [PATCH v2 8/9] hw/audio/virtio-sound: add missing vmstate fields
Quoted 49 lines
The virtio-sound device is currently not migratable. Add the missing VMSTATE fields, enable migration and reconnect the audio streams after migration.
The queue_inuse[] array variables mimic the inuse variable in struct VirtQueue which is private. They are needed to restart the virtio queues after migration.
Signed-off-by Volker Rümelin <[email protected]>[AM: trivial rebase changes]Signed-off-by Alexander Mikhalitsyn <[email protected]>Message-ID: <[email protected]>
include/hw/audio/virtio-snd.h +1
-0@@ -197,6 +197,7 @@ struct VirtIOSound {VirtIODevice parent_obj;VirtQueue *queues[VIRTIO_SND_VQ_MAX ]; +uint32_t queue_inuse[VIRTIO_SND_VQ_MAX ];uint64_t features;VirtIOSoundPCMStream *streams;AudioBackend *audio_be;hw/audio/virtio-snd.c +8
-1@@ -24,7 +24,6 @@#include "qapi/error.h" #include "hw/audio/virtio-snd.h" -#define VIRTIO_SOUND_VM_VERSION 1#define VIRTIO_SOUND_JACK_DEFAULT 0#define VIRTIO_SOUND_STREAM_DEFAULT 2#define VIRTIO_SOUND_CHMAP_DEFAULT 0@@ -78,17 +77,40 @@ static uint32_t supported_rates = BIT(VIRTIO_SND_PCM_RATE_5512) | BIT(VIRTIO_SND_PCM_RATE_192000) |BIT (VIRTIO_SND_PCM_RATE_384000 ); +static const VMStateDescription vmstate_virtio_snd_stream = { + .name ="virtio-sound-stream" , + .fields = (const VMStateField []) { +VMSTATE_UINT32 (state, VirtIOSoundPCMStream), +VMSTATE_UINT32 (info.hdr.hda_fn_nid, VirtIOSoundPCMStream), +VMSTATE_UINT32 (info.features, VirtIOSoundPCMStream), +VMSTATE_UINT64 (info.formats, VirtIOSoundPCMStream), +VMSTATE_UINT64 (info.rates, VirtIOSoundPCMStream),
hw/audio/virtio-snd.c static uint32_t supported_rates = BIT(VIRTIO_SND_PCM_RATE_5512) · line 97
Quoted 10 lines
hw/audio/virtio-snd.c +10
-0+VMSTATE_UINT8 (info.direction, VirtIOSoundPCMStream), +VMSTATE_UINT8 (info.channels_min, VirtIOSoundPCMStream), +VMSTATE_UINT8 (info.channels_max, VirtIOSoundPCMStream), +VMSTATE_UINT32 (params.buffer_bytes, VirtIOSoundPCMStream), +VMSTATE_UINT32 (params.period_bytes, VirtIOSoundPCMStream), +VMSTATE_UINT32 (params.features, VirtIOSoundPCMStream), +VMSTATE_UINT8 (params.channels, VirtIOSoundPCMStream), +VMSTATE_UINT8 (params.format, VirtIOSoundPCMStream), +VMSTATE_UINT8 (params.rate, VirtIOSoundPCMStream), +VMSTATE_END_OF_LIST ()
While reviewing the VirtIOSoundPCMStream state:
- "id" field should be removed after or with "allocate an array of streams"
- "buf_wpos" seems dead code, drop it?
- "info" seems immutable from the guest side, I don't think we should migrate it
- "positions" seems to be dead code. Drop it?
Otherwise, the patch looks ok to me
Quoted 89 lines
hw/audio/virtio-snd.c +29
-7+ }, +}; +static const VMStateDescription vmstate_virtio_snd_device = { - .name =TYPE_VIRTIO_SND , - .version_id =VIRTIO_SOUND_VM_VERSION , - .minimum_version_id =VIRTIO_SOUND_VM_VERSION , + .name ="virtio-sound-device" , + .fields = (const VMStateField []) { +VMSTATE_UINT32_ARRAY (queue_inuse, VirtIOSound,VIRTIO_SND_VQ_MAX ), +VMSTATE_STRUCT_VARRAY_POINTER_UINT32 (streams, VirtIOSound, + snd_conf.streams, + vmstate_virtio_snd_stream, VirtIOSoundPCMStream), +VMSTATE_END_OF_LIST () + }, };static const VMStateDescription vmstate_virtio_snd = { - .name =TYPE_VIRTIO_SND , - .unmigratable =1 , - .minimum_version_id =VIRTIO_SOUND_VM_VERSION , - .version_id =VIRTIO_SOUND_VM_VERSION , + .name ="virtio-sound" , .fields = (const VMStateField []) {VMSTATE_VIRTIO_DEVICE ,VMSTATE_END_OF_LIST ()@@ -800,6 +822,7 @@ process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cmd) sizeof(virtio_snd_hdr));virtqueue_push (cmd->vq, cmd->elem,sizeof (virtio_snd_hdr) + cmd->payload_size); + s->queue_inuse[VIRTIO_SND_VQ_CONTROL ] -=1 ;virtio_notify (VIRTIO_DEVICE (s), cmd->vq); }@@ -846,6 +869,7 @@ static void virtio_snd_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) elem =virtqueue_pop (vq,sizeof (VirtQueueElement));while (elem) { + s->queue_inuse[VIRTIO_SND_VQ_CONTROL ] +=1 ; cmd =g_new0 (virtio_snd_ctrl_command,1 ); cmd->elem = elem; cmd->vq = vq;@@ -956,6 +980,7 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq) goto tx_err; } + vsnd->queue_inuse[VIRTIO_SND_VQ_TX ] +=1 ; size =iov_size (elem->out_sg, elem->out_num) - msg_sz; buffer =g_malloc0 (sizeof (VirtIOSoundPCMBuffer) + size);@@ -1035,6 +1060,7 @@ static void virtio_snd_handle_rx_xfer(VirtIODevice *vdev, VirtQueue *vq) goto rx_err; } + vsnd->queue_inuse[VIRTIO_SND_VQ_RX ] +=1 ; size =iov_size (elem->in_sg, elem->in_num) -sizeof (virtio_snd_pcm_status); buffer =g_malloc0 (sizeof (VirtIOSoundPCMBuffer) + size);@@ -1177,6 +1203,7 @@ static inline void return_tx_buffer(VirtIOSoundPCMStream *stream,virtqueue_push (buffer->vq, buffer->elem,sizeof (virtio_snd_pcm_status)); + stream->s->queue_inuse[VIRTIO_SND_VQ_TX ] -=1 ;virtio_notify (VIRTIO_DEVICE (stream->s), buffer->vq); QSIMPLEQ_REMOVE(&stream->queue, buffer,@@ -1270,6 +1297,7 @@ static inline void return_rx_buffer(VirtIOSoundPCMStream *stream,virtqueue_push (buffer->vq, buffer->elem,sizeof (virtio_snd_pcm_status) + buffer->size); + stream->s->queue_inuse[VIRTIO_SND_VQ_RX ] -=1 ;virtio_notify (VIRTIO_DEVICE (stream->s), buffer->vq); QSIMPLEQ_REMOVE(&stream->queue, buffer,@@ -1387,6 +1415,40 @@ static void virtio_snd_unrealize(DeviceState *dev)virtio_cleanup (vdev); } +static int virtio_snd_post_load (VirtIODevice *vdev) +{ +VirtIOSound *s =VIRTIO_SND (vdev); +uint32_t i; + +for (i =0 ; i < s->snd_conf.streams; i++) { +struct VirtIOSoundPCMStream *stream; + + stream =virtio_snd_pcm_get_stream (s, i); +if (stream->state &VSND_PCMSTREAM_STATE_F_PREPARED ) { +virtio_snd_pcm_open (stream);
hw/audio/virtio-snd.c static void virtio_snd_unrealize(DeviceState *dev) · line 1438
Quoted 10 lines
hw/audio/virtio-snd.c +10
-0+ +if (stream->state &VSND_PCMSTREAM_STATE_F_ACTIVE ) { +virtio_snd_pcm_set_active (stream, true); + } + } + } + +for (i =0 ; i <VIRTIO_SND_VQ_MAX ; i++) { +if (s->queue_inuse[i]) { +bool rc;
I would just drop the variable ("rc" is rather for int in general)
Signature · 1 line
Marc-André Lureau <[email protected]>
Re: [PATCH v2 8/9] hw/audio/virtio-sound: add missing vmstate fields
Am Do., 25. Juni 2026 um 17:00 Uhr schrieb <[email protected]>:
Quoted 63 lines
Quoted 50 lines
The virtio-sound device is currently not migratable. Add the missing VMSTATE fields, enable migration and reconnect the audio streams after migration.
The queue_inuse[] array variables mimic the inuse variable in struct VirtQueue which is private. They are needed to restart the virtio queues after migration.
Signed-off-by Volker Rümelin <[email protected]>[AM: trivial rebase changes]Signed-off-by Alexander Mikhalitsyn <[email protected]>Message-ID: <[email protected]>
include/hw/audio/virtio-snd.h +1
-0@@ -197,6 +197,7 @@ struct VirtIOSound {VirtIODevice parent_obj;VirtQueue *queues[VIRTIO_SND_VQ_MAX ]; +uint32_t queue_inuse[VIRTIO_SND_VQ_MAX ];uint64_t features;VirtIOSoundPCMStream *streams;AudioBackend *audio_be;hw/audio/virtio-snd.c +8
-1@@ -24,7 +24,6 @@#include "qapi/error.h" #include "hw/audio/virtio-snd.h" -#define VIRTIO_SOUND_VM_VERSION 1#define VIRTIO_SOUND_JACK_DEFAULT 0#define VIRTIO_SOUND_STREAM_DEFAULT 2#define VIRTIO_SOUND_CHMAP_DEFAULT 0@@ -78,17 +77,40 @@ static uint32_t supported_rates = BIT(VIRTIO_SND_PCM_RATE_5512) | BIT(VIRTIO_SND_PCM_RATE_192000) |BIT (VIRTIO_SND_PCM_RATE_384000 ); +static const VMStateDescription vmstate_virtio_snd_stream = { + .name ="virtio-sound-stream" , + .fields = (const VMStateField []) { +VMSTATE_UINT32 (state, VirtIOSoundPCMStream), +VMSTATE_UINT32 (info.hdr.hda_fn_nid, VirtIOSoundPCMStream), +VMSTATE_UINT32 (info.features, VirtIOSoundPCMStream), +VMSTATE_UINT64 (info.formats, VirtIOSoundPCMStream), +VMSTATE_UINT64 (info.rates, VirtIOSoundPCMStream),hw/audio/virtio-snd.c static uint32_t supported_rates = BIT(VIRTIO_SND_PCM_RATE_5512) · line 97
Quoted 10 lines
hw/audio/virtio-snd.c +10
-0+VMSTATE_UINT8 (info.direction, VirtIOSoundPCMStream), +VMSTATE_UINT8 (info.channels_min, VirtIOSoundPCMStream), +VMSTATE_UINT8 (info.channels_max, VirtIOSoundPCMStream), +VMSTATE_UINT32 (params.buffer_bytes, VirtIOSoundPCMStream), +VMSTATE_UINT32 (params.period_bytes, VirtIOSoundPCMStream), +VMSTATE_UINT32 (params.features, VirtIOSoundPCMStream), +VMSTATE_UINT8 (params.channels, VirtIOSoundPCMStream), +VMSTATE_UINT8 (params.format, VirtIOSoundPCMStream), +VMSTATE_UINT8 (params.rate, VirtIOSoundPCMStream), +VMSTATE_END_OF_LIST ()While reviewing the VirtIOSoundPCMStream state:
Quoted 6 lines
- "id" field should be removed after or with "allocate an array of streams"
- "buf_wpos" seems dead code, drop it?
- "info" seems immutable from the guest side, I don't think we should migrate it
- "positions" seems to be dead code. Drop it?Otherwise, the patch looks ok to me
Dear Marc-André,
thank you again for such a detailed review comments. I have addressed all these suggestions in -v3 (have just sent). And also, it looks like previously I missedlatency_bytes field too (it was added in upstream QEMU after the original patches were published).
Kind regards,
Alex
Quoted 94 lines
Quoted 94 lines
hw/audio/virtio-snd.c +33
-7+ }, +}; +static const VMStateDescription vmstate_virtio_snd_device = { - .name =TYPE_VIRTIO_SND , - .version_id =VIRTIO_SOUND_VM_VERSION , - .minimum_version_id =VIRTIO_SOUND_VM_VERSION , + .name ="virtio-sound-device" , + .fields = (const VMStateField []) { +VMSTATE_UINT32_ARRAY (queue_inuse, VirtIOSound,VIRTIO_SND_VQ_MAX ), +VMSTATE_STRUCT_VARRAY_POINTER_UINT32 (streams, VirtIOSound, + snd_conf.streams, + vmstate_virtio_snd_stream, VirtIOSoundPCMStream), +VMSTATE_END_OF_LIST () + }, };static const VMStateDescription vmstate_virtio_snd = { - .name =TYPE_VIRTIO_SND , - .unmigratable =1 , - .minimum_version_id =VIRTIO_SOUND_VM_VERSION , - .version_id =VIRTIO_SOUND_VM_VERSION , + .name ="virtio-sound" , .fields = (const VMStateField []) {VMSTATE_VIRTIO_DEVICE ,VMSTATE_END_OF_LIST ()@@ -800,6 +822,7 @@ process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cmd) sizeof(virtio_snd_hdr));virtqueue_push (cmd->vq, cmd->elem,sizeof (virtio_snd_hdr) + cmd->payload_size); + s->queue_inuse[VIRTIO_SND_VQ_CONTROL ] -=1 ;virtio_notify (VIRTIO_DEVICE (s), cmd->vq); }@@ -846,6 +869,7 @@ static void virtio_snd_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) elem =virtqueue_pop (vq,sizeof (VirtQueueElement));while (elem) { + s->queue_inuse[VIRTIO_SND_VQ_CONTROL ] +=1 ; cmd =g_new0 (virtio_snd_ctrl_command,1 ); cmd->elem = elem; cmd->vq = vq;@@ -956,6 +980,7 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq) goto tx_err; } + vsnd->queue_inuse[VIRTIO_SND_VQ_TX ] +=1 ; size =iov_size (elem->out_sg, elem->out_num) - msg_sz; buffer =g_malloc0 (sizeof (VirtIOSoundPCMBuffer) + size);@@ -1035,6 +1060,7 @@ static void virtio_snd_handle_rx_xfer(VirtIODevice *vdev, VirtQueue *vq) goto rx_err; } + vsnd->queue_inuse[VIRTIO_SND_VQ_RX ] +=1 ; size =iov_size (elem->in_sg, elem->in_num) -sizeof (virtio_snd_pcm_status); buffer =g_malloc0 (sizeof (VirtIOSoundPCMBuffer) + size);@@ -1177,6 +1203,7 @@ static inline void return_tx_buffer(VirtIOSoundPCMStream *stream,virtqueue_push (buffer->vq, buffer->elem,sizeof (virtio_snd_pcm_status)); + stream->s->queue_inuse[VIRTIO_SND_VQ_TX ] -=1 ;virtio_notify (VIRTIO_DEVICE (stream->s), buffer->vq); QSIMPLEQ_REMOVE(&stream->queue, buffer,@@ -1270,6 +1297,7 @@ static inline void return_rx_buffer(VirtIOSoundPCMStream *stream,virtqueue_push (buffer->vq, buffer->elem,sizeof (virtio_snd_pcm_status) + buffer->size); + stream->s->queue_inuse[VIRTIO_SND_VQ_RX ] -=1 ;virtio_notify (VIRTIO_DEVICE (stream->s), buffer->vq); QSIMPLEQ_REMOVE(&stream->queue, buffer,@@ -1387,6 +1415,40 @@ static void virtio_snd_unrealize(DeviceState *dev)virtio_cleanup (vdev); } +static int virtio_snd_post_load (VirtIODevice *vdev) +{ +VirtIOSound *s =VIRTIO_SND (vdev); +uint32_t i; + +for (i =0 ; i < s->snd_conf.streams; i++) { +struct VirtIOSoundPCMStream *stream; + + stream =virtio_snd_pcm_get_stream (s, i); +if (stream->state &VSND_PCMSTREAM_STATE_F_PREPARED ) { +virtio_snd_pcm_open (stream); + +if (stream->state &VSND_PCMSTREAM_STATE_F_ACTIVE ) { +virtio_snd_pcm_set_active (stream, true); + }
Quoted 8 lines
hw/audio/virtio-snd.c static void virtio_snd_unrealize(DeviceState *dev) · line 1438
Quoted 6 lines
hw/audio/virtio-snd.c +6
-0+ } + } + +for (i =0 ; i <VIRTIO_SND_VQ_MAX ; i++) { +if (s->queue_inuse[i]) { +bool rc;I would just drop the variable ("rc" is rather for int in general)
+
Quoted 4 lines
--
Marc-André Lureau <[email protected]>
PATCH v2 9/9 hw/audio/virtio-sound: add placeholder for buffer write position
In reply to Alexander MikhalitsynFrom: Volker Rümelin <[email protected]>
When a running audio stream is migrated, on average half of a recording stream buffer is lost or half of a playback stream buffer is played twice. Add a placeholder for the write position of the current stream buffer to the migrated data. Additional program code is required to resolve the above issues. However, the placeholder makes it possible to add code in a backwards and forwards compatible way.
--- hw/audio/virtio-snd.c | 2 ++ include/hw/audio/virtio-snd.h | 2 ++ 2 files changed, 4 insertions(+)
hw/audio/virtio-snd.c +2 -0
@@ -81,6 +81,7 @@ static const VMStateDescription vmstate_virtio_snd_stream = { .name = "virtio-sound-stream", .fields = (const VMStateField []) {VMSTATE_UINT32 (state, VirtIOSoundPCMStream), +VMSTATE_UINT32 (buf_wpos, VirtIOSoundPCMStream),VMSTATE_UINT32 (info.hdr.hda_fn_nid, VirtIOSoundPCMStream),VMSTATE_UINT32 (info.features, VirtIOSoundPCMStream),VMSTATE_UINT64 (info.formats, VirtIOSoundPCMStream),
@@ -1431,6 +1432,7 @@ static int virtio_snd_post_load(VirtIODevice *vdev)virtio_snd_pcm_set_active (stream, true); } } + stream->buf_wpos =0 ; }for (i =0 ; i <VIRTIO_SND_VQ_MAX ; i++) {
include/hw/audio/virtio-snd.h +2 -0
@@ -124,6 +124,8 @@ struct VirtIOSoundPCMStream {virtio_snd_pcm_set_params params;uint32_t id;uint32_t state; +/* placeholder: write position in current VirtIOSoundPCMBuffer */ +uint32_t buf_wpos;/* channel position values (VIRTIO_SND_CHMAP_XXX) */ uint8_t positions[VIRTIO_SND_CHMAP_MAX_SIZE ];VirtIOSound *s;
Signature · 1 line
2.47.3
Re: [PATCH v2 9/9] hw/audio/virtio-sound: add placeholder for buffer write position
Hi
Quoted 10 lines · Alexander Mikhalitsyn
From: Volker Rümelin <[email protected]>
When a running audio stream is migrated, on average half of a recording stream buffer is lost or half of a playback stream buffer is played twice. Add a placeholder for the write position of the current stream buffer to the migrated data. Additional program code is required to resolve the above issues. However, the placeholder makes it possible to add code in a backwards and forwards compatible way.
I don't think it's convincing enough. I'd rather have proper versioning if / when such an improvement is made.
Quoted 44 lines
Signed-off-by Volker Rümelin <[email protected]>Signed-off-by Alexander Mikhalitsyn <[email protected]>--- hw/audio/virtio-snd.c | 2 ++ include/hw/audio/virtio-snd.h | 2 ++ 2 files changed, 4 insertions(+)hw/audio/virtio-snd.c +2
-0@@ -81,6 +81,7 @@ static const VMStateDescription vmstate_virtio_snd_stream = { .name = "virtio-sound-stream", .fields = (const VMStateField []) {VMSTATE_UINT32 (state, VirtIOSoundPCMStream), +VMSTATE_UINT32 (buf_wpos, VirtIOSoundPCMStream),VMSTATE_UINT32 (info.hdr.hda_fn_nid, VirtIOSoundPCMStream),VMSTATE_UINT32 (info.features, VirtIOSoundPCMStream),VMSTATE_UINT64 (info.formats, VirtIOSoundPCMStream),@@ -1431,6 +1432,7 @@ static int virtio_snd_post_load(VirtIODevice *vdev)virtio_snd_pcm_set_active (stream, true); } } + stream->buf_wpos =0 ; }for (i =0 ; i <VIRTIO_SND_VQ_MAX ; i++) {include/hw/audio/virtio-snd.h +2
-1@@ -124,6 +124,8 @@ struct VirtIOSoundPCMStream {virtio_snd_pcm_set_params params;uint32_t id;uint32_t state; +/* placeholder: write position in current VirtIOSoundPCMBuffer */ +uint32_t buf_wpos;/* channel position values (VIRTIO_SND_CHMAP_XXX) */ uint8_t positions[VIRTIO_SND_CHMAP_MAX_SIZE ];VirtIOSound *s; --2.47.3
Re: [PATCH v2 9/9] hw/audio/virtio-sound: add placeholder for buffer write position
Am Do., 25. Juni 2026 um 17:08 Uhr schrieb Marc-André Lureau <[email protected]>:
Quoted 16 lines
Hi
Quoted 10 lines · Alexander Mikhalitsyn
From: Volker Rümelin <[email protected]>
When a running audio stream is migrated, on average half of a recording stream buffer is lost or half of a playback stream buffer is played twice. Add a placeholder for the write position of the current stream buffer to the migrated data. Additional program code is required to resolve the above issues. However, the placeholder makes it possible to add code in a backwards and forwards compatible way.
Quoted 2 lines
I don't think it's convincing enough. I'd rather have proper versioning if / when such an improvement is made.
completely agree, I have added versioning and dropped this patch in -v3.
Quoted 46 lines
Quoted 45 lines
Signed-off-by Volker Rümelin <[email protected]>Signed-off-by Alexander Mikhalitsyn <[email protected]>--- hw/audio/virtio-snd.c | 2 ++ include/hw/audio/virtio-snd.h | 2 ++ 2 files changed, 4 insertions(+)hw/audio/virtio-snd.c +2
-0@@ -81,6 +81,7 @@ static const VMStateDescription vmstate_virtio_snd_stream = { .name = "virtio-sound-stream", .fields = (const VMStateField []) {VMSTATE_UINT32 (state, VirtIOSoundPCMStream), +VMSTATE_UINT32 (buf_wpos, VirtIOSoundPCMStream),VMSTATE_UINT32 (info.hdr.hda_fn_nid, VirtIOSoundPCMStream),VMSTATE_UINT32 (info.features, VirtIOSoundPCMStream),VMSTATE_UINT64 (info.formats, VirtIOSoundPCMStream),@@ -1431,6 +1432,7 @@ static int virtio_snd_post_load(VirtIODevice *vdev)virtio_snd_pcm_set_active (stream, true); } } + stream->buf_wpos =0 ; }for (i =0 ; i <VIRTIO_SND_VQ_MAX ; i++) {include/hw/audio/virtio-snd.h +2
-1@@ -124,6 +124,8 @@ struct VirtIOSoundPCMStream {virtio_snd_pcm_set_params params;uint32_t id;uint32_t state; +/* placeholder: write position in current VirtIOSoundPCMBuffer */ +uint32_t buf_wpos;/* channel position values (VIRTIO_SND_CHMAP_XXX) */ uint8_t positions[VIRTIO_SND_CHMAP_MAX_SIZE ];VirtIOSound *s; --2.47.3