1 From 87ba8310e9f0882e85926ac1ef91333f8906b303 Mon Sep 17 00:00:00 2001
2 From: Takashi Iwai <tiwai@suse.de>
3 Date: Tue, 4 Sep 2018 17:58:49 +0200
4 Subject: [PATCH] staging: bcm2835-audio: Operate non-atomic PCM ops
6 commit 5c7883e5f27e829f3f3a2ba174d4a724bfd5f026 upstream.
8 This is the most significant part in the patch series.
10 The bcm2835-audio driver used to queue the commands to vc04 core via
11 workqueue, but basically the whole accesses to vc04 core are done in
12 the sleepable context, including the callback calls. In such a case,
13 rewriting the code using non-atomic PCM ops will simplify the logic a
16 This patch does it: all workqueue are gone and each former-work
17 implementation is now directly called from PCM ops like trigger and
20 Along with it, the DMA position updater, bcm2835_playback_fifo(), was
21 also rewritten to use a simpler logic. Now it handles the XRUN and
22 draining properly by calling snd_pcm_stop() conditionally.
24 The current position is kept in atomic_t value so that it can be read
25 concurrently from the pointer callback.
27 Also, the bcm2835_audio_instance object is allocated at the beginning
28 of bcm2835_audio_open(). This makes the resource management clearer.
30 Signed-off-by: Takashi Iwai <tiwai@suse.de>
31 Tested-by: Stefan Wahren <stefan.wahren@i2se.com>
32 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
34 .../vc04_services/bcm2835-audio/bcm2835-pcm.c | 74 +++---
35 .../bcm2835-audio/bcm2835-vchiq.c | 244 +++---------------
36 .../vc04_services/bcm2835-audio/bcm2835.h | 9 +-
37 3 files changed, 82 insertions(+), 245 deletions(-)
39 --- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835-pcm.c
40 +++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835-pcm.c
42 /* hardware definition */
43 static const struct snd_pcm_hardware snd_bcm2835_playback_hw = {
44 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
45 - SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
46 + SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
47 + SNDRV_PCM_INFO_DRAIN_TRIGGER),
48 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
49 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
51 @@ -27,7 +28,8 @@ static const struct snd_pcm_hardware snd
53 static const struct snd_pcm_hardware snd_bcm2835_playback_spdif_hw = {
54 .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
55 - SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
56 + SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
57 + SNDRV_PCM_INFO_DRAIN_TRIGGER),
58 .formats = SNDRV_PCM_FMTBIT_S16_LE,
59 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_44100 |
61 @@ -47,42 +49,34 @@ static void snd_bcm2835_playback_free(st
62 kfree(runtime->private_data);
65 -void bcm2835_playback_fifo(struct bcm2835_alsa_stream *alsa_stream)
66 +void bcm2835_playback_fifo(struct bcm2835_alsa_stream *alsa_stream,
69 - unsigned int consumed = 0;
71 + struct snd_pcm_substream *substream = alsa_stream->substream;
74 - audio_info("alsa_stream=%p substream=%p\n", alsa_stream,
75 - alsa_stream ? alsa_stream->substream : 0);
76 + if (!alsa_stream->period_size)
79 - consumed = bcm2835_audio_retrieve_buffers(alsa_stream);
81 - /* We get called only if playback was triggered, So, the number of buffers we retrieve in
82 - * each iteration are the buffers that have been played out already
85 - if (alsa_stream->period_size) {
86 - if ((alsa_stream->pos / alsa_stream->period_size) !=
87 - ((alsa_stream->pos + consumed) / alsa_stream->period_size))
90 - audio_debug("updating pos cur: %d + %d max:%d period_bytes:%d, hw_ptr: %d new_period:%d\n",
93 - alsa_stream->buffer_size,
94 - (int) (alsa_stream->period_size * alsa_stream->substream->runtime->periods),
95 - frames_to_bytes(alsa_stream->substream->runtime, alsa_stream->substream->runtime->status->hw_ptr),
97 - if (alsa_stream->buffer_size) {
98 - alsa_stream->pos += consumed & ~(1 << 30);
99 - alsa_stream->pos %= alsa_stream->buffer_size;
100 + if (bytes >= alsa_stream->buffer_size) {
101 + snd_pcm_stream_lock(substream);
102 + snd_pcm_stop(substream,
103 + alsa_stream->draining ?
104 + SNDRV_PCM_STATE_SETUP :
105 + SNDRV_PCM_STATE_XRUN);
106 + snd_pcm_stream_unlock(substream);
110 - if (alsa_stream->substream) {
112 - snd_pcm_period_elapsed(alsa_stream->substream);
114 - audio_warning(" unexpected NULL substream\n");
115 + pos = atomic_read(&alsa_stream->pos);
117 + pos %= alsa_stream->buffer_size;
118 + atomic_set(&alsa_stream->pos, pos);
120 + alsa_stream->period_offset += bytes;
121 + if (alsa_stream->period_offset >= alsa_stream->period_size) {
122 + alsa_stream->period_offset %= alsa_stream->period_size;
123 + snd_pcm_period_elapsed(substream);
127 @@ -246,7 +240,8 @@ static int snd_bcm2835_pcm_prepare(struc
129 alsa_stream->buffer_size = snd_pcm_lib_buffer_bytes(substream);
130 alsa_stream->period_size = snd_pcm_lib_period_bytes(substream);
131 - alsa_stream->pos = 0;
132 + atomic_set(&alsa_stream->pos, 0);
133 + alsa_stream->period_offset = 0;
134 alsa_stream->draining = false;
137 @@ -283,7 +278,7 @@ static int snd_bcm2835_pcm_trigger(struc
138 return bcm2835_audio_start(alsa_stream);
139 case SNDRV_PCM_TRIGGER_DRAIN:
140 alsa_stream->draining = true;
142 + return bcm2835_audio_drain(alsa_stream);
143 case SNDRV_PCM_TRIGGER_STOP:
144 return bcm2835_audio_stop(alsa_stream);
146 @@ -300,7 +295,7 @@ snd_bcm2835_pcm_pointer(struct snd_pcm_s
148 return snd_pcm_indirect_playback_pointer(substream,
149 &alsa_stream->pcm_indirect,
151 + atomic_read(&alsa_stream->pos));
155 @@ -338,6 +333,7 @@ int snd_bcm2835_new_pcm(struct bcm2835_c
158 pcm->private_data = chip;
159 + pcm->nonatomic = true;
160 strcpy(pcm->name, "bcm2835 ALSA");
162 chip->dest = AUDIO_DEST_AUTO;
163 @@ -367,6 +363,7 @@ int snd_bcm2835_new_spdif_pcm(struct bcm
166 pcm->private_data = chip;
167 + pcm->nonatomic = true;
168 strcpy(pcm->name, "bcm2835 IEC958/HDMI");
169 chip->pcm_spdif = pcm;
170 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
171 @@ -395,6 +392,7 @@ int snd_bcm2835_new_simple_pcm(struct bc
174 pcm->private_data = chip;
175 + pcm->nonatomic = true;
176 strcpy(pcm->name, name);
179 --- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835-vchiq.c
180 +++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835-vchiq.c
183 /* ---- Private Constants and Types ------------------------------------------ */
185 -#define BCM2835_AUDIO_STOP 0
186 -#define BCM2835_AUDIO_START 1
187 -#define BCM2835_AUDIO_WRITE 2
189 /* Logging macros (for remapping to other logging mechanisms, i.e., printf) */
190 #ifdef AUDIO_DEBUG_ENABLE
191 #define LOG_ERR(fmt, arg...) pr_err("%s:%d " fmt, __func__, __LINE__, ##arg)
192 @@ -55,17 +51,6 @@ struct bcm2835_audio_instance {
194 static bool force_bulk;
196 -/* ---- Private Variables ---------------------------------------------------- */
198 -/* ---- Private Function Prototypes ------------------------------------------ */
200 -/* ---- Private Functions ---------------------------------------------------- */
202 -static int bcm2835_audio_stop_worker(struct bcm2835_alsa_stream *alsa_stream);
203 -static int bcm2835_audio_start_worker(struct bcm2835_alsa_stream *alsa_stream);
204 -static int bcm2835_audio_write_worker(struct bcm2835_alsa_stream *alsa_stream,
205 - unsigned int count, void *src);
207 static void bcm2835_audio_lock(struct bcm2835_audio_instance *instance)
209 mutex_lock(&instance->vchi_mutex);
210 @@ -135,108 +120,6 @@ static const u32 BCM2835_AUDIO_WRITE_COO
211 static const u32 BCM2835_AUDIO_WRITE_COOKIE2 = ('D' << 24 | 'A' << 16 |
214 -struct bcm2835_audio_work {
215 - struct work_struct my_work;
216 - struct bcm2835_alsa_stream *alsa_stream;
219 - unsigned int count;
222 -static void my_wq_function(struct work_struct *work)
224 - struct bcm2835_audio_work *w =
225 - container_of(work, struct bcm2835_audio_work, my_work);
229 - case BCM2835_AUDIO_START:
230 - ret = bcm2835_audio_start_worker(w->alsa_stream);
232 - case BCM2835_AUDIO_STOP:
233 - ret = bcm2835_audio_stop_worker(w->alsa_stream);
235 - case BCM2835_AUDIO_WRITE:
236 - ret = bcm2835_audio_write_worker(w->alsa_stream, w->count,
240 - LOG_ERR(" Unexpected work: %p:%d\n", w->alsa_stream, w->cmd);
243 - kfree((void *)work);
246 -int bcm2835_audio_start(struct bcm2835_alsa_stream *alsa_stream)
248 - struct bcm2835_audio_work *work;
250 - work = kmalloc(sizeof(*work), GFP_ATOMIC);
251 - /*--- Queue some work (item 1) ---*/
253 - LOG_ERR(" .. Error: NULL work kmalloc\n");
256 - INIT_WORK(&work->my_work, my_wq_function);
257 - work->alsa_stream = alsa_stream;
258 - work->cmd = BCM2835_AUDIO_START;
259 - if (!queue_work(alsa_stream->my_wq, &work->my_work)) {
266 -int bcm2835_audio_stop(struct bcm2835_alsa_stream *alsa_stream)
268 - struct bcm2835_audio_work *work;
270 - work = kmalloc(sizeof(*work), GFP_ATOMIC);
271 - /*--- Queue some work (item 1) ---*/
273 - LOG_ERR(" .. Error: NULL work kmalloc\n");
276 - INIT_WORK(&work->my_work, my_wq_function);
277 - work->alsa_stream = alsa_stream;
278 - work->cmd = BCM2835_AUDIO_STOP;
279 - if (!queue_work(alsa_stream->my_wq, &work->my_work)) {
286 -int bcm2835_audio_write(struct bcm2835_alsa_stream *alsa_stream,
287 - unsigned int count, void *src)
289 - struct bcm2835_audio_work *work;
291 - work = kmalloc(sizeof(*work), GFP_ATOMIC);
292 - /*--- Queue some work (item 1) ---*/
294 - LOG_ERR(" .. Error: NULL work kmalloc\n");
297 - INIT_WORK(&work->my_work, my_wq_function);
298 - work->alsa_stream = alsa_stream;
299 - work->cmd = BCM2835_AUDIO_WRITE;
301 - work->count = count;
302 - if (!queue_work(alsa_stream->my_wq, &work->my_work)) {
309 -static void my_workqueue_quit(struct bcm2835_alsa_stream *alsa_stream)
311 - flush_workqueue(alsa_stream->my_wq);
312 - destroy_workqueue(alsa_stream->my_wq);
313 - alsa_stream->my_wq = NULL;
316 static void audio_vchi_callback(void *param,
317 const VCHI_CALLBACK_REASON_T reason,
319 @@ -249,47 +132,27 @@ static void audio_vchi_callback(void *pa
320 if (reason != VCHI_CALLBACK_MSG_AVAILABLE)
324 - LOG_ERR(" .. instance is null\n");
328 - if (!instance->vchi_handle) {
329 - LOG_ERR(" .. instance->vchi_handle is null\n");
333 status = vchi_msg_dequeue(instance->vchi_handle,
334 &m, sizeof(m), &msg_len, VCHI_FLAGS_NONE);
335 if (m.type == VC_AUDIO_MSG_TYPE_RESULT) {
336 - LOG_DBG(" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_RESULT, success=%d\n",
337 - instance, m.u.result.success);
338 instance->result = m.u.result.success;
339 complete(&instance->msg_avail_comp);
340 } else if (m.type == VC_AUDIO_MSG_TYPE_COMPLETE) {
341 - struct bcm2835_alsa_stream *alsa_stream = instance->alsa_stream;
343 - LOG_DBG(" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_COMPLETE, complete=%d\n",
344 - instance, m.u.complete.count);
345 if (m.u.complete.cookie1 != BCM2835_AUDIO_WRITE_COOKIE1 ||
346 m.u.complete.cookie2 != BCM2835_AUDIO_WRITE_COOKIE2)
347 - LOG_ERR(" .. response is corrupt\n");
348 - else if (alsa_stream) {
349 - atomic_add(m.u.complete.count,
350 - &alsa_stream->retrieved);
351 - bcm2835_playback_fifo(alsa_stream);
353 - LOG_ERR(" .. unexpected alsa_stream=%p\n",
356 + LOG_ERR("invalid cookie\n");
358 + bcm2835_playback_fifo(instance->alsa_stream,
359 + m.u.complete.count);
361 - LOG_ERR(" .. unexpected m.type=%d\n", m.type);
362 + LOG_ERR("unexpected callback type=%d\n", m.type);
366 -static struct bcm2835_audio_instance *
368 vc_vchi_audio_init(VCHI_INSTANCE_T vchi_instance,
369 - VCHI_CONNECTION_T *vchi_connection)
370 + VCHI_CONNECTION_T *vchi_connection,
371 + struct bcm2835_audio_instance *instance)
373 SERVICE_CREATION_T params = {
374 .version = VCHI_VERSION_EX(VC_AUDIOSERV_VER, VC_AUDIOSERV_MIN_VER),
375 @@ -298,23 +161,14 @@ vc_vchi_audio_init(VCHI_INSTANCE_T vchi_
378 .callback = audio_vchi_callback,
379 + .callback_param = instance,
380 .want_unaligned_bulk_rx = 1, //TODO: remove VCOS_FALSE
381 .want_unaligned_bulk_tx = 1, //TODO: remove VCOS_FALSE
384 - struct bcm2835_audio_instance *instance;
387 - /* Allocate memory for this instance */
388 - instance = kzalloc(sizeof(*instance), GFP_KERNEL);
390 - return ERR_PTR(-ENOMEM);
392 - /* Create a lock for exclusive, serialized VCHI connection access */
393 - mutex_init(&instance->vchi_mutex);
394 /* Open the VCHI service connections */
395 - params.callback_param = instance,
397 status = vchi_service_open(vchi_instance, ¶ms,
398 &instance->vchi_handle);
400 @@ -322,16 +176,16 @@ vc_vchi_audio_init(VCHI_INSTANCE_T vchi_
401 LOG_ERR("%s: failed to open VCHI service connection (status=%d)\n",
404 - return ERR_PTR(-EPERM);
408 /* Finished with the service for now */
409 vchi_service_release(instance->vchi_handle);
415 -static int vc_vchi_audio_deinit(struct bcm2835_audio_instance *instance)
416 +static void vc_vchi_audio_deinit(struct bcm2835_audio_instance *instance)
420 @@ -346,10 +200,6 @@ static int vc_vchi_audio_deinit(struct b
423 mutex_unlock(&instance->vchi_mutex);
430 int bcm2835_new_vchi_ctx(struct bcm2835_vchi_ctx *vchi_ctx)
431 @@ -387,39 +237,25 @@ void bcm2835_free_vchi_ctx(struct bcm283
432 vchi_ctx->vchi_instance = NULL;
435 -static int bcm2835_audio_open_connection(struct bcm2835_alsa_stream *alsa_stream)
437 - struct bcm2835_audio_instance *instance =
438 - (struct bcm2835_audio_instance *)alsa_stream->instance;
439 - struct bcm2835_vchi_ctx *vhci_ctx = alsa_stream->chip->vchi_ctx;
441 - /* Initialize an instance of the audio service */
442 - instance = vc_vchi_audio_init(vhci_ctx->vchi_instance,
443 - vhci_ctx->vchi_connection);
445 - if (IS_ERR(instance))
446 - return PTR_ERR(instance);
448 - instance->alsa_stream = alsa_stream;
449 - alsa_stream->instance = instance;
454 int bcm2835_audio_open(struct bcm2835_alsa_stream *alsa_stream)
456 + struct bcm2835_vchi_ctx *vchi_ctx = alsa_stream->chip->vchi_ctx;
457 struct bcm2835_audio_instance *instance;
460 - alsa_stream->my_wq = alloc_workqueue("my_queue", WQ_HIGHPRI, 1);
461 - if (!alsa_stream->my_wq)
462 + /* Allocate memory for this instance */
463 + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
466 + mutex_init(&instance->vchi_mutex);
467 + instance->alsa_stream = alsa_stream;
468 + alsa_stream->instance = instance;
470 - err = bcm2835_audio_open_connection(alsa_stream);
471 + err = vc_vchi_audio_init(vchi_ctx->vchi_instance,
472 + vchi_ctx->vchi_connection,
477 - instance = alsa_stream->instance;
478 + goto free_instance;
480 err = bcm2835_audio_send_simple(instance, VC_AUDIO_MSG_TYPE_OPEN,
482 @@ -438,8 +274,9 @@ int bcm2835_audio_open(struct bcm2835_al
485 vc_vchi_audio_deinit(instance);
487 - destroy_workqueue(alsa_stream->my_wq);
489 + alsa_stream->instance = NULL;
494 @@ -478,37 +315,46 @@ int bcm2835_audio_set_params(struct bcm2
495 return bcm2835_audio_send_msg(alsa_stream->instance, &m, true);
498 -static int bcm2835_audio_start_worker(struct bcm2835_alsa_stream *alsa_stream)
499 +int bcm2835_audio_start(struct bcm2835_alsa_stream *alsa_stream)
501 return bcm2835_audio_send_simple(alsa_stream->instance,
502 VC_AUDIO_MSG_TYPE_START, false);
505 -static int bcm2835_audio_stop_worker(struct bcm2835_alsa_stream *alsa_stream)
506 +int bcm2835_audio_stop(struct bcm2835_alsa_stream *alsa_stream)
508 return bcm2835_audio_send_simple(alsa_stream->instance,
509 VC_AUDIO_MSG_TYPE_STOP, false);
512 +int bcm2835_audio_drain(struct bcm2835_alsa_stream *alsa_stream)
514 + struct vc_audio_msg m = {
515 + .type = VC_AUDIO_MSG_TYPE_STOP,
516 + .u.stop.draining = 1,
519 + return bcm2835_audio_send_msg(alsa_stream->instance, &m, false);
522 int bcm2835_audio_close(struct bcm2835_alsa_stream *alsa_stream)
524 struct bcm2835_audio_instance *instance = alsa_stream->instance;
527 - my_workqueue_quit(alsa_stream);
529 err = bcm2835_audio_send_simple(alsa_stream->instance,
530 VC_AUDIO_MSG_TYPE_CLOSE, true);
532 /* Stop the audio service */
533 vc_vchi_audio_deinit(instance);
534 alsa_stream->instance = NULL;
540 -static int bcm2835_audio_write_worker(struct bcm2835_alsa_stream *alsa_stream,
541 - unsigned int size, void *src)
542 +int bcm2835_audio_write(struct bcm2835_alsa_stream *alsa_stream,
543 + unsigned int size, void *src)
545 struct bcm2835_audio_instance *instance = alsa_stream->instance;
546 struct vc_audio_msg m = {
547 @@ -558,13 +404,5 @@ static int bcm2835_audio_write_worker(st
551 -unsigned int bcm2835_audio_retrieve_buffers(struct bcm2835_alsa_stream *alsa_stream)
553 - unsigned int count = atomic_read(&alsa_stream->retrieved);
555 - atomic_sub(count, &alsa_stream->retrieved);
559 module_param(force_bulk, bool, 0444);
560 MODULE_PARM_DESC(force_bulk, "Force use of vchiq bulk for audio");
561 --- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.h
562 +++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.h
563 @@ -121,13 +121,12 @@ struct bcm2835_alsa_stream {
569 + unsigned int period_offset;
570 unsigned int buffer_size;
571 unsigned int period_size;
573 - atomic_t retrieved;
574 struct bcm2835_audio_instance *instance;
575 - struct workqueue_struct *my_wq;
579 @@ -152,11 +151,13 @@ int bcm2835_audio_set_params(struct bcm2
581 int bcm2835_audio_start(struct bcm2835_alsa_stream *alsa_stream);
582 int bcm2835_audio_stop(struct bcm2835_alsa_stream *alsa_stream);
583 +int bcm2835_audio_drain(struct bcm2835_alsa_stream *alsa_stream);
584 int bcm2835_audio_set_ctls(struct bcm2835_alsa_stream *alsa_stream);
585 int bcm2835_audio_write(struct bcm2835_alsa_stream *alsa_stream,
588 -void bcm2835_playback_fifo(struct bcm2835_alsa_stream *alsa_stream);
589 +void bcm2835_playback_fifo(struct bcm2835_alsa_stream *alsa_stream,
590 + unsigned int size);
591 unsigned int bcm2835_audio_retrieve_buffers(struct bcm2835_alsa_stream *alsa_stream);
593 #endif /* __SOUND_ARM_BCM2835_H */