1 From e60175c8c7a51861c6f31af4cf99b95f3da7a59f Mon Sep 17 00:00:00 2001
2 From: Anton Yakovlev <anton.yakovlev@opensynergy.com>
3 Date: Tue, 2 Mar 2021 17:47:05 +0100
4 Subject: [PATCH] ALSA: virtio: handling control and I/O messages for the PCM
7 The driver implements a message-based transport for I/O substream
8 operations. Before the start of the substream, the hardware buffer is
9 sliced into I/O messages, the number of which is equal to the current
10 number of periods. The size of each message is equal to the current
13 I/O messages are organized in an ordered queue. The completion of the
14 I/O message indicates an elapsed period (the only exception is the end
15 of the stream for the capture substream). Upon completion, the message
16 is automatically re-added to the end of the queue.
18 Signed-off-by: Anton Yakovlev <anton.yakovlev@opensynergy.com>
19 Link: https://lore.kernel.org/r/20210302164709.3142702-6-anton.yakovlev@opensynergy.com
20 Signed-off-by: Takashi Iwai <tiwai@suse.de>
21 Signed-off-by: Vasyl Vavrychuk <vasyl.vavrychuk@opensynergy.com>
23 sound/virtio/Makefile | 3 +-
24 sound/virtio/virtio_card.c | 22 +-
25 sound/virtio/virtio_card.h | 9 +
26 sound/virtio/virtio_pcm.c | 32 +++
27 sound/virtio/virtio_pcm.h | 40 ++++
28 sound/virtio/virtio_pcm_msg.c | 414 ++++++++++++++++++++++++++++++++++
29 6 files changed, 515 insertions(+), 5 deletions(-)
30 create mode 100644 sound/virtio/virtio_pcm_msg.c
32 diff --git a/sound/virtio/Makefile b/sound/virtio/Makefile
33 index 69162a545a41..626af3cc3ed7 100644
34 --- a/sound/virtio/Makefile
35 +++ b/sound/virtio/Makefile
36 @@ -5,5 +5,6 @@ obj-$(CONFIG_SND_VIRTIO) += virtio_snd.o
44 diff --git a/sound/virtio/virtio_card.c b/sound/virtio/virtio_card.c
45 index 11c76ee311b7..57b9b7f3a9c0 100644
46 --- a/sound/virtio/virtio_card.c
47 +++ b/sound/virtio/virtio_card.c
48 @@ -55,6 +55,12 @@ static void virtsnd_event_send(struct virtqueue *vqueue,
49 static void virtsnd_event_dispatch(struct virtio_snd *snd,
50 struct virtio_snd_event *event)
52 + switch (le32_to_cpu(event->hdr.code)) {
53 + case VIRTIO_SND_EVT_PCM_PERIOD_ELAPSED:
54 + case VIRTIO_SND_EVT_PCM_XRUN:
55 + virtsnd_pcm_event(snd, event);
61 @@ -101,11 +107,15 @@ static int virtsnd_find_vqs(struct virtio_snd *snd)
62 struct virtio_device *vdev = snd->vdev;
63 static vq_callback_t *callbacks[VIRTIO_SND_VQ_MAX] = {
64 [VIRTIO_SND_VQ_CONTROL] = virtsnd_ctl_notify_cb,
65 - [VIRTIO_SND_VQ_EVENT] = virtsnd_event_notify_cb
66 + [VIRTIO_SND_VQ_EVENT] = virtsnd_event_notify_cb,
67 + [VIRTIO_SND_VQ_TX] = virtsnd_pcm_tx_notify_cb,
68 + [VIRTIO_SND_VQ_RX] = virtsnd_pcm_rx_notify_cb
70 static const char *names[VIRTIO_SND_VQ_MAX] = {
71 [VIRTIO_SND_VQ_CONTROL] = "virtsnd-ctl",
72 - [VIRTIO_SND_VQ_EVENT] = "virtsnd-event"
73 + [VIRTIO_SND_VQ_EVENT] = "virtsnd-event",
74 + [VIRTIO_SND_VQ_TX] = "virtsnd-tx",
75 + [VIRTIO_SND_VQ_RX] = "virtsnd-rx"
77 struct virtqueue *vqs[VIRTIO_SND_VQ_MAX] = { 0 };
79 @@ -318,8 +328,12 @@ static void virtsnd_remove(struct virtio_device *vdev)
80 vdev->config->del_vqs(vdev);
81 vdev->config->reset(vdev);
83 - for (i = 0; snd->substreams && i < snd->nsubstreams; ++i)
84 - cancel_work_sync(&snd->substreams[i].elapsed_period);
85 + for (i = 0; snd->substreams && i < snd->nsubstreams; ++i) {
86 + struct virtio_pcm_substream *vss = &snd->substreams[i];
88 + cancel_work_sync(&vss->elapsed_period);
89 + virtsnd_pcm_msg_free(vss);
92 kfree(snd->event_msgs);
94 diff --git a/sound/virtio/virtio_card.h b/sound/virtio/virtio_card.h
95 index 77a1b7255370..c43f9744d362 100644
96 --- a/sound/virtio/virtio_card.h
97 +++ b/sound/virtio/virtio_card.h
98 @@ -79,4 +79,13 @@ virtsnd_rx_queue(struct virtio_snd *snd)
99 return &snd->queues[VIRTIO_SND_VQ_RX];
102 +static inline struct virtio_snd_queue *
103 +virtsnd_pcm_queue(struct virtio_pcm_substream *vss)
105 + if (vss->direction == SNDRV_PCM_STREAM_PLAYBACK)
106 + return virtsnd_tx_queue(vss->snd);
108 + return virtsnd_rx_queue(vss->snd);
111 #endif /* VIRTIO_SND_CARD_H */
112 diff --git a/sound/virtio/virtio_pcm.c b/sound/virtio/virtio_pcm.c
113 index e16567e2e214..2dcd763efa29 100644
114 --- a/sound/virtio/virtio_pcm.c
115 +++ b/sound/virtio/virtio_pcm.c
116 @@ -353,6 +353,8 @@ int virtsnd_pcm_parse_cfg(struct virtio_snd *snd)
119 INIT_WORK(&vss->elapsed_period, virtsnd_pcm_period_elapsed);
120 + init_waitqueue_head(&vss->msg_empty);
121 + spin_lock_init(&vss->lock);
123 rc = virtsnd_pcm_build_hw(vss, &info[i]);
125 @@ -477,3 +479,33 @@ int virtsnd_pcm_build_devs(struct virtio_snd *snd)
131 + * virtsnd_pcm_event() - Handle the PCM device event notification.
132 + * @snd: VirtIO sound device.
133 + * @event: VirtIO sound event.
135 + * Context: Interrupt context.
137 +void virtsnd_pcm_event(struct virtio_snd *snd, struct virtio_snd_event *event)
139 + struct virtio_pcm_substream *vss;
140 + u32 sid = le32_to_cpu(event->data);
142 + if (sid >= snd->nsubstreams)
145 + vss = &snd->substreams[sid];
147 + switch (le32_to_cpu(event->hdr.code)) {
148 + case VIRTIO_SND_EVT_PCM_PERIOD_ELAPSED:
149 + /* TODO: deal with shmem elapsed period */
151 + case VIRTIO_SND_EVT_PCM_XRUN:
152 + spin_lock(&vss->lock);
153 + if (vss->xfer_enabled)
154 + vss->xfer_xrun = true;
155 + spin_unlock(&vss->lock);
159 diff --git a/sound/virtio/virtio_pcm.h b/sound/virtio/virtio_pcm.h
160 index 84f2f3f14f48..6722f1139666 100644
161 --- a/sound/virtio/virtio_pcm.h
162 +++ b/sound/virtio/virtio_pcm.h
163 @@ -23,6 +23,17 @@ struct virtio_pcm_msg;
164 * @substream: Kernel ALSA substream.
165 * @hw: Kernel ALSA substream hardware descriptor.
166 * @elapsed_period: Kernel work to handle the elapsed period state.
167 + * @lock: Spinlock that protects fields shared by interrupt handlers and
168 + * substream operators.
169 + * @buffer_bytes: Current buffer size in bytes.
170 + * @hw_ptr: Substream hardware pointer value in bytes [0 ... buffer_bytes).
171 + * @xfer_enabled: Data transfer state (0 - off, 1 - on).
172 + * @xfer_xrun: Data underflow/overflow state (0 - no xrun, 1 - xrun).
173 + * @msgs: Allocated I/O messages.
174 + * @nmsgs: Number of allocated I/O messages.
175 + * @msg_last_enqueued: Index of the last I/O message added to the virtqueue.
176 + * @msg_count: Number of pending I/O messages in the virtqueue.
177 + * @msg_empty: Notify when msg_count is zero.
179 struct virtio_pcm_substream {
180 struct virtio_snd *snd;
181 @@ -33,6 +44,16 @@ struct virtio_pcm_substream {
182 struct snd_pcm_substream *substream;
183 struct snd_pcm_hardware hw;
184 struct work_struct elapsed_period;
186 + size_t buffer_bytes;
190 + struct virtio_pcm_msg **msgs;
191 + unsigned int nmsgs;
192 + int msg_last_enqueued;
193 + unsigned int msg_count;
194 + wait_queue_head_t msg_empty;
198 @@ -65,8 +86,27 @@ int virtsnd_pcm_parse_cfg(struct virtio_snd *snd);
200 int virtsnd_pcm_build_devs(struct virtio_snd *snd);
202 +void virtsnd_pcm_event(struct virtio_snd *snd, struct virtio_snd_event *event);
204 +void virtsnd_pcm_tx_notify_cb(struct virtqueue *vqueue);
206 +void virtsnd_pcm_rx_notify_cb(struct virtqueue *vqueue);
208 struct virtio_pcm *virtsnd_pcm_find(struct virtio_snd *snd, u32 nid);
210 struct virtio_pcm *virtsnd_pcm_find_or_create(struct virtio_snd *snd, u32 nid);
212 +struct virtio_snd_msg *
213 +virtsnd_pcm_ctl_msg_alloc(struct virtio_pcm_substream *vss,
214 + unsigned int command, gfp_t gfp);
216 +int virtsnd_pcm_msg_alloc(struct virtio_pcm_substream *vss,
217 + unsigned int periods, unsigned int period_bytes);
219 +void virtsnd_pcm_msg_free(struct virtio_pcm_substream *vss);
221 +int virtsnd_pcm_msg_send(struct virtio_pcm_substream *vss);
223 +unsigned int virtsnd_pcm_msg_pending_num(struct virtio_pcm_substream *vss);
225 #endif /* VIRTIO_SND_PCM_H */
226 diff --git a/sound/virtio/virtio_pcm_msg.c b/sound/virtio/virtio_pcm_msg.c
228 index 000000000000..f88c8f29cbd8
230 +++ b/sound/virtio/virtio_pcm_msg.c
232 +// SPDX-License-Identifier: GPL-2.0+
234 + * virtio-snd: Virtio sound device
235 + * Copyright (C) 2021 OpenSynergy GmbH
237 +#include <sound/pcm_params.h>
239 +#include "virtio_card.h"
242 + * struct virtio_pcm_msg - VirtIO I/O message.
243 + * @substream: VirtIO PCM substream.
244 + * @xfer: Request header payload.
245 + * @status: Response header payload.
246 + * @length: Data length in bytes.
247 + * @sgs: Payload scatter-gather table.
249 +struct virtio_pcm_msg {
250 + struct virtio_pcm_substream *substream;
251 + struct virtio_snd_pcm_xfer xfer;
252 + struct virtio_snd_pcm_status status;
254 + struct scatterlist sgs[0];
258 + * enum pcm_msg_sg_index - Index values for the virtio_pcm_msg->sgs field in
260 + * @PCM_MSG_SG_XFER: Element containing a virtio_snd_pcm_xfer structure.
261 + * @PCM_MSG_SG_STATUS: Element containing a virtio_snd_pcm_status structure.
262 + * @PCM_MSG_SG_DATA: The first element containing a data buffer.
264 +enum pcm_msg_sg_index {
265 + PCM_MSG_SG_XFER = 0,
271 + * virtsnd_pcm_sg_num() - Count the number of sg-elements required to represent
272 + * vmalloc'ed buffer.
273 + * @data: Pointer to vmalloc'ed buffer.
274 + * @length: Buffer size.
276 + * Context: Any context.
277 + * Return: Number of physically contiguous parts in the @data.
279 +static int virtsnd_pcm_sg_num(u8 *data, unsigned int length)
281 + phys_addr_t sg_address;
282 + unsigned int sg_length;
286 + struct page *pg = vmalloc_to_page(data);
287 + phys_addr_t pg_address = page_to_phys(pg);
290 + pg_length = PAGE_SIZE - offset_in_page(data);
291 + if (pg_length > length)
292 + pg_length = length;
294 + if (!num || sg_address + sg_length != pg_address) {
295 + sg_address = pg_address;
296 + sg_length = pg_length;
299 + sg_length += pg_length;
303 + length -= pg_length;
310 + * virtsnd_pcm_sg_from() - Build sg-list from vmalloc'ed buffer.
311 + * @sgs: Preallocated sg-list to populate.
312 + * @nsgs: The maximum number of elements in the @sgs.
313 + * @data: Pointer to vmalloc'ed buffer.
314 + * @length: Buffer size.
316 + * Splits the buffer into physically contiguous parts and makes an sg-list of
319 + * Context: Any context.
321 +static void virtsnd_pcm_sg_from(struct scatterlist *sgs, int nsgs, u8 *data,
322 + unsigned int length)
327 + struct page *pg = vmalloc_to_page(data);
330 + pg_length = PAGE_SIZE - offset_in_page(data);
331 + if (pg_length > length)
332 + pg_length = length;
335 + sg_phys(&sgs[idx]) + sgs[idx].length != page_to_phys(pg)) {
336 + if (idx + 1 == nsgs)
338 + sg_set_page(&sgs[++idx], pg, pg_length,
339 + offset_in_page(data));
341 + sgs[idx].length += pg_length;
345 + length -= pg_length;
348 + sg_mark_end(&sgs[idx]);
352 + * virtsnd_pcm_msg_alloc() - Allocate I/O messages.
353 + * @vss: VirtIO PCM substream.
354 + * @periods: Current number of periods.
355 + * @period_bytes: Current period size in bytes.
357 + * The function slices the buffer into @periods parts (each with the size of
358 + * @period_bytes), and creates @periods corresponding I/O messages.
360 + * Context: Any context that permits to sleep.
361 + * Return: 0 on success, -ENOMEM on failure.
363 +int virtsnd_pcm_msg_alloc(struct virtio_pcm_substream *vss,
364 + unsigned int periods, unsigned int period_bytes)
366 + struct snd_pcm_runtime *runtime = vss->substream->runtime;
369 + vss->msgs = kcalloc(periods, sizeof(*vss->msgs), GFP_KERNEL);
373 + vss->nmsgs = periods;
375 + for (i = 0; i < periods; ++i) {
376 + u8 *data = runtime->dma_area + period_bytes * i;
377 + int sg_num = virtsnd_pcm_sg_num(data, period_bytes);
378 + struct virtio_pcm_msg *msg;
380 + msg = kzalloc(sizeof(*msg) + sizeof(*msg->sgs) * (sg_num + 2),
385 + msg->substream = vss;
386 + sg_init_one(&msg->sgs[PCM_MSG_SG_XFER], &msg->xfer,
387 + sizeof(msg->xfer));
388 + sg_init_one(&msg->sgs[PCM_MSG_SG_STATUS], &msg->status,
389 + sizeof(msg->status));
390 + msg->length = period_bytes;
391 + virtsnd_pcm_sg_from(&msg->sgs[PCM_MSG_SG_DATA], sg_num, data,
394 + vss->msgs[i] = msg;
401 + * virtsnd_pcm_msg_free() - Free all allocated I/O messages.
402 + * @vss: VirtIO PCM substream.
404 + * Context: Any context.
406 +void virtsnd_pcm_msg_free(struct virtio_pcm_substream *vss)
410 + for (i = 0; vss->msgs && i < vss->nmsgs; ++i)
411 + kfree(vss->msgs[i]);
419 + * virtsnd_pcm_msg_send() - Send asynchronous I/O messages.
420 + * @vss: VirtIO PCM substream.
422 + * All messages are organized in an ordered circular list. Each time the
423 + * function is called, all currently non-enqueued messages are added to the
424 + * virtqueue. For this, the function keeps track of two values:
426 + * msg_last_enqueued = index of the last enqueued message,
427 + * msg_count = # of pending messages in the virtqueue.
429 + * Context: Any context. Expects the tx/rx queue and the VirtIO substream
430 + * spinlocks to be held by caller.
431 + * Return: 0 on success, -errno on failure.
433 +int virtsnd_pcm_msg_send(struct virtio_pcm_substream *vss)
435 + struct snd_pcm_runtime *runtime = vss->substream->runtime;
436 + struct virtio_snd *snd = vss->snd;
437 + struct virtio_device *vdev = snd->vdev;
438 + struct virtqueue *vqueue = virtsnd_pcm_queue(vss)->vqueue;
441 + bool notify = false;
443 + i = (vss->msg_last_enqueued + 1) % runtime->periods;
444 + n = runtime->periods - vss->msg_count;
446 + for (; n; --n, i = (i + 1) % runtime->periods) {
447 + struct virtio_pcm_msg *msg = vss->msgs[i];
448 + struct scatterlist *psgs[] = {
449 + &msg->sgs[PCM_MSG_SG_XFER],
450 + &msg->sgs[PCM_MSG_SG_DATA],
451 + &msg->sgs[PCM_MSG_SG_STATUS]
455 + msg->xfer.stream_id = cpu_to_le32(vss->sid);
456 + memset(&msg->status, 0, sizeof(msg->status));
458 + if (vss->direction == SNDRV_PCM_STREAM_PLAYBACK)
459 + rc = virtqueue_add_sgs(vqueue, psgs, 2, 1, msg,
462 + rc = virtqueue_add_sgs(vqueue, psgs, 1, 2, msg,
466 + dev_err(&vdev->dev,
467 + "SID %u: failed to send I/O message\n",
472 + vss->msg_last_enqueued = i;
476 + if (!(vss->features & (1U << VIRTIO_SND_PCM_F_MSG_POLLING)))
477 + notify = virtqueue_kick_prepare(vqueue);
480 + virtqueue_notify(vqueue);
486 + * virtsnd_pcm_msg_pending_num() - Returns the number of pending I/O messages.
487 + * @vss: VirtIO substream.
489 + * Context: Any context.
490 + * Return: Number of messages.
492 +unsigned int virtsnd_pcm_msg_pending_num(struct virtio_pcm_substream *vss)
495 + unsigned long flags;
497 + spin_lock_irqsave(&vss->lock, flags);
498 + num = vss->msg_count;
499 + spin_unlock_irqrestore(&vss->lock, flags);
505 + * virtsnd_pcm_msg_complete() - Complete an I/O message.
506 + * @msg: I/O message.
507 + * @written_bytes: Number of bytes written to the message.
509 + * Completion of the message means the elapsed period. If transmission is
510 + * allowed, then each completed message is immediately placed back at the end
513 + * For the playback substream, @written_bytes is equal to sizeof(msg->status).
515 + * For the capture substream, @written_bytes is equal to sizeof(msg->status)
516 + * plus the number of captured bytes.
518 + * Context: Interrupt context. Takes and releases the VirtIO substream spinlock.
520 +static void virtsnd_pcm_msg_complete(struct virtio_pcm_msg *msg,
521 + size_t written_bytes)
523 + struct virtio_pcm_substream *vss = msg->substream;
526 + * hw_ptr always indicates the buffer position of the first I/O message
527 + * in the virtqueue. Therefore, on each completion of an I/O message,
528 + * the hw_ptr value is unconditionally advanced.
530 + spin_lock(&vss->lock);
532 + * If the capture substream returned an incorrect status, then just
533 + * increase the hw_ptr by the message size.
535 + if (vss->direction == SNDRV_PCM_STREAM_PLAYBACK ||
536 + written_bytes <= sizeof(msg->status))
537 + vss->hw_ptr += msg->length;
539 + vss->hw_ptr += written_bytes - sizeof(msg->status);
541 + if (vss->hw_ptr >= vss->buffer_bytes)
542 + vss->hw_ptr -= vss->buffer_bytes;
544 + vss->xfer_xrun = false;
547 + if (vss->xfer_enabled) {
548 + struct snd_pcm_runtime *runtime = vss->substream->runtime;
551 + bytes_to_frames(runtime,
552 + le32_to_cpu(msg->status.latency_bytes));
554 + schedule_work(&vss->elapsed_period);
556 + virtsnd_pcm_msg_send(vss);
557 + } else if (!vss->msg_count) {
558 + wake_up_all(&vss->msg_empty);
560 + spin_unlock(&vss->lock);
564 + * virtsnd_pcm_notify_cb() - Process all completed I/O messages.
565 + * @queue: Underlying tx/rx virtqueue.
567 + * Context: Interrupt context. Takes and releases the tx/rx queue spinlock.
569 +static inline void virtsnd_pcm_notify_cb(struct virtio_snd_queue *queue)
571 + struct virtio_pcm_msg *msg;
573 + unsigned long flags;
575 + spin_lock_irqsave(&queue->lock, flags);
577 + virtqueue_disable_cb(queue->vqueue);
578 + while ((msg = virtqueue_get_buf(queue->vqueue, &written_bytes)))
579 + virtsnd_pcm_msg_complete(msg, written_bytes);
580 + if (unlikely(virtqueue_is_broken(queue->vqueue)))
582 + } while (!virtqueue_enable_cb(queue->vqueue));
583 + spin_unlock_irqrestore(&queue->lock, flags);
587 + * virtsnd_pcm_tx_notify_cb() - Process all completed TX messages.
588 + * @vqueue: Underlying tx virtqueue.
590 + * Context: Interrupt context.
592 +void virtsnd_pcm_tx_notify_cb(struct virtqueue *vqueue)
594 + struct virtio_snd *snd = vqueue->vdev->priv;
596 + virtsnd_pcm_notify_cb(virtsnd_tx_queue(snd));
600 + * virtsnd_pcm_rx_notify_cb() - Process all completed RX messages.
601 + * @vqueue: Underlying rx virtqueue.
603 + * Context: Interrupt context.
605 +void virtsnd_pcm_rx_notify_cb(struct virtqueue *vqueue)
607 + struct virtio_snd *snd = vqueue->vdev->priv;
609 + virtsnd_pcm_notify_cb(virtsnd_rx_queue(snd));
613 + * virtsnd_pcm_ctl_msg_alloc() - Allocate and initialize the PCM device control
614 + * message for the specified substream.
615 + * @vss: VirtIO PCM substream.
616 + * @command: Control request code (VIRTIO_SND_R_PCM_XXX).
617 + * @gfp: Kernel flags for memory allocation.
619 + * Context: Any context. May sleep if @gfp flags permit.
620 + * Return: Allocated message on success, NULL on failure.
622 +struct virtio_snd_msg *
623 +virtsnd_pcm_ctl_msg_alloc(struct virtio_pcm_substream *vss,
624 + unsigned int command, gfp_t gfp)
626 + size_t request_size = sizeof(struct virtio_snd_pcm_hdr);
627 + size_t response_size = sizeof(struct virtio_snd_hdr);
628 + struct virtio_snd_msg *msg;
631 + case VIRTIO_SND_R_PCM_SET_PARAMS:
632 + request_size = sizeof(struct virtio_snd_pcm_set_params);
636 + msg = virtsnd_ctl_msg_alloc(request_size, response_size, gfp);
638 + struct virtio_snd_pcm_hdr *hdr = virtsnd_ctl_msg_request(msg);
640 + hdr->hdr.code = cpu_to_le32(command);
641 + hdr->stream_id = cpu_to_le32(vss->sid);