Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * virtio-snd: Virtio sound device
0004  * Copyright (C) 2021 OpenSynergy GmbH
0005  */
0006 #include <sound/pcm_params.h>
0007 
0008 #include "virtio_card.h"
0009 
0010 /**
0011  * struct virtio_pcm_msg - VirtIO I/O message.
0012  * @substream: VirtIO PCM substream.
0013  * @xfer: Request header payload.
0014  * @status: Response header payload.
0015  * @length: Data length in bytes.
0016  * @sgs: Payload scatter-gather table.
0017  */
0018 struct virtio_pcm_msg {
0019     struct virtio_pcm_substream *substream;
0020     struct virtio_snd_pcm_xfer xfer;
0021     struct virtio_snd_pcm_status status;
0022     size_t length;
0023     struct scatterlist sgs[];
0024 };
0025 
0026 /**
0027  * enum pcm_msg_sg_index - Index values for the virtio_pcm_msg->sgs field in
0028  *                         an I/O message.
0029  * @PCM_MSG_SG_XFER: Element containing a virtio_snd_pcm_xfer structure.
0030  * @PCM_MSG_SG_STATUS: Element containing a virtio_snd_pcm_status structure.
0031  * @PCM_MSG_SG_DATA: The first element containing a data buffer.
0032  */
0033 enum pcm_msg_sg_index {
0034     PCM_MSG_SG_XFER = 0,
0035     PCM_MSG_SG_STATUS,
0036     PCM_MSG_SG_DATA
0037 };
0038 
0039 /**
0040  * virtsnd_pcm_sg_num() - Count the number of sg-elements required to represent
0041  *                        vmalloc'ed buffer.
0042  * @data: Pointer to vmalloc'ed buffer.
0043  * @length: Buffer size.
0044  *
0045  * Context: Any context.
0046  * Return: Number of physically contiguous parts in the @data.
0047  */
0048 static int virtsnd_pcm_sg_num(u8 *data, unsigned int length)
0049 {
0050     phys_addr_t sg_address;
0051     unsigned int sg_length;
0052     int num = 0;
0053 
0054     while (length) {
0055         struct page *pg = vmalloc_to_page(data);
0056         phys_addr_t pg_address = page_to_phys(pg);
0057         size_t pg_length;
0058 
0059         pg_length = PAGE_SIZE - offset_in_page(data);
0060         if (pg_length > length)
0061             pg_length = length;
0062 
0063         if (!num || sg_address + sg_length != pg_address) {
0064             sg_address = pg_address;
0065             sg_length = pg_length;
0066             num++;
0067         } else {
0068             sg_length += pg_length;
0069         }
0070 
0071         data += pg_length;
0072         length -= pg_length;
0073     }
0074 
0075     return num;
0076 }
0077 
0078 /**
0079  * virtsnd_pcm_sg_from() - Build sg-list from vmalloc'ed buffer.
0080  * @sgs: Preallocated sg-list to populate.
0081  * @nsgs: The maximum number of elements in the @sgs.
0082  * @data: Pointer to vmalloc'ed buffer.
0083  * @length: Buffer size.
0084  *
0085  * Splits the buffer into physically contiguous parts and makes an sg-list of
0086  * such parts.
0087  *
0088  * Context: Any context.
0089  */
0090 static void virtsnd_pcm_sg_from(struct scatterlist *sgs, int nsgs, u8 *data,
0091                 unsigned int length)
0092 {
0093     int idx = -1;
0094 
0095     while (length) {
0096         struct page *pg = vmalloc_to_page(data);
0097         size_t pg_length;
0098 
0099         pg_length = PAGE_SIZE - offset_in_page(data);
0100         if (pg_length > length)
0101             pg_length = length;
0102 
0103         if (idx == -1 ||
0104             sg_phys(&sgs[idx]) + sgs[idx].length != page_to_phys(pg)) {
0105             if (idx + 1 == nsgs)
0106                 break;
0107             sg_set_page(&sgs[++idx], pg, pg_length,
0108                     offset_in_page(data));
0109         } else {
0110             sgs[idx].length += pg_length;
0111         }
0112 
0113         data += pg_length;
0114         length -= pg_length;
0115     }
0116 
0117     sg_mark_end(&sgs[idx]);
0118 }
0119 
0120 /**
0121  * virtsnd_pcm_msg_alloc() - Allocate I/O messages.
0122  * @vss: VirtIO PCM substream.
0123  * @periods: Current number of periods.
0124  * @period_bytes: Current period size in bytes.
0125  *
0126  * The function slices the buffer into @periods parts (each with the size of
0127  * @period_bytes), and creates @periods corresponding I/O messages.
0128  *
0129  * Context: Any context that permits to sleep.
0130  * Return: 0 on success, -ENOMEM on failure.
0131  */
0132 int virtsnd_pcm_msg_alloc(struct virtio_pcm_substream *vss,
0133               unsigned int periods, unsigned int period_bytes)
0134 {
0135     struct snd_pcm_runtime *runtime = vss->substream->runtime;
0136     unsigned int i;
0137 
0138     vss->msgs = kcalloc(periods, sizeof(*vss->msgs), GFP_KERNEL);
0139     if (!vss->msgs)
0140         return -ENOMEM;
0141 
0142     vss->nmsgs = periods;
0143 
0144     for (i = 0; i < periods; ++i) {
0145         u8 *data = runtime->dma_area + period_bytes * i;
0146         int sg_num = virtsnd_pcm_sg_num(data, period_bytes);
0147         struct virtio_pcm_msg *msg;
0148 
0149         msg = kzalloc(struct_size(msg, sgs, sg_num + 2), GFP_KERNEL);
0150         if (!msg)
0151             return -ENOMEM;
0152 
0153         msg->substream = vss;
0154         sg_init_one(&msg->sgs[PCM_MSG_SG_XFER], &msg->xfer,
0155                 sizeof(msg->xfer));
0156         sg_init_one(&msg->sgs[PCM_MSG_SG_STATUS], &msg->status,
0157                 sizeof(msg->status));
0158         msg->length = period_bytes;
0159         virtsnd_pcm_sg_from(&msg->sgs[PCM_MSG_SG_DATA], sg_num, data,
0160                     period_bytes);
0161 
0162         vss->msgs[i] = msg;
0163     }
0164 
0165     return 0;
0166 }
0167 
0168 /**
0169  * virtsnd_pcm_msg_free() - Free all allocated I/O messages.
0170  * @vss: VirtIO PCM substream.
0171  *
0172  * Context: Any context.
0173  */
0174 void virtsnd_pcm_msg_free(struct virtio_pcm_substream *vss)
0175 {
0176     unsigned int i;
0177 
0178     for (i = 0; vss->msgs && i < vss->nmsgs; ++i)
0179         kfree(vss->msgs[i]);
0180     kfree(vss->msgs);
0181 
0182     vss->msgs = NULL;
0183     vss->nmsgs = 0;
0184 }
0185 
0186 /**
0187  * virtsnd_pcm_msg_send() - Send asynchronous I/O messages.
0188  * @vss: VirtIO PCM substream.
0189  *
0190  * All messages are organized in an ordered circular list. Each time the
0191  * function is called, all currently non-enqueued messages are added to the
0192  * virtqueue. For this, the function keeps track of two values:
0193  *
0194  *   msg_last_enqueued = index of the last enqueued message,
0195  *   msg_count = # of pending messages in the virtqueue.
0196  *
0197  * Context: Any context. Expects the tx/rx queue and the VirtIO substream
0198  *          spinlocks to be held by caller.
0199  * Return: 0 on success, -errno on failure.
0200  */
0201 int virtsnd_pcm_msg_send(struct virtio_pcm_substream *vss)
0202 {
0203     struct snd_pcm_runtime *runtime = vss->substream->runtime;
0204     struct virtio_snd *snd = vss->snd;
0205     struct virtio_device *vdev = snd->vdev;
0206     struct virtqueue *vqueue = virtsnd_pcm_queue(vss)->vqueue;
0207     int i;
0208     int n;
0209     bool notify = false;
0210 
0211     i = (vss->msg_last_enqueued + 1) % runtime->periods;
0212     n = runtime->periods - vss->msg_count;
0213 
0214     for (; n; --n, i = (i + 1) % runtime->periods) {
0215         struct virtio_pcm_msg *msg = vss->msgs[i];
0216         struct scatterlist *psgs[] = {
0217             &msg->sgs[PCM_MSG_SG_XFER],
0218             &msg->sgs[PCM_MSG_SG_DATA],
0219             &msg->sgs[PCM_MSG_SG_STATUS]
0220         };
0221         int rc;
0222 
0223         msg->xfer.stream_id = cpu_to_le32(vss->sid);
0224         memset(&msg->status, 0, sizeof(msg->status));
0225 
0226         if (vss->direction == SNDRV_PCM_STREAM_PLAYBACK)
0227             rc = virtqueue_add_sgs(vqueue, psgs, 2, 1, msg,
0228                            GFP_ATOMIC);
0229         else
0230             rc = virtqueue_add_sgs(vqueue, psgs, 1, 2, msg,
0231                            GFP_ATOMIC);
0232 
0233         if (rc) {
0234             dev_err(&vdev->dev,
0235                 "SID %u: failed to send I/O message\n",
0236                 vss->sid);
0237             return rc;
0238         }
0239 
0240         vss->msg_last_enqueued = i;
0241         vss->msg_count++;
0242     }
0243 
0244     if (!(vss->features & (1U << VIRTIO_SND_PCM_F_MSG_POLLING)))
0245         notify = virtqueue_kick_prepare(vqueue);
0246 
0247     if (notify)
0248         virtqueue_notify(vqueue);
0249 
0250     return 0;
0251 }
0252 
0253 /**
0254  * virtsnd_pcm_msg_pending_num() - Returns the number of pending I/O messages.
0255  * @vss: VirtIO substream.
0256  *
0257  * Context: Any context.
0258  * Return: Number of messages.
0259  */
0260 unsigned int virtsnd_pcm_msg_pending_num(struct virtio_pcm_substream *vss)
0261 {
0262     unsigned int num;
0263     unsigned long flags;
0264 
0265     spin_lock_irqsave(&vss->lock, flags);
0266     num = vss->msg_count;
0267     spin_unlock_irqrestore(&vss->lock, flags);
0268 
0269     return num;
0270 }
0271 
0272 /**
0273  * virtsnd_pcm_msg_complete() - Complete an I/O message.
0274  * @msg: I/O message.
0275  * @written_bytes: Number of bytes written to the message.
0276  *
0277  * Completion of the message means the elapsed period. If transmission is
0278  * allowed, then each completed message is immediately placed back at the end
0279  * of the queue.
0280  *
0281  * For the playback substream, @written_bytes is equal to sizeof(msg->status).
0282  *
0283  * For the capture substream, @written_bytes is equal to sizeof(msg->status)
0284  * plus the number of captured bytes.
0285  *
0286  * Context: Interrupt context. Takes and releases the VirtIO substream spinlock.
0287  */
0288 static void virtsnd_pcm_msg_complete(struct virtio_pcm_msg *msg,
0289                      size_t written_bytes)
0290 {
0291     struct virtio_pcm_substream *vss = msg->substream;
0292 
0293     /*
0294      * hw_ptr always indicates the buffer position of the first I/O message
0295      * in the virtqueue. Therefore, on each completion of an I/O message,
0296      * the hw_ptr value is unconditionally advanced.
0297      */
0298     spin_lock(&vss->lock);
0299     /*
0300      * If the capture substream returned an incorrect status, then just
0301      * increase the hw_ptr by the message size.
0302      */
0303     if (vss->direction == SNDRV_PCM_STREAM_PLAYBACK ||
0304         written_bytes <= sizeof(msg->status))
0305         vss->hw_ptr += msg->length;
0306     else
0307         vss->hw_ptr += written_bytes - sizeof(msg->status);
0308 
0309     if (vss->hw_ptr >= vss->buffer_bytes)
0310         vss->hw_ptr -= vss->buffer_bytes;
0311 
0312     vss->xfer_xrun = false;
0313     vss->msg_count--;
0314 
0315     if (vss->xfer_enabled) {
0316         struct snd_pcm_runtime *runtime = vss->substream->runtime;
0317 
0318         runtime->delay =
0319             bytes_to_frames(runtime,
0320                     le32_to_cpu(msg->status.latency_bytes));
0321 
0322         schedule_work(&vss->elapsed_period);
0323 
0324         virtsnd_pcm_msg_send(vss);
0325     } else if (!vss->msg_count) {
0326         wake_up_all(&vss->msg_empty);
0327     }
0328     spin_unlock(&vss->lock);
0329 }
0330 
0331 /**
0332  * virtsnd_pcm_notify_cb() - Process all completed I/O messages.
0333  * @queue: Underlying tx/rx virtqueue.
0334  *
0335  * Context: Interrupt context. Takes and releases the tx/rx queue spinlock.
0336  */
0337 static inline void virtsnd_pcm_notify_cb(struct virtio_snd_queue *queue)
0338 {
0339     struct virtio_pcm_msg *msg;
0340     u32 written_bytes;
0341     unsigned long flags;
0342 
0343     spin_lock_irqsave(&queue->lock, flags);
0344     do {
0345         virtqueue_disable_cb(queue->vqueue);
0346         while ((msg = virtqueue_get_buf(queue->vqueue, &written_bytes)))
0347             virtsnd_pcm_msg_complete(msg, written_bytes);
0348         if (unlikely(virtqueue_is_broken(queue->vqueue)))
0349             break;
0350     } while (!virtqueue_enable_cb(queue->vqueue));
0351     spin_unlock_irqrestore(&queue->lock, flags);
0352 }
0353 
0354 /**
0355  * virtsnd_pcm_tx_notify_cb() - Process all completed TX messages.
0356  * @vqueue: Underlying tx virtqueue.
0357  *
0358  * Context: Interrupt context.
0359  */
0360 void virtsnd_pcm_tx_notify_cb(struct virtqueue *vqueue)
0361 {
0362     struct virtio_snd *snd = vqueue->vdev->priv;
0363 
0364     virtsnd_pcm_notify_cb(virtsnd_tx_queue(snd));
0365 }
0366 
0367 /**
0368  * virtsnd_pcm_rx_notify_cb() - Process all completed RX messages.
0369  * @vqueue: Underlying rx virtqueue.
0370  *
0371  * Context: Interrupt context.
0372  */
0373 void virtsnd_pcm_rx_notify_cb(struct virtqueue *vqueue)
0374 {
0375     struct virtio_snd *snd = vqueue->vdev->priv;
0376 
0377     virtsnd_pcm_notify_cb(virtsnd_rx_queue(snd));
0378 }
0379 
0380 /**
0381  * virtsnd_pcm_ctl_msg_alloc() - Allocate and initialize the PCM device control
0382  *                               message for the specified substream.
0383  * @vss: VirtIO PCM substream.
0384  * @command: Control request code (VIRTIO_SND_R_PCM_XXX).
0385  * @gfp: Kernel flags for memory allocation.
0386  *
0387  * Context: Any context. May sleep if @gfp flags permit.
0388  * Return: Allocated message on success, NULL on failure.
0389  */
0390 struct virtio_snd_msg *
0391 virtsnd_pcm_ctl_msg_alloc(struct virtio_pcm_substream *vss,
0392               unsigned int command, gfp_t gfp)
0393 {
0394     size_t request_size = sizeof(struct virtio_snd_pcm_hdr);
0395     size_t response_size = sizeof(struct virtio_snd_hdr);
0396     struct virtio_snd_msg *msg;
0397 
0398     switch (command) {
0399     case VIRTIO_SND_R_PCM_SET_PARAMS:
0400         request_size = sizeof(struct virtio_snd_pcm_set_params);
0401         break;
0402     }
0403 
0404     msg = virtsnd_ctl_msg_alloc(request_size, response_size, gfp);
0405     if (msg) {
0406         struct virtio_snd_pcm_hdr *hdr = virtsnd_ctl_msg_request(msg);
0407 
0408         hdr->hdr.code = cpu_to_le32(command);
0409         hdr->stream_id = cpu_to_le32(vss->sid);
0410     }
0411 
0412     return msg;
0413 }