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 / 0004-ALSA-virtio-build-PCM-devices-and-substream-hardware.patch
1 From 12e4e501f9662a02e61acb5966fdceeffb0ff16d Mon Sep 17 00:00:00 2001
2 From: Anton Yakovlev <anton.yakovlev@opensynergy.com>
3 Date: Tue, 2 Mar 2021 17:47:04 +0100
4 Subject: [PATCH] ALSA: virtio: build PCM devices and substream hardware
5  descriptors
6
7 Like the HDA specification, the virtio sound device specification links
8 PCM substreams, jacks and PCM channel maps into functional groups. For
9 each discovered group, a PCM device is created, the number of which
10 coincides with the group number.
11
12 Introduce the module parameters for setting the hardware buffer
13 parameters:
14   pcm_buffer_ms [=160]
15   pcm_periods_min [=2]
16   pcm_periods_max [=16]
17   pcm_period_ms_min [=10]
18   pcm_period_ms_max [=80]
19
20 Signed-off-by: Anton Yakovlev <anton.yakovlev@opensynergy.com>
21 Link: https://lore.kernel.org/r/20210302164709.3142702-5-anton.yakovlev@opensynergy.com
22 Signed-off-by: Takashi Iwai <tiwai@suse.de>
23 Signed-off-by: Vasyl Vavrychuk <vasyl.vavrychuk@opensynergy.com>
24 ---
25  sound/virtio/Makefile      |   3 +-
26  sound/virtio/virtio_card.c |  18 ++
27  sound/virtio/virtio_card.h |  10 +
28  sound/virtio/virtio_pcm.c  | 479 +++++++++++++++++++++++++++++++++++++
29  sound/virtio/virtio_pcm.h  |  72 ++++++
30  5 files changed, 581 insertions(+), 1 deletion(-)
31  create mode 100644 sound/virtio/virtio_pcm.c
32  create mode 100644 sound/virtio/virtio_pcm.h
33
34 diff --git a/sound/virtio/Makefile b/sound/virtio/Makefile
35 index dc551e637441..69162a545a41 100644
36 --- a/sound/virtio/Makefile
37 +++ b/sound/virtio/Makefile
38 @@ -4,5 +4,6 @@ obj-$(CONFIG_SND_VIRTIO) += virtio_snd.o
39  
40  virtio_snd-objs := \
41         virtio_card.o \
42 -       virtio_ctl_msg.o
43 +       virtio_ctl_msg.o \
44 +       virtio_pcm.o
45  
46 diff --git a/sound/virtio/virtio_card.c b/sound/virtio/virtio_card.c
47 index b757b2444078..11c76ee311b7 100644
48 --- a/sound/virtio/virtio_card.c
49 +++ b/sound/virtio/virtio_card.c
50 @@ -209,6 +209,16 @@ static int virtsnd_build_devs(struct virtio_snd *snd)
51                          VIRTIO_SND_CARD_NAME " at %s/%s",
52                          dev_name(dev->parent), dev_name(dev));
53  
54 +       rc = virtsnd_pcm_parse_cfg(snd);
55 +       if (rc)
56 +               return rc;
57 +
58 +       if (snd->nsubstreams) {
59 +               rc = virtsnd_pcm_build_devs(snd);
60 +               if (rc)
61 +                       return rc;
62 +       }
63 +
64         return snd_card_register(snd->card);
65  }
66  
67 @@ -237,6 +247,9 @@ static int virtsnd_validate(struct virtio_device *vdev)
68                 return -EINVAL;
69         }
70  
71 +       if (virtsnd_pcm_validate(vdev))
72 +               return -EINVAL;
73 +
74         return 0;
75  }
76  
77 @@ -259,6 +272,7 @@ static int virtsnd_probe(struct virtio_device *vdev)
78  
79         snd->vdev = vdev;
80         INIT_LIST_HEAD(&snd->ctl_msgs);
81 +       INIT_LIST_HEAD(&snd->pcm_list);
82  
83         vdev->priv = snd;
84  
85 @@ -293,6 +307,7 @@ static int virtsnd_probe(struct virtio_device *vdev)
86  static void virtsnd_remove(struct virtio_device *vdev)
87  {
88         struct virtio_snd *snd = vdev->priv;
89 +       unsigned int i;
90  
91         virtsnd_disable_event_vq(snd);
92         virtsnd_ctl_msg_cancel_all(snd);
93 @@ -303,6 +318,9 @@ static void virtsnd_remove(struct virtio_device *vdev)
94         vdev->config->del_vqs(vdev);
95         vdev->config->reset(vdev);
96  
97 +       for (i = 0; snd->substreams && i < snd->nsubstreams; ++i)
98 +               cancel_work_sync(&snd->substreams[i].elapsed_period);
99 +
100         kfree(snd->event_msgs);
101  }
102  
103 diff --git a/sound/virtio/virtio_card.h b/sound/virtio/virtio_card.h
104 index 1e76eeff160f..77a1b7255370 100644
105 --- a/sound/virtio/virtio_card.h
106 +++ b/sound/virtio/virtio_card.h
107 @@ -12,9 +12,13 @@
108  #include <uapi/linux/virtio_snd.h>
109  
110  #include "virtio_ctl_msg.h"
111 +#include "virtio_pcm.h"
112  
113  #define VIRTIO_SND_CARD_DRIVER "virtio-snd"
114  #define VIRTIO_SND_CARD_NAME   "VirtIO SoundCard"
115 +#define VIRTIO_SND_PCM_NAME    "VirtIO PCM"
116 +
117 +struct virtio_pcm_substream;
118  
119  /**
120   * struct virtio_snd_queue - Virtqueue wrapper structure.
121 @@ -33,6 +37,9 @@ struct virtio_snd_queue {
122   * @card: ALSA sound card.
123   * @ctl_msgs: Pending control request list.
124   * @event_msgs: Device events.
125 + * @pcm_list: VirtIO PCM device list.
126 + * @substreams: VirtIO PCM substreams.
127 + * @nsubstreams: Number of PCM substreams.
128   */
129  struct virtio_snd {
130         struct virtio_device *vdev;
131 @@ -40,6 +47,9 @@ struct virtio_snd {
132         struct snd_card *card;
133         struct list_head ctl_msgs;
134         struct virtio_snd_event *event_msgs;
135 +       struct list_head pcm_list;
136 +       struct virtio_pcm_substream *substreams;
137 +       u32 nsubstreams;
138  };
139  
140  /* Message completion timeout in milliseconds (module parameter). */
141 diff --git a/sound/virtio/virtio_pcm.c b/sound/virtio/virtio_pcm.c
142 new file mode 100644
143 index 000000000000..e16567e2e214
144 --- /dev/null
145 +++ b/sound/virtio/virtio_pcm.c
146 @@ -0,0 +1,479 @@
147 +// SPDX-License-Identifier: GPL-2.0+
148 +/*
149 + * virtio-snd: Virtio sound device
150 + * Copyright (C) 2021 OpenSynergy GmbH
151 + */
152 +#include <linux/moduleparam.h>
153 +#include <linux/virtio_config.h>
154 +
155 +#include "virtio_card.h"
156 +
157 +static u32 pcm_buffer_ms = 160;
158 +module_param(pcm_buffer_ms, uint, 0644);
159 +MODULE_PARM_DESC(pcm_buffer_ms, "PCM substream buffer time in milliseconds");
160 +
161 +static u32 pcm_periods_min = 2;
162 +module_param(pcm_periods_min, uint, 0644);
163 +MODULE_PARM_DESC(pcm_periods_min, "Minimum number of PCM periods");
164 +
165 +static u32 pcm_periods_max = 16;
166 +module_param(pcm_periods_max, uint, 0644);
167 +MODULE_PARM_DESC(pcm_periods_max, "Maximum number of PCM periods");
168 +
169 +static u32 pcm_period_ms_min = 10;
170 +module_param(pcm_period_ms_min, uint, 0644);
171 +MODULE_PARM_DESC(pcm_period_ms_min, "Minimum PCM period time in milliseconds");
172 +
173 +static u32 pcm_period_ms_max = 80;
174 +module_param(pcm_period_ms_max, uint, 0644);
175 +MODULE_PARM_DESC(pcm_period_ms_max, "Maximum PCM period time in milliseconds");
176 +
177 +/* Map for converting VirtIO format to ALSA format. */
178 +static const snd_pcm_format_t g_v2a_format_map[] = {
179 +       [VIRTIO_SND_PCM_FMT_IMA_ADPCM] = SNDRV_PCM_FORMAT_IMA_ADPCM,
180 +       [VIRTIO_SND_PCM_FMT_MU_LAW] = SNDRV_PCM_FORMAT_MU_LAW,
181 +       [VIRTIO_SND_PCM_FMT_A_LAW] = SNDRV_PCM_FORMAT_A_LAW,
182 +       [VIRTIO_SND_PCM_FMT_S8] = SNDRV_PCM_FORMAT_S8,
183 +       [VIRTIO_SND_PCM_FMT_U8] = SNDRV_PCM_FORMAT_U8,
184 +       [VIRTIO_SND_PCM_FMT_S16] = SNDRV_PCM_FORMAT_S16_LE,
185 +       [VIRTIO_SND_PCM_FMT_U16] = SNDRV_PCM_FORMAT_U16_LE,
186 +       [VIRTIO_SND_PCM_FMT_S18_3] = SNDRV_PCM_FORMAT_S18_3LE,
187 +       [VIRTIO_SND_PCM_FMT_U18_3] = SNDRV_PCM_FORMAT_U18_3LE,
188 +       [VIRTIO_SND_PCM_FMT_S20_3] = SNDRV_PCM_FORMAT_S20_3LE,
189 +       [VIRTIO_SND_PCM_FMT_U20_3] = SNDRV_PCM_FORMAT_U20_3LE,
190 +       [VIRTIO_SND_PCM_FMT_S24_3] = SNDRV_PCM_FORMAT_S24_3LE,
191 +       [VIRTIO_SND_PCM_FMT_U24_3] = SNDRV_PCM_FORMAT_U24_3LE,
192 +       [VIRTIO_SND_PCM_FMT_S20] = SNDRV_PCM_FORMAT_S20_LE,
193 +       [VIRTIO_SND_PCM_FMT_U20] = SNDRV_PCM_FORMAT_U20_LE,
194 +       [VIRTIO_SND_PCM_FMT_S24] = SNDRV_PCM_FORMAT_S24_LE,
195 +       [VIRTIO_SND_PCM_FMT_U24] = SNDRV_PCM_FORMAT_U24_LE,
196 +       [VIRTIO_SND_PCM_FMT_S32] = SNDRV_PCM_FORMAT_S32_LE,
197 +       [VIRTIO_SND_PCM_FMT_U32] = SNDRV_PCM_FORMAT_U32_LE,
198 +       [VIRTIO_SND_PCM_FMT_FLOAT] = SNDRV_PCM_FORMAT_FLOAT_LE,
199 +       [VIRTIO_SND_PCM_FMT_FLOAT64] = SNDRV_PCM_FORMAT_FLOAT64_LE,
200 +       [VIRTIO_SND_PCM_FMT_DSD_U8] = SNDRV_PCM_FORMAT_DSD_U8,
201 +       [VIRTIO_SND_PCM_FMT_DSD_U16] = SNDRV_PCM_FORMAT_DSD_U16_LE,
202 +       [VIRTIO_SND_PCM_FMT_DSD_U32] = SNDRV_PCM_FORMAT_DSD_U32_LE,
203 +       [VIRTIO_SND_PCM_FMT_IEC958_SUBFRAME] =
204 +               SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE
205 +};
206 +
207 +/* Map for converting VirtIO frame rate to ALSA frame rate. */
208 +struct virtsnd_v2a_rate {
209 +       unsigned int alsa_bit;
210 +       unsigned int rate;
211 +};
212 +
213 +static const struct virtsnd_v2a_rate g_v2a_rate_map[] = {
214 +       [VIRTIO_SND_PCM_RATE_5512] = { SNDRV_PCM_RATE_5512, 5512 },
215 +       [VIRTIO_SND_PCM_RATE_8000] = { SNDRV_PCM_RATE_8000, 8000 },
216 +       [VIRTIO_SND_PCM_RATE_11025] = { SNDRV_PCM_RATE_11025, 11025 },
217 +       [VIRTIO_SND_PCM_RATE_16000] = { SNDRV_PCM_RATE_16000, 16000 },
218 +       [VIRTIO_SND_PCM_RATE_22050] = { SNDRV_PCM_RATE_22050, 22050 },
219 +       [VIRTIO_SND_PCM_RATE_32000] = { SNDRV_PCM_RATE_32000, 32000 },
220 +       [VIRTIO_SND_PCM_RATE_44100] = { SNDRV_PCM_RATE_44100, 44100 },
221 +       [VIRTIO_SND_PCM_RATE_48000] = { SNDRV_PCM_RATE_48000, 48000 },
222 +       [VIRTIO_SND_PCM_RATE_64000] = { SNDRV_PCM_RATE_64000, 64000 },
223 +       [VIRTIO_SND_PCM_RATE_88200] = { SNDRV_PCM_RATE_88200, 88200 },
224 +       [VIRTIO_SND_PCM_RATE_96000] = { SNDRV_PCM_RATE_96000, 96000 },
225 +       [VIRTIO_SND_PCM_RATE_176400] = { SNDRV_PCM_RATE_176400, 176400 },
226 +       [VIRTIO_SND_PCM_RATE_192000] = { SNDRV_PCM_RATE_192000, 192000 }
227 +};
228 +
229 +/**
230 + * virtsnd_pcm_build_hw() - Parse substream config and build HW descriptor.
231 + * @vss: VirtIO substream.
232 + * @info: VirtIO substream information entry.
233 + *
234 + * Context: Any context.
235 + * Return: 0 on success, -EINVAL if configuration is invalid.
236 + */
237 +static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss,
238 +                               struct virtio_snd_pcm_info *info)
239 +{
240 +       struct virtio_device *vdev = vss->snd->vdev;
241 +       unsigned int i;
242 +       u64 values;
243 +       size_t sample_max = 0;
244 +       size_t sample_min = 0;
245 +
246 +       vss->features = le32_to_cpu(info->features);
247 +
248 +       /*
249 +        * TODO: set SNDRV_PCM_INFO_{BATCH,BLOCK_TRANSFER} if device supports
250 +        * only message-based transport.
251 +        */
252 +       vss->hw.info =
253 +               SNDRV_PCM_INFO_MMAP |
254 +               SNDRV_PCM_INFO_MMAP_VALID |
255 +               SNDRV_PCM_INFO_BATCH |
256 +               SNDRV_PCM_INFO_BLOCK_TRANSFER |
257 +               SNDRV_PCM_INFO_INTERLEAVED |
258 +               SNDRV_PCM_INFO_PAUSE;
259 +
260 +       if (!info->channels_min || info->channels_min > info->channels_max) {
261 +               dev_err(&vdev->dev,
262 +                       "SID %u: invalid channel range [%u %u]\n",
263 +                       vss->sid, info->channels_min, info->channels_max);
264 +               return -EINVAL;
265 +       }
266 +
267 +       vss->hw.channels_min = info->channels_min;
268 +       vss->hw.channels_max = info->channels_max;
269 +
270 +       values = le64_to_cpu(info->formats);
271 +
272 +       vss->hw.formats = 0;
273 +
274 +       for (i = 0; i < ARRAY_SIZE(g_v2a_format_map); ++i)
275 +               if (values & (1ULL << i)) {
276 +                       snd_pcm_format_t alsa_fmt = g_v2a_format_map[i];
277 +                       int bytes = snd_pcm_format_physical_width(alsa_fmt) / 8;
278 +
279 +                       if (!sample_min || sample_min > bytes)
280 +                               sample_min = bytes;
281 +
282 +                       if (sample_max < bytes)
283 +                               sample_max = bytes;
284 +
285 +                       vss->hw.formats |= pcm_format_to_bits(alsa_fmt);
286 +               }
287 +
288 +       if (!vss->hw.formats) {
289 +               dev_err(&vdev->dev,
290 +                       "SID %u: no supported PCM sample formats found\n",
291 +                       vss->sid);
292 +               return -EINVAL;
293 +       }
294 +
295 +       values = le64_to_cpu(info->rates);
296 +
297 +       vss->hw.rates = 0;
298 +
299 +       for (i = 0; i < ARRAY_SIZE(g_v2a_rate_map); ++i)
300 +               if (values & (1ULL << i)) {
301 +                       if (!vss->hw.rate_min ||
302 +                           vss->hw.rate_min > g_v2a_rate_map[i].rate)
303 +                               vss->hw.rate_min = g_v2a_rate_map[i].rate;
304 +
305 +                       if (vss->hw.rate_max < g_v2a_rate_map[i].rate)
306 +                               vss->hw.rate_max = g_v2a_rate_map[i].rate;
307 +
308 +                       vss->hw.rates |= g_v2a_rate_map[i].alsa_bit;
309 +               }
310 +
311 +       if (!vss->hw.rates) {
312 +               dev_err(&vdev->dev,
313 +                       "SID %u: no supported PCM frame rates found\n",
314 +                       vss->sid);
315 +               return -EINVAL;
316 +       }
317 +
318 +       vss->hw.periods_min = pcm_periods_min;
319 +       vss->hw.periods_max = pcm_periods_max;
320 +
321 +       /*
322 +        * We must ensure that there is enough space in the buffer to store
323 +        * pcm_buffer_ms ms for the combination (Cmax, Smax, Rmax), where:
324 +        *   Cmax = maximum supported number of channels,
325 +        *   Smax = maximum supported sample size in bytes,
326 +        *   Rmax = maximum supported frame rate.
327 +        */
328 +       vss->hw.buffer_bytes_max =
329 +               PAGE_ALIGN(sample_max * vss->hw.channels_max * pcm_buffer_ms *
330 +                          (vss->hw.rate_max / MSEC_PER_SEC));
331 +
332 +       /*
333 +        * We must ensure that the minimum period size is enough to store
334 +        * pcm_period_ms_min ms for the combination (Cmin, Smin, Rmin), where:
335 +        *   Cmin = minimum supported number of channels,
336 +        *   Smin = minimum supported sample size in bytes,
337 +        *   Rmin = minimum supported frame rate.
338 +        */
339 +       vss->hw.period_bytes_min =
340 +               sample_min * vss->hw.channels_min * pcm_period_ms_min *
341 +               (vss->hw.rate_min / MSEC_PER_SEC);
342 +
343 +       /*
344 +        * We must ensure that the maximum period size is enough to store
345 +        * pcm_period_ms_max ms for the combination (Cmax, Smax, Rmax).
346 +        */
347 +       vss->hw.period_bytes_max =
348 +               sample_max * vss->hw.channels_max * pcm_period_ms_max *
349 +               (vss->hw.rate_max / MSEC_PER_SEC);
350 +
351 +       return 0;
352 +}
353 +
354 +/**
355 + * virtsnd_pcm_find() - Find the PCM device for the specified node ID.
356 + * @snd: VirtIO sound device.
357 + * @nid: Function node ID.
358 + *
359 + * Context: Any context.
360 + * Return: a pointer to the PCM device or ERR_PTR(-ENOENT).
361 + */
362 +struct virtio_pcm *virtsnd_pcm_find(struct virtio_snd *snd, u32 nid)
363 +{
364 +       struct virtio_pcm *vpcm;
365 +
366 +       list_for_each_entry(vpcm, &snd->pcm_list, list)
367 +               if (vpcm->nid == nid)
368 +                       return vpcm;
369 +
370 +       return ERR_PTR(-ENOENT);
371 +}
372 +
373 +/**
374 + * virtsnd_pcm_find_or_create() - Find or create the PCM device for the
375 + *                                specified node ID.
376 + * @snd: VirtIO sound device.
377 + * @nid: Function node ID.
378 + *
379 + * Context: Any context that permits to sleep.
380 + * Return: a pointer to the PCM device or ERR_PTR(-errno).
381 + */
382 +struct virtio_pcm *virtsnd_pcm_find_or_create(struct virtio_snd *snd, u32 nid)
383 +{
384 +       struct virtio_device *vdev = snd->vdev;
385 +       struct virtio_pcm *vpcm;
386 +
387 +       vpcm = virtsnd_pcm_find(snd, nid);
388 +       if (!IS_ERR(vpcm))
389 +               return vpcm;
390 +
391 +       vpcm = devm_kzalloc(&vdev->dev, sizeof(*vpcm), GFP_KERNEL);
392 +       if (!vpcm)
393 +               return ERR_PTR(-ENOMEM);
394 +
395 +       vpcm->nid = nid;
396 +       list_add_tail(&vpcm->list, &snd->pcm_list);
397 +
398 +       return vpcm;
399 +}
400 +
401 +/**
402 + * virtsnd_pcm_validate() - Validate if the device can be started.
403 + * @vdev: VirtIO parent device.
404 + *
405 + * Context: Any context.
406 + * Return: 0 on success, -EINVAL on failure.
407 + */
408 +int virtsnd_pcm_validate(struct virtio_device *vdev)
409 +{
410 +       if (pcm_periods_min < 2 || pcm_periods_min > pcm_periods_max) {
411 +               dev_err(&vdev->dev,
412 +                       "invalid range [%u %u] of the number of PCM periods\n",
413 +                       pcm_periods_min, pcm_periods_max);
414 +               return -EINVAL;
415 +       }
416 +
417 +       if (!pcm_period_ms_min || pcm_period_ms_min > pcm_period_ms_max) {
418 +               dev_err(&vdev->dev,
419 +                       "invalid range [%u %u] of the size of the PCM period\n",
420 +                       pcm_period_ms_min, pcm_period_ms_max);
421 +               return -EINVAL;
422 +       }
423 +
424 +       if (pcm_buffer_ms < pcm_periods_min * pcm_period_ms_min) {
425 +               dev_err(&vdev->dev,
426 +                       "pcm_buffer_ms(=%u) value cannot be < %u ms\n",
427 +                       pcm_buffer_ms, pcm_periods_min * pcm_period_ms_min);
428 +               return -EINVAL;
429 +       }
430 +
431 +       if (pcm_period_ms_max > pcm_buffer_ms / 2) {
432 +               dev_err(&vdev->dev,
433 +                       "pcm_period_ms_max(=%u) value cannot be > %u ms\n",
434 +                       pcm_period_ms_max, pcm_buffer_ms / 2);
435 +               return -EINVAL;
436 +       }
437 +
438 +       return 0;
439 +}
440 +
441 +/**
442 + * virtsnd_pcm_period_elapsed() - Kernel work function to handle the elapsed
443 + *                                period state.
444 + * @work: Elapsed period work.
445 + *
446 + * The main purpose of this function is to call snd_pcm_period_elapsed() in
447 + * a process context, not in an interrupt context. This is necessary because PCM
448 + * devices operate in non-atomic mode.
449 + *
450 + * Context: Process context.
451 + */
452 +static void virtsnd_pcm_period_elapsed(struct work_struct *work)
453 +{
454 +       struct virtio_pcm_substream *vss =
455 +               container_of(work, struct virtio_pcm_substream, elapsed_period);
456 +
457 +       snd_pcm_period_elapsed(vss->substream);
458 +}
459 +
460 +/**
461 + * virtsnd_pcm_parse_cfg() - Parse the stream configuration.
462 + * @snd: VirtIO sound device.
463 + *
464 + * This function is called during initial device initialization.
465 + *
466 + * Context: Any context that permits to sleep.
467 + * Return: 0 on success, -errno on failure.
468 + */
469 +int virtsnd_pcm_parse_cfg(struct virtio_snd *snd)
470 +{
471 +       struct virtio_device *vdev = snd->vdev;
472 +       struct virtio_snd_pcm_info *info;
473 +       u32 i;
474 +       int rc;
475 +
476 +       virtio_cread_le(vdev, struct virtio_snd_config, streams,
477 +                       &snd->nsubstreams);
478 +       if (!snd->nsubstreams)
479 +               return 0;
480 +
481 +       snd->substreams = devm_kcalloc(&vdev->dev, snd->nsubstreams,
482 +                                      sizeof(*snd->substreams), GFP_KERNEL);
483 +       if (!snd->substreams)
484 +               return -ENOMEM;
485 +
486 +       info = kcalloc(snd->nsubstreams, sizeof(*info), GFP_KERNEL);
487 +       if (!info)
488 +               return -ENOMEM;
489 +
490 +       rc = virtsnd_ctl_query_info(snd, VIRTIO_SND_R_PCM_INFO, 0,
491 +                                   snd->nsubstreams, sizeof(*info), info);
492 +       if (rc)
493 +               goto on_exit;
494 +
495 +       for (i = 0; i < snd->nsubstreams; ++i) {
496 +               struct virtio_pcm_substream *vss = &snd->substreams[i];
497 +               struct virtio_pcm *vpcm;
498 +
499 +               vss->snd = snd;
500 +               vss->sid = i;
501 +               INIT_WORK(&vss->elapsed_period, virtsnd_pcm_period_elapsed);
502 +
503 +               rc = virtsnd_pcm_build_hw(vss, &info[i]);
504 +               if (rc)
505 +                       goto on_exit;
506 +
507 +               vss->nid = le32_to_cpu(info[i].hdr.hda_fn_nid);
508 +
509 +               vpcm = virtsnd_pcm_find_or_create(snd, vss->nid);
510 +               if (IS_ERR(vpcm)) {
511 +                       rc = PTR_ERR(vpcm);
512 +                       goto on_exit;
513 +               }
514 +
515 +               switch (info[i].direction) {
516 +               case VIRTIO_SND_D_OUTPUT:
517 +                       vss->direction = SNDRV_PCM_STREAM_PLAYBACK;
518 +                       break;
519 +               case VIRTIO_SND_D_INPUT:
520 +                       vss->direction = SNDRV_PCM_STREAM_CAPTURE;
521 +                       break;
522 +               default:
523 +                       dev_err(&vdev->dev, "SID %u: unknown direction (%u)\n",
524 +                               vss->sid, info[i].direction);
525 +                       rc = -EINVAL;
526 +                       goto on_exit;
527 +               }
528 +
529 +               vpcm->streams[vss->direction].nsubstreams++;
530 +       }
531 +
532 +on_exit:
533 +       kfree(info);
534 +
535 +       return rc;
536 +}
537 +
538 +/**
539 + * virtsnd_pcm_build_devs() - Build ALSA PCM devices.
540 + * @snd: VirtIO sound device.
541 + *
542 + * Context: Any context that permits to sleep.
543 + * Return: 0 on success, -errno on failure.
544 + */
545 +int virtsnd_pcm_build_devs(struct virtio_snd *snd)
546 +{
547 +       struct virtio_device *vdev = snd->vdev;
548 +       struct virtio_pcm *vpcm;
549 +       u32 i;
550 +       int rc;
551 +
552 +       list_for_each_entry(vpcm, &snd->pcm_list, list) {
553 +               unsigned int npbs =
554 +                       vpcm->streams[SNDRV_PCM_STREAM_PLAYBACK].nsubstreams;
555 +               unsigned int ncps =
556 +                       vpcm->streams[SNDRV_PCM_STREAM_CAPTURE].nsubstreams;
557 +
558 +               if (!npbs && !ncps)
559 +                       continue;
560 +
561 +               rc = snd_pcm_new(snd->card, VIRTIO_SND_CARD_DRIVER, vpcm->nid,
562 +                                npbs, ncps, &vpcm->pcm);
563 +               if (rc) {
564 +                       dev_err(&vdev->dev, "snd_pcm_new[%u] failed: %d\n",
565 +                               vpcm->nid, rc);
566 +                       return rc;
567 +               }
568 +
569 +               vpcm->pcm->info_flags = 0;
570 +               vpcm->pcm->dev_class = SNDRV_PCM_CLASS_GENERIC;
571 +               vpcm->pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
572 +               snprintf(vpcm->pcm->name, sizeof(vpcm->pcm->name),
573 +                        VIRTIO_SND_PCM_NAME " %u", vpcm->pcm->device);
574 +               vpcm->pcm->private_data = vpcm;
575 +               vpcm->pcm->nonatomic = true;
576 +
577 +               for (i = 0; i < ARRAY_SIZE(vpcm->streams); ++i) {
578 +                       struct virtio_pcm_stream *stream = &vpcm->streams[i];
579 +
580 +                       if (!stream->nsubstreams)
581 +                               continue;
582 +
583 +                       stream->substreams =
584 +                               devm_kcalloc(&vdev->dev, stream->nsubstreams,
585 +                                            sizeof(*stream->substreams),
586 +                                            GFP_KERNEL);
587 +                       if (!stream->substreams)
588 +                               return -ENOMEM;
589 +
590 +                       stream->nsubstreams = 0;
591 +               }
592 +       }
593 +
594 +       for (i = 0; i < snd->nsubstreams; ++i) {
595 +               struct virtio_pcm_stream *vs;
596 +               struct virtio_pcm_substream *vss = &snd->substreams[i];
597 +
598 +               vpcm = virtsnd_pcm_find(snd, vss->nid);
599 +               if (IS_ERR(vpcm))
600 +                       return PTR_ERR(vpcm);
601 +
602 +               vs = &vpcm->streams[vss->direction];
603 +               vs->substreams[vs->nsubstreams++] = vss;
604 +       }
605 +
606 +       list_for_each_entry(vpcm, &snd->pcm_list, list) {
607 +               for (i = 0; i < ARRAY_SIZE(vpcm->streams); ++i) {
608 +                       struct virtio_pcm_stream *vs = &vpcm->streams[i];
609 +                       struct snd_pcm_str *ks = &vpcm->pcm->streams[i];
610 +                       struct snd_pcm_substream *kss;
611 +
612 +                       if (!vs->nsubstreams)
613 +                               continue;
614 +
615 +                       for (kss = ks->substream; kss; kss = kss->next)
616 +                               vs->substreams[kss->number]->substream = kss;
617 +               }
618 +
619 +               snd_pcm_set_managed_buffer_all(vpcm->pcm,
620 +                                              SNDRV_DMA_TYPE_VMALLOC, NULL,
621 +                                              0, 0);
622 +       }
623 +
624 +       return 0;
625 +}
626 diff --git a/sound/virtio/virtio_pcm.h b/sound/virtio/virtio_pcm.h
627 new file mode 100644
628 index 000000000000..84f2f3f14f48
629 --- /dev/null
630 +++ b/sound/virtio/virtio_pcm.h
631 @@ -0,0 +1,72 @@
632 +/* SPDX-License-Identifier: GPL-2.0+ */
633 +/*
634 + * virtio-snd: Virtio sound device
635 + * Copyright (C) 2021 OpenSynergy GmbH
636 + */
637 +#ifndef VIRTIO_SND_PCM_H
638 +#define VIRTIO_SND_PCM_H
639 +
640 +#include <linux/atomic.h>
641 +#include <linux/virtio_config.h>
642 +#include <sound/pcm.h>
643 +
644 +struct virtio_pcm;
645 +struct virtio_pcm_msg;
646 +
647 +/**
648 + * struct virtio_pcm_substream - VirtIO PCM substream.
649 + * @snd: VirtIO sound device.
650 + * @nid: Function group node identifier.
651 + * @sid: Stream identifier.
652 + * @direction: Stream data flow direction (SNDRV_PCM_STREAM_XXX).
653 + * @features: Stream VirtIO feature bit map (1 << VIRTIO_SND_PCM_F_XXX).
654 + * @substream: Kernel ALSA substream.
655 + * @hw: Kernel ALSA substream hardware descriptor.
656 + * @elapsed_period: Kernel work to handle the elapsed period state.
657 + */
658 +struct virtio_pcm_substream {
659 +       struct virtio_snd *snd;
660 +       u32 nid;
661 +       u32 sid;
662 +       u32 direction;
663 +       u32 features;
664 +       struct snd_pcm_substream *substream;
665 +       struct snd_pcm_hardware hw;
666 +       struct work_struct elapsed_period;
667 +};
668 +
669 +/**
670 + * struct virtio_pcm_stream - VirtIO PCM stream.
671 + * @substreams: VirtIO substreams belonging to the stream.
672 + * @nsubstreams: Number of substreams.
673 + */
674 +struct virtio_pcm_stream {
675 +       struct virtio_pcm_substream **substreams;
676 +       u32 nsubstreams;
677 +};
678 +
679 +/**
680 + * struct virtio_pcm - VirtIO PCM device.
681 + * @list: VirtIO PCM list entry.
682 + * @nid: Function group node identifier.
683 + * @pcm: Kernel PCM device.
684 + * @streams: VirtIO PCM streams (playback and capture).
685 + */
686 +struct virtio_pcm {
687 +       struct list_head list;
688 +       u32 nid;
689 +       struct snd_pcm *pcm;
690 +       struct virtio_pcm_stream streams[SNDRV_PCM_STREAM_LAST + 1];
691 +};
692 +
693 +int virtsnd_pcm_validate(struct virtio_device *vdev);
694 +
695 +int virtsnd_pcm_parse_cfg(struct virtio_snd *snd);
696 +
697 +int virtsnd_pcm_build_devs(struct virtio_snd *snd);
698 +
699 +struct virtio_pcm *virtsnd_pcm_find(struct virtio_snd *snd, u32 nid);
700 +
701 +struct virtio_pcm *virtsnd_pcm_find_or_create(struct virtio_snd *snd, u32 nid);
702 +
703 +#endif /* VIRTIO_SND_PCM_H */