Skip to contentSkip to conversation

ALSA: add virtio sound driver

PATCH Superseded Anton Yakovlev 2 participants 32 messages Feb 27, 2021 – Mar 2, 2021

Subsystems (3)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Anton Yakovlev

author maintainer
#0 lore raw

v6 cover ALSA: add virtio sound driver

This series implements a driver part of the virtio sound device specification v8 [1].

The driver supports PCM playback and capture substreams, jack and channel map controls. A message-based transport is used to write/read PCM frames to/from a device.

As a device part was used OpenSynergy proprietary implementation.

v6 changes:
- Code readability improvements suggested by Takashi.
- Added additional comments about PCM message lifetime.
- Temporarily removed DEVICE_NEEDS_RESET device status handling. Some kind of assistance from the virtio bus/level is required. In turn, this requires a separate discussion of the possibility of implementing such handling for virtio devices whose configuration may change (which may require re-probing, like in case of the sound device).

[1] https://lists.oasis-open.org/archives/virtio-dev/202003/msg00185.html

Anton Yakovlev (9):
uapi: virtio_ids: add a sound device type ID from OASIS spec
ALSA: virtio: add virtio sound driver
ALSA: virtio: handling control messages
ALSA: virtio: build PCM devices and substream hardware descriptors
ALSA: virtio: handling control and I/O messages for the PCM device
ALSA: virtio: PCM substream operators
ALSA: virtio: introduce jack support
ALSA: virtio: introduce PCM channel map support
ALSA: virtio: introduce device suspend/resume support

 MAINTAINERS                     |   9 +
 include/uapi/linux/virtio_ids.h |   1 +
 include/uapi/linux/virtio_snd.h | 334 +++++++++++++++++++++
 sound/Kconfig                   |   2 +
 sound/Makefile                  |   3 +-
 sound/virtio/Kconfig            |  10 +
 sound/virtio/Makefile           |  13 +
 sound/virtio/virtio_card.c      | 410 ++++++++++++++++++++++++++
 sound/virtio/virtio_card.h      | 111 +++++++
 sound/virtio/virtio_chmap.c     | 219 ++++++++++++++
 sound/virtio/virtio_ctl_msg.c   | 310 ++++++++++++++++++++
 sound/virtio/virtio_ctl_msg.h   |  78 +++++
 sound/virtio/virtio_jack.c      | 233 +++++++++++++++
 sound/virtio/virtio_pcm.c       | 498 ++++++++++++++++++++++++++++++++
 sound/virtio/virtio_pcm.h       | 118 ++++++++
 sound/virtio/virtio_pcm_msg.c   | 417 ++++++++++++++++++++++++++
 sound/virtio/virtio_pcm_ops.c   | 478 ++++++++++++++++++++++++++++++
 17 files changed, 3243 insertions(+), 1 deletion(-)
 create mode 100644 include/uapi/linux/virtio_snd.h
 create mode 100644 sound/virtio/Kconfig
 create mode 100644 sound/virtio/Makefile
 create mode 100644 sound/virtio/virtio_card.c
 create mode 100644 sound/virtio/virtio_card.h
 create mode 100644 sound/virtio/virtio_chmap.c
 create mode 100644 sound/virtio/virtio_ctl_msg.c
 create mode 100644 sound/virtio/virtio_ctl_msg.h
 create mode 100644 sound/virtio/virtio_jack.c
 create mode 100644 sound/virtio/virtio_pcm.c
 create mode 100644 sound/virtio/virtio_pcm.h
 create mode 100644 sound/virtio/virtio_pcm_msg.c
 create mode 100644 sound/virtio/virtio_pcm_ops.c
Signature · 7 lines
2.30.1



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
author maintainer
#1 lore raw

PATCH v6 1/9 uapi: virtio_ids: add a sound device type ID from OASIS spec

The OASIS virtio spec defines a sound device type ID that is not present in the header yet.

---
 include/uapi/linux/virtio_ids.h | 1 +
 1 file changed, 1 insertion(+)
include/uapi/linux/virtio_ids.h +1 -0
index bc1c0621f5ed..029a2e07a7f9 100644
@@ -51,6 +51,7 @@
 #define VIRTIO_ID_PSTORE		22 /* virtio pstore device */
 #define VIRTIO_ID_IOMMU			23 /* virtio IOMMU */
 #define VIRTIO_ID_MEM			24 /* virtio mem */
+#define VIRTIO_ID_SOUND			25 /* virtio sound */
 #define VIRTIO_ID_FS			26 /* virtio filesystem */
 #define VIRTIO_ID_PMEM			27 /* virtio pmem */
 #define VIRTIO_ID_MAC80211_HWSIM	29 /* virtio mac80211-hwsim */
Signature · 7 lines
2.30.1



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
author maintainer
#2 lore raw

PATCH v6 2/9 ALSA: virtio: add virtio sound driver

In reply to Anton Yakovlev

Introduce skeleton of the virtio sound driver. The driver implements the virtio sound device specification, which has become part of the virtio standard.

Initial initialization of the device, virtqueues and creation of an empty ALSA sound device.

---
 MAINTAINERS                     |   9 +
 include/uapi/linux/virtio_snd.h | 334 ++++++++++++++++++++++++++++++++
 sound/Kconfig                   |   2 +
 sound/Makefile                  |   3 +-
 sound/virtio/Kconfig            |  10 +
 sound/virtio/Makefile           |   7 +
 sound/virtio/virtio_card.c      | 289 +++++++++++++++++++++++++++
 sound/virtio/virtio_card.h      |  65 +++++++
 8 files changed, 718 insertions(+), 1 deletion(-)
 create mode 100644 include/uapi/linux/virtio_snd.h
 create mode 100644 sound/virtio/Kconfig
 create mode 100644 sound/virtio/Makefile
 create mode 100644 sound/virtio/virtio_card.c
 create mode 100644 sound/virtio/virtio_card.h
MAINTAINERS +9 -0
index c71664ca8bfd..4369946434ad 100644
@@ -19049,6 +19049,15 @@ W:	https://virtio-mem.gitlab.io/
 F:	drivers/virtio/virtio_mem.c
 F:	include/uapi/linux/virtio_mem.h
 
+VIRTIO SOUND DRIVER
+M:	Anton Yakovlev <[email protected]>
+M:	"Michael S. Tsirkin" <[email protected]>
+L:	[email protected]
+L:	[email protected] (moderated for non-subscribers)
+S:	Maintained
+F:	include/uapi/linux/virtio_snd.h
+F:	sound/virtio/*
+
 VIRTUAL BOX GUEST DEVICE DRIVER
 M:	Hans de Goede <[email protected]>
 M:	Arnd Bergmann <[email protected]>
include/uapi/linux/virtio_snd.h new file +334 -0

334 lines not on this page · raw message

sound/Kconfig +2 -0
index 36785410fbe1..e56d96d2b11c 100644
@@ -99,6 +99,8 @@ source "sound/synth/Kconfig"
 
 source "sound/xen/Kconfig"
 
+source "sound/virtio/Kconfig"
+
 endif # SND
 
 endif # !UML
sound/Makefile +2 -1
index 797ecdcd35e2..04ef04b1168f 100644
@@ -5,7 +5,8 @@
 obj-$(CONFIG_SOUND) += soundcore.o
 obj-$(CONFIG_DMASOUND) += oss/dmasound/
 obj-$(CONFIG_SND) += core/ i2c/ drivers/ isa/ pci/ ppc/ arm/ sh/ synth/ usb/ \
-	firewire/ sparc/ spi/ parisc/ pcmcia/ mips/ soc/ atmel/ hda/ x86/ xen/
+	firewire/ sparc/ spi/ parisc/ pcmcia/ mips/ soc/ atmel/ hda/ x86/ xen/ \
+	virtio/
 obj-$(CONFIG_SND_AOA) += aoa/
 
 # This one must be compilable even if sound is configured out
sound/virtio/Kconfig new file +10 -0
new file mode 100644
index 000000000000..094cba24ee5b
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: GPL-2.0+
+# Sound card driver for virtio
+
+config SND_VIRTIO
+	tristate "Virtio sound driver"
+	depends on VIRTIO
+	select SND_PCM
+	select SND_JACK
+	help
+          This is the virtual sound driver for virtio. Say Y or M.
sound/virtio/Makefile new file +7 -0
new file mode 100644
index 000000000000..8c87ebb9982b
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0+
+
+obj-$(CONFIG_SND_VIRTIO) += virtio_snd.o
+
+virtio_snd-objs := \
+	virtio_card.o
+
sound/virtio/virtio_card.c new file +289 -0

289 lines not on this page · raw message

sound/virtio/virtio_card.h new file +65 -0

65 lines not on this page · raw message

Signature · 7 lines
2.30.1



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
author maintainer
#3 lore raw

PATCH v6 3/9 ALSA: virtio: handling control messages

In reply to Anton Yakovlev

The control queue can be used by different parts of the driver to send commands to the device. Control messages can be either synchronous or asynchronous. The lifetime of a message is controlled by a reference count.

Introduce a module parameter to set the message completion timeout:
msg_timeout_ms [=1000]

---
 sound/virtio/Makefile         |   3 +-
 sound/virtio/virtio_card.c    |  13 ++
 sound/virtio/virtio_card.h    |   7 +
 sound/virtio/virtio_ctl_msg.c | 310 ++++++++++++++++++++++++++++++++++
 sound/virtio/virtio_ctl_msg.h |  78 +++++++++
 5 files changed, 410 insertions(+), 1 deletion(-)
 create mode 100644 sound/virtio/virtio_ctl_msg.c
 create mode 100644 sound/virtio/virtio_ctl_msg.h
sound/virtio/Makefile +2 -1
index 8c87ebb9982b..dc551e637441 100644
@@ -3,5 +3,6 @@
 obj-$(CONFIG_SND_VIRTIO) += virtio_snd.o
 
 virtio_snd-objs := \
-	virtio_card.o
+	virtio_card.o \
+	virtio_ctl_msg.o
 
sound/virtio/virtio_card.c +13 -0
index 697cb5f16e4d..196cb97087b0 100644
@@ -11,6 +11,10 @@
 
 #include "virtio_card.h"
 
+int msg_timeout_ms = MSEC_PER_SEC;
+module_param(msg_timeout_ms, int, 0644);
+MODULE_PARM_DESC(msg_timeout_ms, "Message completion timeout in milliseconds");
+
 static void virtsnd_remove(struct virtio_device *vdev);
 
 /**
@@ -82,6 +86,7 @@ static int virtsnd_find_vqs(struct virtio_snd *snd)
 {
 	struct virtio_device *vdev = snd->vdev;
 	static vq_callback_t *callbacks[VIRTIO_SND_VQ_MAX] = {
+		[VIRTIO_SND_VQ_CONTROL] = virtsnd_ctl_notify_cb,
 		[VIRTIO_SND_VQ_EVENT] = virtsnd_event_notify_cb
 	};
 	static const char *names[VIRTIO_SND_VQ_MAX] = {
@@ -193,6 +198,11 @@ static int virtsnd_validate(struct virtio_device *vdev)
 		return -EINVAL;
 	}
 
+	if (!msg_timeout_ms) {
+		dev_err(&vdev->dev, "msg_timeout_ms value cannot be zero\n");
+		return -EINVAL;
+	}
+
 	return 0;
 }
 
@@ -214,6 +224,7 @@ static int virtsnd_probe(struct virtio_device *vdev)
 		return -ENOMEM;
 
 	snd->vdev = vdev;
+	INIT_LIST_HEAD(&snd->ctl_msgs);
 
 	vdev->priv = snd;
 
@@ -249,6 +260,8 @@ static void virtsnd_remove(struct virtio_device *vdev)
 {
 	struct virtio_snd *snd = vdev->priv;
 
+	virtsnd_ctl_msg_cancel_all(snd);
+
 	if (snd->card)
 		snd_card_free(snd->card);
 
sound/virtio/virtio_card.h +7 -0
index b903b1b12e90..349311a30199 100644
@@ -11,6 +11,8 @@
 #include <sound/core.h>
 #include <uapi/linux/virtio_snd.h>
 
+#include "virtio_ctl_msg.h"
+
 #define VIRTIO_SND_CARD_DRIVER	"virtio-snd"
 #define VIRTIO_SND_CARD_NAME	"VirtIO SoundCard"
 
@@ -29,15 +31,20 @@ struct virtio_snd_queue {
  * @vdev: Underlying virtio device.
  * @queues: Virtqueue wrappers.
  * @card: ALSA sound card.
+ * @ctl_msgs: Pending control request list.
  * @event_msgs: Device events.
  */
 struct virtio_snd {
 	struct virtio_device *vdev;
 	struct virtio_snd_queue queues[VIRTIO_SND_VQ_MAX];
 	struct snd_card *card;
+	struct list_head ctl_msgs;
 	struct virtio_snd_event *event_msgs;
 };
 
+/* Message completion timeout in milliseconds (module parameter). */
+extern int msg_timeout_ms;
+
 static inline struct virtio_snd_queue *
 virtsnd_control_queue(struct virtio_snd *snd)
 {
sound/virtio/virtio_ctl_msg.c new file +310 -0

310 lines not on this page · raw message

sound/virtio/virtio_ctl_msg.h new file +78 -0

78 lines not on this page · raw message

Signature · 7 lines
2.30.1



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
#4 lore raw

Re: [PATCH v6 3/9] ALSA: virtio: handling control messages

sound/virtio/virtio_card.c line 16

Quoted 10 lines · Anton Yakovlev
sound/virtio/virtio_card.c +3 -0
@@ -11,6 +11,10 @@
 
 #include "virtio_card.h"
 
+int msg_timeout_ms = MSEC_PER_SEC;
+module_param(msg_timeout_ms, int, 0644);
+MODULE_PARM_DESC(msg_timeout_ms, "Message completion timeout in milliseconds");

If it's a global variable, better to set a prefix to make it unique, and use module_param_named().

And, it should be unsigned int, no? (unless a negative value has any meaning) Otherwise...

sound/virtio/virtio_card.c static int virtsnd_validate(struct virtio_device *vdev) · line 203

Quoted 3 lines
sound/virtio/virtio_card.c +3 -0
+	if (!msg_timeout_ms) {
+		dev_err(&vdev->dev, "msg_timeout_ms value cannot be zero\n");
+		return -EINVAL;

Here a negative value would pass.

(snip)

Quoted 22 lines
sound/virtio/virtio_card.c +22 -0
+int virtsnd_ctl_msg_send(struct virtio_snd *snd, struct virtio_snd_msg *msg,
+			 struct scatterlist *out_sgs,
+			 struct scatterlist *in_sgs, bool nowait)
+{
+	struct virtio_device *vdev = snd->vdev;
+	struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
+	unsigned int js = msecs_to_jiffies(msg_timeout_ms);
+	struct virtio_snd_hdr *request = virtsnd_ctl_msg_request(msg);
+	struct virtio_snd_hdr *response = virtsnd_ctl_msg_response(msg);
+	unsigned int nouts = 0;
+	unsigned int nins = 0;
+	struct scatterlist *psgs[4];
+	bool notify = false;
+	unsigned long flags;
+	int rc;
+
+	virtsnd_ctl_msg_ref(msg);
+
+	/* Set the default status in case the message was canceled. */
+	response->code = cpu_to_le32(VIRTIO_SND_S_IO_ERR);
+
+	psgs[nouts++] = &msg->sg_request;

sound/virtio/virtio_card.c

Quoted 10 lines
sound/virtio/virtio_card.c +10 -0
+	if (out_sgs)
+		psgs[nouts++] = out_sgs;
+
+	psgs[nouts + nins++] = &msg->sg_response;
+	if (in_sgs)
+		psgs[nouts + nins++] = in_sgs;
+
+	spin_lock_irqsave(&queue->lock, flags);
+	rc = virtqueue_add_sgs(queue->vqueue, psgs, nouts, nins, msg,
+			       GFP_ATOMIC);

It's a bit pity that we have to use GFP_ATOMIC always here... As long as it's in spinlock, it's the only way.

However, this reminds me of another question: may the virtio event be handled in an atomic context, e.g. the period elapsed or xrun events? If so, the current implementation with non-atomic PCM mode is wrong. Since the non-atomic PCM uses mutex instead of spinlock, it'll lead to a sleep-in-atomic in snd_pcm_period_elapsed() handling.

I suggested the non-atomic PCM *iff* the all contexts are sleepable; then the sync can be done in each callback, which makes the code much simpler usually. But you've already implemented the sync via sync_stop call, hence the non-atomic PCM has little benefit by its own.

thanks,

Takashi

author maintainer
#5 lore raw

Re: [PATCH v6 3/9] ALSA: virtio: handling control messages

On 28.02.2021 12:04, Takashi Iwai wrote:> On Sat, 27 Feb 2021 09:59:50 +0100,

Quoted 6 lines
Quoted 5 lines · Anton Yakovlev
sound/virtio/virtio_card.c +0 -0
@@ -11,6 +11,10 @@
 
Quoted 8 lines

sound/virtio/virtio_card.c line 16

Quoted 5 lines
sound/virtio/virtio_card.c +3 -0
  #include "virtio_card.h"
 
+int msg_timeout_ms = MSEC_PER_SEC;
+module_param(msg_timeout_ms, int, 0644);
+MODULE_PARM_DESC(msg_timeout_ms, "Message completion timeout in milliseconds");

If it's a global variable, better to set a prefix to make it unique, and use module_param_named().

Yes, it makes sense.

Quoted 2 lines

And, it should be unsigned int, no? (unless a negative value has any meaning) Otherwise...

And yes, it must be unsigned!

Quoted 34 lines

sound/virtio/virtio_card.c static int virtsnd_validate(struct virtio_device *vdev) · line 203

Quoted 3 lines
sound/virtio/virtio_card.c +3 -0
+     if (!msg_timeout_ms) {
+             dev_err(&vdev->dev, "msg_timeout_ms value cannot be zero\n");
+             return -EINVAL;

Here a negative value would pass.

(snip)

Quoted 27 lines
sound/virtio/virtio_card.c +27 -0
+int virtsnd_ctl_msg_send(struct virtio_snd *snd, struct virtio_snd_msg *msg,
+                      struct scatterlist *out_sgs,
+                      struct scatterlist *in_sgs, bool nowait)
+{
+     struct virtio_device *vdev = snd->vdev;
+     struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
+     unsigned int js = msecs_to_jiffies(msg_timeout_ms);
+     struct virtio_snd_hdr *request = virtsnd_ctl_msg_request(msg);
+     struct virtio_snd_hdr *response = virtsnd_ctl_msg_response(msg);
+     unsigned int nouts = 0;
+     unsigned int nins = 0;
+     struct scatterlist *psgs[4];
+     bool notify = false;
+     unsigned long flags;
+     int rc;
+
+     virtsnd_ctl_msg_ref(msg);
+
+     /* Set the default status in case the message was canceled. */
+     response->code = cpu_to_le32(VIRTIO_SND_S_IO_ERR);
+
+     psgs[nouts++] = &msg->sg_request;
+     if (out_sgs)
+             psgs[nouts++] = out_sgs;
+
+     psgs[nouts + nins++] = &msg->sg_response;
+     if (in_sgs)
Quoted 8 lines

sound/virtio/virtio_card.c

Quoted 5 lines
sound/virtio/virtio_card.c +5 -0
+             psgs[nouts + nins++] = in_sgs;
+
+     spin_lock_irqsave(&queue->lock, flags);
+     rc = virtqueue_add_sgs(queue->vqueue, psgs, nouts, nins, msg,
+                            GFP_ATOMIC);

It's a bit pity that we have to use GFP_ATOMIC always here... As long as it's in spinlock, it's the only way.

Well, here we have no other choices, since we share the queue with an interrupt handler.

Quoted 11 lines

However, this reminds me of another question: may the virtio event be handled in an atomic context, e.g. the period elapsed or xrun events? If so, the current implementation with non-atomic PCM mode is wrong. Since the non-atomic PCM uses mutex instead of spinlock, it'll lead to a sleep-in-atomic in snd_pcm_period_elapsed() handling.

I suggested the non-atomic PCM *iff* the all contexts are sleepable; then the sync can be done in each callback, which makes the code much simpler usually. But you've already implemented the sync via sync_stop call, hence the non-atomic PCM has little benefit by its own.

The device provides 4 separate queues for communication with the driver, and different data is transmitted over these queues:

The control queue (actually shared between all driver components) for sending commands to the device. These requests must be synchronous. For each request, the device must send a response, and we must wait for it. What you can see in PCM ops are exactly sending these commands (set params, prepare, start and so on). But since some ops could be called in atomic context, there was no other choice but to add asynchronous messages and return from the operator without waiting for a response from the device. Because of this, the START command was a headache. We could not say for sure if the substream started at all on the device side. Also, the virtualization overhead was not taken into account (application might think that the substream is already running, but actually it was not).

Then there are 2 queues for sending/receiving PCM frames. These contain i/o messages carrying actual buffer sliced into periods. Actually, snd_pcm_period_elapsed() is called from interrupt handlers here.

And then there is an additional queue for events.

All of these are handled in different ways.

Quoted 5 lines

thanks,

Takashi

Signature · 10 lines
Anton Yakovlev
Senior Software Engineer

OpenSynergy GmbH
Rotherstr. 20, 10245 Berlin


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
author maintainer
#6 lore raw

PATCH v6 4/9 ALSA: virtio: build PCM devices and substream hardware descriptors

In reply to Anton Yakovlev

Like the HDA specification, the virtio sound device specification links PCM substreams, jacks and PCM channel maps into functional groups. For each discovered group, a PCM device is created, the number of which coincides with the group number.

Introduce the module parameters for setting the hardware buffer parameters:
pcm_buffer_ms [=160]
pcm_periods_min [=2]
pcm_periods_max [=16]
pcm_period_ms_min [=10]
pcm_period_ms_max [=80]

---
 sound/virtio/Makefile      |   3 +-
 sound/virtio/virtio_card.c |  14 ++
 sound/virtio/virtio_card.h |  10 +
 sound/virtio/virtio_pcm.c  | 463 +++++++++++++++++++++++++++++++++++++
 sound/virtio/virtio_pcm.h  |  70 ++++++
 5 files changed, 559 insertions(+), 1 deletion(-)
 create mode 100644 sound/virtio/virtio_pcm.c
 create mode 100644 sound/virtio/virtio_pcm.h
sound/virtio/Makefile +2 -1
index dc551e637441..69162a545a41 100644
@@ -4,5 +4,6 @@ obj-$(CONFIG_SND_VIRTIO) += virtio_snd.o
 
 virtio_snd-objs := \
 	virtio_card.o \
-	virtio_ctl_msg.o
+	virtio_ctl_msg.o \
+	virtio_pcm.o
 
sound/virtio/virtio_card.c +14 -0
index 196cb97087b0..c5e9ceaaa8a0 100644
@@ -175,6 +175,16 @@ static int virtsnd_build_devs(struct virtio_snd *snd)
 			 VIRTIO_SND_CARD_NAME " at %s/%s",
 			 dev_name(dev->parent), dev_name(dev));
 
+	rc = virtsnd_pcm_parse_cfg(snd);
+	if (rc)
+		return rc;
+
+	if (snd->nsubstreams) {
+		rc = virtsnd_pcm_build_devs(snd);
+		if (rc)
+			return rc;
+	}
+
 	return snd_card_register(snd->card);
 }
 
@@ -203,6 +213,9 @@ static int virtsnd_validate(struct virtio_device *vdev)
 		return -EINVAL;
 	}
 
+	if (virtsnd_pcm_validate(vdev))
+		return -EINVAL;
+
 	return 0;
 }
 
@@ -225,6 +238,7 @@ static int virtsnd_probe(struct virtio_device *vdev)
 
 	snd->vdev = vdev;
 	INIT_LIST_HEAD(&snd->ctl_msgs);
+	INIT_LIST_HEAD(&snd->pcm_list);
 
 	vdev->priv = snd;
 
sound/virtio/virtio_card.h +10 -0
index 349311a30199..4431cc8f0445 100644
@@ -12,9 +12,13 @@
 #include <uapi/linux/virtio_snd.h>
 
 #include "virtio_ctl_msg.h"
+#include "virtio_pcm.h"
 
 #define VIRTIO_SND_CARD_DRIVER	"virtio-snd"
 #define VIRTIO_SND_CARD_NAME	"VirtIO SoundCard"
+#define VIRTIO_SND_PCM_NAME	"VirtIO PCM"
+
+struct virtio_pcm_substream;
 
 /**
  * struct virtio_snd_queue - Virtqueue wrapper structure.
@@ -33,6 +37,9 @@ struct virtio_snd_queue {
  * @card: ALSA sound card.
  * @ctl_msgs: Pending control request list.
  * @event_msgs: Device events.
+ * @pcm_list: VirtIO PCM device list.
+ * @substreams: VirtIO PCM substreams.
+ * @nsubstreams: Number of PCM substreams.
  */
 struct virtio_snd {
 	struct virtio_device *vdev;
@@ -40,6 +47,9 @@ struct virtio_snd {
 	struct snd_card *card;
 	struct list_head ctl_msgs;
 	struct virtio_snd_event *event_msgs;
+	struct list_head pcm_list;
+	struct virtio_pcm_substream *substreams;
+	u32 nsubstreams;
 };
 
 /* Message completion timeout in milliseconds (module parameter). */
sound/virtio/virtio_pcm.c new file +463 -0

463 lines not on this page · raw message

sound/virtio/virtio_pcm.h new file +70 -0

70 lines not on this page · raw message

Signature · 7 lines
2.30.1



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
#7 lore raw

Re: [PATCH v6 4/9] ALSA: virtio: build PCM devices and substream hardware descriptors

Quoted 3 lines · Anton Yakovlev
sound/virtio/virtio_pcm.c new file +3 -0
+static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss,
+				struct virtio_snd_pcm_info *info)
+{

....

sound/virtio/virtio_pcm.c line 139

Quoted 12 lines
sound/virtio/virtio_pcm.c new file +12 -0
+	for (i = 0; i < ARRAY_SIZE(g_v2a_format_map); ++i)
+		if (values & (1ULL << i)) {
+			snd_pcm_format_t alsa_fmt = g_v2a_format_map[i];
+			int bytes = snd_pcm_format_physical_width(alsa_fmt) / 8;
+
+			if (!sample_min || sample_min > bytes)
+				sample_min = bytes;
+
+			if (sample_max < bytes)
+				sample_max = bytes;
+
+			vss->hw.formats |= (1ULL << (__force int)alsa_fmt);

Please use pcm_format_to_bits().

sound/virtio/virtio_pcm.c line 188

Quoted 3 lines
sound/virtio/virtio_pcm.c new file +3 -0
+	/* Align the buffer size to the page size */
+	vss->hw.buffer_bytes_max =
+		(vss->hw.buffer_bytes_max + PAGE_SIZE - 1) & -PAGE_SIZE;

You can use PAGE_ALIGN() macro.

thanks,

Takashi

author maintainer
#8 lore raw

PATCH v6 5/9 ALSA: virtio: handling control and I/O messages for the PCM device

In reply to Anton Yakovlev

The driver implements a message-based transport for I/O substream operations. Before the start of the substream, the hardware buffer is sliced into I/O messages, the number of which is equal to the current number of periods. The size of each message is equal to the current size of one period.

I/O messages are organized in an ordered queue. The completion of the I/O message indicates an elapsed period (the only exception is the end of the stream for the capture substream). Upon completion, the message is automatically re-added to the end of the queue.

---
 sound/virtio/Makefile         |   3 +-
 sound/virtio/virtio_card.c    |  18 +-
 sound/virtio/virtio_card.h    |   9 +
 sound/virtio/virtio_pcm.c     |  32 +++
 sound/virtio/virtio_pcm.h     |  40 ++++
 sound/virtio/virtio_pcm_msg.c | 417 ++++++++++++++++++++++++++++++++++
 6 files changed, 516 insertions(+), 3 deletions(-)
 create mode 100644 sound/virtio/virtio_pcm_msg.c
sound/virtio/Makefile +2 -1
index 69162a545a41..626af3cc3ed7 100644
@@ -5,5 +5,6 @@ obj-$(CONFIG_SND_VIRTIO) += virtio_snd.o
 virtio_snd-objs := \
 	virtio_card.o \
 	virtio_ctl_msg.o \
-	virtio_pcm.o
+	virtio_pcm.o \
+	virtio_pcm_msg.o
 
sound/virtio/virtio_card.c +16 -2
index c5e9ceaaa8a0..9f74261b234c 100644
@@ -65,8 +65,16 @@ static void virtsnd_event_notify_cb(struct virtqueue *vqueue)
 	spin_lock_irqsave(&queue->lock, flags);
 	do {
 		virtqueue_disable_cb(vqueue);
-		while ((event = virtqueue_get_buf(vqueue, &length)))
+		while ((event = virtqueue_get_buf(vqueue, &length))) {
+			switch (le32_to_cpu(event->hdr.code)) {
+			case VIRTIO_SND_EVT_PCM_PERIOD_ELAPSED:
+			case VIRTIO_SND_EVT_PCM_XRUN:
+				virtsnd_pcm_event(snd, event);
+				break;
+			}
+
 			virtsnd_event_send(vqueue, event, true, GFP_ATOMIC);
+		}
 		if (unlikely(virtqueue_is_broken(vqueue)))
 			break;
 	} while (!virtqueue_enable_cb(vqueue));
@@ -87,7 +95,9 @@ static int virtsnd_find_vqs(struct virtio_snd *snd)
 	struct virtio_device *vdev = snd->vdev;
 	static vq_callback_t *callbacks[VIRTIO_SND_VQ_MAX] = {
 		[VIRTIO_SND_VQ_CONTROL] = virtsnd_ctl_notify_cb,
-		[VIRTIO_SND_VQ_EVENT] = virtsnd_event_notify_cb
+		[VIRTIO_SND_VQ_EVENT] = virtsnd_event_notify_cb,
+		[VIRTIO_SND_VQ_TX] = virtsnd_pcm_tx_notify_cb,
+		[VIRTIO_SND_VQ_RX] = virtsnd_pcm_rx_notify_cb
 	};
 	static const char *names[VIRTIO_SND_VQ_MAX] = {
 		[VIRTIO_SND_VQ_CONTROL] = "virtsnd-ctl",
@@ -273,6 +283,7 @@ static int virtsnd_probe(struct virtio_device *vdev)
 static void virtsnd_remove(struct virtio_device *vdev)
 {
 	struct virtio_snd *snd = vdev->priv;
+	unsigned int i;
 
 	virtsnd_ctl_msg_cancel_all(snd);
 
@@ -282,6 +293,9 @@ static void virtsnd_remove(struct virtio_device *vdev)
 	vdev->config->del_vqs(vdev);
 	vdev->config->reset(vdev);
 
+	for (i = 0; snd->substreams && i < snd->nsubstreams; ++i)
+		virtsnd_pcm_msg_free(&snd->substreams[i]);
+
 	kfree(snd->event_msgs);
 }
 
sound/virtio/virtio_card.h +9 -0
index 4431cc8f0445..7e50f3835ab1 100644
@@ -79,4 +79,13 @@ virtsnd_rx_queue(struct virtio_snd *snd)
 	return &snd->queues[VIRTIO_SND_VQ_RX];
 }
 
+static inline struct virtio_snd_queue *
+virtsnd_pcm_queue(struct virtio_pcm_substream *vss)
+{
+	if (vss->direction == SNDRV_PCM_STREAM_PLAYBACK)
+		return virtsnd_tx_queue(vss->snd);
+	else
+		return virtsnd_rx_queue(vss->snd);
+}
+
 #endif /* VIRTIO_SND_CARD_H */
sound/virtio/virtio_pcm.c +32 -0
index 4348ead63c14..3cfd3520a9c0 100644
@@ -337,6 +337,8 @@ int virtsnd_pcm_parse_cfg(struct virtio_snd *snd)
 
 		vss->snd = snd;
 		vss->sid = i;
+		init_waitqueue_head(&vss->msg_empty);
+		spin_lock_init(&vss->lock);
 
 		rc = virtsnd_pcm_build_hw(vss, &info[i]);
 		if (rc)
@@ -461,3 +463,33 @@ int virtsnd_pcm_build_devs(struct virtio_snd *snd)
 
 	return 0;
 }
+
+/**
+ * virtsnd_pcm_event() - Handle the PCM device event notification.
+ * @snd: VirtIO sound device.
+ * @event: VirtIO sound event.
+ *
+ * Context: Interrupt context.
+ */
+void virtsnd_pcm_event(struct virtio_snd *snd, struct virtio_snd_event *event)
+{
+	struct virtio_pcm_substream *vss;
+	u32 sid = le32_to_cpu(event->data);
+
+	if (sid >= snd->nsubstreams)
+		return;
+
+	vss = &snd->substreams[sid];
+
+	switch (le32_to_cpu(event->hdr.code)) {
+	case VIRTIO_SND_EVT_PCM_PERIOD_ELAPSED:
+		/* TODO: deal with shmem elapsed period */
+		break;
+	case VIRTIO_SND_EVT_PCM_XRUN:
+		spin_lock(&vss->lock);
+		if (vss->xfer_enabled)
+			vss->xfer_xrun = true;
+		spin_unlock(&vss->lock);
+		break;
+	}
+}
sound/virtio/virtio_pcm.h +40 -0
index a2aadc44dce9..7af41cad4e8f 100644
@@ -22,6 +22,17 @@ struct virtio_pcm_msg;
  * @features: Stream VirtIO feature bit map (1 << VIRTIO_SND_PCM_F_XXX).
  * @substream: Kernel ALSA substream.
  * @hw: Kernel ALSA substream hardware descriptor.
+ * @lock: Spinlock that protects fields shared by interrupt handlers and
+ *        substream operators.
+ * @buffer_bytes: Current buffer size in bytes.
+ * @hw_ptr: Substream hardware pointer value in bytes [0 ... buffer_bytes).
+ * @xfer_enabled: Data transfer state (0 - off, 1 - on).
+ * @xfer_xrun: Data underflow/overflow state (0 - no xrun, 1 - xrun).
+ * @msgs: Allocated I/O messages.
+ * @nmsgs: Number of allocated I/O messages.
+ * @msg_last_enqueued: Index of the last I/O message added to the virtqueue.
+ * @msg_count: Number of pending I/O messages in the virtqueue.
+ * @msg_empty: Notify when msg_count is zero.
  */
 struct virtio_pcm_substream {
 	struct virtio_snd *snd;
@@ -31,6 +42,16 @@ struct virtio_pcm_substream {
 	u32 features;
 	struct snd_pcm_substream *substream;
 	struct snd_pcm_hardware hw;
+	spinlock_t lock;
+	size_t buffer_bytes;
+	size_t hw_ptr;
+	bool xfer_enabled;
+	bool xfer_xrun;
+	struct virtio_pcm_msg **msgs;
+	unsigned int nmsgs;
+	int msg_last_enqueued;
+	unsigned int msg_count;
+	wait_queue_head_t msg_empty;
 };
 
 /**
@@ -63,8 +84,27 @@ int virtsnd_pcm_parse_cfg(struct virtio_snd *snd);
 
 int virtsnd_pcm_build_devs(struct virtio_snd *snd);
 
+void virtsnd_pcm_event(struct virtio_snd *snd, struct virtio_snd_event *event);
+
+void virtsnd_pcm_tx_notify_cb(struct virtqueue *vqueue);
+
+void virtsnd_pcm_rx_notify_cb(struct virtqueue *vqueue);
+
 struct virtio_pcm *virtsnd_pcm_find(struct virtio_snd *snd, u32 nid);
 
 struct virtio_pcm *virtsnd_pcm_find_or_create(struct virtio_snd *snd, u32 nid);
 
+struct virtio_snd_msg *
+virtsnd_pcm_ctl_msg_alloc(struct virtio_pcm_substream *vss,
+			  unsigned int command, gfp_t gfp);
+
+int virtsnd_pcm_msg_alloc(struct virtio_pcm_substream *vss,
+			  unsigned int periods, unsigned int period_bytes);
+
+void virtsnd_pcm_msg_free(struct virtio_pcm_substream *vss);
+
+int virtsnd_pcm_msg_send(struct virtio_pcm_substream *vss);
+
+unsigned int virtsnd_pcm_msg_pending_num(struct virtio_pcm_substream *vss);
+
 #endif /* VIRTIO_SND_PCM_H */
sound/virtio/virtio_pcm_msg.c new file +417 -0

417 lines not on this page · raw message

Signature · 7 lines
2.30.1


_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#9 lore raw

Re: [PATCH v6 5/9] ALSA: virtio: handling control and I/O messages for the PCM device

Quoted 6 lines · Anton Yakovlev

+/**
+ * virtsnd_pcm_event() - Handle the PCM device event notification.
+ * @snd: VirtIO sound device.
+ * @event: VirtIO sound event.
+ *
+ * Context: Interrupt context.

OK, then nonatomic PCM flag is invalid...

Quoted 70 lines
sound/virtio/virtio_pcm_msg.c new file +70 -0
+/**
+ * virtsnd_pcm_sg_num() - Count the number of sg-elements required to represent
+ *                        vmalloc'ed buffer.
+ * @data: Pointer to vmalloc'ed buffer.
+ * @length: Buffer size.
+ *
+ * Context: Any context.
+ * Return: Number of physically contiguous parts in the @data.
+ */
+static int virtsnd_pcm_sg_num(u8 *data, unsigned int length)
+{
+	phys_addr_t sg_address;
+	unsigned int sg_length;
+	int num = 0;
+
+	while (length) {
+		struct page *pg = vmalloc_to_page(data);
+		phys_addr_t pg_address = page_to_phys(pg);
+		size_t pg_length;
+
+		pg_length = PAGE_SIZE - offset_in_page(data);
+		if (pg_length > length)
+			pg_length = length;
+
+		if (!num || sg_address + sg_length != pg_address) {
+			sg_address = pg_address;
+			sg_length = pg_length;
+			num++;
+		} else {
+			sg_length += pg_length;
+		}
+
+		data += pg_length;
+		length -= pg_length;
+	}
+
+	return num;
+}
+
+/**
+ * virtsnd_pcm_sg_from() - Build sg-list from vmalloc'ed buffer.
+ * @sgs: Preallocated sg-list to populate.
+ * @nsgs: The maximum number of elements in the @sgs.
+ * @data: Pointer to vmalloc'ed buffer.
+ * @length: Buffer size.
+ *
+ * Splits the buffer into physically contiguous parts and makes an sg-list of
+ * such parts.
+ *
+ * Context: Any context.
+ */
+static void virtsnd_pcm_sg_from(struct scatterlist *sgs, int nsgs, u8 *data,
+				unsigned int length)
+{
+	int idx = -1;
+
+	while (length) {
+		struct page *pg = vmalloc_to_page(data);
+		size_t pg_length;
+
+		pg_length = PAGE_SIZE - offset_in_page(data);
+		if (pg_length > length)
+			pg_length = length;
+
+		if (idx == -1 ||
+		    sg_phys(&sgs[idx]) + sgs[idx].length != page_to_phys(pg)) {
+			if (idx + 1 == nsgs)
+				break;
+			sg_set_page(&sgs[++idx], pg, pg_length,
+				    offset_in_page(data));

sound/virtio/virtio_pcm_msg.c line 116

Quoted 10 lines
sound/virtio/virtio_pcm_msg.c new file +10 -0
+		} else {
+			sgs[idx].length += pg_length;
+		}
+
+		data += pg_length;
+		length -= pg_length;
+	}
+
+	sg_mark_end(&sgs[idx]);
+}

Hmm, I thought there can be already a handy helper to convert vmalloc to sglist, but apparently not. It should have been trivial to get the page list from vmalloc, e.g.

int vmalloc_to_page_list(void *p, struct page **page_ret)

{
	struct vmap_area *va;

	va = find_vmap_area((unsigned long)p);
	if (!va)
		return 0;
	*page_ret = va->vm->pages;
	return va->vm->nr_pages;
}

Then you can set up the sg list in a single call from the given page list.

But it's just a cleanup, and let's mark it as a room for improvements.

(snip)

Quoted 27 lines
sound/virtio/virtio_pcm_msg.c new file +27 -0
+/**
+ * virtsnd_pcm_msg_complete() - Complete an I/O message.
+ * @msg: I/O message.
+ * @written_bytes: Number of bytes written to the message.
+ *
+ * Completion of the message means the elapsed period. If transmission is
+ * allowed, then each completed message is immediately placed back at the end
+ * of the queue.
+ *
+ * For the playback substream, @written_bytes is equal to sizeof(msg->status).
+ *
+ * For the capture substream, @written_bytes is equal to sizeof(msg->status)
+ * plus the number of captured bytes.
+ *
+ * Context: Interrupt context. Takes and releases the VirtIO substream spinlock.
+ */
+static void virtsnd_pcm_msg_complete(struct virtio_pcm_msg *msg,
+				     size_t written_bytes)
+{
+	struct virtio_pcm_substream *vss = msg->substream;
+
+	/*
+	 * hw_ptr always indicates the buffer position of the first I/O message
+	 * in the virtqueue. Therefore, on each completion of an I/O message,
+	 * the hw_ptr value is unconditionally advanced.
+	 */
+	spin_lock(&vss->lock);

sound/virtio/virtio_pcm_msg.c line 306

Quoted 10 lines
sound/virtio/virtio_pcm_msg.c new file +10 -0
+	/*
+	 * If the capture substream returned an incorrect status, then just
+	 * increase the hw_ptr by the message size.
+	 */
+	if (vss->direction == SNDRV_PCM_STREAM_PLAYBACK ||
+	    written_bytes <= sizeof(msg->status)) {
+		struct scatterlist *sg;
+
+		for (sg = &msg->sgs[PCM_MSG_SG_DATA]; sg; sg = sg_next(sg))
+			vss->hw_ptr += sg->length;

So the sg list entries are supposed to be updated? Or if the length there are constant, we don't need to iterate the sg entries but keep the total length beforehand?

thanks,

Takashi

author maintainer
#10 lore raw

Re: [PATCH v6 5/9] ALSA: virtio: handling control and I/O messages for the PCM device

Quoted 10 lines · Takashi Iwai
Quoted 6 lines · Anton Yakovlev

+/**
+ * virtsnd_pcm_event() - Handle the PCM device event notification.
+ * @snd: VirtIO sound device.
+ * @event: VirtIO sound event.
+ *
+ * Context: Interrupt context.

OK, then nonatomic PCM flag is invalid...

Well, no. Here, events are kind of independent entities. PCM-related events are just a special case of more generic events, which can carry any kind of notification/payload. (And at the moment, only XRUN notification is supported for PCM substreams.) So it has nothing to do with the atomicity of the PCM device itself.

Quoted 96 lines
Quoted 70 lines
sound/virtio/virtio_pcm_msg.c new file +70 -0
+/**
+ * virtsnd_pcm_sg_num() - Count the number of sg-elements required to represent
+ *                        vmalloc'ed buffer.
+ * @data: Pointer to vmalloc'ed buffer.
+ * @length: Buffer size.
+ *
+ * Context: Any context.
+ * Return: Number of physically contiguous parts in the @data.
+ */
+static int virtsnd_pcm_sg_num(u8 *data, unsigned int length)
+{
+     phys_addr_t sg_address;
+     unsigned int sg_length;
+     int num = 0;
+
+     while (length) {
+             struct page *pg = vmalloc_to_page(data);
+             phys_addr_t pg_address = page_to_phys(pg);
+             size_t pg_length;
+
+             pg_length = PAGE_SIZE - offset_in_page(data);
+             if (pg_length > length)
+                     pg_length = length;
+
+             if (!num || sg_address + sg_length != pg_address) {
+                     sg_address = pg_address;
+                     sg_length = pg_length;
+                     num++;
+             } else {
+                     sg_length += pg_length;
+             }
+
+             data += pg_length;
+             length -= pg_length;
+     }
+
+     return num;
+}
+
+/**
+ * virtsnd_pcm_sg_from() - Build sg-list from vmalloc'ed buffer.
+ * @sgs: Preallocated sg-list to populate.
+ * @nsgs: The maximum number of elements in the @sgs.
+ * @data: Pointer to vmalloc'ed buffer.
+ * @length: Buffer size.
+ *
+ * Splits the buffer into physically contiguous parts and makes an sg-list of
+ * such parts.
+ *
+ * Context: Any context.
+ */
+static void virtsnd_pcm_sg_from(struct scatterlist *sgs, int nsgs, u8 *data,
+                             unsigned int length)
+{
+     int idx = -1;
+
+     while (length) {
+             struct page *pg = vmalloc_to_page(data);
+             size_t pg_length;
+
+             pg_length = PAGE_SIZE - offset_in_page(data);
+             if (pg_length > length)
+                     pg_length = length;
+
+             if (idx == -1 ||
+                 sg_phys(&sgs[idx]) + sgs[idx].length != page_to_phys(pg)) {
+                     if (idx + 1 == nsgs)
+                             break;
+                     sg_set_page(&sgs[++idx], pg, pg_length,
+                                 offset_in_page(data));

sound/virtio/virtio_pcm_msg.c line 116

Quoted 10 lines
sound/virtio/virtio_pcm_msg.c new file +10 -0
+             } else {
+                     sgs[idx].length += pg_length;
+             }
+
+             data += pg_length;
+             length -= pg_length;
+     }
+
+     sg_mark_end(&sgs[idx]);
+}

Hmm, I thought there can be already a handy helper to convert vmalloc to sglist, but apparently not. It should have been trivial to get the page list from vmalloc, e.g.

int vmalloc_to_page_list(void *p, struct page **page_ret)

{
         struct vmap_area *va;

         va = find_vmap_area((unsigned long)p);
         if (!va)
                 return 0;
         *page_ret = va->vm->pages;
         return va->vm->nr_pages;
}
Quoted 5 lines

Then you can set up the sg list in a single call from the given page list.

But it's just a cleanup, and let's mark it as a room for improvements.

Yeah, we can take a look into some kind of optimizations here. But I suspect, the overall code will look similar. It is not enough just to get a list of pages, you also need to build a list of physically contiguous regions from it. Because the sg-elements are put into a virtqueue that has a limited size. And each sg-element consumes one item in the virtqueue. And since the virtqueue itself is shared between all substreams, the items of the virtqueue become a scarce resource.

Quoted 34 lines

(snip)

Quoted 33 lines
sound/virtio/virtio_pcm_msg.c new file +33 -0
+/**
+ * virtsnd_pcm_msg_complete() - Complete an I/O message.
+ * @msg: I/O message.
+ * @written_bytes: Number of bytes written to the message.
+ *
+ * Completion of the message means the elapsed period. If transmission is
+ * allowed, then each completed message is immediately placed back at the end
+ * of the queue.
+ *
+ * For the playback substream, @written_bytes is equal to sizeof(msg->status).
+ *
+ * For the capture substream, @written_bytes is equal to sizeof(msg->status)
+ * plus the number of captured bytes.
+ *
+ * Context: Interrupt context. Takes and releases the VirtIO substream spinlock.
+ */
+static void virtsnd_pcm_msg_complete(struct virtio_pcm_msg *msg,
+                                  size_t written_bytes)
+{
+     struct virtio_pcm_substream *vss = msg->substream;
+
+     /*
+      * hw_ptr always indicates the buffer position of the first I/O message
+      * in the virtqueue. Therefore, on each completion of an I/O message,
+      * the hw_ptr value is unconditionally advanced.
+      */
+     spin_lock(&vss->lock);
+     /*
+      * If the capture substream returned an incorrect status, then just
+      * increase the hw_ptr by the message size.
+      */
+     if (vss->direction == SNDRV_PCM_STREAM_PLAYBACK ||
+         written_bytes <= sizeof(msg->status)) {
Quoted 8 lines

sound/virtio/virtio_pcm_msg.c line 306

Quoted 4 lines
sound/virtio/virtio_pcm_msg.c new file +4 -0
+             struct scatterlist *sg;
+
+             for (sg = &msg->sgs[PCM_MSG_SG_DATA]; sg; sg = sg_next(sg))
+                     vss->hw_ptr += sg->length;

So the sg list entries are supposed to be updated? Or if the length there are constant, we don't need to iterate the sg entries but keep the total length beforehand?

That's one of options. Since the same info can be derived from sg-list, I thought it might be not necessary to keep it in some additional field. But probably it makes sense to keep total length in the message structure itself. Then it will be more flexible (if we will need to create non-period sized messages in the future).

Quoted 5 lines

thanks,

Takashi

Signature · 10 lines
Anton Yakovlev
Senior Software Engineer

OpenSynergy GmbH
Rotherstr. 20, 10245 Berlin


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
#11 lore raw

Re: [PATCH v6 5/9] ALSA: virtio: handling control and I/O messages for the PCM device

Quoted 11 lines · Anton Yakovlev
Quoted 9 lines · Takashi Iwai
Quoted 6 lines · Anton Yakovlev

+/**
+ * virtsnd_pcm_event() - Handle the PCM device event notification.
+ * @snd: VirtIO sound device.
+ * @event: VirtIO sound event.
+ *
+ * Context: Interrupt context.

Quoted 7 lines
Quoted 1 line

OK, then nonatomic PCM flag is invalid...

Well, no. Here, events are kind of independent entities. PCM-related events are just a special case of more generic events, which can carry any kind of notification/payload. (And at the moment, only XRUN notification is supported for PCM substreams.) So it has nothing to do with the atomicity of the PCM device itself.

OK, thanks.

Basically the only question is how snd_pcm_period_elapsed() is called. And I see that it's called inside queue->lock, and this already invalidates the nonatomic PCM mode. So the code needs the fix: either fix this locking (and the context is guaranteed not to be an irq context), or change to the normal PCM mode without nonatomic flag. Both would bring some side-effect, and we need further changes, I suppose...

Quoted 99 lines
Quoted 99 lines
Quoted 70 lines
sound/virtio/virtio_pcm_msg.c new file +70 -0
+/**
+ * virtsnd_pcm_sg_num() - Count the number of sg-elements required to represent
+ *                        vmalloc'ed buffer.
+ * @data: Pointer to vmalloc'ed buffer.
+ * @length: Buffer size.
+ *
+ * Context: Any context.
+ * Return: Number of physically contiguous parts in the @data.
+ */
+static int virtsnd_pcm_sg_num(u8 *data, unsigned int length)
+{
+     phys_addr_t sg_address;
+     unsigned int sg_length;
+     int num = 0;
+
+     while (length) {
+             struct page *pg = vmalloc_to_page(data);
+             phys_addr_t pg_address = page_to_phys(pg);
+             size_t pg_length;
+
+             pg_length = PAGE_SIZE - offset_in_page(data);
+             if (pg_length > length)
+                     pg_length = length;
+
+             if (!num || sg_address + sg_length != pg_address) {
+                     sg_address = pg_address;
+                     sg_length = pg_length;
+                     num++;
+             } else {
+                     sg_length += pg_length;
+             }
+
+             data += pg_length;
+             length -= pg_length;
+     }
+
+     return num;
+}
+
+/**
+ * virtsnd_pcm_sg_from() - Build sg-list from vmalloc'ed buffer.
+ * @sgs: Preallocated sg-list to populate.
+ * @nsgs: The maximum number of elements in the @sgs.
+ * @data: Pointer to vmalloc'ed buffer.
+ * @length: Buffer size.
+ *
+ * Splits the buffer into physically contiguous parts and makes an sg-list of
+ * such parts.
+ *
+ * Context: Any context.
+ */
+static void virtsnd_pcm_sg_from(struct scatterlist *sgs, int nsgs, u8 *data,
+                             unsigned int length)
+{
+     int idx = -1;
+
+     while (length) {
+             struct page *pg = vmalloc_to_page(data);
+             size_t pg_length;
+
+             pg_length = PAGE_SIZE - offset_in_page(data);
+             if (pg_length > length)
+                     pg_length = length;
+
+             if (idx == -1 ||
+                 sg_phys(&sgs[idx]) + sgs[idx].length != page_to_phys(pg)) {
+                     if (idx + 1 == nsgs)
+                             break;
+                     sg_set_page(&sgs[++idx], pg, pg_length,
+                                 offset_in_page(data));

sound/virtio/virtio_pcm_msg.c line 116

Quoted 10 lines
sound/virtio/virtio_pcm_msg.c new file +10 -0
+             } else {
+                     sgs[idx].length += pg_length;
+             }
+
+             data += pg_length;
+             length -= pg_length;
+     }
+
+     sg_mark_end(&sgs[idx]);
+}

Hmm, I thought there can be already a handy helper to convert vmalloc to sglist, but apparently not. It should have been trivial to get the page list from vmalloc, e.g.

int vmalloc_to_page_list(void *p, struct page **page_ret)

{
         struct vmap_area *va;

         va = find_vmap_area((unsigned long)p);
         if (!va)
                 return 0;
         *page_ret = va->vm->pages;
         return va->vm->nr_pages;
}

Then you can set up the sg list in a single call from the given page list.

Quoted 7 lines
Quoted 2 lines

But it's just a cleanup, and let's mark it as a room for improvements.

Yeah, we can take a look into some kind of optimizations here. But I suspect, the overall code will look similar. It is not enough just to get a list of pages, you also need to build a list of physically contiguous regions from it.

I believe the standard helper does it. But it's for sg_table, hence the plain scatterlist needs to be extracted from there, but most of complex things are in the standard code.

But it's merely an optimization and something for future.

Takashi

author maintainer
#12 lore raw

Re: [PATCH v6 5/9] ALSA: virtio: handling control and I/O messages for the PCM device

Quoted 23 lines · Takashi Iwai
Quoted 11 lines · Anton Yakovlev
Quoted 9 lines · Takashi Iwai
Quoted 6 lines · Anton Yakovlev

+/**
+ * virtsnd_pcm_event() - Handle the PCM device event notification.
+ * @snd: VirtIO sound device.
+ * @event: VirtIO sound event.
+ *
+ * Context: Interrupt context.

Quoted 7 lines
Quoted 1 line

OK, then nonatomic PCM flag is invalid...

Well, no. Here, events are kind of independent entities. PCM-related events are just a special case of more generic events, which can carry any kind of notification/payload. (And at the moment, only XRUN notification is supported for PCM substreams.) So it has nothing to do with the atomicity of the PCM device itself.

OK, thanks.

Quoted 7 lines

Basically the only question is how snd_pcm_period_elapsed() is called. And I see that it's called inside queue->lock, and this already invalidates the nonatomic PCM mode. So the code needs the fix: either fix this locking (and the context is guaranteed not to be an irq context), or change to the normal PCM mode without nonatomic flag. Both would bring some side-effect, and we need further changes, I suppose...

Ok, I understood the problem. Well, I would say the nonatomic PCM mode is more important option, since in this mode we can guarantee the correct operation of the device. And if you say, that we need to get rid of irq context here, then probably workqueue for calling snd_pcm_period_elapsed() should be fine (of course, it should be shared between all available substreams).

Quoted 107 lines
Quoted 99 lines
Quoted 99 lines
Quoted 70 lines
sound/virtio/virtio_pcm_msg.c new file +70 -0
+/**
+ * virtsnd_pcm_sg_num() - Count the number of sg-elements required to represent
+ *                        vmalloc'ed buffer.
+ * @data: Pointer to vmalloc'ed buffer.
+ * @length: Buffer size.
+ *
+ * Context: Any context.
+ * Return: Number of physically contiguous parts in the @data.
+ */
+static int virtsnd_pcm_sg_num(u8 *data, unsigned int length)
+{
+     phys_addr_t sg_address;
+     unsigned int sg_length;
+     int num = 0;
+
+     while (length) {
+             struct page *pg = vmalloc_to_page(data);
+             phys_addr_t pg_address = page_to_phys(pg);
+             size_t pg_length;
+
+             pg_length = PAGE_SIZE - offset_in_page(data);
+             if (pg_length > length)
+                     pg_length = length;
+
+             if (!num || sg_address + sg_length != pg_address) {
+                     sg_address = pg_address;
+                     sg_length = pg_length;
+                     num++;
+             } else {
+                     sg_length += pg_length;
+             }
+
+             data += pg_length;
+             length -= pg_length;
+     }
+
+     return num;
+}
+
+/**
+ * virtsnd_pcm_sg_from() - Build sg-list from vmalloc'ed buffer.
+ * @sgs: Preallocated sg-list to populate.
+ * @nsgs: The maximum number of elements in the @sgs.
+ * @data: Pointer to vmalloc'ed buffer.
+ * @length: Buffer size.
+ *
+ * Splits the buffer into physically contiguous parts and makes an sg-list of
+ * such parts.
+ *
+ * Context: Any context.
+ */
+static void virtsnd_pcm_sg_from(struct scatterlist *sgs, int nsgs, u8 *data,
+                             unsigned int length)
+{
+     int idx = -1;
+
+     while (length) {
+             struct page *pg = vmalloc_to_page(data);
+             size_t pg_length;
+
+             pg_length = PAGE_SIZE - offset_in_page(data);
+             if (pg_length > length)
+                     pg_length = length;
+
+             if (idx == -1 ||
+                 sg_phys(&sgs[idx]) + sgs[idx].length != page_to_phys(pg)) {
+                     if (idx + 1 == nsgs)
+                             break;
+                     sg_set_page(&sgs[++idx], pg, pg_length,
+                                 offset_in_page(data));

sound/virtio/virtio_pcm_msg.c line 116

Quoted 10 lines
sound/virtio/virtio_pcm_msg.c new file +10 -0
+             } else {
+                     sgs[idx].length += pg_length;
+             }
+
+             data += pg_length;
+             length -= pg_length;
+     }
+
+     sg_mark_end(&sgs[idx]);
+}

Hmm, I thought there can be already a handy helper to convert vmalloc to sglist, but apparently not. It should have been trivial to get the page list from vmalloc, e.g.

int vmalloc_to_page_list(void *p, struct page **page_ret)

{
          struct vmap_area *va;

          va = find_vmap_area((unsigned long)p);
          if (!va)
                  return 0;
          *page_ret = va->vm->pages;
          return va->vm->nr_pages;
}

Then you can set up the sg list in a single call from the given page list.

Quoted 7 lines
Quoted 2 lines

But it's just a cleanup, and let's mark it as a room for improvements.

Yeah, we can take a look into some kind of optimizations here. But I suspect, the overall code will look similar. It is not enough just to get a list of pages, you also need to build a list of physically contiguous regions from it.

Quoted 5 lines

I believe the standard helper does it. But it's for sg_table, hence the plain scatterlist needs to be extracted from there, but most of complex things are in the standard code.

But it's merely an optimization and something for future.

I quickly checked it. I think it's hardly possible to do anything here. These functions to deal with vmalloced areas are not exported. And, according to comments, require some proper locking on top of that. At least, it does not look like a trivial things.

Quoted 3 lines

Takashi

Signature · 10 lines
Anton Yakovlev
Senior Software Engineer

OpenSynergy GmbH
Rotherstr. 20, 10245 Berlin


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
#13 lore raw

Re: [PATCH v6 5/9] ALSA: virtio: handling control and I/O messages for the PCM device

Quoted 33 lines · Anton Yakovlev
Quoted 23 lines · Takashi Iwai
Quoted 11 lines · Anton Yakovlev
Quoted 9 lines · Takashi Iwai
Quoted 6 lines · Anton Yakovlev

+/**
+ * virtsnd_pcm_event() - Handle the PCM device event notification.
+ * @snd: VirtIO sound device.
+ * @event: VirtIO sound event.
+ *
+ * Context: Interrupt context.

Quoted 7 lines
Quoted 1 line

OK, then nonatomic PCM flag is invalid...

Well, no. Here, events are kind of independent entities. PCM-related events are just a special case of more generic events, which can carry any kind of notification/payload. (And at the moment, only XRUN notification is supported for PCM substreams.) So it has nothing to do with the atomicity of the PCM device itself.

OK, thanks.

Quoted 7 lines

Basically the only question is how snd_pcm_period_elapsed() is called. And I see that it's called inside queue->lock, and this already invalidates the nonatomic PCM mode. So the code needs the fix: either fix this locking (and the context is guaranteed not to be an irq context), or change to the normal PCM mode without nonatomic flag. Both would bring some side-effect, and we need further changes, I suppose...

Quoted 3 lines

Ok, I understood the problem. Well, I would say the nonatomic PCM mode is more important option, since in this mode we can guarantee the correct operation of the device.

Which operation (except for the resume) in the trigger and the pointer callbacks need the action that need to sleep? I thought the sync with the command queue is done in the sync_stop. And most of others seem already taking the spinlock in themselves, so the non-atomic operation has little merit for them.

Quoted 4 lines

And if you say, that we need to get rid
of irq context here, then probably workqueue for calling snd_pcm_period_elapsed() should be fine (of course, it should be shared between all available substreams).

That would work, but it's of course just papering over it :)

Quoted 111 lines
Quoted 111 lines
Quoted 99 lines
Quoted 99 lines
Quoted 70 lines
sound/virtio/virtio_pcm_msg.c new file +70 -0
+/**
+ * virtsnd_pcm_sg_num() - Count the number of sg-elements required to represent
+ *                        vmalloc'ed buffer.
+ * @data: Pointer to vmalloc'ed buffer.
+ * @length: Buffer size.
+ *
+ * Context: Any context.
+ * Return: Number of physically contiguous parts in the @data.
+ */
+static int virtsnd_pcm_sg_num(u8 *data, unsigned int length)
+{
+     phys_addr_t sg_address;
+     unsigned int sg_length;
+     int num = 0;
+
+     while (length) {
+             struct page *pg = vmalloc_to_page(data);
+             phys_addr_t pg_address = page_to_phys(pg);
+             size_t pg_length;
+
+             pg_length = PAGE_SIZE - offset_in_page(data);
+             if (pg_length > length)
+                     pg_length = length;
+
+             if (!num || sg_address + sg_length != pg_address) {
+                     sg_address = pg_address;
+                     sg_length = pg_length;
+                     num++;
+             } else {
+                     sg_length += pg_length;
+             }
+
+             data += pg_length;
+             length -= pg_length;
+     }
+
+     return num;
+}
+
+/**
+ * virtsnd_pcm_sg_from() - Build sg-list from vmalloc'ed buffer.
+ * @sgs: Preallocated sg-list to populate.
+ * @nsgs: The maximum number of elements in the @sgs.
+ * @data: Pointer to vmalloc'ed buffer.
+ * @length: Buffer size.
+ *
+ * Splits the buffer into physically contiguous parts and makes an sg-list of
+ * such parts.
+ *
+ * Context: Any context.
+ */
+static void virtsnd_pcm_sg_from(struct scatterlist *sgs, int nsgs, u8 *data,
+                             unsigned int length)
+{
+     int idx = -1;
+
+     while (length) {
+             struct page *pg = vmalloc_to_page(data);
+             size_t pg_length;
+
+             pg_length = PAGE_SIZE - offset_in_page(data);
+             if (pg_length > length)
+                     pg_length = length;
+
+             if (idx == -1 ||
+                 sg_phys(&sgs[idx]) + sgs[idx].length != page_to_phys(pg)) {
+                     if (idx + 1 == nsgs)
+                             break;
+                     sg_set_page(&sgs[++idx], pg, pg_length,
+                                 offset_in_page(data));

sound/virtio/virtio_pcm_msg.c line 116

Quoted 10 lines
sound/virtio/virtio_pcm_msg.c new file +10 -0
+             } else {
+                     sgs[idx].length += pg_length;
+             }
+
+             data += pg_length;
+             length -= pg_length;
+     }
+
+     sg_mark_end(&sgs[idx]);
+}

Hmm, I thought there can be already a handy helper to convert vmalloc to sglist, but apparently not. It should have been trivial to get the page list from vmalloc, e.g.

int vmalloc_to_page_list(void *p, struct page **page_ret)

{
          struct vmap_area *va;

          va = find_vmap_area((unsigned long)p);
          if (!va)
                  return 0;
          *page_ret = va->vm->pages;
          return va->vm->nr_pages;
}

Then you can set up the sg list in a single call from the given page list.

Quoted 7 lines
Quoted 2 lines

But it's just a cleanup, and let's mark it as a room for improvements.

Yeah, we can take a look into some kind of optimizations here. But I suspect, the overall code will look similar. It is not enough just to get a list of pages, you also need to build a list of physically contiguous regions from it.

I believe the standard helper does it. But it's for sg_table, hence the plain scatterlist needs to be extracted from there, but most of complex things are in the standard code.

Quoted 6 lines
Quoted 1 line

But it's merely an optimization and something for future.

I quickly checked it. I think it's hardly possible to do anything here. These functions to deal with vmalloced areas are not exported. And, according to comments, require some proper locking on top of that. At least, it does not look like a trivial things.

Sure, it needs a function exposed from vmalloc.c. But I don't think the locking is the problem as find_vmap_area() already takes care of it, and we don't release the vmalloced pages while invoking this function. Of course I might overlook something, but my point is that this kind of work should be rather in the core (or at least most of the important steps in the code should be in the core code).

Takashi

author maintainer
#14 lore raw

Re: [PATCH v6 5/9] ALSA: virtio: handling control and I/O messages for the PCM device

Quoted 39 lines · Takashi Iwai
Quoted 33 lines · Anton Yakovlev
Quoted 23 lines · Takashi Iwai
Quoted 11 lines · Anton Yakovlev
Quoted 9 lines · Takashi Iwai
Quoted 6 lines · Anton Yakovlev

+/**
+ * virtsnd_pcm_event() - Handle the PCM device event notification.
+ * @snd: VirtIO sound device.
+ * @event: VirtIO sound event.
+ *
+ * Context: Interrupt context.

Quoted 7 lines
Quoted 1 line

OK, then nonatomic PCM flag is invalid...

Well, no. Here, events are kind of independent entities. PCM-related events are just a special case of more generic events, which can carry any kind of notification/payload. (And at the moment, only XRUN notification is supported for PCM substreams.) So it has nothing to do with the atomicity of the PCM device itself.

OK, thanks.

Quoted 7 lines

Basically the only question is how snd_pcm_period_elapsed() is called. And I see that it's called inside queue->lock, and this already invalidates the nonatomic PCM mode. So the code needs the fix: either fix this locking (and the context is guaranteed not to be an irq context), or change to the normal PCM mode without nonatomic flag. Both would bring some side-effect, and we need further changes, I suppose...

Quoted 3 lines

Ok, I understood the problem. Well, I would say the nonatomic PCM mode is more important option, since in this mode we can guarantee the correct operation of the device.

Quoted 5 lines

Which operation (except for the resume) in the trigger and the pointer callbacks need the action that need to sleep? I thought the sync with the command queue is done in the sync_stop. And most of others seem already taking the spinlock in themselves, so the non-atomic operation has little merit for them.

All three commands from the beginning of that switch in trigger op:
ops->trigger(START)
ops->trigger(PAUSE_RELEASE)
ops->trigger(RESUME)

They all result in
virtsnd_ctl_msg_send_sync(VIRTIO_SND_R_PCM_START)

And that command must be sync, i.e. we need to wait/sleep until device sent response. There are two major reasons for that: we need to be sure, that substream has been actually started (i.e. device said OK). And we need to take into account the virtualization overhead as well. Since substream starting on device side may take some time, we can not return from the trigger op until it actually happened. In atomic mode all these nuances are lost, and it may lead to incorrect application behavior.

Quoted 126 lines
Quoted 4 lines

And if you say, that we need to get rid
of irq context here, then probably workqueue for calling snd_pcm_period_elapsed() should be fine (of course, it should be shared between all available substreams).

That would work, but it's of course just papering over it :)

Quoted 111 lines
Quoted 111 lines
Quoted 99 lines
Quoted 99 lines
Quoted 70 lines
sound/virtio/virtio_pcm_msg.c new file +70 -0
+/**
+ * virtsnd_pcm_sg_num() - Count the number of sg-elements required to represent
+ *                        vmalloc'ed buffer.
+ * @data: Pointer to vmalloc'ed buffer.
+ * @length: Buffer size.
+ *
+ * Context: Any context.
+ * Return: Number of physically contiguous parts in the @data.
+ */
+static int virtsnd_pcm_sg_num(u8 *data, unsigned int length)
+{
+     phys_addr_t sg_address;
+     unsigned int sg_length;
+     int num = 0;
+
+     while (length) {
+             struct page *pg = vmalloc_to_page(data);
+             phys_addr_t pg_address = page_to_phys(pg);
+             size_t pg_length;
+
+             pg_length = PAGE_SIZE - offset_in_page(data);
+             if (pg_length > length)
+                     pg_length = length;
+
+             if (!num || sg_address + sg_length != pg_address) {
+                     sg_address = pg_address;
+                     sg_length = pg_length;
+                     num++;
+             } else {
+                     sg_length += pg_length;
+             }
+
+             data += pg_length;
+             length -= pg_length;
+     }
+
+     return num;
+}
+
+/**
+ * virtsnd_pcm_sg_from() - Build sg-list from vmalloc'ed buffer.
+ * @sgs: Preallocated sg-list to populate.
+ * @nsgs: The maximum number of elements in the @sgs.
+ * @data: Pointer to vmalloc'ed buffer.
+ * @length: Buffer size.
+ *
+ * Splits the buffer into physically contiguous parts and makes an sg-list of
+ * such parts.
+ *
+ * Context: Any context.
+ */
+static void virtsnd_pcm_sg_from(struct scatterlist *sgs, int nsgs, u8 *data,
+                             unsigned int length)
+{
+     int idx = -1;
+
+     while (length) {
+             struct page *pg = vmalloc_to_page(data);
+             size_t pg_length;
+
+             pg_length = PAGE_SIZE - offset_in_page(data);
+             if (pg_length > length)
+                     pg_length = length;
+
+             if (idx == -1 ||
+                 sg_phys(&sgs[idx]) + sgs[idx].length != page_to_phys(pg)) {
+                     if (idx + 1 == nsgs)
+                             break;
+                     sg_set_page(&sgs[++idx], pg, pg_length,
+                                 offset_in_page(data));

sound/virtio/virtio_pcm_msg.c line 116

Quoted 10 lines
sound/virtio/virtio_pcm_msg.c new file +10 -0
+             } else {
+                     sgs[idx].length += pg_length;
+             }
+
+             data += pg_length;
+             length -= pg_length;
+     }
+
+     sg_mark_end(&sgs[idx]);
+}

Hmm, I thought there can be already a handy helper to convert vmalloc to sglist, but apparently not. It should have been trivial to get the page list from vmalloc, e.g.

int vmalloc_to_page_list(void *p, struct page **page_ret)

{
           struct vmap_area *va;

           va = find_vmap_area((unsigned long)p);
           if (!va)
                   return 0;
           *page_ret = va->vm->pages;
           return va->vm->nr_pages;
}

Then you can set up the sg list in a single call from the given page list.

Quoted 7 lines
Quoted 2 lines

But it's just a cleanup, and let's mark it as a room for improvements.

Yeah, we can take a look into some kind of optimizations here. But I suspect, the overall code will look similar. It is not enough just to get a list of pages, you also need to build a list of physically contiguous regions from it.

I believe the standard helper does it. But it's for sg_table, hence the plain scatterlist needs to be extracted from there, but most of complex things are in the standard code.

Quoted 6 lines
Quoted 1 line

But it's merely an optimization and something for future.

I quickly checked it. I think it's hardly possible to do anything here. These functions to deal with vmalloced areas are not exported. And, according to comments, require some proper locking on top of that. At least, it does not look like a trivial things.

Quoted 6 lines

Sure, it needs a function exposed from vmalloc.c. But I don't think the locking is the problem as find_vmap_area() already takes care of it, and we don't release the vmalloced pages while invoking this function. Of course I might overlook something, but my point is that this kind of work should be rather in the core (or at least most of the important steps in the code should be in the core code).

Then it's definitely some work for future. :)

Quoted 3 lines

Takashi

Signature · 10 lines
Anton Yakovlev
Senior Software Engineer

OpenSynergy GmbH
Rotherstr. 20, 10245 Berlin


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
#15 lore raw

Re: [PATCH v6 5/9] ALSA: virtio: handling control and I/O messages for the PCM device

Quoted 55 lines · Anton Yakovlev
Quoted 39 lines · Takashi Iwai
Quoted 33 lines · Anton Yakovlev
Quoted 23 lines · Takashi Iwai
Quoted 11 lines · Anton Yakovlev
Quoted 9 lines · Takashi Iwai
Quoted 6 lines · Anton Yakovlev

+/**
+ * virtsnd_pcm_event() - Handle the PCM device event notification.
+ * @snd: VirtIO sound device.
+ * @event: VirtIO sound event.
+ *
+ * Context: Interrupt context.

Quoted 7 lines
Quoted 1 line

OK, then nonatomic PCM flag is invalid...

Well, no. Here, events are kind of independent entities. PCM-related events are just a special case of more generic events, which can carry any kind of notification/payload. (And at the moment, only XRUN notification is supported for PCM substreams.) So it has nothing to do with the atomicity of the PCM device itself.

OK, thanks.

Quoted 7 lines

Basically the only question is how snd_pcm_period_elapsed() is called. And I see that it's called inside queue->lock, and this already invalidates the nonatomic PCM mode. So the code needs the fix: either fix this locking (and the context is guaranteed not to be an irq context), or change to the normal PCM mode without nonatomic flag. Both would bring some side-effect, and we need further changes, I suppose...

Quoted 3 lines

Ok, I understood the problem. Well, I would say the nonatomic PCM mode is more important option, since in this mode we can guarantee the correct operation of the device.

Quoted 5 lines

Which operation (except for the resume) in the trigger and the pointer callbacks need the action that need to sleep? I thought the sync with the command queue is done in the sync_stop. And most of others seem already taking the spinlock in themselves, so the non-atomic operation has little merit for them.

All three commands from the beginning of that switch in trigger op:
ops->trigger(START)
ops->trigger(PAUSE_RELEASE)
ops->trigger(RESUME)

They all result in
virtsnd_ctl_msg_send_sync(VIRTIO_SND_R_PCM_START)

Quoted 8 lines

And that command must be sync, i.e. we need to wait/sleep until device sent response. There are two major reasons for that: we need to be sure, that substream has been actually started (i.e. device said OK). And we need to take into account the virtualization overhead as well. Since substream starting on device side may take some time, we can not return from the trigger op until it actually happened. In atomic mode all these nuances are lost, and it may lead to incorrect application behavior.

I see, then the nonatomic mode is the only way to go, indeed.

Takashi

author maintainer
#16 lore raw

PATCH v6 6/9 ALSA: virtio: PCM substream operators

In reply to Anton Yakovlev

Introduce the operators required for the operation of substreams.

---
 sound/virtio/Makefile         |   3 +-
 sound/virtio/virtio_pcm.c     |   2 +
 sound/virtio/virtio_pcm.h     |   4 +
 sound/virtio/virtio_pcm_ops.c | 453 ++++++++++++++++++++++++++++++++++
 4 files changed, 461 insertions(+), 1 deletion(-)
 create mode 100644 sound/virtio/virtio_pcm_ops.c
sound/virtio/Makefile +2 -1
index 626af3cc3ed7..34493226793f 100644
@@ -6,5 +6,6 @@ virtio_snd-objs := \
 	virtio_card.o \
 	virtio_ctl_msg.o \
 	virtio_pcm.o \
-	virtio_pcm_msg.o
+	virtio_pcm_msg.o \
+	virtio_pcm_ops.o
 
sound/virtio/virtio_pcm.c +2 -0
index 3cfd3520a9c0..3605151860f2 100644
@@ -454,6 +454,8 @@ int virtsnd_pcm_build_devs(struct virtio_snd *snd)
 
 			for (kss = ks->substream; kss; kss = kss->next)
 				vs->substreams[kss->number]->substream = kss;
+
+			snd_pcm_set_ops(vpcm->pcm, i, &virtsnd_pcm_ops);
 		}
 
 		snd_pcm_set_managed_buffer_all(vpcm->pcm,
sound/virtio/virtio_pcm.h +4 -0
index 7af41cad4e8f..55dfb19d14b3 100644
@@ -28,6 +28,7 @@ struct virtio_pcm_msg;
  * @hw_ptr: Substream hardware pointer value in bytes [0 ... buffer_bytes).
  * @xfer_enabled: Data transfer state (0 - off, 1 - on).
  * @xfer_xrun: Data underflow/overflow state (0 - no xrun, 1 - xrun).
+ * @release: True if the substream must be released on the device side.
  * @msgs: Allocated I/O messages.
  * @nmsgs: Number of allocated I/O messages.
  * @msg_last_enqueued: Index of the last I/O message added to the virtqueue.
@@ -47,6 +48,7 @@ struct virtio_pcm_substream {
 	size_t hw_ptr;
 	bool xfer_enabled;
 	bool xfer_xrun;
+	bool release;
 	struct virtio_pcm_msg **msgs;
 	unsigned int nmsgs;
 	int msg_last_enqueued;
@@ -78,6 +80,8 @@ struct virtio_pcm {
 	struct virtio_pcm_stream streams[SNDRV_PCM_STREAM_LAST + 1];
 };
 
+extern const struct snd_pcm_ops virtsnd_pcm_ops;
+
 int virtsnd_pcm_validate(struct virtio_device *vdev);
 
 int virtsnd_pcm_parse_cfg(struct virtio_snd *snd);
sound/virtio/virtio_pcm_ops.c new file +453 -0

453 lines not on this page · raw message

Signature · 7 lines
2.30.1


_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#17 lore raw

Re: [PATCH v6 6/9] ALSA: virtio: PCM substream operators

Quoted 16 lines · Anton Yakovlev
sound/virtio/virtio_pcm_ops.c new file +15 -0
+static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command)
+{
+	struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
+	struct virtio_snd *snd = vss->snd;
+	struct virtio_snd_msg *msg;
+	unsigned long flags;
+	int rc;
+
+	switch (command) {
+	case SNDRV_PCM_TRIGGER_START:
+	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: {
+		struct virtio_snd_queue *queue = virtsnd_pcm_queue(vss);
+
+		spin_lock_irqsave(&queue->lock, flags);
+		spin_lock(&vss->lock);

sound/virtio/virtio_pcm_ops.c line 327

Quoted 10 lines
sound/virtio/virtio_pcm_ops.c new file +10 -0
+		rc = virtsnd_pcm_msg_send(vss);
+		if (!rc)
+			vss->xfer_enabled = true;
+		spin_unlock(&vss->lock);
+		spin_unlock_irqrestore(&queue->lock, flags);
+		if (rc)
+			return rc;
+
+		msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_START,
+						GFP_KERNEL);

If we drop nonatomic PCM, this has to be changed: GFP_KERNEL is no longer valid in the trigger and the pointer callbacks. I wonder, though, the code below uses GFP_ATOMIC inconsistently...

Quoted 9 lines
sound/virtio/virtio_pcm_ops.c new file +9 -0
+	case SNDRV_PCM_TRIGGER_STOP:
+	case SNDRV_PCM_TRIGGER_PAUSE_PUSH: {
+		spin_lock_irqsave(&vss->lock, flags);
+		vss->xfer_enabled = false;
+		spin_unlock_irqrestore(&vss->lock, flags);
+
+		/*
+		 * The substream needs to be released on the device side only
+		 * when it is completely stopped.

sound/virtio/virtio_pcm_ops.c line 356

Quoted 10 lines
sound/virtio/virtio_pcm_ops.c new file +10 -0
+		 */
+		vss->release = (command == SNDRV_PCM_TRIGGER_STOP);
+
+		/*
+		 * The STOP command can be issued in an atomic context after
+		 * the drain is complete. Therefore, in general, we cannot sleep
+		 * here.
+		 */
+		msg = virtsnd_pcm_ctl_msg_alloc(vss, VIRTIO_SND_R_PCM_STOP,
+						GFP_ATOMIC);

BTW...

sound/virtio/virtio_pcm_ops.c line 364

Quoted 3 lines
sound/virtio/virtio_pcm_ops.c new file +3 -0
+	default: {
+		return -EINVAL;
+	}

Let's avoid braces inside the switch() unless it's inevitably needed. It makes the code harder to read.

sound/virtio/virtio_pcm_ops.c line 437

Quoted 11 lines
sound/virtio/virtio_pcm_ops.c new file +11 -0
+static snd_pcm_uframes_t
+virtsnd_pcm_pointer(struct snd_pcm_substream *substream)
+{
+	struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
+	snd_pcm_uframes_t hw_ptr = SNDRV_PCM_POS_XRUN;
+	unsigned long flags;
+
+	spin_lock_irqsave(&vss->lock, flags);
+	if (!vss->xfer_xrun)
+		hw_ptr = bytes_to_frames(substream->runtime, vss->hw_ptr);
+	spin_unlock_irqrestore(&vss->lock, flags);

Oh, and if we drop nonatomc PCM, both trigger and pointer callbacks are guaranteed to be called inside the spinlock, hence you can remove *_irqsave() but just us spin_lock() in those two callbacks.

thanks,

Takashi

author maintainer
#18 lore raw

Re: [PATCH v6 6/9] ALSA: virtio: PCM substream operators

Quoted 3 lines · Takashi Iwai
Quoted 1 line · Anton Yakovlev

[snip]

Quoted 7 lines
Quoted 7 lines
sound/virtio/virtio_pcm_ops.c new file +7 -0
+static snd_pcm_uframes_t
+virtsnd_pcm_pointer(struct snd_pcm_substream *substream)
+{
+     struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
+     snd_pcm_uframes_t hw_ptr = SNDRV_PCM_POS_XRUN;
+     unsigned long flags;
+
Quoted 8 lines

sound/virtio/virtio_pcm_ops.c line 437

Quoted 4 lines
sound/virtio/virtio_pcm_ops.c new file +4 -0
+     spin_lock_irqsave(&vss->lock, flags);
+     if (!vss->xfer_xrun)
+             hw_ptr = bytes_to_frames(substream->runtime, vss->hw_ptr);
+     spin_unlock_irqrestore(&vss->lock, flags);

Oh, and if we drop nonatomc PCM, both trigger and pointer callbacks are guaranteed to be called inside the spinlock, hence you can remove *_irqsave() but just us spin_lock() in those two callbacks.

As I understand, spin_lock_irqsave() disables only local interrupts. But what about other CPU cores?

Quoted 5 lines

thanks,

Takashi

Signature · 10 lines
Anton Yakovlev
Senior Software Engineer

OpenSynergy GmbH
Rotherstr. 20, 10245 Berlin


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
#19 lore raw

Re: [PATCH v6 6/9] ALSA: virtio: PCM substream operators

Quoted 26 lines · Anton Yakovlev
Quoted 3 lines · Takashi Iwai
Quoted 1 line · Anton Yakovlev

[snip]

Quoted 7 lines
Quoted 7 lines
sound/virtio/virtio_pcm_ops.c new file +7 -0
+static snd_pcm_uframes_t
+virtsnd_pcm_pointer(struct snd_pcm_substream *substream)
+{
+     struct virtio_pcm_substream *vss = snd_pcm_substream_chip(substream);
+     snd_pcm_uframes_t hw_ptr = SNDRV_PCM_POS_XRUN;
+     unsigned long flags;
+
Quoted 8 lines

sound/virtio/virtio_pcm_ops.c line 437

Quoted 4 lines
sound/virtio/virtio_pcm_ops.c new file +4 -0
+     spin_lock_irqsave(&vss->lock, flags);
+     if (!vss->xfer_xrun)
+             hw_ptr = bytes_to_frames(substream->runtime, vss->hw_ptr);
+     spin_unlock_irqrestore(&vss->lock, flags);

Oh, and if we drop nonatomc PCM, both trigger and pointer callbacks are guaranteed to be called inside the spinlock, hence you can remove *_irqsave() but just us spin_lock() in those two callbacks.

Quoted 2 lines

As I understand, spin_lock_irqsave() disables only local interrupts. But what about other CPU cores?

Those are already under spin_lock_irqsave() of the PCM substream lock (in the case of normal PCM operation without nonatomic flag).

Takashi

author maintainer
#20 lore raw

PATCH v6 7/9 ALSA: virtio: introduce jack support

In reply to Anton Yakovlev

Enumerate all available jacks and create ALSA controls.

At the moment jacks have a simple implementation and can only be used to receive notifications about a plugged in/out device.

---
 sound/virtio/Makefile      |   1 +
 sound/virtio/virtio_card.c |  14 +++
 sound/virtio/virtio_card.h |  12 ++
 sound/virtio/virtio_jack.c | 233 +++++++++++++++++++++++++++++++++++++
 4 files changed, 260 insertions(+)
 create mode 100644 sound/virtio/virtio_jack.c
sound/virtio/Makefile +1 -0
index 34493226793f..09f485291285 100644
@@ -5,6 +5,7 @@ obj-$(CONFIG_SND_VIRTIO) += virtio_snd.o
 virtio_snd-objs := \
 	virtio_card.o \
 	virtio_ctl_msg.o \
+	virtio_jack.o \
 	virtio_pcm.o \
 	virtio_pcm_msg.o \
 	virtio_pcm_ops.o
sound/virtio/virtio_card.c +14 -0
index 9f74261b234c..c852afda5712 100644
@@ -67,6 +67,10 @@ static void virtsnd_event_notify_cb(struct virtqueue *vqueue)
 		virtqueue_disable_cb(vqueue);
 		while ((event = virtqueue_get_buf(vqueue, &length))) {
 			switch (le32_to_cpu(event->hdr.code)) {
+			case VIRTIO_SND_EVT_JACK_CONNECTED:
+			case VIRTIO_SND_EVT_JACK_DISCONNECTED:
+				virtsnd_jack_event(snd, event);
+				break;
 			case VIRTIO_SND_EVT_PCM_PERIOD_ELAPSED:
 			case VIRTIO_SND_EVT_PCM_XRUN:
 				virtsnd_pcm_event(snd, event);
@@ -185,10 +189,20 @@ static int virtsnd_build_devs(struct virtio_snd *snd)
 			 VIRTIO_SND_CARD_NAME " at %s/%s",
 			 dev_name(dev->parent), dev_name(dev));
 
+	rc = virtsnd_jack_parse_cfg(snd);
+	if (rc)
+		return rc;
+
 	rc = virtsnd_pcm_parse_cfg(snd);
 	if (rc)
 		return rc;
 
+	if (snd->njacks) {
+		rc = virtsnd_jack_build_devs(snd);
+		if (rc)
+			return rc;
+	}
+
 	if (snd->nsubstreams) {
 		rc = virtsnd_pcm_build_devs(snd);
 		if (rc)
sound/virtio/virtio_card.h +12 -0
index 7e50f3835ab1..2092da297a9d 100644
@@ -18,6 +18,7 @@
 #define VIRTIO_SND_CARD_NAME	"VirtIO SoundCard"
 #define VIRTIO_SND_PCM_NAME	"VirtIO PCM"
 
+struct virtio_jack;
 struct virtio_pcm_substream;
 
 /**
@@ -38,6 +39,8 @@ struct virtio_snd_queue {
  * @ctl_msgs: Pending control request list.
  * @event_msgs: Device events.
  * @pcm_list: VirtIO PCM device list.
+ * @jacks: VirtIO jacks.
+ * @njacks: Number of jacks.
  * @substreams: VirtIO PCM substreams.
  * @nsubstreams: Number of PCM substreams.
  */
@@ -48,6 +51,8 @@ struct virtio_snd {
 	struct list_head ctl_msgs;
 	struct virtio_snd_event *event_msgs;
 	struct list_head pcm_list;
+	struct virtio_jack *jacks;
+	u32 njacks;
 	struct virtio_pcm_substream *substreams;
 	u32 nsubstreams;
 };
@@ -88,4 +93,11 @@ virtsnd_pcm_queue(struct virtio_pcm_substream *vss)
 		return virtsnd_rx_queue(vss->snd);
 }
 
+int virtsnd_jack_parse_cfg(struct virtio_snd *snd);
+
+int virtsnd_jack_build_devs(struct virtio_snd *snd);
+
+void virtsnd_jack_event(struct virtio_snd *snd,
+			struct virtio_snd_event *event);
+
 #endif /* VIRTIO_SND_CARD_H */
sound/virtio/virtio_jack.c new file +233 -0

233 lines not on this page · raw message

Signature · 7 lines
2.30.1



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
author maintainer
#21 lore raw

PATCH v6 8/9 ALSA: virtio: introduce PCM channel map support

In reply to Anton Yakovlev

Enumerate all available PCM channel maps and create ALSA controls.

---
 sound/virtio/Makefile       |   1 +
 sound/virtio/virtio_card.c  |  10 ++
 sound/virtio/virtio_card.h  |   8 ++
 sound/virtio/virtio_chmap.c | 219 ++++++++++++++++++++++++++++++++++++
 sound/virtio/virtio_pcm.h   |   4 +
 5 files changed, 242 insertions(+)
 create mode 100644 sound/virtio/virtio_chmap.c
sound/virtio/Makefile +1 -0
index 09f485291285..2742bddb8874 100644
@@ -4,6 +4,7 @@ obj-$(CONFIG_SND_VIRTIO) += virtio_snd.o
 
 virtio_snd-objs := \
 	virtio_card.o \
+	virtio_chmap.o \
 	virtio_ctl_msg.o \
 	virtio_jack.o \
 	virtio_pcm.o \
sound/virtio/virtio_card.c +10 -0
index c852afda5712..59455a562018 100644
@@ -197,6 +197,10 @@ static int virtsnd_build_devs(struct virtio_snd *snd)
 	if (rc)
 		return rc;
 
+	rc = virtsnd_chmap_parse_cfg(snd);
+	if (rc)
+		return rc;
+
 	if (snd->njacks) {
 		rc = virtsnd_jack_build_devs(snd);
 		if (rc)
@@ -209,6 +213,12 @@ static int virtsnd_build_devs(struct virtio_snd *snd)
 			return rc;
 	}
 
+	if (snd->nchmaps) {
+		rc = virtsnd_chmap_build_devs(snd);
+		if (rc)
+			return rc;
+	}
+
 	return snd_card_register(snd->card);
 }
 
sound/virtio/virtio_card.h +8 -0
index 2092da297a9d..6af3d4816f57 100644
@@ -43,6 +43,8 @@ struct virtio_snd_queue {
  * @njacks: Number of jacks.
  * @substreams: VirtIO PCM substreams.
  * @nsubstreams: Number of PCM substreams.
+ * @chmaps: VirtIO channel maps.
+ * @nchmaps: Number of channel maps.
  */
 struct virtio_snd {
 	struct virtio_device *vdev;
@@ -55,6 +57,8 @@ struct virtio_snd {
 	u32 njacks;
 	struct virtio_pcm_substream *substreams;
 	u32 nsubstreams;
+	struct virtio_snd_chmap_info *chmaps;
+	u32 nchmaps;
 };
 
 /* Message completion timeout in milliseconds (module parameter). */
@@ -100,4 +104,8 @@ int virtsnd_jack_build_devs(struct virtio_snd *snd);
 void virtsnd_jack_event(struct virtio_snd *snd,
 			struct virtio_snd_event *event);
 
+int virtsnd_chmap_parse_cfg(struct virtio_snd *snd);
+
+int virtsnd_chmap_build_devs(struct virtio_snd *snd);
+
 #endif /* VIRTIO_SND_CARD_H */
sound/virtio/virtio_chmap.c new file +219 -0

219 lines not on this page · raw message

sound/virtio/virtio_pcm.h +4 -0
index 55dfb19d14b3..1e683aeadde9 100644
@@ -60,10 +60,14 @@ struct virtio_pcm_substream {
  * struct virtio_pcm_stream - VirtIO PCM stream.
  * @substreams: VirtIO substreams belonging to the stream.
  * @nsubstreams: Number of substreams.
+ * @chmaps: Kernel channel maps belonging to the stream.
+ * @nchmaps: Number of channel maps.
  */
 struct virtio_pcm_stream {
 	struct virtio_pcm_substream **substreams;
 	u32 nsubstreams;
+	struct snd_pcm_chmap_elem *chmaps;
+	u32 nchmaps;
 };
 
 /**
Signature · 7 lines
2.30.1



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
author maintainer
#22 lore raw

PATCH v6 9/9 ALSA: virtio: introduce device suspend/resume support

In reply to Anton Yakovlev

All running PCM substreams are stopped on device suspend and restarted on device resume.

---
 sound/virtio/virtio_card.c    | 56 +++++++++++++++++++++++++++++++++++
 sound/virtio/virtio_pcm.c     |  1 +
 sound/virtio/virtio_pcm_ops.c | 41 ++++++++++++++++++++-----
 3 files changed, 90 insertions(+), 8 deletions(-)
sound/virtio/virtio_card.c +56 -0
index 59455a562018..c7ae8801991d 100644
@@ -323,6 +323,58 @@ static void virtsnd_remove(struct virtio_device *vdev)
 	kfree(snd->event_msgs);
 }
 
+#ifdef CONFIG_PM_SLEEP
+/**
+ * virtsnd_freeze() - Suspend device.
+ * @vdev: VirtIO parent device.
+ *
+ * Context: Any context.
+ * Return: 0 on success, -errno on failure.
+ */
+static int virtsnd_freeze(struct virtio_device *vdev)
+{
+	struct virtio_snd *snd = vdev->priv;
+
+	virtsnd_ctl_msg_cancel_all(snd);
+
+	vdev->config->del_vqs(vdev);
+	vdev->config->reset(vdev);
+
+	kfree(snd->event_msgs);
+
+	/*
+	 * If the virtsnd_restore() fails before re-allocating events, then we
+	 * get a dangling pointer here.
+	 */
+	snd->event_msgs = NULL;
+
+	return 0;
+}
+
+/**
+ * virtsnd_restore() - Resume device.
+ * @vdev: VirtIO parent device.
+ *
+ * Context: Any context.
+ * Return: 0 on success, -errno on failure.
+ */
+static int virtsnd_restore(struct virtio_device *vdev)
+{
+	struct virtio_snd *snd = vdev->priv;
+	int rc;
+
+	rc = virtsnd_find_vqs(snd);
+	if (rc)
+		return rc;
+
+	virtio_device_ready(vdev);
+
+	virtsnd_enable_event_vq(snd);
+
+	return 0;
+}
+#endif /* CONFIG_PM_SLEEP */
+
 static const struct virtio_device_id id_table[] = {
 	{ VIRTIO_ID_SOUND, VIRTIO_DEV_ANY_ID },
 	{ 0 },
@@ -335,6 +387,10 @@ static struct virtio_driver virtsnd_driver = {
 	.validate = virtsnd_validate,
 	.probe = virtsnd_probe,
 	.remove = virtsnd_remove,
+#ifdef CONFIG_PM_SLEEP
+	.freeze = virtsnd_freeze,
+	.restore = virtsnd_restore,
+#endif
 };
 
 static int __init init(void)
sound/virtio/virtio_pcm.c +1 -0
index 3605151860f2..4a4a6583b002 100644
@@ -109,6 +109,7 @@ static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss,
 		SNDRV_PCM_INFO_BATCH |
 		SNDRV_PCM_INFO_BLOCK_TRANSFER |
 		SNDRV_PCM_INFO_INTERLEAVED |
+		SNDRV_PCM_INFO_RESUME |
 		SNDRV_PCM_INFO_PAUSE;
 
 	if (!info->channels_min || info->channels_min > info->channels_max) {
sound/virtio/virtio_pcm_ops.c +33 -8

78 lines not on this page · raw message

Signature · 7 lines
2.30.1


_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#23 lore raw

Re: [PATCH v6 9/9] ALSA: virtio: introduce device suspend/resume support

Quoted 35 lines · Anton Yakovlev

All running PCM substreams are stopped on device suspend and restarted on device resume.

---
 sound/virtio/virtio_card.c    | 56 +++++++++++++++++++++++++++++++++++
 sound/virtio/virtio_pcm.c     |  1 +
 sound/virtio/virtio_pcm_ops.c | 41 ++++++++++++++++++++-----
 3 files changed, 90 insertions(+), 8 deletions(-)
sound/virtio/virtio_card.c +16 -0
index 59455a562018..c7ae8801991d 100644
@@ -323,6 +323,58 @@ static void virtsnd_remove(struct virtio_device *vdev)
 	kfree(snd->event_msgs);
 }
 
+#ifdef CONFIG_PM_SLEEP
+/**
+ * virtsnd_freeze() - Suspend device.
+ * @vdev: VirtIO parent device.
+ *
+ * Context: Any context.
+ * Return: 0 on success, -errno on failure.
+ */
+static int virtsnd_freeze(struct virtio_device *vdev)
+{
+	struct virtio_snd *snd = vdev->priv;
+
+	virtsnd_ctl_msg_cancel_all(snd);
+
+	vdev->config->del_vqs(vdev);
+	vdev->config->reset(vdev);

sound/virtio/virtio_card.c static void virtsnd_remove(struct virtio_device *vdev) · line 351

Quoted 10 lines
sound/virtio/virtio_card.c +10 -0
+
+	kfree(snd->event_msgs);
+
+	/*
+	 * If the virtsnd_restore() fails before re-allocating events, then we
+	 * get a dangling pointer here.
+	 */
+	snd->event_msgs = NULL;
+
+	return 0;

I suppose some cancel of inflight works is needed? Ditto for the device removal, too.

sound/virtio/virtio_pcm.c static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss, · line 113

Quoted 8 lines
sound/virtio/virtio_pcm.c +1 -0
@@ -109,6 +109,7 @@ static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss,
 		SNDRV_PCM_INFO_BATCH |
 		SNDRV_PCM_INFO_BLOCK_TRANSFER |
 		SNDRV_PCM_INFO_INTERLEAVED |
+		SNDRV_PCM_INFO_RESUME |
 		SNDRV_PCM_INFO_PAUSE;

Actually you don't need to set SNDRV_PCM_INFO_RESUME. This flag means that the driver supports the full resume procedure, which isn't often the case; with this, the driver is supposed to resume the stream exactly from the suspended position.

Most drivers don't set this but implement only the suspend-stop action. Then the application (or the sound backend) will re-setup the stream and restart accordingly.

Quoted 6 lines
sound/virtio/virtio_pcm.c +2 -0
@@ -309,6 +318,21 @@ static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command)
 	int rc;
 
 	switch (command) {
+	case SNDRV_PCM_TRIGGER_RESUME: {
+		/*

sound/virtio/virtio_pcm.c static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command) · line 332

Quoted 10 lines
sound/virtio/virtio_pcm.c +10 -0
+		 * We restart the substream by executing the standard command
+		 * sequence.
+		 */
+		rc = virtsnd_pcm_hw_params(substream, NULL);
+		if (rc)
+			return rc;
+
+		rc = virtsnd_pcm_prepare(substream);
+		if (rc)
+			return rc;

And this won't work at all unless nonatomic PCM ops.

thanks,

Takashi

author maintainer
#24 lore raw

Re: [PATCH v6 9/9] ALSA: virtio: introduce device suspend/resume support

Quoted 42 lines · Takashi Iwai
Quoted 40 lines · Anton Yakovlev

All running PCM substreams are stopped on device suspend and restarted on device resume.

---
  sound/virtio/virtio_card.c    | 56 +++++++++++++++++++++++++++++++++++
  sound/virtio/virtio_pcm.c     |  1 +
  sound/virtio/virtio_pcm_ops.c | 41 ++++++++++++++++++++-----
  3 files changed, 90 insertions(+), 8 deletions(-)
sound/virtio/virtio_card.c +21 -0
index 59455a562018..c7ae8801991d 100644
@@ -323,6 +323,58 @@ static void virtsnd_remove(struct virtio_device *vdev)
       kfree(snd->event_msgs);
  }
 
+#ifdef CONFIG_PM_SLEEP
+/**
+ * virtsnd_freeze() - Suspend device.
+ * @vdev: VirtIO parent device.
+ *
+ * Context: Any context.
+ * Return: 0 on success, -errno on failure.
+ */
+static int virtsnd_freeze(struct virtio_device *vdev)
+{
+     struct virtio_snd *snd = vdev->priv;
+
+     virtsnd_ctl_msg_cancel_all(snd);
+
+     vdev->config->del_vqs(vdev);
+     vdev->config->reset(vdev);
+
+     kfree(snd->event_msgs);
+
+     /*
+      * If the virtsnd_restore() fails before re-allocating events, then we
Quoted 8 lines

sound/virtio/virtio_card.c static void virtsnd_remove(struct virtio_device *vdev) · line 351

Quoted 5 lines
sound/virtio/virtio_card.c +5 -0
+      * get a dangling pointer here.
+      */
+     snd->event_msgs = NULL;
+
+     return 0;

I suppose some cancel of inflight works is needed? Ditto for the device removal, too.

It's not necessary here, since the device is reset and all of this are happened automatically. But in the device remove it makes sense also to disable events before calling snd_card_free(), since the device is still able to send notifications at that moment. Thanks!

Quoted 5 lines
Quoted 5 lines
sound/virtio/virtio_pcm.c +0 -0
@@ -109,6 +109,7 @@ static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss,
               SNDRV_PCM_INFO_BATCH |
               SNDRV_PCM_INFO_BLOCK_TRANSFER |
Quoted 8 lines

sound/virtio/virtio_pcm.c static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss, · line 113

Quoted 3 lines
sound/virtio/virtio_pcm.c +1 -0
               SNDRV_PCM_INFO_INTERLEAVED |
+             SNDRV_PCM_INFO_RESUME |
               SNDRV_PCM_INFO_PAUSE;

Actually you don't need to set SNDRV_PCM_INFO_RESUME. This flag means that the driver supports the full resume procedure, which isn't often the case; with this, the driver is supposed to resume the stream exactly from the suspended position.

If I understood you right, that's exactly how resume is implemented now in the driver. Although we fully restart substream on the device side, from an application point of view it is resumed exactly at the same position.

Quoted 3 lines

Most drivers don't set this but implement only the suspend-stop action. Then the application (or the sound backend) will re-setup the stream and restart accordingly.

And an application must be aware of such possible situation? Since I have no doubt in alsa-lib, but I don't think that for example tinyalsa can handle this right.

Quoted 24 lines
Quoted 6 lines
sound/virtio/virtio_pcm.c +2 -0
@@ -309,6 +318,21 @@ static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command)
       int rc;
 
       switch (command) {
+     case SNDRV_PCM_TRIGGER_RESUME: {
+             /*

sound/virtio/virtio_pcm.c static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command) · line 332

Quoted 10 lines
sound/virtio/virtio_pcm.c +10 -0
+              * We restart the substream by executing the standard command
+              * sequence.
+              */
+             rc = virtsnd_pcm_hw_params(substream, NULL);
+             if (rc)
+                     return rc;
+
+             rc = virtsnd_pcm_prepare(substream);
+             if (rc)
+                     return rc;

And this won't work at all unless nonatomic PCM ops.

thanks,

Takashi

Signature · 10 lines
Anton Yakovlev
Senior Software Engineer

OpenSynergy GmbH
Rotherstr. 20, 10245 Berlin


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
#25 lore raw

Re: [PATCH v6 9/9] ALSA: virtio: introduce device suspend/resume support

Quoted 53 lines · Anton Yakovlev
Quoted 42 lines · Takashi Iwai
Quoted 40 lines · Anton Yakovlev

All running PCM substreams are stopped on device suspend and restarted on device resume.

---
  sound/virtio/virtio_card.c    | 56 +++++++++++++++++++++++++++++++++++
  sound/virtio/virtio_pcm.c     |  1 +
  sound/virtio/virtio_pcm_ops.c | 41 ++++++++++++++++++++-----
  3 files changed, 90 insertions(+), 8 deletions(-)
sound/virtio/virtio_card.c +21 -0
index 59455a562018..c7ae8801991d 100644
@@ -323,6 +323,58 @@ static void virtsnd_remove(struct virtio_device *vdev)
       kfree(snd->event_msgs);
  }
 
+#ifdef CONFIG_PM_SLEEP
+/**
+ * virtsnd_freeze() - Suspend device.
+ * @vdev: VirtIO parent device.
+ *
+ * Context: Any context.
+ * Return: 0 on success, -errno on failure.
+ */
+static int virtsnd_freeze(struct virtio_device *vdev)
+{
+     struct virtio_snd *snd = vdev->priv;
+
+     virtsnd_ctl_msg_cancel_all(snd);
+
+     vdev->config->del_vqs(vdev);
+     vdev->config->reset(vdev);
+
+     kfree(snd->event_msgs);
+
+     /*
+      * If the virtsnd_restore() fails before re-allocating events, then we
Quoted 8 lines

sound/virtio/virtio_card.c static void virtsnd_remove(struct virtio_device *vdev) · line 351

Quoted 5 lines
sound/virtio/virtio_card.c +5 -0
+      * get a dangling pointer here.
+      */
+     snd->event_msgs = NULL;
+
+     return 0;

I suppose some cancel of inflight works is needed? Ditto for the device removal, too.

Quoted 2 lines

It's not necessary here, since the device is reset and all of this are happened automatically.

Hrm, but the reset call itself might conflict with the inflight reset work? I haven't see any work canceling or flushing, so...

Quoted 25 lines

But in the device remove it makes sense also to
disable events before calling snd_card_free(), since the device is still able to send notifications at that moment. Thanks!

Quoted 5 lines
Quoted 5 lines
sound/virtio/virtio_pcm.c +0 -0
@@ -109,6 +109,7 @@ static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss,
               SNDRV_PCM_INFO_BATCH |
               SNDRV_PCM_INFO_BLOCK_TRANSFER |
Quoted 8 lines

sound/virtio/virtio_pcm.c static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss, · line 113

Quoted 3 lines
sound/virtio/virtio_pcm.c +1 -0
               SNDRV_PCM_INFO_INTERLEAVED |
+             SNDRV_PCM_INFO_RESUME |
               SNDRV_PCM_INFO_PAUSE;

Actually you don't need to set SNDRV_PCM_INFO_RESUME. This flag means that the driver supports the full resume procedure, which isn't often the case; with this, the driver is supposed to resume the stream exactly from the suspended position.

If I understood you right, that's exactly how resume is implemented now in the driver. Although we fully restart substream on the device side, from an application point of view it is resumed exactly at the same position.

Quoted 7 lines
Quoted 3 lines

Most drivers don't set this but implement only the suspend-stop action. Then the application (or the sound backend) will re-setup the stream and restart accordingly.

And an application must be aware of such possible situation? Since I have no doubt in alsa-lib, but I don't think that for example tinyalsa can handle this right.

Tiny ALSA should work, too. Actually there are only few drivers that have the full PCM resume. The majority of drivers are without the resume support (including a large one like HD-audio).

And, with the resume implementation, I'm worried by the style like:

Quoted 16 lines
Quoted 16 lines
Quoted 6 lines
sound/virtio/virtio_pcm.c +2 -0
@@ -309,6 +318,21 @@ static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command)
       int rc;
 
       switch (command) {
+     case SNDRV_PCM_TRIGGER_RESUME: {
+             /*

sound/virtio/virtio_pcm.c static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command) · line 332

Quoted 10 lines
sound/virtio/virtio_pcm.c +10 -0
+              * We restart the substream by executing the standard command
+              * sequence.
+              */
+             rc = virtsnd_pcm_hw_params(substream, NULL);
+             if (rc)
+                     return rc;
+
+             rc = virtsnd_pcm_prepare(substream);
+             if (rc)
+                     return rc;

... and this is rather what the core code should do, and it's exactly the same procedure that would be done without RESUME flag.

Takashi

author maintainer
#26 lore raw

Re: [PATCH v6 9/9] ALSA: virtio: introduce device suspend/resume support

Quoted 55 lines · Takashi Iwai
Quoted 53 lines · Anton Yakovlev
Quoted 50 lines · Takashi Iwai
Quoted 35 lines · Anton Yakovlev

All running PCM substreams are stopped on device suspend and restarted on device resume.

---
   sound/virtio/virtio_card.c    | 56 +++++++++++++++++++++++++++++++++++
   sound/virtio/virtio_pcm.c     |  1 +
   sound/virtio/virtio_pcm_ops.c | 41 ++++++++++++++++++++-----
   3 files changed, 90 insertions(+), 8 deletions(-)
sound/virtio/virtio_card.c +16 -0
index 59455a562018..c7ae8801991d 100644
@@ -323,6 +323,58 @@ static void virtsnd_remove(struct virtio_device *vdev)
        kfree(snd->event_msgs);
   }
 
+#ifdef CONFIG_PM_SLEEP
+/**
+ * virtsnd_freeze() - Suspend device.
+ * @vdev: VirtIO parent device.
+ *
+ * Context: Any context.
+ * Return: 0 on success, -errno on failure.
+ */
+static int virtsnd_freeze(struct virtio_device *vdev)
+{
+     struct virtio_snd *snd = vdev->priv;
+
+     virtsnd_ctl_msg_cancel_all(snd);
+
+     vdev->config->del_vqs(vdev);
+     vdev->config->reset(vdev);

sound/virtio/virtio_card.c static void virtsnd_remove(struct virtio_device *vdev) · line 351

Quoted 10 lines
sound/virtio/virtio_card.c +10 -0
+
+     kfree(snd->event_msgs);
+
+     /*
+      * If the virtsnd_restore() fails before re-allocating events, then we
+      * get a dangling pointer here.
+      */
+     snd->event_msgs = NULL;
+
+     return 0;

I suppose some cancel of inflight works is needed? Ditto for the device removal, too.

Quoted 5 lines
Quoted 2 lines

It's not necessary here, since the device is reset and all of this are happened automatically.

Hrm, but the reset call itself might conflict with the inflight reset work? I haven't see any work canceling or flushing, so...

There maybe the following:

1. Some pending control requests -> these are cancelled in the virtsnd_ctl_msg_cancel_all() call.

2. PCM messages -> these must not be cancelled, since they will be requeued by driver on resume (starting with suspended position).

3. Some pending events from the device. These will be lost. Yeah, I think we can process all pending events before destroying virtqueue.

Other that these, there are no other inflight works or so.

Quoted 29 lines
Quoted 29 lines

But in the device remove it makes sense also to
disable events before calling snd_card_free(), since the device is still able to send notifications at that moment. Thanks!

Quoted 5 lines
Quoted 5 lines
sound/virtio/virtio_pcm.c +0 -0
@@ -109,6 +109,7 @@ static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss,
                SNDRV_PCM_INFO_BATCH |
                SNDRV_PCM_INFO_BLOCK_TRANSFER |
Quoted 8 lines

sound/virtio/virtio_pcm.c static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss, · line 113

Quoted 3 lines
sound/virtio/virtio_pcm.c +1 -0
                SNDRV_PCM_INFO_INTERLEAVED |
+             SNDRV_PCM_INFO_RESUME |
                SNDRV_PCM_INFO_PAUSE;

Actually you don't need to set SNDRV_PCM_INFO_RESUME. This flag means that the driver supports the full resume procedure, which isn't often the case; with this, the driver is supposed to resume the stream exactly from the suspended position.

If I understood you right, that's exactly how resume is implemented now in the driver. Although we fully restart substream on the device side, from an application point of view it is resumed exactly at the same position.

Quoted 3 lines

Most drivers don't set this but implement only the suspend-stop action. Then the application (or the sound backend) will re-setup the stream and restart accordingly.

Quoted 7 lines
Quoted 3 lines

And an application must be aware of such possible situation? Since I have no doubt in alsa-lib, but I don't think that for example tinyalsa can handle this right.

Tiny ALSA should work, too. Actually there are only few drivers that have the full PCM resume. The majority of drivers are without the resume support (including a large one like HD-audio).

Then it's a great news! Since we can simplify code a lot.

Quoted 25 lines

And, with the resume implementation, I'm worried by the style like:

Quoted 16 lines
Quoted 16 lines
Quoted 6 lines
sound/virtio/virtio_pcm.c +2 -0
@@ -309,6 +318,21 @@ static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command)
        int rc;
 
        switch (command) {
+     case SNDRV_PCM_TRIGGER_RESUME: {
+             /*

sound/virtio/virtio_pcm.c static int virtsnd_pcm_trigger(struct snd_pcm_substream *substream, int command) · line 332

Quoted 10 lines
sound/virtio/virtio_pcm.c +10 -0
+              * We restart the substream by executing the standard command
+              * sequence.
+              */
+             rc = virtsnd_pcm_hw_params(substream, NULL);
+             if (rc)
+                     return rc;
+
+             rc = virtsnd_pcm_prepare(substream);
+             if (rc)
+                     return rc;

... and this is rather what the core code should do, and it's exactly the same procedure that would be done without RESUME flag.

Takashi

Signature · 10 lines
Anton Yakovlev
Senior Software Engineer

OpenSynergy GmbH
Rotherstr. 20, 10245 Berlin


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
author maintainer
#27 lore raw

Re: [PATCH v6 9/9] ALSA: virtio: introduce device suspend/resume support

In reply to Takashi Iwai
Quoted 3 lines · Takashi Iwai
Quoted 1 line · Anton Yakovlev

[snip]

Quoted 14 lines

sound/virtio/virtio_pcm.c static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss, · line 113

Quoted 8 lines
sound/virtio/virtio_pcm.c +1 -0
@@ -109,6 +109,7 @@ static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss,
               SNDRV_PCM_INFO_BATCH |
               SNDRV_PCM_INFO_BLOCK_TRANSFER |
               SNDRV_PCM_INFO_INTERLEAVED |
+             SNDRV_PCM_INFO_RESUME |
               SNDRV_PCM_INFO_PAUSE;

Actually you don't need to set SNDRV_PCM_INFO_RESUME. This flag means that the driver supports the full resume procedure, which isn't often the case; with this, the driver is supposed to resume the stream exactly from the suspended position.

Quoted 3 lines

Most drivers don't set this but implement only the suspend-stop action. Then the application (or the sound backend) will re-setup the stream and restart accordingly.

I tried to resume driver without SNDRV_PCM_INFO_RESUME, and alsa-lib called only ops->prepare(). It makes sense for a typical hw, but we have "clean" unconfigured device on resume. And we must set hw parameters as a first step. It means, that code should be more or less the same. And maybe it's better to set SNDRV_PCM_INFO_RESUME, since it allows us to resume substream in any situation (regardless of application behavior). I can refactor code to only send requests from trigger(RESUME) path and not to call ops itself. It should make code more straitforward. What do you say?

Signature · 10 lines
Anton Yakovlev
Senior Software Engineer

OpenSynergy GmbH
Rotherstr. 20, 10245 Berlin


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
#28 lore raw

Re: [PATCH v6 9/9] ALSA: virtio: introduce device suspend/resume support

Quoted 26 lines · Anton Yakovlev
Quoted 3 lines · Takashi Iwai
Quoted 1 line · Anton Yakovlev

[snip]

Quoted 14 lines

sound/virtio/virtio_pcm.c static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss, · line 113

Quoted 8 lines
sound/virtio/virtio_pcm.c +1 -0
@@ -109,6 +109,7 @@ static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss,
               SNDRV_PCM_INFO_BATCH |
               SNDRV_PCM_INFO_BLOCK_TRANSFER |
               SNDRV_PCM_INFO_INTERLEAVED |
+             SNDRV_PCM_INFO_RESUME |
               SNDRV_PCM_INFO_PAUSE;

Actually you don't need to set SNDRV_PCM_INFO_RESUME. This flag means that the driver supports the full resume procedure, which isn't often the case; with this, the driver is supposed to resume the stream exactly from the suspended position.

Quoted 3 lines

Most drivers don't set this but implement only the suspend-stop action. Then the application (or the sound backend) will re-setup the stream and restart accordingly.

Quoted 9 lines

I tried to resume driver without SNDRV_PCM_INFO_RESUME, and alsa-lib called only ops->prepare(). It makes sense for a typical hw, but we have "clean" unconfigured device on resume. And we must set hw parameters as a first step. It means, that code should be more or less the same. And maybe it's better to set SNDRV_PCM_INFO_RESUME, since it allows us to resume substream in any situation (regardless of application behavior). I can refactor code to only send requests from trigger(RESUME) path and not to call ops itself. It should make code more straitforward. What do you say?

How about calling hw_params(NULL) conditionally in the prepare? Doing the full stack work in the trigger callback is bad from the API design POV; in general the trigger callback is supposed to be as short as possible.

Takashi

author maintainer
#29 lore raw

Re: [PATCH v6 9/9] ALSA: virtio: introduce device suspend/resume support

Quoted 38 lines · Takashi Iwai
Quoted 26 lines · Anton Yakovlev
Quoted 3 lines · Takashi Iwai
Quoted 1 line · Anton Yakovlev

[snip]

Quoted 14 lines

sound/virtio/virtio_pcm.c static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss, · line 113

Quoted 8 lines
sound/virtio/virtio_pcm.c +1 -0
@@ -109,6 +109,7 @@ static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss,
                SNDRV_PCM_INFO_BATCH |
                SNDRV_PCM_INFO_BLOCK_TRANSFER |
                SNDRV_PCM_INFO_INTERLEAVED |
+             SNDRV_PCM_INFO_RESUME |
                SNDRV_PCM_INFO_PAUSE;

Actually you don't need to set SNDRV_PCM_INFO_RESUME. This flag means that the driver supports the full resume procedure, which isn't often the case; with this, the driver is supposed to resume the stream exactly from the suspended position.

Quoted 3 lines

Most drivers don't set this but implement only the suspend-stop action. Then the application (or the sound backend) will re-setup the stream and restart accordingly.

Quoted 9 lines

I tried to resume driver without SNDRV_PCM_INFO_RESUME, and alsa-lib called only ops->prepare(). It makes sense for a typical hw, but we have "clean" unconfigured device on resume. And we must set hw parameters as a first step. It means, that code should be more or less the same. And maybe it's better to set SNDRV_PCM_INFO_RESUME, since it allows us to resume substream in any situation (regardless of application behavior). I can refactor code to only send requests from trigger(RESUME) path and not to call ops itself. It should make code more straitforward. What do you say?

Quoted 1 line

How about calling hw_params(NULL) conditionally in the prepare?

Then the question is that condition. When ops->prepare() is called, the substream is in SUSPENDED state or not? If not then we need to track this in some additional field (and it will make logic a little bit clumsy, since that field is needed to be carefully handled in other places).

Quoted 3 lines

Doing the full stack work in the trigger callback is bad from the API design POV; in general the trigger callback is supposed to be as short as possible.

Yeah, but usually original subsystem design does not take into account para-virtualized devices, which usually have it's own slightly different reality. And we need to introduce some tricks.

Quoted 3 lines

Takashi

Signature · 11 lines
Anton Yakovlev
Senior Software Engineer

OpenSynergy GmbH
Rotherstr. 20, 10245 Berlin



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
#30 lore raw

Re: [PATCH v6 9/9] ALSA: virtio: introduce device suspend/resume support

Quoted 40 lines · Anton Yakovlev
Quoted 38 lines · Takashi Iwai
Quoted 35 lines · Anton Yakovlev
Quoted 3 lines · Takashi Iwai
Quoted 1 line · Anton Yakovlev

[snip]

Quoted 14 lines

sound/virtio/virtio_pcm.c static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss, · line 113

Quoted 8 lines
sound/virtio/virtio_pcm.c +1 -0
@@ -109,6 +109,7 @@ static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss,
                SNDRV_PCM_INFO_BATCH |
                SNDRV_PCM_INFO_BLOCK_TRANSFER |
                SNDRV_PCM_INFO_INTERLEAVED |
+             SNDRV_PCM_INFO_RESUME |
                SNDRV_PCM_INFO_PAUSE;

Actually you don't need to set SNDRV_PCM_INFO_RESUME. This flag means that the driver supports the full resume procedure, which isn't often the case; with this, the driver is supposed to resume the stream exactly from the suspended position.

Quoted 3 lines

Most drivers don't set this but implement only the suspend-stop action. Then the application (or the sound backend) will re-setup the stream and restart accordingly.

I tried to resume driver without SNDRV_PCM_INFO_RESUME, and alsa-lib called only ops->prepare(). It makes sense for a typical hw, but we have "clean" unconfigured device on resume. And we must set hw parameters as a first step. It means, that code should be more or less the same. And maybe it's better to set SNDRV_PCM_INFO_RESUME, since it allows us to resume substream in any situation (regardless of application behavior). I can refactor code to only send requests from trigger(RESUME) path and not to call ops itself. It should make code more straitforward. What do you say?

Quoted 7 lines
Quoted 1 line

How about calling hw_params(NULL) conditionally in the prepare?

Then the question is that condition. When ops->prepare() is called, the substream is in SUSPENDED state or not? If not then we need to track this in some additional field (and it will make logic a little bit clumsy, since that field is needed to be carefully handled in other places).

Yes, you'd need to have a suspend/resume PM callback in the driver that flips the internal flag to invalidate the hw_parmas, and in the prepare callback, just call hw_params(NULL) if that flag is set.

Quoted 7 lines
Quoted 3 lines

Doing the full stack work in the trigger callback is bad from the API design POV; in general the trigger callback is supposed to be as short as possible.

Yeah, but usually original subsystem design does not take into account para-virtualized devices, which usually have it's own slightly different reality. And we need to introduce some tricks.

The hardware drivers do a lot of more things in either suspend/resume PM callbacks or prepare callback for re-setup of the hardware. We can follow the similar pattern. Heavy-lifting works in the trigger callbacks is really something to avoid.

Takashi

author maintainer
#31 lore raw

Re: [PATCH v6 9/9] ALSA: virtio: introduce device suspend/resume support

Quoted 58 lines · Takashi Iwai
Quoted 40 lines · Anton Yakovlev
Quoted 38 lines · Takashi Iwai
Quoted 35 lines · Anton Yakovlev
Quoted 3 lines · Takashi Iwai
Quoted 1 line · Anton Yakovlev

[snip]

Quoted 14 lines

sound/virtio/virtio_pcm.c static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss, · line 113

Quoted 8 lines
sound/virtio/virtio_pcm.c +1 -0
@@ -109,6 +109,7 @@ static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss,
                 SNDRV_PCM_INFO_BATCH |
                 SNDRV_PCM_INFO_BLOCK_TRANSFER |
                 SNDRV_PCM_INFO_INTERLEAVED |
+             SNDRV_PCM_INFO_RESUME |
                 SNDRV_PCM_INFO_PAUSE;

Actually you don't need to set SNDRV_PCM_INFO_RESUME. This flag means that the driver supports the full resume procedure, which isn't often the case; with this, the driver is supposed to resume the stream exactly from the suspended position.

Quoted 3 lines

Most drivers don't set this but implement only the suspend-stop action. Then the application (or the sound backend) will re-setup the stream and restart accordingly.

I tried to resume driver without SNDRV_PCM_INFO_RESUME, and alsa-lib called only ops->prepare(). It makes sense for a typical hw, but we have "clean" unconfigured device on resume. And we must set hw parameters as a first step. It means, that code should be more or less the same. And maybe it's better to set SNDRV_PCM_INFO_RESUME, since it allows us to resume substream in any situation (regardless of application behavior). I can refactor code to only send requests from trigger(RESUME) path and not to call ops itself. It should make code more straitforward. What do you say?

Quoted 7 lines
Quoted 1 line

How about calling hw_params(NULL) conditionally in the prepare?

Then the question is that condition. When ops->prepare() is called, the substream is in SUSPENDED state or not? If not then we need to track this in some additional field (and it will make logic a little bit clumsy, since that field is needed to be carefully handled in other places).

Yes, you'd need to have a suspend/resume PM callback in the driver that flips the internal flag to invalidate the hw_parmas, and in the prepare callback, just call hw_params(NULL) if that flag is set.

Quoted 4 lines
Quoted 3 lines

Doing the full stack work in the trigger callback is bad from the API design POV; in general the trigger callback is supposed to be as short as possible.

Quoted 8 lines
Quoted 3 lines

Yeah, but usually original subsystem design does not take into account para-virtualized devices, which usually have it's own slightly different reality. And we need to introduce some tricks.

The hardware drivers do a lot of more things in either suspend/resume PM callbacks or prepare callback for re-setup of the hardware. We can follow the similar pattern. Heavy-lifting works in the trigger callbacks is really something to avoid.

Ok, I redone this part and now the driver sets parameters for the device in ops->prepare() if the substream was suspended. And everything works fine. Thanks! I will send a new patch set soon.

Quoted 2 lines

Takashi

Signature · 10 lines
Anton Yakovlev
Senior Software Engineer

OpenSynergy GmbH
Rotherstr. 20, 10245 Berlin


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]