virtio: Backport virtio sound driver.
[AGL/meta-agl.git] / meta-agl-bsp / virtualization-layer / recipes-kernel / linux / linux-yocto / virtio-kmeta / bsp / virtio / virtio-snd / 0005-ALSA-virtio-handling-control-and-I-O-messages-for-th.patch
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
5  device
6
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
11 size of one period.
12
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.
17
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>
22 ---
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
31
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
37  virtio_snd-objs := \
38         virtio_card.o \
39         virtio_ctl_msg.o \
40 -       virtio_pcm.o
41 +       virtio_pcm.o \
42 +       virtio_pcm_msg.o
43  
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)
51  {
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);
56 +               break;
57 +       }
58  }
59  
60  /**
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
69         };
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"
76         };
77         struct virtqueue *vqs[VIRTIO_SND_VQ_MAX] = { 0 };
78         unsigned int i;
79 @@ -318,8 +328,12 @@ static void virtsnd_remove(struct virtio_device *vdev)
80         vdev->config->del_vqs(vdev);
81         vdev->config->reset(vdev);
82  
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];
87 +
88 +               cancel_work_sync(&vss->elapsed_period);
89 +               virtsnd_pcm_msg_free(vss);
90 +       }
91  
92         kfree(snd->event_msgs);
93  }
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];
100  }
101  
102 +static inline struct virtio_snd_queue *
103 +virtsnd_pcm_queue(struct virtio_pcm_substream *vss)
104 +{
105 +       if (vss->direction == SNDRV_PCM_STREAM_PLAYBACK)
106 +               return virtsnd_tx_queue(vss->snd);
107 +       else
108 +               return virtsnd_rx_queue(vss->snd);
109 +}
110 +
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)
117                 vss->snd = snd;
118                 vss->sid = i;
119                 INIT_WORK(&vss->elapsed_period, virtsnd_pcm_period_elapsed);
120 +               init_waitqueue_head(&vss->msg_empty);
121 +               spin_lock_init(&vss->lock);
122  
123                 rc = virtsnd_pcm_build_hw(vss, &info[i]);
124                 if (rc)
125 @@ -477,3 +479,33 @@ int virtsnd_pcm_build_devs(struct virtio_snd *snd)
126  
127         return 0;
128  }
129 +
130 +/**
131 + * virtsnd_pcm_event() - Handle the PCM device event notification.
132 + * @snd: VirtIO sound device.
133 + * @event: VirtIO sound event.
134 + *
135 + * Context: Interrupt context.
136 + */
137 +void virtsnd_pcm_event(struct virtio_snd *snd, struct virtio_snd_event *event)
138 +{
139 +       struct virtio_pcm_substream *vss;
140 +       u32 sid = le32_to_cpu(event->data);
141 +
142 +       if (sid >= snd->nsubstreams)
143 +               return;
144 +
145 +       vss = &snd->substreams[sid];
146 +
147 +       switch (le32_to_cpu(event->hdr.code)) {
148 +       case VIRTIO_SND_EVT_PCM_PERIOD_ELAPSED:
149 +               /* TODO: deal with shmem elapsed period */
150 +               break;
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);
156 +               break;
157 +       }
158 +}
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.
178   */
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;
185 +       spinlock_t lock;
186 +       size_t buffer_bytes;
187 +       size_t hw_ptr;
188 +       bool xfer_enabled;
189 +       bool xfer_xrun;
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;
195  };
196  
197  /**
198 @@ -65,8 +86,27 @@ int virtsnd_pcm_parse_cfg(struct virtio_snd *snd);
199  
200  int virtsnd_pcm_build_devs(struct virtio_snd *snd);
201  
202 +void virtsnd_pcm_event(struct virtio_snd *snd, struct virtio_snd_event *event);
203 +
204 +void virtsnd_pcm_tx_notify_cb(struct virtqueue *vqueue);
205 +
206 +void virtsnd_pcm_rx_notify_cb(struct virtqueue *vqueue);
207 +
208  struct virtio_pcm *virtsnd_pcm_find(struct virtio_snd *snd, u32 nid);
209  
210  struct virtio_pcm *virtsnd_pcm_find_or_create(struct virtio_snd *snd, u32 nid);
211  
212 +struct virtio_snd_msg *
213 +virtsnd_pcm_ctl_msg_alloc(struct virtio_pcm_substream *vss,
214 +                         unsigned int command, gfp_t gfp);
215 +
216 +int virtsnd_pcm_msg_alloc(struct virtio_pcm_substream *vss,
217 +                         unsigned int periods, unsigned int period_bytes);
218 +
219 +void virtsnd_pcm_msg_free(struct virtio_pcm_substream *vss);
220 +
221 +int virtsnd_pcm_msg_send(struct virtio_pcm_substream *vss);
222 +
223 +unsigned int virtsnd_pcm_msg_pending_num(struct virtio_pcm_substream *vss);
224 +
225  #endif /* VIRTIO_SND_PCM_H */
226 diff --git a/sound/virtio/virtio_pcm_msg.c b/sound/virtio/virtio_pcm_msg.c
227 new file mode 100644
228 index 000000000000..f88c8f29cbd8
229 --- /dev/null
230 +++ b/sound/virtio/virtio_pcm_msg.c
231 @@ -0,0 +1,414 @@
232 +// SPDX-License-Identifier: GPL-2.0+
233 +/*
234 + * virtio-snd: Virtio sound device
235 + * Copyright (C) 2021 OpenSynergy GmbH
236 + */
237 +#include <sound/pcm_params.h>
238 +
239 +#include "virtio_card.h"
240 +
241 +/**
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.
248 + */
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;
253 +       size_t length;
254 +       struct scatterlist sgs[0];
255 +};
256 +
257 +/**
258 + * enum pcm_msg_sg_index - Index values for the virtio_pcm_msg->sgs field in
259 + *                         an I/O message.
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.
263 + */
264 +enum pcm_msg_sg_index {
265 +       PCM_MSG_SG_XFER = 0,
266 +       PCM_MSG_SG_STATUS,
267 +       PCM_MSG_SG_DATA
268 +};
269 +
270 +/**
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.
275 + *
276 + * Context: Any context.
277 + * Return: Number of physically contiguous parts in the @data.
278 + */
279 +static int virtsnd_pcm_sg_num(u8 *data, unsigned int length)
280 +{
281 +       phys_addr_t sg_address;
282 +       unsigned int sg_length;
283 +       int num = 0;
284 +
285 +       while (length) {
286 +               struct page *pg = vmalloc_to_page(data);
287 +               phys_addr_t pg_address = page_to_phys(pg);
288 +               size_t pg_length;
289 +
290 +               pg_length = PAGE_SIZE - offset_in_page(data);
291 +               if (pg_length > length)
292 +                       pg_length = length;
293 +
294 +               if (!num || sg_address + sg_length != pg_address) {
295 +                       sg_address = pg_address;
296 +                       sg_length = pg_length;
297 +                       num++;
298 +               } else {
299 +                       sg_length += pg_length;
300 +               }
301 +
302 +               data += pg_length;
303 +               length -= pg_length;
304 +       }
305 +
306 +       return num;
307 +}
308 +
309 +/**
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.
315 + *
316 + * Splits the buffer into physically contiguous parts and makes an sg-list of
317 + * such parts.
318 + *
319 + * Context: Any context.
320 + */
321 +static void virtsnd_pcm_sg_from(struct scatterlist *sgs, int nsgs, u8 *data,
322 +                               unsigned int length)
323 +{
324 +       int idx = -1;
325 +
326 +       while (length) {
327 +               struct page *pg = vmalloc_to_page(data);
328 +               size_t pg_length;
329 +
330 +               pg_length = PAGE_SIZE - offset_in_page(data);
331 +               if (pg_length > length)
332 +                       pg_length = length;
333 +
334 +               if (idx == -1 ||
335 +                   sg_phys(&sgs[idx]) + sgs[idx].length != page_to_phys(pg)) {
336 +                       if (idx + 1 == nsgs)
337 +                               break;
338 +                       sg_set_page(&sgs[++idx], pg, pg_length,
339 +                                   offset_in_page(data));
340 +               } else {
341 +                       sgs[idx].length += pg_length;
342 +               }
343 +
344 +               data += pg_length;
345 +               length -= pg_length;
346 +       }
347 +
348 +       sg_mark_end(&sgs[idx]);
349 +}
350 +
351 +/**
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.
356 + *
357 + * The function slices the buffer into @periods parts (each with the size of
358 + * @period_bytes), and creates @periods corresponding I/O messages.
359 + *
360 + * Context: Any context that permits to sleep.
361 + * Return: 0 on success, -ENOMEM on failure.
362 + */
363 +int virtsnd_pcm_msg_alloc(struct virtio_pcm_substream *vss,
364 +                         unsigned int periods, unsigned int period_bytes)
365 +{
366 +       struct snd_pcm_runtime *runtime = vss->substream->runtime;
367 +       unsigned int i;
368 +
369 +       vss->msgs = kcalloc(periods, sizeof(*vss->msgs), GFP_KERNEL);
370 +       if (!vss->msgs)
371 +               return -ENOMEM;
372 +
373 +       vss->nmsgs = periods;
374 +
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;
379 +
380 +               msg = kzalloc(sizeof(*msg) + sizeof(*msg->sgs) * (sg_num + 2),
381 +                             GFP_KERNEL);
382 +               if (!msg)
383 +                       return -ENOMEM;
384 +
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,
392 +                                   period_bytes);
393 +
394 +               vss->msgs[i] = msg;
395 +       }
396 +
397 +       return 0;
398 +}
399 +
400 +/**
401 + * virtsnd_pcm_msg_free() - Free all allocated I/O messages.
402 + * @vss: VirtIO PCM substream.
403 + *
404 + * Context: Any context.
405 + */
406 +void virtsnd_pcm_msg_free(struct virtio_pcm_substream *vss)
407 +{
408 +       unsigned int i;
409 +
410 +       for (i = 0; vss->msgs && i < vss->nmsgs; ++i)
411 +               kfree(vss->msgs[i]);
412 +       kfree(vss->msgs);
413 +
414 +       vss->msgs = NULL;
415 +       vss->nmsgs = 0;
416 +}
417 +
418 +/**
419 + * virtsnd_pcm_msg_send() - Send asynchronous I/O messages.
420 + * @vss: VirtIO PCM substream.
421 + *
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:
425 + *
426 + *   msg_last_enqueued = index of the last enqueued message,
427 + *   msg_count = # of pending messages in the virtqueue.
428 + *
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.
432 + */
433 +int virtsnd_pcm_msg_send(struct virtio_pcm_substream *vss)
434 +{
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;
439 +       int i;
440 +       int n;
441 +       bool notify = false;
442 +
443 +       i = (vss->msg_last_enqueued + 1) % runtime->periods;
444 +       n = runtime->periods - vss->msg_count;
445 +
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]
452 +               };
453 +               int rc;
454 +
455 +               msg->xfer.stream_id = cpu_to_le32(vss->sid);
456 +               memset(&msg->status, 0, sizeof(msg->status));
457 +
458 +               if (vss->direction == SNDRV_PCM_STREAM_PLAYBACK)
459 +                       rc = virtqueue_add_sgs(vqueue, psgs, 2, 1, msg,
460 +                                              GFP_ATOMIC);
461 +               else
462 +                       rc = virtqueue_add_sgs(vqueue, psgs, 1, 2, msg,
463 +                                              GFP_ATOMIC);
464 +
465 +               if (rc) {
466 +                       dev_err(&vdev->dev,
467 +                               "SID %u: failed to send I/O message\n",
468 +                               vss->sid);
469 +                       return rc;
470 +               }
471 +
472 +               vss->msg_last_enqueued = i;
473 +               vss->msg_count++;
474 +       }
475 +
476 +       if (!(vss->features & (1U << VIRTIO_SND_PCM_F_MSG_POLLING)))
477 +               notify = virtqueue_kick_prepare(vqueue);
478 +
479 +       if (notify)
480 +               virtqueue_notify(vqueue);
481 +
482 +       return 0;
483 +}
484 +
485 +/**
486 + * virtsnd_pcm_msg_pending_num() - Returns the number of pending I/O messages.
487 + * @vss: VirtIO substream.
488 + *
489 + * Context: Any context.
490 + * Return: Number of messages.
491 + */
492 +unsigned int virtsnd_pcm_msg_pending_num(struct virtio_pcm_substream *vss)
493 +{
494 +       unsigned int num;
495 +       unsigned long flags;
496 +
497 +       spin_lock_irqsave(&vss->lock, flags);
498 +       num = vss->msg_count;
499 +       spin_unlock_irqrestore(&vss->lock, flags);
500 +
501 +       return num;
502 +}
503 +
504 +/**
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.
508 + *
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
511 + * of the queue.
512 + *
513 + * For the playback substream, @written_bytes is equal to sizeof(msg->status).
514 + *
515 + * For the capture substream, @written_bytes is equal to sizeof(msg->status)
516 + * plus the number of captured bytes.
517 + *
518 + * Context: Interrupt context. Takes and releases the VirtIO substream spinlock.
519 + */
520 +static void virtsnd_pcm_msg_complete(struct virtio_pcm_msg *msg,
521 +                                    size_t written_bytes)
522 +{
523 +       struct virtio_pcm_substream *vss = msg->substream;
524 +
525 +       /*
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.
529 +        */
530 +       spin_lock(&vss->lock);
531 +       /*
532 +        * If the capture substream returned an incorrect status, then just
533 +        * increase the hw_ptr by the message size.
534 +        */
535 +       if (vss->direction == SNDRV_PCM_STREAM_PLAYBACK ||
536 +           written_bytes <= sizeof(msg->status))
537 +               vss->hw_ptr += msg->length;
538 +       else
539 +               vss->hw_ptr += written_bytes - sizeof(msg->status);
540 +
541 +       if (vss->hw_ptr >= vss->buffer_bytes)
542 +               vss->hw_ptr -= vss->buffer_bytes;
543 +
544 +       vss->xfer_xrun = false;
545 +       vss->msg_count--;
546 +
547 +       if (vss->xfer_enabled) {
548 +               struct snd_pcm_runtime *runtime = vss->substream->runtime;
549 +
550 +               runtime->delay =
551 +                       bytes_to_frames(runtime,
552 +                                       le32_to_cpu(msg->status.latency_bytes));
553 +
554 +               schedule_work(&vss->elapsed_period);
555 +
556 +               virtsnd_pcm_msg_send(vss);
557 +       } else if (!vss->msg_count) {
558 +               wake_up_all(&vss->msg_empty);
559 +       }
560 +       spin_unlock(&vss->lock);
561 +}
562 +
563 +/**
564 + * virtsnd_pcm_notify_cb() - Process all completed I/O messages.
565 + * @queue: Underlying tx/rx virtqueue.
566 + *
567 + * Context: Interrupt context. Takes and releases the tx/rx queue spinlock.
568 + */
569 +static inline void virtsnd_pcm_notify_cb(struct virtio_snd_queue *queue)
570 +{
571 +       struct virtio_pcm_msg *msg;
572 +       u32 written_bytes;
573 +       unsigned long flags;
574 +
575 +       spin_lock_irqsave(&queue->lock, flags);
576 +       do {
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)))
581 +                       break;
582 +       } while (!virtqueue_enable_cb(queue->vqueue));
583 +       spin_unlock_irqrestore(&queue->lock, flags);
584 +}
585 +
586 +/**
587 + * virtsnd_pcm_tx_notify_cb() - Process all completed TX messages.
588 + * @vqueue: Underlying tx virtqueue.
589 + *
590 + * Context: Interrupt context.
591 + */
592 +void virtsnd_pcm_tx_notify_cb(struct virtqueue *vqueue)
593 +{
594 +       struct virtio_snd *snd = vqueue->vdev->priv;
595 +
596 +       virtsnd_pcm_notify_cb(virtsnd_tx_queue(snd));
597 +}
598 +
599 +/**
600 + * virtsnd_pcm_rx_notify_cb() - Process all completed RX messages.
601 + * @vqueue: Underlying rx virtqueue.
602 + *
603 + * Context: Interrupt context.
604 + */
605 +void virtsnd_pcm_rx_notify_cb(struct virtqueue *vqueue)
606 +{
607 +       struct virtio_snd *snd = vqueue->vdev->priv;
608 +
609 +       virtsnd_pcm_notify_cb(virtsnd_rx_queue(snd));
610 +}
611 +
612 +/**
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.
618 + *
619 + * Context: Any context. May sleep if @gfp flags permit.
620 + * Return: Allocated message on success, NULL on failure.
621 + */
622 +struct virtio_snd_msg *
623 +virtsnd_pcm_ctl_msg_alloc(struct virtio_pcm_substream *vss,
624 +                         unsigned int command, gfp_t gfp)
625 +{
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;
629 +
630 +       switch (command) {
631 +       case VIRTIO_SND_R_PCM_SET_PARAMS:
632 +               request_size = sizeof(struct virtio_snd_pcm_set_params);
633 +               break;
634 +       }
635 +
636 +       msg = virtsnd_ctl_msg_alloc(request_size, response_size, gfp);
637 +       if (msg) {
638 +               struct virtio_snd_pcm_hdr *hdr = virtsnd_ctl_msg_request(msg);
639 +
640 +               hdr->hdr.code = cpu_to_le32(command);
641 +               hdr->stream_id = cpu_to_le32(vss->sid);
642 +       }
643 +
644 +       return msg;
645 +}