From nobody Tue Nov 26 22:36:58 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 1704400558410359.92921918043544; Thu, 4 Jan 2024 12:35:58 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rLUQc-0003Zd-79; Thu, 04 Jan 2024 15:34:58 -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 1rLUQZ-0003YO-6V for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:55 -0500 Received: from mailout04.t-online.de ([194.25.134.18]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rLUQW-0004N0-Fq for qemu-devel@nongnu.org; Thu, 04 Jan 2024 15:34:54 -0500 Received: from fwd72.aul.t-online.de (fwd72.aul.t-online.de [10.223.144.98]) by mailout04.t-online.de (Postfix) with SMTP id AF78D3BBF; Thu, 4 Jan 2024 21:34:50 +0100 (CET) Received: from linpower.localnet ([93.236.158.98]) by fwd72.t-online.de with (TLSv1.3:TLS_AES_256_GCM_SHA384 encrypted) esmtp id 1rLUQL-3uNmmv0; Thu, 4 Jan 2024 21:34:41 +0100 Received: by linpower.localnet (Postfix, from userid 1000) id 5C83E2001D1; Thu, 4 Jan 2024 21:34:22 +0100 (CET) From: =?UTF-8?q?Volker=20R=C3=BCmelin?= To: Gerd Hoffmann , Manos Pitsidianakis , "Michael S. Tsirkin" , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Cc: qemu-devel@nongnu.org Subject: [PATCH 09/10] hw/audio/virtio-sound: add missing vmstate fields Date: Thu, 4 Jan 2024 21:34:21 +0100 Message-Id: <20240104203422.12308-9-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::1704400481-5181C95D-F891D282/0/0 CLEAN NORMAL X-TOI-MSGID: 19aac2dd-b5b8-4c83-8744-8f765b1dcaf1 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.18; envelope-from=volker.ruemelin@t-online.de; helo=mailout04.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: 1704400560117100003 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 | 81 +++++++++++++++++++++++++++++++---- include/hw/audio/virtio-snd.h | 1 + 2 files changed, 74 insertions(+), 8 deletions(-) diff --git a/hw/audio/virtio-snd.c b/hw/audio/virtio-snd.c index 92b10287ad..328ee54808 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 @@ -80,17 +79,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() @@ -820,6 +842,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 @@ -866,6 +889,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; @@ -914,6 +938,7 @@ static void empty_invalid_queue(VirtIODevice *vdev, uin= t32_t queue_index) virtqueue_push(vq, buffer->elem, sizeof(virtio_snd_pcm_status)); + s->queue_inuse[queue_index] -=3D 1; QSIMPLEQ_REMOVE_HEAD(invalidq, entry); virtio_snd_pcm_buffer_free(buffer); } @@ -958,6 +983,7 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *vde= v, VirtQueue *vq) if (!elem) { break; } + s->queue_inuse[VIRTIO_SND_VQ_TX] +=3D 1; /* get the message hdr object */ msg_sz =3D iov_to_buf(elem->out_sg, elem->out_num, @@ -1035,6 +1061,7 @@ static void virtio_snd_handle_rx_xfer(VirtIODevice *v= dev, VirtQueue *vq) if (!elem) { break; } + s->queue_inuse[VIRTIO_SND_VQ_RX] +=3D 1; /* get the message hdr object */ msg_sz =3D iov_to_buf(elem->out_sg, elem->out_num, @@ -1192,6 +1219,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, @@ -1283,6 +1311,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, @@ -1388,6 +1417,41 @@ 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) { + QSIMPLEQ_INIT(&stream->queue); + 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) { @@ -1412,6 +1476,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 9b81f66b05..141e60e23c 100644 --- a/include/hw/audio/virtio-snd.h +++ b/include/hw/audio/virtio-snd.h @@ -200,6 +200,7 @@ struct VirtIOSound { =20 VirtQueue *queues[VIRTIO_SND_VQ_MAX]; VirtIOSoundPCMBufferSimpleQueue *invalidq[VIRTIO_SND_VQ_MAX]; + uint32_t queue_inuse[VIRTIO_SND_VQ_MAX]; uint64_t features; VirtIOSoundPCMStream *streams; QEMUSoundCard card; --=20 2.35.3