From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2BC9EC433E6 for ; Sun, 24 Jan 2021 16:57:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E9FEA22CA2 for ; Sun, 24 Jan 2021 16:57:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726356AbhAXQ5O (ORCPT ); Sun, 24 Jan 2021 11:57:14 -0500 Received: from mx1.opensynergy.com ([217.66.60.4]:19156 "EHLO mx1.opensynergy.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725798AbhAXQz3 (ORCPT ); Sun, 24 Jan 2021 11:55:29 -0500 Received: from SR-MAILGATE-02.opensynergy.com (localhost.localdomain [127.0.0.1]) by mx1.opensynergy.com (Proxmox) with ESMTP id A3DDFA146E; Sun, 24 Jan 2021 17:54:41 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=opensynergy.com; h=cc:cc:content-transfer-encoding:content-type:content-type :date:from:from:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=srmailgate02; bh=VOjdMlszU3u7 iXPzdQ1k8GoYyH1Sujlr//n1EymoP+8=; b=r21UG6M/H9rUafvy/q+F33avwnKP 7FSRx4IR2RjdwacLomKOh1J88GOsGOYiCUTKy9ypDP6CQsy28VkLsao4ADMIkddQ +jTlpK8JKoDFf91AOft6iI6p9mmbkEVpyaFWN/AtEZsbXLiJvG96DuWtTGF3vk6B xXYjjgpZ10fj07tsxIG4OD1r+FJjSs0bCaxyFTxkb/oWFDm9DzN2v6bghE5jNHLp YJDbZegXFZSFTbUuSPjDHGxIn+p9UUD2p47CYGe4+Ou4Ud/YkBfQwu+/mW6ks/l3 Z1KDQzZpU2U99iGSTDRIAaiMVLMQT+YOp9FOpnCKtzicvULdeoxXVIAjRA== From: Anton Yakovlev To: , , CC: "Michael S. Tsirkin" , Jaroslav Kysela , Takashi Iwai , Subject: [PATCH v2 5/9] ALSA: virtio: handling control and I/O messages for the PCM device Date: Sun, 24 Jan 2021 17:54:04 +0100 Message-ID: <20210124165408.1122868-6-anton.yakovlev@opensynergy.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210124165408.1122868-1-anton.yakovlev@opensynergy.com> References: <20210124165408.1122868-1-anton.yakovlev@opensynergy.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SR-MAIL-02.open-synergy.com (10.26.10.22) To SR-MAIL-01.open-synergy.com (10.26.10.21) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Signed-off-by: Anton Yakovlev --- sound/virtio/Makefile | 3 +- sound/virtio/virtio_card.c | 10 ++ sound/virtio/virtio_card.h | 9 + sound/virtio/virtio_pcm.c | 3 + sound/virtio/virtio_pcm.h | 31 ++++ sound/virtio/virtio_pcm_msg.c | 325 ++++++++++++++++++++++++++++++++++ 6 files changed, 380 insertions(+), 1 deletion(-) create mode 100644 sound/virtio/virtio_pcm_msg.c diff --git a/sound/virtio/Makefile b/sound/virtio/Makefile index 69162a545a41..626af3cc3ed7 100644 --- a/sound/virtio/Makefile +++ b/sound/virtio/Makefile @@ -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 diff --git a/sound/virtio/virtio_card.c b/sound/virtio/virtio_card.c index 39fe13b43dd1..11d025ee77c2 100644 --- a/sound/virtio/virtio_card.c +++ b/sound/virtio/virtio_card.c @@ -143,6 +143,12 @@ static int virtsnd_find_vqs(struct virtio_snd *snd) callbacks[VIRTIO_SND_VQ_CONTROL] = virtsnd_ctl_notify_cb; callbacks[VIRTIO_SND_VQ_EVENT] = virtsnd_event_notify_cb; + virtio_cread(vdev, struct virtio_snd_config, streams, &n); + if (n) { + callbacks[VIRTIO_SND_VQ_TX] = virtsnd_pcm_tx_notify_cb; + callbacks[VIRTIO_SND_VQ_RX] = virtsnd_pcm_rx_notify_cb; + } + rc = virtio_find_vqs(vdev, VIRTIO_SND_VQ_MAX, vqs, callbacks, names, NULL); if (rc) { @@ -177,6 +183,10 @@ static int virtsnd_find_vqs(struct virtio_snd *snd) * virtsnd_enable_event_vq() - Enable the event virtqueue. * @snd: VirtIO sound device. * + * The tx queue is enabled only if the device supports playback stream(s). + * + * The rx queue is enabled only if the device supports capture stream(s). + * * Context: Any context. */ static void virtsnd_enable_event_vq(struct virtio_snd *snd) diff --git a/sound/virtio/virtio_card.h b/sound/virtio/virtio_card.h index be6651a6aaf8..b11c09984882 100644 --- a/sound/virtio/virtio_card.h +++ b/sound/virtio/virtio_card.h @@ -89,4 +89,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 *substream) +{ + if (substream->direction == SNDRV_PCM_STREAM_PLAYBACK) + return virtsnd_tx_queue(substream->snd); + else + return virtsnd_rx_queue(substream->snd); +} + #endif /* VIRTIO_SND_CARD_H */ diff --git a/sound/virtio/virtio_pcm.c b/sound/virtio/virtio_pcm.c index 036990b7b78a..1ab50dcc88c8 100644 --- a/sound/virtio/virtio_pcm.c +++ b/sound/virtio/virtio_pcm.c @@ -376,6 +376,7 @@ int virtsnd_pcm_parse_cfg(struct virtio_snd *snd) substream->snd = snd; substream->sid = i; + init_waitqueue_head(&substream->msg_empty); rc = virtsnd_pcm_build_hw(substream, &info[i]); if (rc) @@ -530,6 +531,8 @@ void virtsnd_pcm_event(struct virtio_snd *snd, struct virtio_snd_event *event) break; } case VIRTIO_SND_EVT_PCM_XRUN: { + if (atomic_read(&substream->xfer_enabled)) + atomic_set(&substream->xfer_xrun, 1); break; } } diff --git a/sound/virtio/virtio_pcm.h b/sound/virtio/virtio_pcm.h index 73fb4d9dc524..d011b7e1d18d 100644 --- a/sound/virtio/virtio_pcm.h +++ b/sound/virtio/virtio_pcm.h @@ -24,6 +24,7 @@ #include struct virtio_pcm; +struct virtio_pcm_msg; /** * struct virtio_pcm_substream - VirtIO PCM substream. @@ -34,6 +35,16 @@ struct virtio_pcm; * @features: Stream VirtIO feature bit map (1 << VIRTIO_SND_PCM_F_XXX). * @substream: Kernel ALSA substream. * @hw: Kernel ALSA substream hardware descriptor. + * @frame_bytes: Current frame size in bytes. + * @period_size: Current period size in frames. + * @buffer_size: Current buffer size in frames. + * @hw_ptr: Substream hardware pointer value in frames [0 ... buffer_size). + * @xfer_enabled: Data transfer state (0 - off, 1 - on). + * @xfer_xrun: Data underflow/overflow state (0 - no xrun, 1 - xrun). + * @msgs: 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; @@ -43,6 +54,16 @@ struct virtio_pcm_substream { u32 features; struct snd_pcm_substream *substream; struct snd_pcm_hardware hw; + unsigned int frame_bytes; + snd_pcm_uframes_t period_size; + snd_pcm_uframes_t buffer_size; + atomic_t hw_ptr; + atomic_t xfer_enabled; + atomic_t xfer_xrun; + struct virtio_pcm_msg *msgs; + int msg_last_enqueued; + atomic_t msg_count; + wait_queue_head_t msg_empty; }; /** @@ -86,4 +107,14 @@ struct virtio_pcm *virtsnd_pcm_find(struct virtio_snd *snd, unsigned int nid); struct virtio_pcm *virtsnd_pcm_find_or_create(struct virtio_snd *snd, unsigned int nid); +struct virtio_snd_msg * +virtsnd_pcm_ctl_msg_alloc(struct virtio_pcm_substream *substream, + unsigned int command, gfp_t gfp); + +int virtsnd_pcm_msg_alloc(struct virtio_pcm_substream *substream, + unsigned int nmsg, u8 *dma_area, + unsigned int period_bytes); + +int virtsnd_pcm_msg_send(struct virtio_pcm_substream *substream); + #endif /* VIRTIO_SND_PCM_H */ diff --git a/sound/virtio/virtio_pcm_msg.c b/sound/virtio/virtio_pcm_msg.c new file mode 100644 index 000000000000..d524e7cbe43f --- /dev/null +++ b/sound/virtio/virtio_pcm_msg.c @@ -0,0 +1,325 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Sound card driver for virtio + * Copyright (C) 2020 OpenSynergy GmbH + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ +#include + +#include "virtio_card.h" + +/** + * enum pcm_msg_sg_index - Scatter-gather element indexes for an I/O message. + * @PCM_MSG_SG_XFER: Element containing a virtio_snd_pcm_xfer structure. + * @PCM_MSG_SG_DATA: Element containing a data buffer. + * @PCM_MSG_SG_STATUS: Element containing a virtio_snd_pcm_status structure. + * @PCM_MSG_SG_MAX: The maximum number of elements in the scatter-gather table. + * + * These values are used as the index of the payload scatter-gather table. + */ +enum pcm_msg_sg_index { + PCM_MSG_SG_XFER = 0, + PCM_MSG_SG_DATA, + PCM_MSG_SG_STATUS, + PCM_MSG_SG_MAX +}; + +/** + * struct virtio_pcm_msg - VirtIO I/O message. + * @substream: VirtIO PCM substream. + * @xfer: Request header payload. + * @status: Response header payload. + * @sgs: Payload scatter-gather table. + */ +struct virtio_pcm_msg { + struct virtio_pcm_substream *substream; + struct virtio_snd_pcm_xfer xfer; + struct virtio_snd_pcm_status status; + struct scatterlist sgs[PCM_MSG_SG_MAX]; +}; + +/** + * virtsnd_pcm_msg_alloc() - Allocate I/O messages. + * @substream: VirtIO PCM substream. + * @nmsg: Number of messages (equal to the number of periods). + * @dma_area: Pointer to used audio buffer. + * @period_bytes: Period (message payload) size. + * + * The function slices the buffer into nmsg parts (each with the size of + * period_bytes), and creates nmsg corresponding I/O messages. + * + * Context: Any context that permits to sleep. + * Return: 0 on success, -ENOMEM on failure. + */ +int virtsnd_pcm_msg_alloc(struct virtio_pcm_substream *substream, + unsigned int nmsg, u8 *dma_area, + unsigned int period_bytes) +{ + struct virtio_device *vdev = substream->snd->vdev; + unsigned int i; + + if (substream->msgs) + devm_kfree(&vdev->dev, substream->msgs); + + substream->msgs = devm_kcalloc(&vdev->dev, nmsg, + sizeof(*substream->msgs), GFP_KERNEL); + if (!substream->msgs) + return -ENOMEM; + + for (i = 0; i < nmsg; ++i) { + struct virtio_pcm_msg *msg = &substream->msgs[i]; + + msg->substream = substream; + + sg_init_table(msg->sgs, PCM_MSG_SG_MAX); + sg_init_one(&msg->sgs[PCM_MSG_SG_XFER], &msg->xfer, + sizeof(msg->xfer)); + sg_init_one(&msg->sgs[PCM_MSG_SG_DATA], + dma_area + period_bytes * i, period_bytes); + sg_init_one(&msg->sgs[PCM_MSG_SG_STATUS], &msg->status, + sizeof(msg->status)); + } + + return 0; +} + +/** + * virtsnd_pcm_msg_send() - Send asynchronous I/O messages. + * @substream: VirtIO PCM substream. + * + * All messages are organized in an ordered circular list. Each time the + * function is called, all currently non-enqueued messages are added to the + * virtqueue. For this, the function keeps track of two values: + * + * msg_last_enqueued = index of the last enqueued message, + * msg_count = # of pending messages in the virtqueue. + * + * Context: Any context. + * Return: 0 on success, -EIO on failure. + */ +int virtsnd_pcm_msg_send(struct virtio_pcm_substream *substream) +{ + struct snd_pcm_runtime *runtime = substream->substream->runtime; + struct virtio_snd *snd = substream->snd; + struct virtio_device *vdev = snd->vdev; + struct virtqueue *vqueue = virtsnd_pcm_queue(substream)->vqueue; + int i; + int n; + bool notify = false; + + if (!vqueue) + return -EIO; + + i = (substream->msg_last_enqueued + 1) % runtime->periods; + n = runtime->periods - atomic_read(&substream->msg_count); + + for (; n; --n, i = (i + 1) % runtime->periods) { + struct virtio_pcm_msg *msg = &substream->msgs[i]; + struct scatterlist *psgs[PCM_MSG_SG_MAX] = { + [PCM_MSG_SG_XFER] = &msg->sgs[PCM_MSG_SG_XFER], + [PCM_MSG_SG_DATA] = &msg->sgs[PCM_MSG_SG_DATA], + [PCM_MSG_SG_STATUS] = &msg->sgs[PCM_MSG_SG_STATUS] + }; + int rc; + + msg->xfer.stream_id = cpu_to_virtio32(vdev, substream->sid); + memset(&msg->status, 0, sizeof(msg->status)); + + atomic_inc(&substream->msg_count); + + if (substream->direction == SNDRV_PCM_STREAM_PLAYBACK) + rc = virtqueue_add_sgs(vqueue, psgs, 2, 1, msg, + GFP_ATOMIC); + else + rc = virtqueue_add_sgs(vqueue, psgs, 1, 2, msg, + GFP_ATOMIC); + + if (rc) { + atomic_dec(&substream->msg_count); + return -EIO; + } + + substream->msg_last_enqueued = i; + } + + if (!(substream->features & (1U << VIRTIO_SND_PCM_F_MSG_POLLING))) + notify = virtqueue_kick_prepare(vqueue); + + if (notify) + if (!virtqueue_notify(vqueue)) + return -EIO; + + return 0; +} + +/** + * virtsnd_pcm_msg_complete() - Complete an I/O message. + * @msg: I/O message. + * @size: Number of bytes written. + * + * Completion of the message means the elapsed period. + * + * The interrupt handler modifies three fields of the substream structure + * (hw_ptr, xfer_xrun, msg_count) that are used in operator functions. These + * values are atomic to avoid frequent interlocks with the interrupt handler. + * This becomes especially important in the case of multiple running substreams + * that share both the virtqueue and interrupt handler. + * + * Context: Interrupt context. + */ +static void virtsnd_pcm_msg_complete(struct virtio_pcm_msg *msg, size_t size) +{ + struct virtio_pcm_substream *substream = msg->substream; + snd_pcm_uframes_t hw_ptr; + unsigned int msg_count; + + /* + * 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. + */ + hw_ptr = (snd_pcm_uframes_t)atomic_read(&substream->hw_ptr); + + /* + * If the capture substream returned an incorrect status, then just + * increase the hw_ptr by the period size. + */ + if (substream->direction == SNDRV_PCM_STREAM_PLAYBACK || + size <= sizeof(msg->status)) { + hw_ptr += substream->period_size; + } else { + size -= sizeof(msg->status); + hw_ptr += size / substream->frame_bytes; + } + + atomic_set(&substream->hw_ptr, (u32)(hw_ptr % substream->buffer_size)); + atomic_set(&substream->xfer_xrun, 0); + + msg_count = atomic_dec_return(&substream->msg_count); + + if (atomic_read(&substream->xfer_enabled)) { + struct snd_pcm_runtime *runtime = substream->substream->runtime; + + runtime->delay = + bytes_to_frames(runtime, + le32_to_cpu(msg->status.latency_bytes)); + + snd_pcm_period_elapsed(substream->substream); + + virtsnd_pcm_msg_send(substream); + } else if (!msg_count) { + wake_up_all(&substream->msg_empty); + } +} + +/** + * virtsnd_pcm_notify_cb() - Process all completed I/O messages. + * @vqueue: Underlying tx/rx virtqueue. + * + * If transmission is allowed, then each completed message is immediately placed + * back at the end of the queue. + * + * Context: Interrupt context. Takes and releases the tx/rx queue spinlock. + */ +static inline void virtsnd_pcm_notify_cb(struct virtio_snd_queue *queue) +{ + unsigned long flags; + + spin_lock_irqsave(&queue->lock, flags); + while (queue->vqueue) { + virtqueue_disable_cb(queue->vqueue); + + for (;;) { + struct virtio_pcm_msg *msg; + u32 length; + + msg = virtqueue_get_buf(queue->vqueue, &length); + if (!msg) + break; + + virtsnd_pcm_msg_complete(msg, length); + } + + if (unlikely(virtqueue_is_broken(queue->vqueue))) + break; + + if (virtqueue_enable_cb(queue->vqueue)) + break; + } + spin_unlock_irqrestore(&queue->lock, flags); +} + +/** + * virtsnd_pcm_tx_notify_cb() - Process all completed TX messages. + * @vqueue: Underlying tx virtqueue. + * + * Context: Interrupt context. + */ +void virtsnd_pcm_tx_notify_cb(struct virtqueue *vqueue) +{ + struct virtio_snd *snd = vqueue->vdev->priv; + + virtsnd_pcm_notify_cb(virtsnd_tx_queue(snd)); +} + +/** + * virtsnd_pcm_rx_notify_cb() - Process all completed RX messages. + * @vqueue: Underlying rx virtqueue. + * + * Context: Interrupt context. + */ +void virtsnd_pcm_rx_notify_cb(struct virtqueue *vqueue) +{ + struct virtio_snd *snd = vqueue->vdev->priv; + + virtsnd_pcm_notify_cb(virtsnd_rx_queue(snd)); +} + +/** + * virtsnd_pcm_ctl_msg_alloc() - Allocate and initialize the PCM device control + * message for the specified substream. + * @substream: VirtIO PCM substream. + * @command: Control request code (VIRTIO_SND_R_PCM_XXX). + * @gfp: Kernel flags for memory allocation. + * + * Context: Any context. May sleep if @gfp flags permit. + * Return: Allocated message on success, ERR_PTR(-errno) on failure. + */ +struct virtio_snd_msg * +virtsnd_pcm_ctl_msg_alloc(struct virtio_pcm_substream *substream, + unsigned int command, gfp_t gfp) +{ + struct virtio_device *vdev = substream->snd->vdev; + size_t request_size = sizeof(struct virtio_snd_pcm_hdr); + size_t response_size = sizeof(struct virtio_snd_hdr); + struct virtio_snd_msg *msg; + + switch (command) { + case VIRTIO_SND_R_PCM_SET_PARAMS: { + request_size = sizeof(struct virtio_snd_pcm_set_params); + break; + } + } + + msg = virtsnd_ctl_msg_alloc(vdev, request_size, response_size, gfp); + if (!IS_ERR(msg)) { + struct virtio_snd_pcm_hdr *hdr = sg_virt(&msg->sg_request); + + hdr->hdr.code = cpu_to_virtio32(vdev, command); + hdr->stream_id = cpu_to_virtio32(vdev, substream->sid); + } + + return msg; +} -- 2.30.0 From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 125E6C433DB for ; Sun, 24 Jan 2021 16:57:41 +0000 (UTC) Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 76CC922CA2 for ; Sun, 24 Jan 2021 16:57:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 76CC922CA2 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=opensynergy.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=alsa-devel-bounces@alsa-project.org Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 0F0D61873; Sun, 24 Jan 2021 17:56:49 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 0F0D61873 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1611507459; bh=Vzl2BJA4Gy9uznRzwwtqRFnSNsDnF0V4+q8XcjkRdQ4=; h=From:To:Subject:Date:In-Reply-To:References:Cc:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=sszPW/wgraU333GGWNmsje+mVz6NtfVu02bZXCG5hwmnpdZz5fN1eVmDBLWo7qSmK C/cIPM5vmOFRH5caSl8kyGSbF9qb5JvOTiYTtJl1H3E7F5Jjj1gj8QvPgM0kZvmM9H masiPGF+ObZ9hKFTlLiLYl6eO0hQ5HMxVdIroBIU= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 4A958F804FD; Sun, 24 Jan 2021 17:54:47 +0100 (CET) Received: by alsa1.perex.cz (Postfix, from userid 50401) id 4C67FF804FD; Sun, 24 Jan 2021 17:54:45 +0100 (CET) Received: from mx1.opensynergy.com (mx1.opensynergy.com [217.66.60.4]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 132D4F804FB for ; Sun, 24 Jan 2021 17:54:42 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 132D4F804FB Authentication-Results: alsa1.perex.cz; dkim=pass (2048-bit key) header.d=opensynergy.com header.i=@opensynergy.com header.b="r21UG6M/" Received: from SR-MAILGATE-02.opensynergy.com (localhost.localdomain [127.0.0.1]) by mx1.opensynergy.com (Proxmox) with ESMTP id A3DDFA146E; Sun, 24 Jan 2021 17:54:41 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=opensynergy.com; h=cc:cc:content-transfer-encoding:content-type:content-type :date:from:from:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=srmailgate02; bh=VOjdMlszU3u7 iXPzdQ1k8GoYyH1Sujlr//n1EymoP+8=; b=r21UG6M/H9rUafvy/q+F33avwnKP 7FSRx4IR2RjdwacLomKOh1J88GOsGOYiCUTKy9ypDP6CQsy28VkLsao4ADMIkddQ +jTlpK8JKoDFf91AOft6iI6p9mmbkEVpyaFWN/AtEZsbXLiJvG96DuWtTGF3vk6B xXYjjgpZ10fj07tsxIG4OD1r+FJjSs0bCaxyFTxkb/oWFDm9DzN2v6bghE5jNHLp YJDbZegXFZSFTbUuSPjDHGxIn+p9UUD2p47CYGe4+Ou4Ud/YkBfQwu+/mW6ks/l3 Z1KDQzZpU2U99iGSTDRIAaiMVLMQT+YOp9FOpnCKtzicvULdeoxXVIAjRA== From: Anton Yakovlev To: , , Subject: [PATCH v2 5/9] ALSA: virtio: handling control and I/O messages for the PCM device Date: Sun, 24 Jan 2021 17:54:04 +0100 Message-ID: <20210124165408.1122868-6-anton.yakovlev@opensynergy.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210124165408.1122868-1-anton.yakovlev@opensynergy.com> References: <20210124165408.1122868-1-anton.yakovlev@opensynergy.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SR-MAIL-02.open-synergy.com (10.26.10.22) To SR-MAIL-01.open-synergy.com (10.26.10.21) Cc: linux-kernel@vger.kernel.org, Takashi Iwai , "Michael S. Tsirkin" X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" 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. Signed-off-by: Anton Yakovlev --- sound/virtio/Makefile | 3 +- sound/virtio/virtio_card.c | 10 ++ sound/virtio/virtio_card.h | 9 + sound/virtio/virtio_pcm.c | 3 + sound/virtio/virtio_pcm.h | 31 ++++ sound/virtio/virtio_pcm_msg.c | 325 ++++++++++++++++++++++++++++++++++ 6 files changed, 380 insertions(+), 1 deletion(-) create mode 100644 sound/virtio/virtio_pcm_msg.c diff --git a/sound/virtio/Makefile b/sound/virtio/Makefile index 69162a545a41..626af3cc3ed7 100644 --- a/sound/virtio/Makefile +++ b/sound/virtio/Makefile @@ -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 diff --git a/sound/virtio/virtio_card.c b/sound/virtio/virtio_card.c index 39fe13b43dd1..11d025ee77c2 100644 --- a/sound/virtio/virtio_card.c +++ b/sound/virtio/virtio_card.c @@ -143,6 +143,12 @@ static int virtsnd_find_vqs(struct virtio_snd *snd) callbacks[VIRTIO_SND_VQ_CONTROL] = virtsnd_ctl_notify_cb; callbacks[VIRTIO_SND_VQ_EVENT] = virtsnd_event_notify_cb; + virtio_cread(vdev, struct virtio_snd_config, streams, &n); + if (n) { + callbacks[VIRTIO_SND_VQ_TX] = virtsnd_pcm_tx_notify_cb; + callbacks[VIRTIO_SND_VQ_RX] = virtsnd_pcm_rx_notify_cb; + } + rc = virtio_find_vqs(vdev, VIRTIO_SND_VQ_MAX, vqs, callbacks, names, NULL); if (rc) { @@ -177,6 +183,10 @@ static int virtsnd_find_vqs(struct virtio_snd *snd) * virtsnd_enable_event_vq() - Enable the event virtqueue. * @snd: VirtIO sound device. * + * The tx queue is enabled only if the device supports playback stream(s). + * + * The rx queue is enabled only if the device supports capture stream(s). + * * Context: Any context. */ static void virtsnd_enable_event_vq(struct virtio_snd *snd) diff --git a/sound/virtio/virtio_card.h b/sound/virtio/virtio_card.h index be6651a6aaf8..b11c09984882 100644 --- a/sound/virtio/virtio_card.h +++ b/sound/virtio/virtio_card.h @@ -89,4 +89,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 *substream) +{ + if (substream->direction == SNDRV_PCM_STREAM_PLAYBACK) + return virtsnd_tx_queue(substream->snd); + else + return virtsnd_rx_queue(substream->snd); +} + #endif /* VIRTIO_SND_CARD_H */ diff --git a/sound/virtio/virtio_pcm.c b/sound/virtio/virtio_pcm.c index 036990b7b78a..1ab50dcc88c8 100644 --- a/sound/virtio/virtio_pcm.c +++ b/sound/virtio/virtio_pcm.c @@ -376,6 +376,7 @@ int virtsnd_pcm_parse_cfg(struct virtio_snd *snd) substream->snd = snd; substream->sid = i; + init_waitqueue_head(&substream->msg_empty); rc = virtsnd_pcm_build_hw(substream, &info[i]); if (rc) @@ -530,6 +531,8 @@ void virtsnd_pcm_event(struct virtio_snd *snd, struct virtio_snd_event *event) break; } case VIRTIO_SND_EVT_PCM_XRUN: { + if (atomic_read(&substream->xfer_enabled)) + atomic_set(&substream->xfer_xrun, 1); break; } } diff --git a/sound/virtio/virtio_pcm.h b/sound/virtio/virtio_pcm.h index 73fb4d9dc524..d011b7e1d18d 100644 --- a/sound/virtio/virtio_pcm.h +++ b/sound/virtio/virtio_pcm.h @@ -24,6 +24,7 @@ #include struct virtio_pcm; +struct virtio_pcm_msg; /** * struct virtio_pcm_substream - VirtIO PCM substream. @@ -34,6 +35,16 @@ struct virtio_pcm; * @features: Stream VirtIO feature bit map (1 << VIRTIO_SND_PCM_F_XXX). * @substream: Kernel ALSA substream. * @hw: Kernel ALSA substream hardware descriptor. + * @frame_bytes: Current frame size in bytes. + * @period_size: Current period size in frames. + * @buffer_size: Current buffer size in frames. + * @hw_ptr: Substream hardware pointer value in frames [0 ... buffer_size). + * @xfer_enabled: Data transfer state (0 - off, 1 - on). + * @xfer_xrun: Data underflow/overflow state (0 - no xrun, 1 - xrun). + * @msgs: 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; @@ -43,6 +54,16 @@ struct virtio_pcm_substream { u32 features; struct snd_pcm_substream *substream; struct snd_pcm_hardware hw; + unsigned int frame_bytes; + snd_pcm_uframes_t period_size; + snd_pcm_uframes_t buffer_size; + atomic_t hw_ptr; + atomic_t xfer_enabled; + atomic_t xfer_xrun; + struct virtio_pcm_msg *msgs; + int msg_last_enqueued; + atomic_t msg_count; + wait_queue_head_t msg_empty; }; /** @@ -86,4 +107,14 @@ struct virtio_pcm *virtsnd_pcm_find(struct virtio_snd *snd, unsigned int nid); struct virtio_pcm *virtsnd_pcm_find_or_create(struct virtio_snd *snd, unsigned int nid); +struct virtio_snd_msg * +virtsnd_pcm_ctl_msg_alloc(struct virtio_pcm_substream *substream, + unsigned int command, gfp_t gfp); + +int virtsnd_pcm_msg_alloc(struct virtio_pcm_substream *substream, + unsigned int nmsg, u8 *dma_area, + unsigned int period_bytes); + +int virtsnd_pcm_msg_send(struct virtio_pcm_substream *substream); + #endif /* VIRTIO_SND_PCM_H */ diff --git a/sound/virtio/virtio_pcm_msg.c b/sound/virtio/virtio_pcm_msg.c new file mode 100644 index 000000000000..d524e7cbe43f --- /dev/null +++ b/sound/virtio/virtio_pcm_msg.c @@ -0,0 +1,325 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Sound card driver for virtio + * Copyright (C) 2020 OpenSynergy GmbH + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ +#include + +#include "virtio_card.h" + +/** + * enum pcm_msg_sg_index - Scatter-gather element indexes for an I/O message. + * @PCM_MSG_SG_XFER: Element containing a virtio_snd_pcm_xfer structure. + * @PCM_MSG_SG_DATA: Element containing a data buffer. + * @PCM_MSG_SG_STATUS: Element containing a virtio_snd_pcm_status structure. + * @PCM_MSG_SG_MAX: The maximum number of elements in the scatter-gather table. + * + * These values are used as the index of the payload scatter-gather table. + */ +enum pcm_msg_sg_index { + PCM_MSG_SG_XFER = 0, + PCM_MSG_SG_DATA, + PCM_MSG_SG_STATUS, + PCM_MSG_SG_MAX +}; + +/** + * struct virtio_pcm_msg - VirtIO I/O message. + * @substream: VirtIO PCM substream. + * @xfer: Request header payload. + * @status: Response header payload. + * @sgs: Payload scatter-gather table. + */ +struct virtio_pcm_msg { + struct virtio_pcm_substream *substream; + struct virtio_snd_pcm_xfer xfer; + struct virtio_snd_pcm_status status; + struct scatterlist sgs[PCM_MSG_SG_MAX]; +}; + +/** + * virtsnd_pcm_msg_alloc() - Allocate I/O messages. + * @substream: VirtIO PCM substream. + * @nmsg: Number of messages (equal to the number of periods). + * @dma_area: Pointer to used audio buffer. + * @period_bytes: Period (message payload) size. + * + * The function slices the buffer into nmsg parts (each with the size of + * period_bytes), and creates nmsg corresponding I/O messages. + * + * Context: Any context that permits to sleep. + * Return: 0 on success, -ENOMEM on failure. + */ +int virtsnd_pcm_msg_alloc(struct virtio_pcm_substream *substream, + unsigned int nmsg, u8 *dma_area, + unsigned int period_bytes) +{ + struct virtio_device *vdev = substream->snd->vdev; + unsigned int i; + + if (substream->msgs) + devm_kfree(&vdev->dev, substream->msgs); + + substream->msgs = devm_kcalloc(&vdev->dev, nmsg, + sizeof(*substream->msgs), GFP_KERNEL); + if (!substream->msgs) + return -ENOMEM; + + for (i = 0; i < nmsg; ++i) { + struct virtio_pcm_msg *msg = &substream->msgs[i]; + + msg->substream = substream; + + sg_init_table(msg->sgs, PCM_MSG_SG_MAX); + sg_init_one(&msg->sgs[PCM_MSG_SG_XFER], &msg->xfer, + sizeof(msg->xfer)); + sg_init_one(&msg->sgs[PCM_MSG_SG_DATA], + dma_area + period_bytes * i, period_bytes); + sg_init_one(&msg->sgs[PCM_MSG_SG_STATUS], &msg->status, + sizeof(msg->status)); + } + + return 0; +} + +/** + * virtsnd_pcm_msg_send() - Send asynchronous I/O messages. + * @substream: VirtIO PCM substream. + * + * All messages are organized in an ordered circular list. Each time the + * function is called, all currently non-enqueued messages are added to the + * virtqueue. For this, the function keeps track of two values: + * + * msg_last_enqueued = index of the last enqueued message, + * msg_count = # of pending messages in the virtqueue. + * + * Context: Any context. + * Return: 0 on success, -EIO on failure. + */ +int virtsnd_pcm_msg_send(struct virtio_pcm_substream *substream) +{ + struct snd_pcm_runtime *runtime = substream->substream->runtime; + struct virtio_snd *snd = substream->snd; + struct virtio_device *vdev = snd->vdev; + struct virtqueue *vqueue = virtsnd_pcm_queue(substream)->vqueue; + int i; + int n; + bool notify = false; + + if (!vqueue) + return -EIO; + + i = (substream->msg_last_enqueued + 1) % runtime->periods; + n = runtime->periods - atomic_read(&substream->msg_count); + + for (; n; --n, i = (i + 1) % runtime->periods) { + struct virtio_pcm_msg *msg = &substream->msgs[i]; + struct scatterlist *psgs[PCM_MSG_SG_MAX] = { + [PCM_MSG_SG_XFER] = &msg->sgs[PCM_MSG_SG_XFER], + [PCM_MSG_SG_DATA] = &msg->sgs[PCM_MSG_SG_DATA], + [PCM_MSG_SG_STATUS] = &msg->sgs[PCM_MSG_SG_STATUS] + }; + int rc; + + msg->xfer.stream_id = cpu_to_virtio32(vdev, substream->sid); + memset(&msg->status, 0, sizeof(msg->status)); + + atomic_inc(&substream->msg_count); + + if (substream->direction == SNDRV_PCM_STREAM_PLAYBACK) + rc = virtqueue_add_sgs(vqueue, psgs, 2, 1, msg, + GFP_ATOMIC); + else + rc = virtqueue_add_sgs(vqueue, psgs, 1, 2, msg, + GFP_ATOMIC); + + if (rc) { + atomic_dec(&substream->msg_count); + return -EIO; + } + + substream->msg_last_enqueued = i; + } + + if (!(substream->features & (1U << VIRTIO_SND_PCM_F_MSG_POLLING))) + notify = virtqueue_kick_prepare(vqueue); + + if (notify) + if (!virtqueue_notify(vqueue)) + return -EIO; + + return 0; +} + +/** + * virtsnd_pcm_msg_complete() - Complete an I/O message. + * @msg: I/O message. + * @size: Number of bytes written. + * + * Completion of the message means the elapsed period. + * + * The interrupt handler modifies three fields of the substream structure + * (hw_ptr, xfer_xrun, msg_count) that are used in operator functions. These + * values are atomic to avoid frequent interlocks with the interrupt handler. + * This becomes especially important in the case of multiple running substreams + * that share both the virtqueue and interrupt handler. + * + * Context: Interrupt context. + */ +static void virtsnd_pcm_msg_complete(struct virtio_pcm_msg *msg, size_t size) +{ + struct virtio_pcm_substream *substream = msg->substream; + snd_pcm_uframes_t hw_ptr; + unsigned int msg_count; + + /* + * 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. + */ + hw_ptr = (snd_pcm_uframes_t)atomic_read(&substream->hw_ptr); + + /* + * If the capture substream returned an incorrect status, then just + * increase the hw_ptr by the period size. + */ + if (substream->direction == SNDRV_PCM_STREAM_PLAYBACK || + size <= sizeof(msg->status)) { + hw_ptr += substream->period_size; + } else { + size -= sizeof(msg->status); + hw_ptr += size / substream->frame_bytes; + } + + atomic_set(&substream->hw_ptr, (u32)(hw_ptr % substream->buffer_size)); + atomic_set(&substream->xfer_xrun, 0); + + msg_count = atomic_dec_return(&substream->msg_count); + + if (atomic_read(&substream->xfer_enabled)) { + struct snd_pcm_runtime *runtime = substream->substream->runtime; + + runtime->delay = + bytes_to_frames(runtime, + le32_to_cpu(msg->status.latency_bytes)); + + snd_pcm_period_elapsed(substream->substream); + + virtsnd_pcm_msg_send(substream); + } else if (!msg_count) { + wake_up_all(&substream->msg_empty); + } +} + +/** + * virtsnd_pcm_notify_cb() - Process all completed I/O messages. + * @vqueue: Underlying tx/rx virtqueue. + * + * If transmission is allowed, then each completed message is immediately placed + * back at the end of the queue. + * + * Context: Interrupt context. Takes and releases the tx/rx queue spinlock. + */ +static inline void virtsnd_pcm_notify_cb(struct virtio_snd_queue *queue) +{ + unsigned long flags; + + spin_lock_irqsave(&queue->lock, flags); + while (queue->vqueue) { + virtqueue_disable_cb(queue->vqueue); + + for (;;) { + struct virtio_pcm_msg *msg; + u32 length; + + msg = virtqueue_get_buf(queue->vqueue, &length); + if (!msg) + break; + + virtsnd_pcm_msg_complete(msg, length); + } + + if (unlikely(virtqueue_is_broken(queue->vqueue))) + break; + + if (virtqueue_enable_cb(queue->vqueue)) + break; + } + spin_unlock_irqrestore(&queue->lock, flags); +} + +/** + * virtsnd_pcm_tx_notify_cb() - Process all completed TX messages. + * @vqueue: Underlying tx virtqueue. + * + * Context: Interrupt context. + */ +void virtsnd_pcm_tx_notify_cb(struct virtqueue *vqueue) +{ + struct virtio_snd *snd = vqueue->vdev->priv; + + virtsnd_pcm_notify_cb(virtsnd_tx_queue(snd)); +} + +/** + * virtsnd_pcm_rx_notify_cb() - Process all completed RX messages. + * @vqueue: Underlying rx virtqueue. + * + * Context: Interrupt context. + */ +void virtsnd_pcm_rx_notify_cb(struct virtqueue *vqueue) +{ + struct virtio_snd *snd = vqueue->vdev->priv; + + virtsnd_pcm_notify_cb(virtsnd_rx_queue(snd)); +} + +/** + * virtsnd_pcm_ctl_msg_alloc() - Allocate and initialize the PCM device control + * message for the specified substream. + * @substream: VirtIO PCM substream. + * @command: Control request code (VIRTIO_SND_R_PCM_XXX). + * @gfp: Kernel flags for memory allocation. + * + * Context: Any context. May sleep if @gfp flags permit. + * Return: Allocated message on success, ERR_PTR(-errno) on failure. + */ +struct virtio_snd_msg * +virtsnd_pcm_ctl_msg_alloc(struct virtio_pcm_substream *substream, + unsigned int command, gfp_t gfp) +{ + struct virtio_device *vdev = substream->snd->vdev; + size_t request_size = sizeof(struct virtio_snd_pcm_hdr); + size_t response_size = sizeof(struct virtio_snd_hdr); + struct virtio_snd_msg *msg; + + switch (command) { + case VIRTIO_SND_R_PCM_SET_PARAMS: { + request_size = sizeof(struct virtio_snd_pcm_set_params); + break; + } + } + + msg = virtsnd_ctl_msg_alloc(vdev, request_size, response_size, gfp); + if (!IS_ERR(msg)) { + struct virtio_snd_pcm_hdr *hdr = sg_virt(&msg->sg_request); + + hdr->hdr.code = cpu_to_virtio32(vdev, command); + hdr->stream_id = cpu_to_virtio32(vdev, substream->sid); + } + + return msg; +} -- 2.30.0 From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_RED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5396EC433DB for ; Sun, 24 Jan 2021 16:54:54 +0000 (UTC) Received: from hemlock.osuosl.org (smtp2.osuosl.org [140.211.166.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 0482722CA2 for ; Sun, 24 Jan 2021 16:54:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0482722CA2 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=opensynergy.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=virtualization-bounces@lists.linux-foundation.org Received: from localhost (localhost [127.0.0.1]) by hemlock.osuosl.org (Postfix) with ESMTP id DBC7C871FD; Sun, 24 Jan 2021 16:54:53 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from hemlock.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id qsnXBFgPxGyn; Sun, 24 Jan 2021 16:54:52 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by hemlock.osuosl.org (Postfix) with ESMTP id 93993871EE; Sun, 24 Jan 2021 16:54:52 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 7FC3CC0FA7; Sun, 24 Jan 2021 16:54:52 +0000 (UTC) Received: from silver.osuosl.org (smtp3.osuosl.org [140.211.166.136]) by lists.linuxfoundation.org (Postfix) with ESMTP id 9A1BBC013A for ; Sun, 24 Jan 2021 16:54:51 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by silver.osuosl.org (Postfix) with ESMTP id 7C6D02046F for ; Sun, 24 Jan 2021 16:54:51 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from silver.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id HKp6ytbkvFjn for ; Sun, 24 Jan 2021 16:54:47 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mx1.opensynergy.com (mx1.opensynergy.com [217.66.60.4]) by silver.osuosl.org (Postfix) with ESMTPS id 05D80204DD for ; Sun, 24 Jan 2021 16:54:43 +0000 (UTC) Received: from SR-MAILGATE-02.opensynergy.com (localhost.localdomain [127.0.0.1]) by mx1.opensynergy.com (Proxmox) with ESMTP id A3DDFA146E; Sun, 24 Jan 2021 17:54:41 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=opensynergy.com; h=cc:cc:content-transfer-encoding:content-type:content-type :date:from:from:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=srmailgate02; bh=VOjdMlszU3u7 iXPzdQ1k8GoYyH1Sujlr//n1EymoP+8=; b=r21UG6M/H9rUafvy/q+F33avwnKP 7FSRx4IR2RjdwacLomKOh1J88GOsGOYiCUTKy9ypDP6CQsy28VkLsao4ADMIkddQ +jTlpK8JKoDFf91AOft6iI6p9mmbkEVpyaFWN/AtEZsbXLiJvG96DuWtTGF3vk6B xXYjjgpZ10fj07tsxIG4OD1r+FJjSs0bCaxyFTxkb/oWFDm9DzN2v6bghE5jNHLp YJDbZegXFZSFTbUuSPjDHGxIn+p9UUD2p47CYGe4+Ou4Ud/YkBfQwu+/mW6ks/l3 Z1KDQzZpU2U99iGSTDRIAaiMVLMQT+YOp9FOpnCKtzicvULdeoxXVIAjRA== From: Anton Yakovlev To: , , Subject: [PATCH v2 5/9] ALSA: virtio: handling control and I/O messages for the PCM device Date: Sun, 24 Jan 2021 17:54:04 +0100 Message-ID: <20210124165408.1122868-6-anton.yakovlev@opensynergy.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210124165408.1122868-1-anton.yakovlev@opensynergy.com> References: <20210124165408.1122868-1-anton.yakovlev@opensynergy.com> MIME-Version: 1.0 X-ClientProxiedBy: SR-MAIL-02.open-synergy.com (10.26.10.22) To SR-MAIL-01.open-synergy.com (10.26.10.21) Cc: linux-kernel@vger.kernel.org, Jaroslav Kysela , Takashi Iwai , "Michael S. Tsirkin" X-BeenThere: virtualization@lists.linux-foundation.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Linux virtualization List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: virtualization-bounces@lists.linux-foundation.org Sender: "Virtualization" 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. Signed-off-by: Anton Yakovlev --- sound/virtio/Makefile | 3 +- sound/virtio/virtio_card.c | 10 ++ sound/virtio/virtio_card.h | 9 + sound/virtio/virtio_pcm.c | 3 + sound/virtio/virtio_pcm.h | 31 ++++ sound/virtio/virtio_pcm_msg.c | 325 ++++++++++++++++++++++++++++++++++ 6 files changed, 380 insertions(+), 1 deletion(-) create mode 100644 sound/virtio/virtio_pcm_msg.c diff --git a/sound/virtio/Makefile b/sound/virtio/Makefile index 69162a545a41..626af3cc3ed7 100644 --- a/sound/virtio/Makefile +++ b/sound/virtio/Makefile @@ -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 diff --git a/sound/virtio/virtio_card.c b/sound/virtio/virtio_card.c index 39fe13b43dd1..11d025ee77c2 100644 --- a/sound/virtio/virtio_card.c +++ b/sound/virtio/virtio_card.c @@ -143,6 +143,12 @@ static int virtsnd_find_vqs(struct virtio_snd *snd) callbacks[VIRTIO_SND_VQ_CONTROL] = virtsnd_ctl_notify_cb; callbacks[VIRTIO_SND_VQ_EVENT] = virtsnd_event_notify_cb; + virtio_cread(vdev, struct virtio_snd_config, streams, &n); + if (n) { + callbacks[VIRTIO_SND_VQ_TX] = virtsnd_pcm_tx_notify_cb; + callbacks[VIRTIO_SND_VQ_RX] = virtsnd_pcm_rx_notify_cb; + } + rc = virtio_find_vqs(vdev, VIRTIO_SND_VQ_MAX, vqs, callbacks, names, NULL); if (rc) { @@ -177,6 +183,10 @@ static int virtsnd_find_vqs(struct virtio_snd *snd) * virtsnd_enable_event_vq() - Enable the event virtqueue. * @snd: VirtIO sound device. * + * The tx queue is enabled only if the device supports playback stream(s). + * + * The rx queue is enabled only if the device supports capture stream(s). + * * Context: Any context. */ static void virtsnd_enable_event_vq(struct virtio_snd *snd) diff --git a/sound/virtio/virtio_card.h b/sound/virtio/virtio_card.h index be6651a6aaf8..b11c09984882 100644 --- a/sound/virtio/virtio_card.h +++ b/sound/virtio/virtio_card.h @@ -89,4 +89,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 *substream) +{ + if (substream->direction == SNDRV_PCM_STREAM_PLAYBACK) + return virtsnd_tx_queue(substream->snd); + else + return virtsnd_rx_queue(substream->snd); +} + #endif /* VIRTIO_SND_CARD_H */ diff --git a/sound/virtio/virtio_pcm.c b/sound/virtio/virtio_pcm.c index 036990b7b78a..1ab50dcc88c8 100644 --- a/sound/virtio/virtio_pcm.c +++ b/sound/virtio/virtio_pcm.c @@ -376,6 +376,7 @@ int virtsnd_pcm_parse_cfg(struct virtio_snd *snd) substream->snd = snd; substream->sid = i; + init_waitqueue_head(&substream->msg_empty); rc = virtsnd_pcm_build_hw(substream, &info[i]); if (rc) @@ -530,6 +531,8 @@ void virtsnd_pcm_event(struct virtio_snd *snd, struct virtio_snd_event *event) break; } case VIRTIO_SND_EVT_PCM_XRUN: { + if (atomic_read(&substream->xfer_enabled)) + atomic_set(&substream->xfer_xrun, 1); break; } } diff --git a/sound/virtio/virtio_pcm.h b/sound/virtio/virtio_pcm.h index 73fb4d9dc524..d011b7e1d18d 100644 --- a/sound/virtio/virtio_pcm.h +++ b/sound/virtio/virtio_pcm.h @@ -24,6 +24,7 @@ #include struct virtio_pcm; +struct virtio_pcm_msg; /** * struct virtio_pcm_substream - VirtIO PCM substream. @@ -34,6 +35,16 @@ struct virtio_pcm; * @features: Stream VirtIO feature bit map (1 << VIRTIO_SND_PCM_F_XXX). * @substream: Kernel ALSA substream. * @hw: Kernel ALSA substream hardware descriptor. + * @frame_bytes: Current frame size in bytes. + * @period_size: Current period size in frames. + * @buffer_size: Current buffer size in frames. + * @hw_ptr: Substream hardware pointer value in frames [0 ... buffer_size). + * @xfer_enabled: Data transfer state (0 - off, 1 - on). + * @xfer_xrun: Data underflow/overflow state (0 - no xrun, 1 - xrun). + * @msgs: 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; @@ -43,6 +54,16 @@ struct virtio_pcm_substream { u32 features; struct snd_pcm_substream *substream; struct snd_pcm_hardware hw; + unsigned int frame_bytes; + snd_pcm_uframes_t period_size; + snd_pcm_uframes_t buffer_size; + atomic_t hw_ptr; + atomic_t xfer_enabled; + atomic_t xfer_xrun; + struct virtio_pcm_msg *msgs; + int msg_last_enqueued; + atomic_t msg_count; + wait_queue_head_t msg_empty; }; /** @@ -86,4 +107,14 @@ struct virtio_pcm *virtsnd_pcm_find(struct virtio_snd *snd, unsigned int nid); struct virtio_pcm *virtsnd_pcm_find_or_create(struct virtio_snd *snd, unsigned int nid); +struct virtio_snd_msg * +virtsnd_pcm_ctl_msg_alloc(struct virtio_pcm_substream *substream, + unsigned int command, gfp_t gfp); + +int virtsnd_pcm_msg_alloc(struct virtio_pcm_substream *substream, + unsigned int nmsg, u8 *dma_area, + unsigned int period_bytes); + +int virtsnd_pcm_msg_send(struct virtio_pcm_substream *substream); + #endif /* VIRTIO_SND_PCM_H */ diff --git a/sound/virtio/virtio_pcm_msg.c b/sound/virtio/virtio_pcm_msg.c new file mode 100644 index 000000000000..d524e7cbe43f --- /dev/null +++ b/sound/virtio/virtio_pcm_msg.c @@ -0,0 +1,325 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Sound card driver for virtio + * Copyright (C) 2020 OpenSynergy GmbH + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ +#include + +#include "virtio_card.h" + +/** + * enum pcm_msg_sg_index - Scatter-gather element indexes for an I/O message. + * @PCM_MSG_SG_XFER: Element containing a virtio_snd_pcm_xfer structure. + * @PCM_MSG_SG_DATA: Element containing a data buffer. + * @PCM_MSG_SG_STATUS: Element containing a virtio_snd_pcm_status structure. + * @PCM_MSG_SG_MAX: The maximum number of elements in the scatter-gather table. + * + * These values are used as the index of the payload scatter-gather table. + */ +enum pcm_msg_sg_index { + PCM_MSG_SG_XFER = 0, + PCM_MSG_SG_DATA, + PCM_MSG_SG_STATUS, + PCM_MSG_SG_MAX +}; + +/** + * struct virtio_pcm_msg - VirtIO I/O message. + * @substream: VirtIO PCM substream. + * @xfer: Request header payload. + * @status: Response header payload. + * @sgs: Payload scatter-gather table. + */ +struct virtio_pcm_msg { + struct virtio_pcm_substream *substream; + struct virtio_snd_pcm_xfer xfer; + struct virtio_snd_pcm_status status; + struct scatterlist sgs[PCM_MSG_SG_MAX]; +}; + +/** + * virtsnd_pcm_msg_alloc() - Allocate I/O messages. + * @substream: VirtIO PCM substream. + * @nmsg: Number of messages (equal to the number of periods). + * @dma_area: Pointer to used audio buffer. + * @period_bytes: Period (message payload) size. + * + * The function slices the buffer into nmsg parts (each with the size of + * period_bytes), and creates nmsg corresponding I/O messages. + * + * Context: Any context that permits to sleep. + * Return: 0 on success, -ENOMEM on failure. + */ +int virtsnd_pcm_msg_alloc(struct virtio_pcm_substream *substream, + unsigned int nmsg, u8 *dma_area, + unsigned int period_bytes) +{ + struct virtio_device *vdev = substream->snd->vdev; + unsigned int i; + + if (substream->msgs) + devm_kfree(&vdev->dev, substream->msgs); + + substream->msgs = devm_kcalloc(&vdev->dev, nmsg, + sizeof(*substream->msgs), GFP_KERNEL); + if (!substream->msgs) + return -ENOMEM; + + for (i = 0; i < nmsg; ++i) { + struct virtio_pcm_msg *msg = &substream->msgs[i]; + + msg->substream = substream; + + sg_init_table(msg->sgs, PCM_MSG_SG_MAX); + sg_init_one(&msg->sgs[PCM_MSG_SG_XFER], &msg->xfer, + sizeof(msg->xfer)); + sg_init_one(&msg->sgs[PCM_MSG_SG_DATA], + dma_area + period_bytes * i, period_bytes); + sg_init_one(&msg->sgs[PCM_MSG_SG_STATUS], &msg->status, + sizeof(msg->status)); + } + + return 0; +} + +/** + * virtsnd_pcm_msg_send() - Send asynchronous I/O messages. + * @substream: VirtIO PCM substream. + * + * All messages are organized in an ordered circular list. Each time the + * function is called, all currently non-enqueued messages are added to the + * virtqueue. For this, the function keeps track of two values: + * + * msg_last_enqueued = index of the last enqueued message, + * msg_count = # of pending messages in the virtqueue. + * + * Context: Any context. + * Return: 0 on success, -EIO on failure. + */ +int virtsnd_pcm_msg_send(struct virtio_pcm_substream *substream) +{ + struct snd_pcm_runtime *runtime = substream->substream->runtime; + struct virtio_snd *snd = substream->snd; + struct virtio_device *vdev = snd->vdev; + struct virtqueue *vqueue = virtsnd_pcm_queue(substream)->vqueue; + int i; + int n; + bool notify = false; + + if (!vqueue) + return -EIO; + + i = (substream->msg_last_enqueued + 1) % runtime->periods; + n = runtime->periods - atomic_read(&substream->msg_count); + + for (; n; --n, i = (i + 1) % runtime->periods) { + struct virtio_pcm_msg *msg = &substream->msgs[i]; + struct scatterlist *psgs[PCM_MSG_SG_MAX] = { + [PCM_MSG_SG_XFER] = &msg->sgs[PCM_MSG_SG_XFER], + [PCM_MSG_SG_DATA] = &msg->sgs[PCM_MSG_SG_DATA], + [PCM_MSG_SG_STATUS] = &msg->sgs[PCM_MSG_SG_STATUS] + }; + int rc; + + msg->xfer.stream_id = cpu_to_virtio32(vdev, substream->sid); + memset(&msg->status, 0, sizeof(msg->status)); + + atomic_inc(&substream->msg_count); + + if (substream->direction == SNDRV_PCM_STREAM_PLAYBACK) + rc = virtqueue_add_sgs(vqueue, psgs, 2, 1, msg, + GFP_ATOMIC); + else + rc = virtqueue_add_sgs(vqueue, psgs, 1, 2, msg, + GFP_ATOMIC); + + if (rc) { + atomic_dec(&substream->msg_count); + return -EIO; + } + + substream->msg_last_enqueued = i; + } + + if (!(substream->features & (1U << VIRTIO_SND_PCM_F_MSG_POLLING))) + notify = virtqueue_kick_prepare(vqueue); + + if (notify) + if (!virtqueue_notify(vqueue)) + return -EIO; + + return 0; +} + +/** + * virtsnd_pcm_msg_complete() - Complete an I/O message. + * @msg: I/O message. + * @size: Number of bytes written. + * + * Completion of the message means the elapsed period. + * + * The interrupt handler modifies three fields of the substream structure + * (hw_ptr, xfer_xrun, msg_count) that are used in operator functions. These + * values are atomic to avoid frequent interlocks with the interrupt handler. + * This becomes especially important in the case of multiple running substreams + * that share both the virtqueue and interrupt handler. + * + * Context: Interrupt context. + */ +static void virtsnd_pcm_msg_complete(struct virtio_pcm_msg *msg, size_t size) +{ + struct virtio_pcm_substream *substream = msg->substream; + snd_pcm_uframes_t hw_ptr; + unsigned int msg_count; + + /* + * 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. + */ + hw_ptr = (snd_pcm_uframes_t)atomic_read(&substream->hw_ptr); + + /* + * If the capture substream returned an incorrect status, then just + * increase the hw_ptr by the period size. + */ + if (substream->direction == SNDRV_PCM_STREAM_PLAYBACK || + size <= sizeof(msg->status)) { + hw_ptr += substream->period_size; + } else { + size -= sizeof(msg->status); + hw_ptr += size / substream->frame_bytes; + } + + atomic_set(&substream->hw_ptr, (u32)(hw_ptr % substream->buffer_size)); + atomic_set(&substream->xfer_xrun, 0); + + msg_count = atomic_dec_return(&substream->msg_count); + + if (atomic_read(&substream->xfer_enabled)) { + struct snd_pcm_runtime *runtime = substream->substream->runtime; + + runtime->delay = + bytes_to_frames(runtime, + le32_to_cpu(msg->status.latency_bytes)); + + snd_pcm_period_elapsed(substream->substream); + + virtsnd_pcm_msg_send(substream); + } else if (!msg_count) { + wake_up_all(&substream->msg_empty); + } +} + +/** + * virtsnd_pcm_notify_cb() - Process all completed I/O messages. + * @vqueue: Underlying tx/rx virtqueue. + * + * If transmission is allowed, then each completed message is immediately placed + * back at the end of the queue. + * + * Context: Interrupt context. Takes and releases the tx/rx queue spinlock. + */ +static inline void virtsnd_pcm_notify_cb(struct virtio_snd_queue *queue) +{ + unsigned long flags; + + spin_lock_irqsave(&queue->lock, flags); + while (queue->vqueue) { + virtqueue_disable_cb(queue->vqueue); + + for (;;) { + struct virtio_pcm_msg *msg; + u32 length; + + msg = virtqueue_get_buf(queue->vqueue, &length); + if (!msg) + break; + + virtsnd_pcm_msg_complete(msg, length); + } + + if (unlikely(virtqueue_is_broken(queue->vqueue))) + break; + + if (virtqueue_enable_cb(queue->vqueue)) + break; + } + spin_unlock_irqrestore(&queue->lock, flags); +} + +/** + * virtsnd_pcm_tx_notify_cb() - Process all completed TX messages. + * @vqueue: Underlying tx virtqueue. + * + * Context: Interrupt context. + */ +void virtsnd_pcm_tx_notify_cb(struct virtqueue *vqueue) +{ + struct virtio_snd *snd = vqueue->vdev->priv; + + virtsnd_pcm_notify_cb(virtsnd_tx_queue(snd)); +} + +/** + * virtsnd_pcm_rx_notify_cb() - Process all completed RX messages. + * @vqueue: Underlying rx virtqueue. + * + * Context: Interrupt context. + */ +void virtsnd_pcm_rx_notify_cb(struct virtqueue *vqueue) +{ + struct virtio_snd *snd = vqueue->vdev->priv; + + virtsnd_pcm_notify_cb(virtsnd_rx_queue(snd)); +} + +/** + * virtsnd_pcm_ctl_msg_alloc() - Allocate and initialize the PCM device control + * message for the specified substream. + * @substream: VirtIO PCM substream. + * @command: Control request code (VIRTIO_SND_R_PCM_XXX). + * @gfp: Kernel flags for memory allocation. + * + * Context: Any context. May sleep if @gfp flags permit. + * Return: Allocated message on success, ERR_PTR(-errno) on failure. + */ +struct virtio_snd_msg * +virtsnd_pcm_ctl_msg_alloc(struct virtio_pcm_substream *substream, + unsigned int command, gfp_t gfp) +{ + struct virtio_device *vdev = substream->snd->vdev; + size_t request_size = sizeof(struct virtio_snd_pcm_hdr); + size_t response_size = sizeof(struct virtio_snd_hdr); + struct virtio_snd_msg *msg; + + switch (command) { + case VIRTIO_SND_R_PCM_SET_PARAMS: { + request_size = sizeof(struct virtio_snd_pcm_set_params); + break; + } + } + + msg = virtsnd_ctl_msg_alloc(vdev, request_size, response_size, gfp); + if (!IS_ERR(msg)) { + struct virtio_snd_pcm_hdr *hdr = sg_virt(&msg->sg_request); + + hdr->hdr.code = cpu_to_virtio32(vdev, command); + hdr->stream_id = cpu_to_virtio32(vdev, substream->sid); + } + + return msg; +} -- 2.30.0 _______________________________________________ Virtualization mailing list Virtualization@lists.linux-foundation.org https://lists.linuxfoundation.org/mailman/listinfo/virtualization From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: virtio-dev-return-7947-cohuck=redhat.com@lists.oasis-open.org Sender: List-Post: List-Help: List-Unsubscribe: List-Subscribe: Received: from lists.oasis-open.org (oasis-open.org [10.110.1.242]) by lists.oasis-open.org (Postfix) with ESMTP id D278B985B9F for ; Sun, 24 Jan 2021 16:54:43 +0000 (UTC) From: Anton Yakovlev Date: Sun, 24 Jan 2021 17:54:04 +0100 Message-ID: <20210124165408.1122868-6-anton.yakovlev@opensynergy.com> In-Reply-To: <20210124165408.1122868-1-anton.yakovlev@opensynergy.com> References: <20210124165408.1122868-1-anton.yakovlev@opensynergy.com> MIME-Version: 1.0 Subject: [virtio-dev] [PATCH v2 5/9] ALSA: virtio: handling control and I/O messages for the PCM device Content-Type: text/plain Content-Transfer-Encoding: quoted-printable To: virtualization@lists.linux-foundation.org, alsa-devel@alsa-project.org, virtio-dev@lists.oasis-open.org Cc: "Michael S. Tsirkin" , Jaroslav Kysela , Takashi Iwai , linux-kernel@vger.kernel.org List-ID: 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. Signed-off-by: Anton Yakovlev --- sound/virtio/Makefile | 3 +- sound/virtio/virtio_card.c | 10 ++ sound/virtio/virtio_card.h | 9 + sound/virtio/virtio_pcm.c | 3 + sound/virtio/virtio_pcm.h | 31 ++++ sound/virtio/virtio_pcm_msg.c | 325 ++++++++++++++++++++++++++++++++++ 6 files changed, 380 insertions(+), 1 deletion(-) create mode 100644 sound/virtio/virtio_pcm_msg.c diff --git a/sound/virtio/Makefile b/sound/virtio/Makefile index 69162a545a41..626af3cc3ed7 100644 --- a/sound/virtio/Makefile +++ b/sound/virtio/Makefile @@ -5,5 +5,6 @@ obj-$(CONFIG_SND_VIRTIO) +=3D virtio_snd.o virtio_snd-objs :=3D \ =09virtio_card.o \ =09virtio_ctl_msg.o \ -=09virtio_pcm.o +=09virtio_pcm.o \ +=09virtio_pcm_msg.o =20 diff --git a/sound/virtio/virtio_card.c b/sound/virtio/virtio_card.c index 39fe13b43dd1..11d025ee77c2 100644 --- a/sound/virtio/virtio_card.c +++ b/sound/virtio/virtio_card.c @@ -143,6 +143,12 @@ static int virtsnd_find_vqs(struct virtio_snd *snd) =09callbacks[VIRTIO_SND_VQ_CONTROL] =3D virtsnd_ctl_notify_cb; =09callbacks[VIRTIO_SND_VQ_EVENT] =3D virtsnd_event_notify_cb; =20 +=09virtio_cread(vdev, struct virtio_snd_config, streams, &n); +=09if (n) { +=09=09callbacks[VIRTIO_SND_VQ_TX] =3D virtsnd_pcm_tx_notify_cb; +=09=09callbacks[VIRTIO_SND_VQ_RX] =3D virtsnd_pcm_rx_notify_cb; +=09} + =09rc =3D virtio_find_vqs(vdev, VIRTIO_SND_VQ_MAX, vqs, callbacks, names, =09=09=09 NULL); =09if (rc) { @@ -177,6 +183,10 @@ static int virtsnd_find_vqs(struct virtio_snd *snd) * virtsnd_enable_event_vq() - Enable the event virtqueue. * @snd: VirtIO sound device. * + * The tx queue is enabled only if the device supports playback stream(s). + * + * The rx queue is enabled only if the device supports capture stream(s). + * * Context: Any context. */ static void virtsnd_enable_event_vq(struct virtio_snd *snd) diff --git a/sound/virtio/virtio_card.h b/sound/virtio/virtio_card.h index be6651a6aaf8..b11c09984882 100644 --- a/sound/virtio/virtio_card.h +++ b/sound/virtio/virtio_card.h @@ -89,4 +89,13 @@ virtsnd_rx_queue(struct virtio_snd *snd) =09return &snd->queues[VIRTIO_SND_VQ_RX]; } =20 +static inline struct virtio_snd_queue * +virtsnd_pcm_queue(struct virtio_pcm_substream *substream) +{ +=09if (substream->direction =3D=3D SNDRV_PCM_STREAM_PLAYBACK) +=09=09return virtsnd_tx_queue(substream->snd); +=09else +=09=09return virtsnd_rx_queue(substream->snd); +} + #endif /* VIRTIO_SND_CARD_H */ diff --git a/sound/virtio/virtio_pcm.c b/sound/virtio/virtio_pcm.c index 036990b7b78a..1ab50dcc88c8 100644 --- a/sound/virtio/virtio_pcm.c +++ b/sound/virtio/virtio_pcm.c @@ -376,6 +376,7 @@ int virtsnd_pcm_parse_cfg(struct virtio_snd *snd) =20 =09=09substream->snd =3D snd; =09=09substream->sid =3D i; +=09=09init_waitqueue_head(&substream->msg_empty); =20 =09=09rc =3D virtsnd_pcm_build_hw(substream, &info[i]); =09=09if (rc) @@ -530,6 +531,8 @@ void virtsnd_pcm_event(struct virtio_snd *snd, struct v= irtio_snd_event *event) =09=09break; =09} =09case VIRTIO_SND_EVT_PCM_XRUN: { +=09=09if (atomic_read(&substream->xfer_enabled)) +=09=09=09atomic_set(&substream->xfer_xrun, 1); =09=09break; =09} =09} diff --git a/sound/virtio/virtio_pcm.h b/sound/virtio/virtio_pcm.h index 73fb4d9dc524..d011b7e1d18d 100644 --- a/sound/virtio/virtio_pcm.h +++ b/sound/virtio/virtio_pcm.h @@ -24,6 +24,7 @@ #include =20 struct virtio_pcm; +struct virtio_pcm_msg; =20 /** * struct virtio_pcm_substream - VirtIO PCM substream. @@ -34,6 +35,16 @@ struct virtio_pcm; * @features: Stream VirtIO feature bit map (1 << VIRTIO_SND_PCM_F_XXX). * @substream: Kernel ALSA substream. * @hw: Kernel ALSA substream hardware descriptor. + * @frame_bytes: Current frame size in bytes. + * @period_size: Current period size in frames. + * @buffer_size: Current buffer size in frames. + * @hw_ptr: Substream hardware pointer value in frames [0 ... buffer_size)= . + * @xfer_enabled: Data transfer state (0 - off, 1 - on). + * @xfer_xrun: Data underflow/overflow state (0 - no xrun, 1 - xrun). + * @msgs: I/O messages. + * @msg_last_enqueued: Index of the last I/O message added to the virtqueu= e. + * @msg_count: Number of pending I/O messages in the virtqueue. + * @msg_empty: Notify when msg_count is zero. */ struct virtio_pcm_substream { =09struct virtio_snd *snd; @@ -43,6 +54,16 @@ struct virtio_pcm_substream { =09u32 features; =09struct snd_pcm_substream *substream; =09struct snd_pcm_hardware hw; +=09unsigned int frame_bytes; +=09snd_pcm_uframes_t period_size; +=09snd_pcm_uframes_t buffer_size; +=09atomic_t hw_ptr; +=09atomic_t xfer_enabled; +=09atomic_t xfer_xrun; +=09struct virtio_pcm_msg *msgs; +=09int msg_last_enqueued; +=09atomic_t msg_count; +=09wait_queue_head_t msg_empty; }; =20 /** @@ -86,4 +107,14 @@ struct virtio_pcm *virtsnd_pcm_find(struct virtio_snd *= snd, unsigned int nid); struct virtio_pcm *virtsnd_pcm_find_or_create(struct virtio_snd *snd, =09=09=09=09=09 unsigned int nid); =20 +struct virtio_snd_msg * +virtsnd_pcm_ctl_msg_alloc(struct virtio_pcm_substream *substream, +=09=09=09 unsigned int command, gfp_t gfp); + +int virtsnd_pcm_msg_alloc(struct virtio_pcm_substream *substream, +=09=09=09 unsigned int nmsg, u8 *dma_area, +=09=09=09 unsigned int period_bytes); + +int virtsnd_pcm_msg_send(struct virtio_pcm_substream *substream); + #endif /* VIRTIO_SND_PCM_H */ diff --git a/sound/virtio/virtio_pcm_msg.c b/sound/virtio/virtio_pcm_msg.c new file mode 100644 index 000000000000..d524e7cbe43f --- /dev/null +++ b/sound/virtio/virtio_pcm_msg.c @@ -0,0 +1,325 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Sound card driver for virtio + * Copyright (C) 2020 OpenSynergy GmbH + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ +#include + +#include "virtio_card.h" + +/** + * enum pcm_msg_sg_index - Scatter-gather element indexes for an I/O messa= ge. + * @PCM_MSG_SG_XFER: Element containing a virtio_snd_pcm_xfer structure. + * @PCM_MSG_SG_DATA: Element containing a data buffer. + * @PCM_MSG_SG_STATUS: Element containing a virtio_snd_pcm_status structur= e. + * @PCM_MSG_SG_MAX: The maximum number of elements in the scatter-gather t= able. + * + * These values are used as the index of the payload scatter-gather table. + */ +enum pcm_msg_sg_index { +=09PCM_MSG_SG_XFER =3D 0, +=09PCM_MSG_SG_DATA, +=09PCM_MSG_SG_STATUS, +=09PCM_MSG_SG_MAX +}; + +/** + * struct virtio_pcm_msg - VirtIO I/O message. + * @substream: VirtIO PCM substream. + * @xfer: Request header payload. + * @status: Response header payload. + * @sgs: Payload scatter-gather table. + */ +struct virtio_pcm_msg { +=09struct virtio_pcm_substream *substream; +=09struct virtio_snd_pcm_xfer xfer; +=09struct virtio_snd_pcm_status status; +=09struct scatterlist sgs[PCM_MSG_SG_MAX]; +}; + +/** + * virtsnd_pcm_msg_alloc() - Allocate I/O messages. + * @substream: VirtIO PCM substream. + * @nmsg: Number of messages (equal to the number of periods). + * @dma_area: Pointer to used audio buffer. + * @period_bytes: Period (message payload) size. + * + * The function slices the buffer into nmsg parts (each with the size of + * period_bytes), and creates nmsg corresponding I/O messages. + * + * Context: Any context that permits to sleep. + * Return: 0 on success, -ENOMEM on failure. + */ +int virtsnd_pcm_msg_alloc(struct virtio_pcm_substream *substream, +=09=09=09 unsigned int nmsg, u8 *dma_area, +=09=09=09 unsigned int period_bytes) +{ +=09struct virtio_device *vdev =3D substream->snd->vdev; +=09unsigned int i; + +=09if (substream->msgs) +=09=09devm_kfree(&vdev->dev, substream->msgs); + +=09substream->msgs =3D devm_kcalloc(&vdev->dev, nmsg, +=09=09=09=09 sizeof(*substream->msgs), GFP_KERNEL); +=09if (!substream->msgs) +=09=09return -ENOMEM; + +=09for (i =3D 0; i < nmsg; ++i) { +=09=09struct virtio_pcm_msg *msg =3D &substream->msgs[i]; + +=09=09msg->substream =3D substream; + +=09=09sg_init_table(msg->sgs, PCM_MSG_SG_MAX); +=09=09sg_init_one(&msg->sgs[PCM_MSG_SG_XFER], &msg->xfer, +=09=09=09 sizeof(msg->xfer)); +=09=09sg_init_one(&msg->sgs[PCM_MSG_SG_DATA], +=09=09=09 dma_area + period_bytes * i, period_bytes); +=09=09sg_init_one(&msg->sgs[PCM_MSG_SG_STATUS], &msg->status, +=09=09=09 sizeof(msg->status)); +=09} + +=09return 0; +} + +/** + * virtsnd_pcm_msg_send() - Send asynchronous I/O messages. + * @substream: VirtIO PCM substream. + * + * All messages are organized in an ordered circular list. Each time the + * function is called, all currently non-enqueued messages are added to th= e + * virtqueue. For this, the function keeps track of two values: + * + * msg_last_enqueued =3D index of the last enqueued message, + * msg_count =3D # of pending messages in the virtqueue. + * + * Context: Any context. + * Return: 0 on success, -EIO on failure. + */ +int virtsnd_pcm_msg_send(struct virtio_pcm_substream *substream) +{ +=09struct snd_pcm_runtime *runtime =3D substream->substream->runtime; +=09struct virtio_snd *snd =3D substream->snd; +=09struct virtio_device *vdev =3D snd->vdev; +=09struct virtqueue *vqueue =3D virtsnd_pcm_queue(substream)->vqueue; +=09int i; +=09int n; +=09bool notify =3D false; + +=09if (!vqueue) +=09=09return -EIO; + +=09i =3D (substream->msg_last_enqueued + 1) % runtime->periods; +=09n =3D runtime->periods - atomic_read(&substream->msg_count); + +=09for (; n; --n, i =3D (i + 1) % runtime->periods) { +=09=09struct virtio_pcm_msg *msg =3D &substream->msgs[i]; +=09=09struct scatterlist *psgs[PCM_MSG_SG_MAX] =3D { +=09=09=09[PCM_MSG_SG_XFER] =3D &msg->sgs[PCM_MSG_SG_XFER], +=09=09=09[PCM_MSG_SG_DATA] =3D &msg->sgs[PCM_MSG_SG_DATA], +=09=09=09[PCM_MSG_SG_STATUS] =3D &msg->sgs[PCM_MSG_SG_STATUS] +=09=09}; +=09=09int rc; + +=09=09msg->xfer.stream_id =3D cpu_to_virtio32(vdev, substream->sid); +=09=09memset(&msg->status, 0, sizeof(msg->status)); + +=09=09atomic_inc(&substream->msg_count); + +=09=09if (substream->direction =3D=3D SNDRV_PCM_STREAM_PLAYBACK) +=09=09=09rc =3D virtqueue_add_sgs(vqueue, psgs, 2, 1, msg, +=09=09=09=09=09 GFP_ATOMIC); +=09=09else +=09=09=09rc =3D virtqueue_add_sgs(vqueue, psgs, 1, 2, msg, +=09=09=09=09=09 GFP_ATOMIC); + +=09=09if (rc) { +=09=09=09atomic_dec(&substream->msg_count); +=09=09=09return -EIO; +=09=09} + +=09=09substream->msg_last_enqueued =3D i; +=09} + +=09if (!(substream->features & (1U << VIRTIO_SND_PCM_F_MSG_POLLING))) +=09=09notify =3D virtqueue_kick_prepare(vqueue); + +=09if (notify) +=09=09if (!virtqueue_notify(vqueue)) +=09=09=09return -EIO; + +=09return 0; +} + +/** + * virtsnd_pcm_msg_complete() - Complete an I/O message. + * @msg: I/O message. + * @size: Number of bytes written. + * + * Completion of the message means the elapsed period. + * + * The interrupt handler modifies three fields of the substream structure + * (hw_ptr, xfer_xrun, msg_count) that are used in operator functions. The= se + * values are atomic to avoid frequent interlocks with the interrupt handl= er. + * This becomes especially important in the case of multiple running subst= reams + * that share both the virtqueue and interrupt handler. + * + * Context: Interrupt context. + */ +static void virtsnd_pcm_msg_complete(struct virtio_pcm_msg *msg, size_t si= ze) +{ +=09struct virtio_pcm_substream *substream =3D msg->substream; +=09snd_pcm_uframes_t hw_ptr; +=09unsigned int msg_count; + +=09/* +=09 * hw_ptr always indicates the buffer position of the first I/O message +=09 * in the virtqueue. Therefore, on each completion of an I/O message, +=09 * the hw_ptr value is unconditionally advanced. +=09 */ +=09hw_ptr =3D (snd_pcm_uframes_t)atomic_read(&substream->hw_ptr); + +=09/* +=09 * If the capture substream returned an incorrect status, then just +=09 * increase the hw_ptr by the period size. +=09 */ +=09if (substream->direction =3D=3D SNDRV_PCM_STREAM_PLAYBACK || +=09 size <=3D sizeof(msg->status)) { +=09=09hw_ptr +=3D substream->period_size; +=09} else { +=09=09size -=3D sizeof(msg->status); +=09=09hw_ptr +=3D size / substream->frame_bytes; +=09} + +=09atomic_set(&substream->hw_ptr, (u32)(hw_ptr % substream->buffer_size)); +=09atomic_set(&substream->xfer_xrun, 0); + +=09msg_count =3D atomic_dec_return(&substream->msg_count); + +=09if (atomic_read(&substream->xfer_enabled)) { +=09=09struct snd_pcm_runtime *runtime =3D substream->substream->runtime; + +=09=09runtime->delay =3D +=09=09=09bytes_to_frames(runtime, +=09=09=09=09=09le32_to_cpu(msg->status.latency_bytes)); + +=09=09snd_pcm_period_elapsed(substream->substream); + +=09=09virtsnd_pcm_msg_send(substream); +=09} else if (!msg_count) { +=09=09wake_up_all(&substream->msg_empty); +=09} +} + +/** + * virtsnd_pcm_notify_cb() - Process all completed I/O messages. + * @vqueue: Underlying tx/rx virtqueue. + * + * If transmission is allowed, then each completed message is immediately = placed + * back at the end of the queue. + * + * Context: Interrupt context. Takes and releases the tx/rx queue spinlock= . + */ +static inline void virtsnd_pcm_notify_cb(struct virtio_snd_queue *queue) +{ +=09unsigned long flags; + +=09spin_lock_irqsave(&queue->lock, flags); +=09while (queue->vqueue) { +=09=09virtqueue_disable_cb(queue->vqueue); + +=09=09for (;;) { +=09=09=09struct virtio_pcm_msg *msg; +=09=09=09u32 length; + +=09=09=09msg =3D virtqueue_get_buf(queue->vqueue, &length); +=09=09=09if (!msg) +=09=09=09=09break; + +=09=09=09virtsnd_pcm_msg_complete(msg, length); +=09=09} + +=09=09if (unlikely(virtqueue_is_broken(queue->vqueue))) +=09=09=09break; + +=09=09if (virtqueue_enable_cb(queue->vqueue)) +=09=09=09break; +=09} +=09spin_unlock_irqrestore(&queue->lock, flags); +} + +/** + * virtsnd_pcm_tx_notify_cb() - Process all completed TX messages. + * @vqueue: Underlying tx virtqueue. + * + * Context: Interrupt context. + */ +void virtsnd_pcm_tx_notify_cb(struct virtqueue *vqueue) +{ +=09struct virtio_snd *snd =3D vqueue->vdev->priv; + +=09virtsnd_pcm_notify_cb(virtsnd_tx_queue(snd)); +} + +/** + * virtsnd_pcm_rx_notify_cb() - Process all completed RX messages. + * @vqueue: Underlying rx virtqueue. + * + * Context: Interrupt context. + */ +void virtsnd_pcm_rx_notify_cb(struct virtqueue *vqueue) +{ +=09struct virtio_snd *snd =3D vqueue->vdev->priv; + +=09virtsnd_pcm_notify_cb(virtsnd_rx_queue(snd)); +} + +/** + * virtsnd_pcm_ctl_msg_alloc() - Allocate and initialize the PCM device co= ntrol + * message for the specified substream. + * @substream: VirtIO PCM substream. + * @command: Control request code (VIRTIO_SND_R_PCM_XXX). + * @gfp: Kernel flags for memory allocation. + * + * Context: Any context. May sleep if @gfp flags permit. + * Return: Allocated message on success, ERR_PTR(-errno) on failure. + */ +struct virtio_snd_msg * +virtsnd_pcm_ctl_msg_alloc(struct virtio_pcm_substream *substream, +=09=09=09 unsigned int command, gfp_t gfp) +{ +=09struct virtio_device *vdev =3D substream->snd->vdev; +=09size_t request_size =3D sizeof(struct virtio_snd_pcm_hdr); +=09size_t response_size =3D sizeof(struct virtio_snd_hdr); +=09struct virtio_snd_msg *msg; + +=09switch (command) { +=09case VIRTIO_SND_R_PCM_SET_PARAMS: { +=09=09request_size =3D sizeof(struct virtio_snd_pcm_set_params); +=09=09break; +=09} +=09} + +=09msg =3D virtsnd_ctl_msg_alloc(vdev, request_size, response_size, gfp); +=09if (!IS_ERR(msg)) { +=09=09struct virtio_snd_pcm_hdr *hdr =3D sg_virt(&msg->sg_request); + +=09=09hdr->hdr.code =3D cpu_to_virtio32(vdev, command); +=09=09hdr->stream_id =3D cpu_to_virtio32(vdev, substream->sid); +=09} + +=09return msg; +} --=20 2.30.0 --------------------------------------------------------------------- To unsubscribe, e-mail: virtio-dev-unsubscribe@lists.oasis-open.org For additional commands, e-mail: virtio-dev-help@lists.oasis-open.org