From nobody Thu Nov 14 18:09:00 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17082452743731020.7854314108916; Sun, 18 Feb 2024 00:34:34 -0800 (PST) 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=20R=C3=BCmelin?= To: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , 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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-TOI-EXPURGATEID: 150726::1708245253-2F7FDC95-DD51B8F2/0/0 CLEAN NORMAL X-TOI-MSGID: 4f1ab26d-e627-4650-892b-1cbbf11b995e Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1708245276314100007 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=C3=BCmelin --- 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" =20 -#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 =3D BIT(VIRTIO_SND_PCM_= RATE_5512) | BIT(VIRTIO_SND_PCM_RATE_192000) | BIT(VIRTIO_SND_PCM_RATE_384000); =20 +static const VMStateDescription vmstate_virtio_snd_stream =3D { + .name =3D "virtio-sound-stream", + .fields =3D (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 =3D { - .name =3D TYPE_VIRTIO_SND, - .version_id =3D VIRTIO_SOUND_VM_VERSION, - .minimum_version_id =3D VIRTIO_SOUND_VM_VERSION, + .name =3D "virtio-sound-device", + .fields =3D (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() + }, }; =20 static const VMStateDescription vmstate_virtio_snd =3D { - .name =3D TYPE_VIRTIO_SND, - .unmigratable =3D 1, - .minimum_version_id =3D VIRTIO_SOUND_VM_VERSION, - .version_id =3D VIRTIO_SOUND_VM_VERSION, + .name =3D "virtio-sound", .fields =3D (const VMStateField[]) { VMSTATE_VIRTIO_DEVICE, VMSTATE_END_OF_LIST() @@ -812,6 +834,7 @@ process_cmd(VirtIOSound *s, virtio_snd_ctrl_command *cm= d) sizeof(virtio_snd_hdr)); virtqueue_push(cmd->vq, cmd->elem, sizeof(virtio_snd_hdr) + cmd->payload_size); + s->queue_inuse[VIRTIO_SND_VQ_CONTROL] -=3D 1; virtio_notify(VIRTIO_DEVICE(s), cmd->vq); } =20 @@ -858,6 +881,7 @@ static void virtio_snd_handle_ctrl(VirtIODevice *vdev, = VirtQueue *vq) =20 elem =3D virtqueue_pop(vq, sizeof(VirtQueueElement)); while (elem) { + s->queue_inuse[VIRTIO_SND_VQ_CONTROL] +=3D 1; cmd =3D g_new0(virtio_snd_ctrl_command, 1); cmd->elem =3D elem; cmd->vq =3D vq; @@ -946,6 +970,7 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *vde= v, VirtQueue *vq) goto tx_err; } =20 + s->queue_inuse[VIRTIO_SND_VQ_TX] +=3D 1; size =3D iov_size(elem->out_sg, elem->out_num) - msg_sz; =20 buffer =3D g_malloc0(sizeof(VirtIOSoundPCMBuffer) + size); @@ -1019,6 +1044,8 @@ static void virtio_snd_handle_rx_xfer(VirtIODevice *v= dev, VirtQueue *vq) if (stream->info.direction !=3D VIRTIO_SND_D_INPUT) { goto rx_err; } + + s->queue_inuse[VIRTIO_SND_VQ_RX] +=3D 1; size =3D iov_size(elem->in_sg, elem->in_num) - sizeof(virtio_snd_pcm_status); buffer =3D g_malloc0(sizeof(VirtIOSoundPCMBuffer) + size); @@ -1154,6 +1181,7 @@ static inline void return_tx_buffer(VirtIOSoundPCMStr= eam *stream, virtqueue_push(buffer->vq, buffer->elem, sizeof(virtio_snd_pcm_status)); + stream->s->queue_inuse[VIRTIO_SND_VQ_TX] -=3D 1; virtio_notify(VIRTIO_DEVICE(stream->s), buffer->vq); QSIMPLEQ_REMOVE(&stream->queue, buffer, @@ -1245,6 +1273,7 @@ static inline void return_rx_buffer(VirtIOSoundPCMStr= eam *stream, virtqueue_push(buffer->vq, buffer->elem, sizeof(virtio_snd_pcm_status) + buffer->size); + stream->s->queue_inuse[VIRTIO_SND_VQ_RX] -=3D 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); } =20 +static int virtio_snd_post_load(VirtIODevice *vdev) +{ + VirtIOSound *s =3D VIRTIO_SND(vdev); + uint32_t i; + + for (i =3D 0; i < s->snd_conf.streams; i++) { + struct VirtIOSoundPCMStream *stream; + + stream =3D 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 =3D 0; i < VIRTIO_SND_VQ_MAX; i++) { + if (s->queue_inuse[i]) { + bool rc; + + rc =3D 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] =3D 0; + } + } + + return 0; +} =20 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 =3D 0; i < VIRTIO_SND_VQ_MAX; i++) { + s->queue_inuse[i] =3D 0; + } } =20 static void virtio_snd_class_init(ObjectClass *klass, void *data) @@ -1388,6 +1455,7 @@ static void virtio_snd_class_init(ObjectClass *klass,= void *data) =20 dc->vmsd =3D &vmstate_virtio_snd; vdc->vmsd =3D &vmstate_virtio_snd_device; + vdc->post_load =3D virtio_snd_post_load; vdc->realize =3D virtio_snd_realize; vdc->unrealize =3D virtio_snd_unrealize; vdc->get_config =3D 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; =20 VirtQueue *queues[VIRTIO_SND_VQ_MAX]; + uint32_t queue_inuse[VIRTIO_SND_VQ_MAX]; uint64_t features; VirtIOSoundPCMStream *streams; QEMUSoundCard card; --=20 2.35.3