From patchwork Sun Feb 18 08:33:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Volker_R=C3=BCmelin?= X-Patchwork-Id: 1900601 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TczVg3BmLz1ybm for ; Sun, 18 Feb 2024 19:34:35 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rbccs-0003Jz-R1; Sun, 18 Feb 2024 03:34:18 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rbccr-0003F9-0W for qemu-devel@nongnu.org; Sun, 18 Feb 2024 03:34:17 -0500 Received: from mailout05.t-online.de ([194.25.134.82]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rbccp-0006MT-6v for qemu-devel@nongnu.org; Sun, 18 Feb 2024 03:34:16 -0500 Received: from fwd86.aul.t-online.de (fwd86.aul.t-online.de [10.223.144.112]) by mailout05.t-online.de (Postfix) with SMTP id 059CBF253; Sun, 18 Feb 2024 09:34:14 +0100 (CET) Received: from linpower.localnet ([79.208.24.6]) by fwd86.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1rbccn-3vm8930; Sun, 18 Feb 2024 09:34:13 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 7C2AC200385; Sun, 18 Feb 2024 09:33:51 +0100 (CET) From: =?utf-8?q?Volker_R=C3=BCmelin?= To: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , Gerd Hoffmann , Manos Pitsidianakis , "Michael S. Tsirkin" Cc: qemu-devel@nongnu.org Subject: [PATCH v2 10/11] hw/audio/virtio-sound: add missing vmstate fields Date: Sun, 18 Feb 2024 09:33:50 +0100 Message-Id: <20240218083351.8524-10-vr_qemu@t-online.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: References: MIME-Version: 1.0 X-TOI-EXPURGATEID: 150726::1708245253-2F7FDC95-DD51B8F2/0/0 CLEAN NORMAL X-TOI-MSGID: 4f1ab26d-e627-4650-892b-1cbbf11b995e Received-SPF: pass client-ip=194.25.134.82; envelope-from=volker.ruemelin@t-online.de; helo=mailout05.t-online.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- hw/audio/virtio-snd.c | 84 +++++++++++++++++++++++++++++++---- include/hw/audio/virtio-snd.h | 1 + 2 files changed, 77 insertions(+), 8 deletions(-) diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c index 06a27ef8d9..b0a0ff2456 100644 --- a/hw/audio/virtio-snd.c +++ b/hw/audio/virtio-snd.c @@ -25,7 +25,6 @@ #include "hw/audio/virtio-snd.h" #include "hw/core/cpu.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 @@ -79,17 +78,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() @@ -812,6 +834,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); } @@ -858,6 +881,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; @@ -946,6 +970,7 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq) goto tx_err; } + s->queue_inuse[VIRTIO_SND_VQ_TX] += 1; size = iov_size(elem->out_sg, elem->out_num) - msg_sz; buffer = g_malloc0(sizeof(VirtIOSoundPCMBuffer) + size); @@ -1019,6 +1044,8 @@ static void virtio_snd_handle_rx_xfer(VirtIODevice *vdev, VirtQueue *vq) if (stream->info.direction != VIRTIO_SND_D_INPUT) { goto rx_err; } + + s->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); @@ -1154,6 +1181,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, @@ -1245,6 +1273,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, @@ -1350,6 +1379,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) { @@ -1375,6 +1438,10 @@ static void virtio_snd_reset(VirtIODevice *vdev) virtio_snd_pcm_buffer_free(buffer); } } + + for (i = 0; i < VIRTIO_SND_VQ_MAX; i++) { + s->queue_inuse[i] = 0; + } } static void virtio_snd_class_init(ObjectClass *klass, void *data) @@ -1388,6 +1455,7 @@ static void virtio_snd_class_init(ObjectClass *klass, 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; diff --git a/include/hw/audio/virtio-snd.h b/include/hw/audio/virtio-snd.h index 65afa6c184..457d18d196 100644 --- a/include/hw/audio/virtio-snd.h +++ b/include/hw/audio/virtio-snd.h @@ -196,6 +196,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; QEMUSoundCard card;