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 / 0003-ALSA-virtio-handling-control-messages.patch
1 From d4c8a3a4b9de5a25b6963f3ae1b8a5cb32081de5 Mon Sep 17 00:00:00 2001
2 From: Anton Yakovlev <anton.yakovlev@opensynergy.com>
3 Date: Tue, 2 Mar 2021 17:47:03 +0100
4 Subject: [PATCH] ALSA: virtio: handling control messages
5
6 The control queue can be used by different parts of the driver to send
7 commands to the device. Control messages can be either synchronous or
8 asynchronous. The lifetime of a message is controlled by a reference
9 count.
10
11 Introduce a module parameter to set the message completion timeout:
12   msg_timeout_ms [=1000]
13
14 Signed-off-by: Anton Yakovlev <anton.yakovlev@opensynergy.com>
15 Link: https://lore.kernel.org/r/20210302164709.3142702-4-anton.yakovlev@opensynergy.com
16 Signed-off-by: Takashi Iwai <tiwai@suse.de>
17 Signed-off-by: Vasyl Vavrychuk <vasyl.vavrychuk@opensynergy.com>
18 ---
19  sound/virtio/Makefile         |   3 +-
20  sound/virtio/virtio_card.c    |  13 ++
21  sound/virtio/virtio_card.h    |   7 +
22  sound/virtio/virtio_ctl_msg.c | 310 ++++++++++++++++++++++++++++++++++
23  sound/virtio/virtio_ctl_msg.h |  78 +++++++++
24  5 files changed, 410 insertions(+), 1 deletion(-)
25  create mode 100644 sound/virtio/virtio_ctl_msg.c
26  create mode 100644 sound/virtio/virtio_ctl_msg.h
27
28 diff --git a/sound/virtio/Makefile b/sound/virtio/Makefile
29 index 8c87ebb9982b..dc551e637441 100644
30 --- a/sound/virtio/Makefile
31 +++ b/sound/virtio/Makefile
32 @@ -3,5 +3,6 @@
33  obj-$(CONFIG_SND_VIRTIO) += virtio_snd.o
34  
35  virtio_snd-objs := \
36 -       virtio_card.o
37 +       virtio_card.o \
38 +       virtio_ctl_msg.o
39  
40 diff --git a/sound/virtio/virtio_card.c b/sound/virtio/virtio_card.c
41 index 5a37056858e9..b757b2444078 100644
42 --- a/sound/virtio/virtio_card.c
43 +++ b/sound/virtio/virtio_card.c
44 @@ -11,6 +11,10 @@
45  
46  #include "virtio_card.h"
47  
48 +u32 virtsnd_msg_timeout_ms = MSEC_PER_SEC;
49 +module_param_named(msg_timeout_ms, virtsnd_msg_timeout_ms, uint, 0644);
50 +MODULE_PARM_DESC(msg_timeout_ms, "Message completion timeout in milliseconds");
51 +
52  static void virtsnd_remove(struct virtio_device *vdev);
53  
54  /**
55 @@ -96,9 +100,11 @@ static int virtsnd_find_vqs(struct virtio_snd *snd)
56  {
57         struct virtio_device *vdev = snd->vdev;
58         static vq_callback_t *callbacks[VIRTIO_SND_VQ_MAX] = {
59 +               [VIRTIO_SND_VQ_CONTROL] = virtsnd_ctl_notify_cb,
60                 [VIRTIO_SND_VQ_EVENT] = virtsnd_event_notify_cb
61         };
62         static const char *names[VIRTIO_SND_VQ_MAX] = {
63 +               [VIRTIO_SND_VQ_CONTROL] = "virtsnd-ctl",
64                 [VIRTIO_SND_VQ_EVENT] = "virtsnd-event"
65         };
66         struct virtqueue *vqs[VIRTIO_SND_VQ_MAX] = { 0 };
67 @@ -226,6 +232,11 @@ static int virtsnd_validate(struct virtio_device *vdev)
68                 return -EINVAL;
69         }
70  
71 +       if (!virtsnd_msg_timeout_ms) {
72 +               dev_err(&vdev->dev, "msg_timeout_ms value cannot be zero\n");
73 +               return -EINVAL;
74 +       }
75 +
76         return 0;
77  }
78  
79 @@ -247,6 +258,7 @@ static int virtsnd_probe(struct virtio_device *vdev)
80                 return -ENOMEM;
81  
82         snd->vdev = vdev;
83 +       INIT_LIST_HEAD(&snd->ctl_msgs);
84  
85         vdev->priv = snd;
86  
87 @@ -283,6 +295,7 @@ static void virtsnd_remove(struct virtio_device *vdev)
88         struct virtio_snd *snd = vdev->priv;
89  
90         virtsnd_disable_event_vq(snd);
91 +       virtsnd_ctl_msg_cancel_all(snd);
92  
93         if (snd->card)
94                 snd_card_free(snd->card);
95 diff --git a/sound/virtio/virtio_card.h b/sound/virtio/virtio_card.h
96 index b903b1b12e90..1e76eeff160f 100644
97 --- a/sound/virtio/virtio_card.h
98 +++ b/sound/virtio/virtio_card.h
99 @@ -11,6 +11,8 @@
100  #include <sound/core.h>
101  #include <uapi/linux/virtio_snd.h>
102  
103 +#include "virtio_ctl_msg.h"
104 +
105  #define VIRTIO_SND_CARD_DRIVER "virtio-snd"
106  #define VIRTIO_SND_CARD_NAME   "VirtIO SoundCard"
107  
108 @@ -29,15 +31,20 @@ struct virtio_snd_queue {
109   * @vdev: Underlying virtio device.
110   * @queues: Virtqueue wrappers.
111   * @card: ALSA sound card.
112 + * @ctl_msgs: Pending control request list.
113   * @event_msgs: Device events.
114   */
115  struct virtio_snd {
116         struct virtio_device *vdev;
117         struct virtio_snd_queue queues[VIRTIO_SND_VQ_MAX];
118         struct snd_card *card;
119 +       struct list_head ctl_msgs;
120         struct virtio_snd_event *event_msgs;
121  };
122  
123 +/* Message completion timeout in milliseconds (module parameter). */
124 +extern u32 virtsnd_msg_timeout_ms;
125 +
126  static inline struct virtio_snd_queue *
127  virtsnd_control_queue(struct virtio_snd *snd)
128  {
129 diff --git a/sound/virtio/virtio_ctl_msg.c b/sound/virtio/virtio_ctl_msg.c
130 new file mode 100644
131 index 000000000000..26ff7e7cc041
132 --- /dev/null
133 +++ b/sound/virtio/virtio_ctl_msg.c
134 @@ -0,0 +1,310 @@
135 +// SPDX-License-Identifier: GPL-2.0+
136 +/*
137 + * virtio-snd: Virtio sound device
138 + * Copyright (C) 2021 OpenSynergy GmbH
139 + */
140 +#include <linux/moduleparam.h>
141 +#include <linux/virtio_config.h>
142 +
143 +#include "virtio_card.h"
144 +
145 +/**
146 + * struct virtio_snd_msg - Control message.
147 + * @sg_request: Scattergather list containing a device request (header).
148 + * @sg_response: Scattergather list containing a device response (status).
149 + * @list: Pending message list entry.
150 + * @notify: Request completed notification.
151 + * @ref_count: Reference count used to manage a message lifetime.
152 + */
153 +struct virtio_snd_msg {
154 +       struct scatterlist sg_request;
155 +       struct scatterlist sg_response;
156 +       struct list_head list;
157 +       struct completion notify;
158 +       refcount_t ref_count;
159 +};
160 +
161 +/**
162 + * virtsnd_ctl_msg_ref() - Increment reference counter for the message.
163 + * @msg: Control message.
164 + *
165 + * Context: Any context.
166 + */
167 +void virtsnd_ctl_msg_ref(struct virtio_snd_msg *msg)
168 +{
169 +       refcount_inc(&msg->ref_count);
170 +}
171 +
172 +/**
173 + * virtsnd_ctl_msg_unref() - Decrement reference counter for the message.
174 + * @msg: Control message.
175 + *
176 + * The message will be freed when the ref_count value is 0.
177 + *
178 + * Context: Any context.
179 + */
180 +void virtsnd_ctl_msg_unref(struct virtio_snd_msg *msg)
181 +{
182 +       if (refcount_dec_and_test(&msg->ref_count))
183 +               kfree(msg);
184 +}
185 +
186 +/**
187 + * virtsnd_ctl_msg_request() - Get a pointer to the request header.
188 + * @msg: Control message.
189 + *
190 + * Context: Any context.
191 + */
192 +void *virtsnd_ctl_msg_request(struct virtio_snd_msg *msg)
193 +{
194 +       return sg_virt(&msg->sg_request);
195 +}
196 +
197 +/**
198 + * virtsnd_ctl_msg_request() - Get a pointer to the response header.
199 + * @msg: Control message.
200 + *
201 + * Context: Any context.
202 + */
203 +void *virtsnd_ctl_msg_response(struct virtio_snd_msg *msg)
204 +{
205 +       return sg_virt(&msg->sg_response);
206 +}
207 +
208 +/**
209 + * virtsnd_ctl_msg_alloc() - Allocate and initialize a control message.
210 + * @request_size: Size of request header.
211 + * @response_size: Size of response header.
212 + * @gfp: Kernel flags for memory allocation.
213 + *
214 + * The message will be automatically freed when the ref_count value is 0.
215 + *
216 + * Context: Any context. May sleep if @gfp flags permit.
217 + * Return: Allocated message on success, NULL on failure.
218 + */
219 +struct virtio_snd_msg *virtsnd_ctl_msg_alloc(size_t request_size,
220 +                                            size_t response_size, gfp_t gfp)
221 +{
222 +       struct virtio_snd_msg *msg;
223 +
224 +       if (!request_size || !response_size)
225 +               return NULL;
226 +
227 +       msg = kzalloc(sizeof(*msg) + request_size + response_size, gfp);
228 +       if (!msg)
229 +               return NULL;
230 +
231 +       sg_init_one(&msg->sg_request, (u8 *)msg + sizeof(*msg), request_size);
232 +       sg_init_one(&msg->sg_response, (u8 *)msg + sizeof(*msg) + request_size,
233 +                   response_size);
234 +
235 +       INIT_LIST_HEAD(&msg->list);
236 +       init_completion(&msg->notify);
237 +       /* This reference is dropped in virtsnd_ctl_msg_complete(). */
238 +       refcount_set(&msg->ref_count, 1);
239 +
240 +       return msg;
241 +}
242 +
243 +/**
244 + * virtsnd_ctl_msg_send() - Send a control message.
245 + * @snd: VirtIO sound device.
246 + * @msg: Control message.
247 + * @out_sgs: Additional sg-list to attach to the request header (may be NULL).
248 + * @in_sgs: Additional sg-list to attach to the response header (may be NULL).
249 + * @nowait: Flag indicating whether to wait for completion.
250 + *
251 + * Context: Any context. Takes and releases the control queue spinlock.
252 + *          May sleep if @nowait is false.
253 + * Return: 0 on success, -errno on failure.
254 + */
255 +int virtsnd_ctl_msg_send(struct virtio_snd *snd, struct virtio_snd_msg *msg,
256 +                        struct scatterlist *out_sgs,
257 +                        struct scatterlist *in_sgs, bool nowait)
258 +{
259 +       struct virtio_device *vdev = snd->vdev;
260 +       struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
261 +       unsigned int js = msecs_to_jiffies(virtsnd_msg_timeout_ms);
262 +       struct virtio_snd_hdr *request = virtsnd_ctl_msg_request(msg);
263 +       struct virtio_snd_hdr *response = virtsnd_ctl_msg_response(msg);
264 +       unsigned int nouts = 0;
265 +       unsigned int nins = 0;
266 +       struct scatterlist *psgs[4];
267 +       bool notify = false;
268 +       unsigned long flags;
269 +       int rc;
270 +
271 +       virtsnd_ctl_msg_ref(msg);
272 +
273 +       /* Set the default status in case the message was canceled. */
274 +       response->code = cpu_to_le32(VIRTIO_SND_S_IO_ERR);
275 +
276 +       psgs[nouts++] = &msg->sg_request;
277 +       if (out_sgs)
278 +               psgs[nouts++] = out_sgs;
279 +
280 +       psgs[nouts + nins++] = &msg->sg_response;
281 +       if (in_sgs)
282 +               psgs[nouts + nins++] = in_sgs;
283 +
284 +       spin_lock_irqsave(&queue->lock, flags);
285 +       rc = virtqueue_add_sgs(queue->vqueue, psgs, nouts, nins, msg,
286 +                              GFP_ATOMIC);
287 +       if (!rc) {
288 +               notify = virtqueue_kick_prepare(queue->vqueue);
289 +
290 +               list_add_tail(&msg->list, &snd->ctl_msgs);
291 +       }
292 +       spin_unlock_irqrestore(&queue->lock, flags);
293 +
294 +       if (rc) {
295 +               dev_err(&vdev->dev, "failed to send control message (0x%08x)\n",
296 +                       le32_to_cpu(request->code));
297 +
298 +               /*
299 +                * Since in this case virtsnd_ctl_msg_complete() will not be
300 +                * called, it is necessary to decrement the reference count.
301 +                */
302 +               virtsnd_ctl_msg_unref(msg);
303 +
304 +               goto on_exit;
305 +       }
306 +
307 +       if (notify)
308 +               virtqueue_notify(queue->vqueue);
309 +
310 +       if (nowait)
311 +               goto on_exit;
312 +
313 +       rc = wait_for_completion_interruptible_timeout(&msg->notify, js);
314 +       if (rc <= 0) {
315 +               if (!rc) {
316 +                       dev_err(&vdev->dev,
317 +                               "control message (0x%08x) timeout\n",
318 +                               le32_to_cpu(request->code));
319 +                       rc = -ETIMEDOUT;
320 +               }
321 +
322 +               goto on_exit;
323 +       }
324 +
325 +       switch (le32_to_cpu(response->code)) {
326 +       case VIRTIO_SND_S_OK:
327 +               rc = 0;
328 +               break;
329 +       case VIRTIO_SND_S_NOT_SUPP:
330 +               rc = -EOPNOTSUPP;
331 +               break;
332 +       case VIRTIO_SND_S_IO_ERR:
333 +               rc = -EIO;
334 +               break;
335 +       default:
336 +               rc = -EINVAL;
337 +               break;
338 +       }
339 +
340 +on_exit:
341 +       virtsnd_ctl_msg_unref(msg);
342 +
343 +       return rc;
344 +}
345 +
346 +/**
347 + * virtsnd_ctl_msg_complete() - Complete a control message.
348 + * @msg: Control message.
349 + *
350 + * Context: Any context. Expects the control queue spinlock to be held by
351 + *          caller.
352 + */
353 +void virtsnd_ctl_msg_complete(struct virtio_snd_msg *msg)
354 +{
355 +       list_del(&msg->list);
356 +       complete(&msg->notify);
357 +
358 +       virtsnd_ctl_msg_unref(msg);
359 +}
360 +
361 +/**
362 + * virtsnd_ctl_msg_cancel_all() - Cancel all pending control messages.
363 + * @snd: VirtIO sound device.
364 + *
365 + * Context: Any context.
366 + */
367 +void virtsnd_ctl_msg_cancel_all(struct virtio_snd *snd)
368 +{
369 +       struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
370 +       unsigned long flags;
371 +
372 +       spin_lock_irqsave(&queue->lock, flags);
373 +       while (!list_empty(&snd->ctl_msgs)) {
374 +               struct virtio_snd_msg *msg =
375 +                       list_first_entry(&snd->ctl_msgs, struct virtio_snd_msg,
376 +                                        list);
377 +
378 +               virtsnd_ctl_msg_complete(msg);
379 +       }
380 +       spin_unlock_irqrestore(&queue->lock, flags);
381 +}
382 +
383 +/**
384 + * virtsnd_ctl_query_info() - Query the item configuration from the device.
385 + * @snd: VirtIO sound device.
386 + * @command: Control request code (VIRTIO_SND_R_XXX_INFO).
387 + * @start_id: Item start identifier.
388 + * @count: Item count to query.
389 + * @size: Item information size in bytes.
390 + * @info: Buffer for storing item information.
391 + *
392 + * Context: Any context that permits to sleep.
393 + * Return: 0 on success, -errno on failure.
394 + */
395 +int virtsnd_ctl_query_info(struct virtio_snd *snd, int command, int start_id,
396 +                          int count, size_t size, void *info)
397 +{
398 +       struct virtio_snd_msg *msg;
399 +       struct virtio_snd_query_info *query;
400 +       struct scatterlist sg;
401 +
402 +       msg = virtsnd_ctl_msg_alloc(sizeof(*query),
403 +                                   sizeof(struct virtio_snd_hdr), GFP_KERNEL);
404 +       if (!msg)
405 +               return -ENOMEM;
406 +
407 +       query = virtsnd_ctl_msg_request(msg);
408 +       query->hdr.code = cpu_to_le32(command);
409 +       query->start_id = cpu_to_le32(start_id);
410 +       query->count = cpu_to_le32(count);
411 +       query->size = cpu_to_le32(size);
412 +
413 +       sg_init_one(&sg, info, count * size);
414 +
415 +       return virtsnd_ctl_msg_send(snd, msg, NULL, &sg, false);
416 +}
417 +
418 +/**
419 + * virtsnd_ctl_notify_cb() - Process all completed control messages.
420 + * @vqueue: Underlying control virtqueue.
421 + *
422 + * This callback function is called upon a vring interrupt request from the
423 + * device.
424 + *
425 + * Context: Interrupt context. Takes and releases the control queue spinlock.
426 + */
427 +void virtsnd_ctl_notify_cb(struct virtqueue *vqueue)
428 +{
429 +       struct virtio_snd *snd = vqueue->vdev->priv;
430 +       struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
431 +       struct virtio_snd_msg *msg;
432 +       u32 length;
433 +       unsigned long flags;
434 +
435 +       spin_lock_irqsave(&queue->lock, flags);
436 +       do {
437 +               virtqueue_disable_cb(vqueue);
438 +               while ((msg = virtqueue_get_buf(vqueue, &length)))
439 +                       virtsnd_ctl_msg_complete(msg);
440 +               if (unlikely(virtqueue_is_broken(vqueue)))
441 +                       break;
442 +       } while (!virtqueue_enable_cb(vqueue));
443 +       spin_unlock_irqrestore(&queue->lock, flags);
444 +}
445 diff --git a/sound/virtio/virtio_ctl_msg.h b/sound/virtio/virtio_ctl_msg.h
446 new file mode 100644
447 index 000000000000..7f4db044f28e
448 --- /dev/null
449 +++ b/sound/virtio/virtio_ctl_msg.h
450 @@ -0,0 +1,78 @@
451 +/* SPDX-License-Identifier: GPL-2.0+ */
452 +/*
453 + * virtio-snd: Virtio sound device
454 + * Copyright (C) 2021 OpenSynergy GmbH
455 + */
456 +#ifndef VIRTIO_SND_MSG_H
457 +#define VIRTIO_SND_MSG_H
458 +
459 +#include <linux/atomic.h>
460 +#include <linux/virtio.h>
461 +
462 +struct virtio_snd;
463 +struct virtio_snd_msg;
464 +
465 +void virtsnd_ctl_msg_ref(struct virtio_snd_msg *msg);
466 +
467 +void virtsnd_ctl_msg_unref(struct virtio_snd_msg *msg);
468 +
469 +void *virtsnd_ctl_msg_request(struct virtio_snd_msg *msg);
470 +
471 +void *virtsnd_ctl_msg_response(struct virtio_snd_msg *msg);
472 +
473 +struct virtio_snd_msg *virtsnd_ctl_msg_alloc(size_t request_size,
474 +                                            size_t response_size, gfp_t gfp);
475 +
476 +int virtsnd_ctl_msg_send(struct virtio_snd *snd, struct virtio_snd_msg *msg,
477 +                        struct scatterlist *out_sgs,
478 +                        struct scatterlist *in_sgs, bool nowait);
479 +
480 +/**
481 + * virtsnd_ctl_msg_send_sync() - Simplified sending of synchronous message.
482 + * @snd: VirtIO sound device.
483 + * @msg: Control message.
484 + *
485 + * After returning from this function, the message will be deleted. If message
486 + * content is still needed, the caller must additionally to
487 + * virtsnd_ctl_msg_ref/unref() it.
488 + *
489 + * The msg_timeout_ms module parameter defines the message completion timeout.
490 + * If the message is not completed within this time, the function will return an
491 + * error.
492 + *
493 + * Context: Any context that permits to sleep.
494 + * Return: 0 on success, -errno on failure.
495 + *
496 + * The return value is a message status code (VIRTIO_SND_S_XXX) converted to an
497 + * appropriate -errno value.
498 + */
499 +static inline int virtsnd_ctl_msg_send_sync(struct virtio_snd *snd,
500 +                                           struct virtio_snd_msg *msg)
501 +{
502 +       return virtsnd_ctl_msg_send(snd, msg, NULL, NULL, false);
503 +}
504 +
505 +/**
506 + * virtsnd_ctl_msg_send_async() - Simplified sending of asynchronous message.
507 + * @snd: VirtIO sound device.
508 + * @msg: Control message.
509 + *
510 + * Context: Any context.
511 + * Return: 0 on success, -errno on failure.
512 + */
513 +static inline int virtsnd_ctl_msg_send_async(struct virtio_snd *snd,
514 +                                            struct virtio_snd_msg *msg)
515 +{
516 +       return virtsnd_ctl_msg_send(snd, msg, NULL, NULL, true);
517 +}
518 +
519 +void virtsnd_ctl_msg_cancel_all(struct virtio_snd *snd);
520 +
521 +void virtsnd_ctl_msg_complete(struct virtio_snd_msg *msg);
522 +
523 +int virtsnd_ctl_query_info(struct virtio_snd *snd, int command, int start_id,
524 +                          int count, size_t size, void *info);
525 +
526 +void virtsnd_ctl_notify_cb(struct virtqueue *vqueue);
527 +
528 +#endif /* VIRTIO_SND_MSG_H */