From 3dda59aa10c8e5a458bfa6bfc1d29f17ebb3c611 Mon Sep 17 00:00:00 2001 From: Jyri Sarha Date: Wed, 3 Jun 2026 17:56:45 +0300 Subject: [PATCH 1/4] vregion: remove need to include rtos/alloc.h in !CONFIG_SOF_VREGIONS build Remove need to include rtos/alloc.h in !CONFIG_SOF_VREGIONS build. We do not need the dummy vregion objects for anything, so get rid of them. From now on creating a vregion object when CONFIG_SOF_VREGIONS is not defined will fail. Signed-off-by: Jyri Sarha --- src/include/sof/lib/vregion.h | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/include/sof/lib/vregion.h b/src/include/sof/lib/vregion.h index 612443f5bc48..d64249ed64c8 100644 --- a/src/include/sof/lib/vregion.h +++ b/src/include/sof/lib/vregion.h @@ -125,29 +125,20 @@ void vregion_mem_info(struct vregion *vr, size_t *size, uintptr_t *start); #else /* CONFIG_SOF_VREGIONS */ -#include - struct vregion { unsigned int use_count; }; static inline struct vregion *vregion_create(size_t lifetime_size, size_t interim_size) { - struct vregion *vr = rmalloc(0, sizeof(*vr)); - - vr->use_count = 1; - return vr; + return NULL; } static inline struct vregion *vregion_get(struct vregion *vr) { - if (vr) - vr->use_count++; return vr; } static inline struct vregion *vregion_put(struct vregion *vr) { - if (vr && !--vr->use_count) - rfree(vr); return vr; } static inline void *vregion_alloc(struct vregion *vr, enum vregion_mem_type type, size_t size) From 4f71ecd61a7d7b95cf46669b8d9f24d53c913f91 Mon Sep 17 00:00:00 2001 From: Jyri Sarha Date: Mon, 1 Jun 2026 23:19:28 +0300 Subject: [PATCH 2/4] zephyr: lib: Remove ctx_alloc.h and put the contents into rtos/alloc.h Remove the ctx_alloc.h header and move the implementation into rtos/alloc.h. Signed-off-by: Jyri Sarha --- src/audio/buffers/comp_buffer.c | 1 - src/audio/buffers/ring_buffer.c | 1 - src/audio/module_adapter/module/generic.c | 1 - src/include/sof/audio/component.h | 1 - src/include/sof/ctx_alloc.h | 81 ----------------------- src/include/sof/lib/dai-zephyr.h | 1 - zephyr/include/rtos/alloc.h | 68 +++++++++++++++++++ 7 files changed, 68 insertions(+), 86 deletions(-) delete mode 100644 src/include/sof/ctx_alloc.h diff --git a/src/audio/buffers/comp_buffer.c b/src/audio/buffers/comp_buffer.c index 502103693689..862189d3626d 100644 --- a/src/audio/buffers/comp_buffer.c +++ b/src/audio/buffers/comp_buffer.c @@ -17,7 +17,6 @@ #include #include #include -#include #include #include #include diff --git a/src/audio/buffers/ring_buffer.c b/src/audio/buffers/ring_buffer.c index 62ac121acda4..51245e91ca40 100644 --- a/src/audio/buffers/ring_buffer.c +++ b/src/audio/buffers/ring_buffer.c @@ -7,7 +7,6 @@ #include #include #include -#include #include #include diff --git a/src/audio/module_adapter/module/generic.c b/src/audio/module_adapter/module/generic.c index f9531033dadb..b62ae38f415f 100644 --- a/src/audio/module_adapter/module/generic.c +++ b/src/audio/module_adapter/module/generic.c @@ -18,7 +18,6 @@ #include #include #include -#include #include #if CONFIG_IPC_MAJOR_4 #include diff --git a/src/include/sof/audio/component.h b/src/include/sof/audio/component.h index ebb88c8f79a1..13f2524ac909 100644 --- a/src/include/sof/audio/component.h +++ b/src/include/sof/audio/component.h @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include diff --git a/src/include/sof/ctx_alloc.h b/src/include/sof/ctx_alloc.h deleted file mode 100644 index 389c16a27507..000000000000 --- a/src/include/sof/ctx_alloc.h +++ /dev/null @@ -1,81 +0,0 @@ -/* 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 - -struct mod_alloc_ctx { - struct k_heap *heap; - struct vregion *vreg; -}; - -/** - * 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__ */ diff --git a/src/include/sof/lib/dai-zephyr.h b/src/include/sof/lib/dai-zephyr.h index 595d11de9b47..3e40c6e682af 100644 --- a/src/include/sof/lib/dai-zephyr.h +++ b/src/include/sof/lib/dai-zephyr.h @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include diff --git a/zephyr/include/rtos/alloc.h b/zephyr/include/rtos/alloc.h index e21e498f0471..cdd9b9f4064e 100644 --- a/zephyr/include/rtos/alloc.h +++ b/zephyr/include/rtos/alloc.h @@ -150,4 +150,72 @@ size_t get_shared_buffer_heap_size(void); #endif +#include + +struct mod_alloc_ctx { + struct k_heap *heap; + struct vregion *vreg; +}; + +/** + * 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 /* __ZEPHYR_RTOS_ALLOC_H__ */ From ab3367939d037dd7c30c5f5295b927e5dc8d0acb Mon Sep 17 00:00:00 2001 From: Jyri Sarha Date: Wed, 3 Jun 2026 18:57:12 +0300 Subject: [PATCH 3/4] posix: alloc.h: Add dummy mod_alloc_ctx and sof_ctx_alloc() support Signed-off-by: Jyri Sarha --- posix/include/rtos/alloc.h | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/posix/include/rtos/alloc.h b/posix/include/rtos/alloc.h index 84751ef9f1f4..c599afe08d82 100644 --- a/posix/include/rtos/alloc.h +++ b/posix/include/rtos/alloc.h @@ -120,6 +120,56 @@ void sof_heap_free(struct k_heap *heap, void *addr); struct k_heap *sof_sys_heap_get(void); struct k_heap *sof_sys_user_heap_get(void); +/* Posix version of struct mod_alloc_ctx without vregion support */ +struct vregion; + +struct mod_alloc_ctx { + struct k_heap *heap; + struct vregion *vreg; +}; + +/** + * Allocate memory from a mod_alloc_ctx context. + * Dummy version, only heap allocation is supported + */ +static inline void *sof_ctx_alloc(struct mod_alloc_ctx *ctx, uint32_t flags, + size_t size, size_t alignment) +{ + return sof_heap_alloc(ctx ? ctx->heap : NULL, flags, 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; + + sof_heap_free(ctx ? ctx->heap : NULL, ptr); +} + /** * Calculates length of the null-terminated string. * @param s String. From c00d82ce567edb48bd3bb4071a05e9e537781055 Mon Sep 17 00:00:00 2001 From: Jyri Sarha Date: Wed, 3 Jun 2026 13:33:15 +0300 Subject: [PATCH 4/4] ipc4: Replace interim_heap_bytes and lifetime_heap_bytes with heap_bytes Simplify the IPC4 memory data structs by replacing separate interim_heap_bytes, lifetime_heap_bytes, and shared_bytes fields with a single heap_bytes field, matching the Linux driver side changes. Affects both ipc4_module_init_ext_obj_dp_data and ipc4_pipeline_ext_obj_mem_data structs, and their corresponding log messages. Signed-off-by: Jyri Sarha --- src/audio/module_adapter/module_adapter_ipc4.c | 5 ++--- src/include/ipc4/module.h | 4 +--- src/include/ipc4/pipeline.h | 4 +--- src/ipc/ipc4/helper.c | 5 ++--- 4 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/audio/module_adapter/module_adapter_ipc4.c b/src/audio/module_adapter/module_adapter_ipc4.c index 74d39559a004..a639755a31e2 100644 --- a/src/audio/module_adapter/module_adapter_ipc4.c +++ b/src/audio/module_adapter/module_adapter_ipc4.c @@ -81,10 +81,9 @@ int module_ext_init_decode(const struct comp_driver *drv, struct module_ext_init } ext_data->dp_data = dp_data; comp_cl_info(drv, - "init_ext_obj_dp_data domain %u stack %u interim %u lifetime %u shared %u", + "init_ext_obj_dp_data domain %u stack %u heap %u", dp_data->domain_id, dp_data->stack_bytes, - dp_data->interim_heap_bytes, dp_data->lifetime_heap_bytes, - dp_data->shared_bytes); + dp_data->heap_bytes); break; } case IPC4_MOD_INIT_DATA_ID_MODULE_DATA: diff --git a/src/include/ipc4/module.h b/src/include/ipc4/module.h index dfe0f02e628e..4e60ebc9bc13 100644 --- a/src/include/ipc4/module.h +++ b/src/include/ipc4/module.h @@ -93,9 +93,7 @@ struct ipc4_module_init_ext_object { struct ipc4_module_init_ext_obj_dp_data { uint32_t domain_id; /* userspace domain ID */ uint32_t stack_bytes; /* required stack size in bytes */ - uint32_t interim_heap_bytes; /* required interim heap size in bytes */ - uint32_t lifetime_heap_bytes; /* required lifetime heap size in bytes */ - uint32_t shared_bytes; /* required shared memory size in bytes */ + uint32_t heap_bytes; /* required heap size in bytes */ } __attribute__((packed, aligned(4))); /* diff --git a/src/include/ipc4/pipeline.h b/src/include/ipc4/pipeline.h index 198918cf8577..1e85072fd15c 100644 --- a/src/include/ipc4/pipeline.h +++ b/src/include/ipc4/pipeline.h @@ -88,9 +88,7 @@ struct ipc4_pipeline_ext_object { struct ipc4_pipeline_ext_obj_mem_data { uint32_t domain_id; /* userspace domain ID */ uint32_t stack_bytes; /* required stack size in bytes */ - uint32_t interim_heap_bytes; /* required interim heap size in bytes */ - uint32_t lifetime_heap_bytes; /* required lifetime heap size in bytes */ - uint32_t shared_bytes; /* required shared memory in bytes */ + uint32_t heap_bytes; /* required heap size in bytes */ } __packed __aligned(4); /* diff --git a/src/ipc/ipc4/helper.c b/src/ipc/ipc4/helper.c index 8e3073ab7797..88b181450521 100644 --- a/src/ipc/ipc4/helper.c +++ b/src/ipc/ipc4/helper.c @@ -317,10 +317,9 @@ __cold static int ipc4_create_pipeline_payload_decode(char *data, } pparams->mem_data = mem_data; tr_info(&ipc_tr, - "init_ext_obj_mem_data domain %u stack %u interim %u lifetime %u shared %u", + "init_ext_obj_mem_data domain %u stack %u heap %u", mem_data->domain_id, mem_data->stack_bytes, - mem_data->interim_heap_bytes, mem_data->lifetime_heap_bytes, - mem_data->shared_bytes); + mem_data->heap_bytes); break; } default: