diff --git a/src/audio/buffers/comp_buffer.c b/src/audio/buffers/comp_buffer.c index cc38a7dfead7..502103693689 100644 --- a/src/audio/buffers/comp_buffer.c +++ b/src/audio/buffers/comp_buffer.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -158,10 +159,7 @@ static void comp_buffer_free(struct sof_audio_buffer *audio_buffer) #ifdef CONFIG_SOF_USERSPACE_LL assert(alloc); - if (alloc->vreg) - vregion_free(alloc->vreg, buffer->stream.addr); - else - sof_heap_free(alloc->heap, buffer->stream.addr); + sof_ctx_free(alloc, buffer->stream.addr); #else rfree(buffer->stream.addr); #endif @@ -214,12 +212,7 @@ static struct comp_buffer *buffer_alloc_struct(struct mod_alloc_ctx *alloc, if (is_shared) flags |= SOF_MEM_FLAG_COHERENT; - if (!alloc || !alloc->vreg) - buffer = sof_heap_alloc(alloc ? alloc->heap : NULL, flags, sizeof(*buffer), 0); - else if (is_shared) - buffer = vregion_alloc_coherent(alloc->vreg, VREGION_MEM_TYPE_INTERIM, sizeof(*buffer)); - else - buffer = vregion_alloc(alloc->vreg, VREGION_MEM_TYPE_INTERIM, sizeof(*buffer)); + buffer = sof_ctx_alloc(alloc, flags, sizeof(*buffer), 0); if (!buffer) { tr_err(&buffer_tr, "could not alloc structure"); return NULL; @@ -265,10 +258,7 @@ struct comp_buffer *buffer_alloc(struct mod_alloc_ctx *alloc, size_t size, uint3 #ifdef CONFIG_SOF_USERSPACE_LL assert(alloc); - if (alloc->vreg) - stream_addr = vregion_alloc_align(alloc->vreg, VREGION_MEM_TYPE_INTERIM, size, align); - else - stream_addr = sof_heap_alloc(alloc->heap, flags, size, align); + stream_addr = sof_ctx_alloc(alloc, flags, size, align); #else stream_addr = rballoc_align(flags, size, align); #endif @@ -283,10 +273,7 @@ struct comp_buffer *buffer_alloc(struct mod_alloc_ctx *alloc, size_t size, uint3 tr_err(&buffer_tr, "could not alloc buffer structure"); #ifdef CONFIG_SOF_USERSPACE_LL assert(alloc); - if (alloc->vreg) - vregion_free(alloc->vreg, stream_addr); - else - sof_heap_free(alloc->heap, stream_addr); + sof_ctx_free(alloc, stream_addr); #else rfree(stream_addr); #endif @@ -319,10 +306,7 @@ struct comp_buffer *buffer_alloc_range(struct mod_alloc_ctx *alloc, size_t prefe for (size = preferred_size; size >= minimum_size; size -= minimum_size) { #ifdef CONFIG_SOF_USERSPACE_LL assert(alloc); - if (alloc->vreg) - stream_addr = vregion_alloc_align(alloc->vreg, VREGION_MEM_TYPE_INTERIM, size, align); - else - stream_addr = sof_heap_alloc(alloc->heap, flags, size, align); + stream_addr = sof_ctx_alloc(alloc, flags, size, align); #else stream_addr = rballoc_align(flags, size, align); #endif @@ -343,10 +327,7 @@ struct comp_buffer *buffer_alloc_range(struct mod_alloc_ctx *alloc, size_t prefe tr_err(&buffer_tr, "could not alloc buffer structure"); #ifdef CONFIG_SOF_USERSPACE_LL assert(alloc); - if (alloc->vreg) - vregion_free(alloc->vreg, stream_addr); - else - sof_heap_free(alloc->heap, stream_addr); + sof_ctx_free(alloc, stream_addr); #else rfree(stream_addr); #endif @@ -387,10 +368,7 @@ int buffer_set_size(struct comp_buffer *buffer, uint32_t size, uint32_t alignmen #ifdef CONFIG_SOF_USERSPACE_LL assert(alloc); - if (alloc->vreg) - new_ptr = vregion_alloc_align(alloc->vreg, VREGION_MEM_TYPE_INTERIM, size, alignment); - else - new_ptr = sof_heap_alloc(alloc->heap, buffer->flags, size, alignment); + new_ptr = sof_ctx_alloc(alloc, buffer->flags, size, alignment); #else new_ptr = rballoc_align(buffer->flags, size, alignment); #endif @@ -406,10 +384,7 @@ int buffer_set_size(struct comp_buffer *buffer, uint32_t size, uint32_t alignmen if (new_ptr) { #ifdef CONFIG_SOF_USERSPACE_LL assert(alloc); - if (alloc->vreg) - vregion_free(alloc->vreg, audio_stream_get_addr(&buffer->stream)); - else - sof_heap_free(alloc->heap, audio_stream_get_addr(&buffer->stream)); + sof_ctx_free(alloc, audio_stream_get_addr(&buffer->stream)); #else rfree(audio_stream_get_addr(&buffer->stream)); #endif @@ -451,10 +426,7 @@ int buffer_set_size_range(struct comp_buffer *buffer, size_t preferred_size, siz new_size -= minimum_size) { #ifdef CONFIG_SOF_USERSPACE_LL assert(alloc); - if (alloc->vreg) - new_ptr = vregion_alloc_align(alloc->vreg, VREGION_MEM_TYPE_INTERIM, new_size, alignment); - else - new_ptr = sof_heap_alloc(alloc->heap, buffer->flags, new_size, alignment); + new_ptr = sof_ctx_alloc(alloc, buffer->flags, new_size, alignment); #else new_ptr = rballoc_align(buffer->flags, new_size, alignment); #endif @@ -473,10 +445,7 @@ int buffer_set_size_range(struct comp_buffer *buffer, size_t preferred_size, siz if (new_ptr) { #ifdef CONFIG_SOF_USERSPACE_LL assert(alloc); - if (alloc->vreg) - vregion_free(alloc->vreg, audio_stream_get_addr(&buffer->stream)); - else - sof_heap_free(alloc->heap, audio_stream_get_addr(&buffer->stream)); + sof_ctx_free(alloc, audio_stream_get_addr(&buffer->stream)); #else rfree(audio_stream_get_addr(&buffer->stream)); #endif diff --git a/src/audio/buffers/ring_buffer.c b/src/audio/buffers/ring_buffer.c index fe67027df8db..62ac121acda4 100644 --- a/src/audio/buffers/ring_buffer.c +++ b/src/audio/buffers/ring_buffer.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -99,13 +100,8 @@ static void ring_buffer_free(struct sof_audio_buffer *audio_buffer) struct ring_buffer, audio_buffer); struct mod_alloc_ctx *alloc = audio_buffer->alloc; - if (alloc->vreg) { - vregion_free(alloc->vreg, (__sparse_force void *)ring_buffer->_data_buffer); - vregion_free(alloc->vreg, ring_buffer); - } else { - sof_heap_free(alloc->heap, (__sparse_force void *)ring_buffer->_data_buffer); - sof_heap_free(alloc->heap, ring_buffer); - } + sof_ctx_free(alloc, (__sparse_force void *)ring_buffer->_data_buffer); + sof_ctx_free(alloc, ring_buffer); } static void ring_buffer_reset(struct sof_audio_buffer *audio_buffer) @@ -296,17 +292,11 @@ struct ring_buffer *ring_buffer_create(struct comp_dev *dev, size_t min_availabl struct ring_buffer *ring_buffer; struct mod_alloc_ctx *alloc = dev->mod->priv.resources.alloc; struct k_heap *heap = alloc->heap; - struct vregion *vreg = alloc->vreg; int memory_flags = (is_shared ? SOF_MEM_FLAG_COHERENT : 0) | user_get_buffer_memory_region(dev->drv); /* allocate ring_buffer structure */ - if (!vreg) - ring_buffer = sof_heap_alloc(heap, memory_flags, sizeof(*ring_buffer), 0); - else if (is_shared) - ring_buffer = vregion_alloc_coherent(vreg, VREGION_MEM_TYPE_INTERIM, sizeof(*ring_buffer)); - else - ring_buffer = vregion_alloc(vreg, VREGION_MEM_TYPE_INTERIM, sizeof(*ring_buffer)); + ring_buffer = sof_ctx_alloc(alloc, memory_flags, sizeof(*ring_buffer), 0); if (!ring_buffer) return NULL; @@ -382,12 +372,8 @@ struct ring_buffer *ring_buffer_create(struct comp_dev *dev, size_t min_availabl void *data_buf; - if (vreg) - data_buf = vregion_alloc_align(vreg, VREGION_MEM_TYPE_INTERIM, ring_buffer->data_buffer_size, - PLATFORM_DCACHE_ALIGN); - else - data_buf = sof_heap_alloc(heap, user_get_buffer_memory_region(dev->drv), - ring_buffer->data_buffer_size, PLATFORM_DCACHE_ALIGN); + data_buf = sof_ctx_alloc(alloc, user_get_buffer_memory_region(dev->drv), + ring_buffer->data_buffer_size, PLATFORM_DCACHE_ALIGN); if (!data_buf) goto err; @@ -402,9 +388,6 @@ struct ring_buffer *ring_buffer_create(struct comp_dev *dev, size_t min_availabl return ring_buffer; err: tr_err(&ring_buffer_tr, "Ring buffer creation failure"); - if (vreg) - vregion_free(vreg, ring_buffer); - else - sof_heap_free(heap, ring_buffer); + sof_ctx_free(alloc, ring_buffer); return NULL; } diff --git a/src/audio/module_adapter/module/generic.c b/src/audio/module_adapter/module/generic.c index 2989bceb160b..f9531033dadb 100644 --- a/src/audio/module_adapter/module/generic.c +++ b/src/audio/module_adapter/module/generic.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #if CONFIG_IPC_MAJOR_4 #include @@ -252,16 +253,7 @@ void *z_impl_mod_alloc_ext(struct processing_module *mod, uint32_t flags, size_t } /* Allocate memory for module */ - void *ptr; - - if (!res->alloc->vreg) - ptr = sof_heap_alloc(res->alloc->heap, flags, size, alignment); - else if (flags & SOF_MEM_FLAG_COHERENT) - ptr = vregion_alloc_coherent_align(res->alloc->vreg, VREGION_MEM_TYPE_INTERIM, - size, alignment); - else - ptr = vregion_alloc_align(res->alloc->vreg, VREGION_MEM_TYPE_INTERIM, - size, alignment); + void *ptr = sof_ctx_alloc(res->alloc, flags, size, alignment); if (!ptr) { comp_err(mod->dev, "Failed to alloc %zu bytes %zu alignment for comp %#x.", @@ -362,10 +354,7 @@ static int free_contents(struct processing_module *mod, struct module_resource * switch (container->type) { case MOD_RES_HEAP: - if (res->alloc->vreg) - vregion_free(res->alloc->vreg, container->ptr); - else - sof_heap_free(res->alloc->heap, container->ptr); + sof_ctx_free(res->alloc, container->ptr); res->heap_usage -= container->size; return 0; #if CONFIG_COMP_BLOB diff --git a/src/include/sof/ctx_alloc.h b/src/include/sof/ctx_alloc.h new file mode 100644 index 000000000000..b292a73975a1 --- /dev/null +++ b/src/include/sof/ctx_alloc.h @@ -0,0 +1,77 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright(c) 2026 Intel Corporation. All rights reserved. + */ + +#ifndef __SOF_CTX_ALLOC_H__ +#define __SOF_CTX_ALLOC_H__ + +#include +#include +#include +#include +#include +#include + +/** + * Allocate memory from a mod_alloc_ctx context. + * + * When the context has a vregion, allocates from the vregion interim + * partition. Coherent memory is used when SOF_MEM_FLAG_COHERENT is set + * in flags. Falls back to sof_heap_alloc() otherwise. + * + * @param ctx Allocation context (heap + optional vregion). + * @param flags Allocation flags (SOF_MEM_FLAG_*). + * @param size Size in bytes. + * @param alignment Required alignment in bytes. + * @return Pointer to allocated memory or NULL on failure. + */ +static inline void *sof_ctx_alloc(struct mod_alloc_ctx *ctx, uint32_t flags, + size_t size, size_t alignment) +{ + if (!ctx || !ctx->vreg) + return sof_heap_alloc(ctx ? ctx->heap : NULL, flags, size, alignment); + + if (flags & SOF_MEM_FLAG_COHERENT) + return vregion_alloc_coherent_align(ctx->vreg, VREGION_MEM_TYPE_INTERIM, + size, alignment); + + return vregion_alloc_align(ctx->vreg, VREGION_MEM_TYPE_INTERIM, size, alignment); +} + +/** + * Allocate zero-initialized memory from a mod_alloc_ctx context. + * @param ctx Allocation context. + * @param flags Allocation flags (SOF_MEM_FLAG_*). + * @param size Size in bytes. + * @param alignment Required alignment in bytes. + * @return Pointer to allocated memory or NULL on failure. + */ +static inline void *sof_ctx_zalloc(struct mod_alloc_ctx *ctx, uint32_t flags, + size_t size, size_t alignment) +{ + void *ptr = sof_ctx_alloc(ctx, flags, size, alignment); + + if (ptr) + memset(ptr, 0, size); + + return ptr; +} + +/** + * Free memory allocated from a mod_alloc_ctx context. + * @param ctx Allocation context. + * @param ptr Pointer to free. + */ +static inline void sof_ctx_free(struct mod_alloc_ctx *ctx, void *ptr) +{ + if (!ptr) + return; + + if (ctx && ctx->vreg) + vregion_free(ctx->vreg, ptr); + else + sof_heap_free(ctx ? ctx->heap : NULL, ptr); +} + +#endif /* __SOF_CTX_ALLOC_H__ */