From 5ab25fbaea5db975872007455aa9304183abdbe9 Mon Sep 17 00:00:00 2001 From: Marcin Szkudlinski Date: Tue, 15 Oct 2024 15:13:31 +0200 Subject: [PATCH] exttension to audio/audio_buffer.h API add .clean --- src/audio/buffers/comp_buffer.c | 14 ++++++++ src/audio/buffers/ring_buffer.c | 44 +++++++++++++++++------ src/audio/module_adapter/module_adapter.c | 4 +-- src/audio/pipeline/pipeline-graph.c | 5 +++ src/audio/pipeline/pipeline-params.c | 5 +++ src/include/sof/audio/audio_buffer.h | 18 ++++++++++ src/include/sof/audio/buffer.h | 14 -------- tools/plugin/modules/alsa.c | 4 +-- 8 files changed, 80 insertions(+), 28 deletions(-) diff --git a/src/audio/buffers/comp_buffer.c b/src/audio/buffers/comp_buffer.c index ff32f3942244..b7bdd4dbdb1b 100644 --- a/src/audio/buffers/comp_buffer.c +++ b/src/audio/buffers/comp_buffer.c @@ -152,6 +152,19 @@ static int comp_buffer_sink_set_alignment_constants(struct sof_sink *sink, return 0; } +static void comp_buffer_clean(struct sof_audio_buffer *audio_buffer) +{ + if (!audio_buffer) + return; + + struct comp_buffer *buffer = container_of(audio_buffer, struct comp_buffer, audio_buffer); + + /* reset rw pointers and avail/free bytes counters */ + audio_stream_reset(&buffer->stream); + /* clear buffer contents */ + buffer_zero(buffer); +} + /* free component in the pipeline */ static void comp_buffer_free(struct sof_audio_buffer *audio_buffer) { @@ -197,6 +210,7 @@ static const struct sink_ops comp_buffer_sink_ops = { static const struct audio_buffer_ops audio_buffer_ops = { .free = comp_buffer_free, + .clean = comp_buffer_clean, }; static struct comp_buffer *buffer_alloc_struct(void *stream_addr, size_t size, uint32_t caps, diff --git a/src/audio/buffers/ring_buffer.c b/src/audio/buffers/ring_buffer.c index 45849b0b0669..d0ebb00d42bd 100644 --- a/src/audio/buffers/ring_buffer.c +++ b/src/audio/buffers/ring_buffer.c @@ -31,16 +31,6 @@ static inline struct ring_buffer *ring_buffer_from_source(struct sof_source *sou return container_of(audio_buffer, struct ring_buffer, audio_buffer); } -/** - * @brief remove the queue from the list, free memory - */ -static void ring_buffer_free(struct sof_audio_buffer *buffer) -{ - struct ring_buffer *ring_buffer = (struct ring_buffer *)buffer; - - rfree((__sparse_force void *)ring_buffer->_data_buffer); -} - /** * @brief return true if the ring buffer is shared between 2 cores */ @@ -93,6 +83,39 @@ static inline void ring_buffer_writeback_shared(struct ring_buffer *ring_buffer, dcache_writeback_region(ptr, size); } + +/** + * @brief remove the queue from the list, free memory + */ +static void ring_buffer_free(struct sof_audio_buffer *audio_buffer) +{ + if (!audio_buffer) + return; + + struct ring_buffer *ring_buffer = + container_of(audio_buffer, struct ring_buffer, audio_buffer); + + rfree((__sparse_force void *)ring_buffer->_data_buffer); +} + +static void ring_buffer_clean(struct sof_audio_buffer *audio_buffer) +{ + if (!audio_buffer) + return; + + struct ring_buffer *ring_buffer = + container_of(audio_buffer, struct ring_buffer, audio_buffer); + + ring_buffer->_write_offset = 0; + ring_buffer->_read_offset = 0; + + ring_buffer_invalidate_shared(ring_buffer, ring_buffer->_data_buffer, + ring_buffer->data_buffer_size); + bzero(ring_buffer->_data_buffer, ring_buffer->data_buffer_size); + ring_buffer_writeback_shared(ring_buffer, ring_buffer->_data_buffer, + ring_buffer->data_buffer_size); +} + static inline uint8_t __sparse_cache *ring_buffer_get_pointer(struct ring_buffer *ring_buffer, size_t offset) { @@ -277,6 +300,7 @@ static const struct sink_ops ring_buffer_sink_ops = { static const struct audio_buffer_ops audio_buffer_ops = { .free = ring_buffer_free, + .clean = ring_buffer_clean }; struct ring_buffer *ring_buffer_create(size_t min_available, size_t min_free_space, bool is_shared, diff --git a/src/audio/module_adapter/module_adapter.c b/src/audio/module_adapter/module_adapter.c index 33049f69f20e..9bccebd95f8a 100644 --- a/src/audio/module_adapter/module_adapter.c +++ b/src/audio/module_adapter/module_adapter.c @@ -385,7 +385,7 @@ int module_adapter_prepare(struct comp_dev *dev) irq_local_enable(flags); buffer_set_params(buffer, mod->stream_params, BUFFER_UPDATE_FORCE); - buffer_reset_pos(buffer, NULL); + audio_buffer_clean(&buffer->audio_buffer); } } else { list_for_item(blist, &mod->sink_buffer_list) { @@ -400,7 +400,7 @@ int module_adapter_prepare(struct comp_dev *dev) } buffer_set_params(buffer, mod->stream_params, BUFFER_UPDATE_FORCE); - buffer_reset_pos(buffer, NULL); + audio_buffer_clean(&buffer->audio_buffer); } } diff --git a/src/audio/pipeline/pipeline-graph.c b/src/audio/pipeline/pipeline-graph.c index 8b21774c6d19..bb865cc83229 100644 --- a/src/audio/pipeline/pipeline-graph.c +++ b/src/audio/pipeline/pipeline-graph.c @@ -360,6 +360,11 @@ static int pipeline_comp_reset(struct comp_dev *current, return pipeline_for_each_comp(current, ctx, dir); } +static inline void buffer_reset_params(struct comp_buffer *buffer, void *data) +{ + audio_buffer_reset_params(&buffer->audio_buffer); +} + /* reset the whole pipeline */ int pipeline_reset(struct pipeline *p, struct comp_dev *host) { diff --git a/src/audio/pipeline/pipeline-params.c b/src/audio/pipeline/pipeline-params.c index c9b4f132c9e4..859df19d37cb 100644 --- a/src/audio/pipeline/pipeline-params.c +++ b/src/audio/pipeline/pipeline-params.c @@ -289,6 +289,11 @@ static int pipeline_comp_prepare(struct comp_dev *current, return pipeline_for_each_comp(current, ctx, dir); } +static void buffer_reset_pos(struct comp_buffer *buffer, void *data) +{ + audio_buffer_clean(&buffer->audio_buffer); +} + /* prepare the pipeline for usage */ int pipeline_prepare(struct pipeline *p, struct comp_dev *dev) { diff --git a/src/include/sof/audio/audio_buffer.h b/src/include/sof/audio/audio_buffer.h index 3b8aedd30fb4..f00af703f182 100644 --- a/src/include/sof/audio/audio_buffer.h +++ b/src/include/sof/audio/audio_buffer.h @@ -25,6 +25,13 @@ struct audio_buffer_ops { * OPTIONAL */ void (*free)(struct sof_audio_buffer *buffer); + + /** + * @brief clean all buffer data leaving config as is + * the procedure is to be called only when buffer is not in use + * OPTIONAL + */ + void (*clean)(struct sof_audio_buffer *buffer); }; /* base class for all buffers, all buffers must inherit from it */ @@ -267,4 +274,15 @@ void audio_buffer_init(struct sof_audio_buffer *buffer, uint32_t buffer_type, bo */ void audio_buffer_free(struct sof_audio_buffer *buffer); +/** + * @brief clean all buffer data leaving config as is + * the procedure is to be called only when buffer is not in use + */ +static inline +void audio_buffer_clean(struct sof_audio_buffer *buffer) +{ + if (buffer->ops->clean) + buffer->ops->clean(buffer); +} + #endif /* __SOF_AUDIO_BUFFER__ */ diff --git a/src/include/sof/audio/buffer.h b/src/include/sof/audio/buffer.h index 55e01b624ff9..aaaa20f399c8 100644 --- a/src/include/sof/audio/buffer.h +++ b/src/include/sof/audio/buffer.h @@ -279,15 +279,6 @@ static inline uint32_t buffer_pipeline_id(const struct comp_buffer *buffer) return buffer->stream.runtime_stream_params.pipeline_id; } -static inline void buffer_reset_pos(struct comp_buffer *buffer, void *data) -{ - /* reset rw pointers and avail/free bytes counters */ - audio_stream_reset(&buffer->stream); - - /* clear buffer contents */ - buffer_zero(buffer); -} - /* Run-time buffer re-configuration calls this too, so it must use cached access */ static inline void buffer_init_stream(struct comp_buffer *buffer, size_t size) { @@ -295,9 +286,4 @@ static inline void buffer_init_stream(struct comp_buffer *buffer, size_t size) audio_stream_init(&buffer->stream, buffer->stream.addr, size); } -static inline void buffer_reset_params(struct comp_buffer *buffer, void *data) -{ - audio_buffer_reset_params(&buffer->audio_buffer); -} - #endif /* __SOF_AUDIO_BUFFER_H__ */ diff --git a/tools/plugin/modules/alsa.c b/tools/plugin/modules/alsa.c index 2d946529d05f..e1301c2d52bd 100644 --- a/tools/plugin/modules/alsa.c +++ b/tools/plugin/modules/alsa.c @@ -429,7 +429,7 @@ static int arecord_params(struct comp_dev *dev, struct sof_ipc_stream_params *pa /* file component sink/source buffer period count */ buffer = comp_dev_get_first_data_consumer(dev); - buffer_reset_pos(buffer, NULL); + audio_buffer_clean(&buffer->audio_buffer); comp_dbg(dev, "prepare done ret = %d", ret); @@ -461,7 +461,7 @@ static int aplay_params(struct comp_dev *dev, struct sof_ipc_stream_params *para /* file component sink/source buffer period count */ buffer = comp_dev_get_first_data_producer(dev); - buffer_reset_pos(buffer, NULL); + audio_buffer_clean(&buffer->audio_buffer); comp_dbg(dev, "prepare done ret = %d", ret); return 0;