From 4833ba67d1c4d19a5a48c40d6624a3424127a78c Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Thu, 8 May 2025 18:07:52 +0100 Subject: [PATCH 1/7] kconfig: heap: Add Kconfig option to set heap size. Allow the developer to set the maximum heap size so that power savings can be made when full memory is not required for device use cases. Signed-off-by: Liam Girdwood --- src/platform/ace30/include/platform/lib/memory.h | 2 +- .../lunarlake/include/platform/lib/memory.h | 2 +- .../meteorlake/include/platform/lib/memory.h | 2 +- zephyr/Kconfig | 14 ++++++++++++++ 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/platform/ace30/include/platform/lib/memory.h b/src/platform/ace30/include/platform/lib/memory.h index 8fa1f1229a59..70888798e22a 100644 --- a/src/platform/ace30/include/platform/lib/memory.h +++ b/src/platform/ace30/include/platform/lib/memory.h @@ -54,7 +54,7 @@ /** * size of HPSRAM system heap */ -#define HEAPMEM_SIZE 0xD0000 +#define HEAPMEM_SIZE CONFIG_SOF_ZEPHYR_HEAP_SIZE #if CONFIG_COLD_STORE_EXECUTE_DRAM && \ (CONFIG_LLEXT_TYPE_ELF_RELOCATABLE || !defined(LL_EXTENSION_BUILD)) diff --git a/src/platform/lunarlake/include/platform/lib/memory.h b/src/platform/lunarlake/include/platform/lib/memory.h index b5bda0ede464..276c61d17d58 100644 --- a/src/platform/lunarlake/include/platform/lib/memory.h +++ b/src/platform/lunarlake/include/platform/lib/memory.h @@ -56,7 +56,7 @@ /** * size of HPSRAM system heap */ -#define HEAPMEM_SIZE 0xF0000 +#define HEAPMEM_SIZE CONFIG_SOF_ZEPHYR_HEAP_SIZE #if CONFIG_COLD_STORE_EXECUTE_DRAM && \ (CONFIG_LLEXT_TYPE_ELF_RELOCATABLE || !defined(LL_EXTENSION_BUILD)) diff --git a/src/platform/meteorlake/include/platform/lib/memory.h b/src/platform/meteorlake/include/platform/lib/memory.h index 76b60f98fabb..cd279b603c64 100644 --- a/src/platform/meteorlake/include/platform/lib/memory.h +++ b/src/platform/meteorlake/include/platform/lib/memory.h @@ -56,7 +56,7 @@ /** * size of HPSRAM system heap */ -#define HEAPMEM_SIZE 0xF0000 +#define HEAPMEM_SIZE CONFIG_SOF_ZEPHYR_HEAP_SIZE #if CONFIG_COLD_STORE_EXECUTE_DRAM && \ (CONFIG_LLEXT_TYPE_ELF_RELOCATABLE || !defined(LL_EXTENSION_BUILD)) diff --git a/zephyr/Kconfig b/zephyr/Kconfig index 983c0f13c71c..43a0115495c0 100644 --- a/zephyr/Kconfig +++ b/zephyr/Kconfig @@ -19,6 +19,20 @@ config SOF_ZEPHYR_HEAP_CACHED Enable cached heap by mapping cached SOF memory zones to different Zephyr sys_heap objects and enable caching for non-shared zones. +config SOF_ZEPHYR_HEAP_SIZE + hex "Size of the Zephyr heap for SOF" + default 0xF0000 if SOC_INTEL_ACE15_MTPM || SOC_INTEL_ACE20_LNL + default 0xD0000 if SOC_INTEL_ACE30 + default 0x0 + help + Support scaling of the heap size for different platforms and different types + of heaps. This is the default heap size for most users. + TODO: Currently this setting is only available on platforms with a + simplified heap size configuration. i.e. a single macro that defines the + heap size. This is not the case for all platforms. + NOTE: Keep in mind that the heap size should not be greater than the physical + memory size of the system defined in DT (and this includes baseFW text/data). + config ZEPHYR_NATIVE_DRIVERS bool "Use Zephyr native drivers" default n From ac9c5c0253a55d4e297010ddb4e85aa355b65a65 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 9 May 2025 16:42:22 +0100 Subject: [PATCH 2/7] kconfig: heap: Add Kconfig option to set virtual memory heap size. Allow the developer to set the maximum virtual heap size so that power savings can be made when full memory is not required for device use cases. Signed-off-by: Liam Girdwood --- zephyr/Kconfig | 9 +++++++++ zephyr/lib/alloc.c | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/zephyr/Kconfig b/zephyr/Kconfig index 43a0115495c0..ce0765c6d256 100644 --- a/zephyr/Kconfig +++ b/zephyr/Kconfig @@ -33,6 +33,15 @@ config SOF_ZEPHYR_HEAP_SIZE NOTE: Keep in mind that the heap size should not be greater than the physical memory size of the system defined in DT (and this includes baseFW text/data). +config SOF_ZEPHYR_VIRTUAL_HEAP_SIZE + hex "Size of the Zephyr virtual heap for SOF" + depends on VIRTUAL_HEAP + default 0x40000 + help + Support scaling of the virtual address heap size for different platforms. + NOTE: Keep in mind that the heap size should not be greater than the physical + memory size of the system defined in DT (and this includes baseFW text/data). + config ZEPHYR_NATIVE_DRIVERS bool "Use Zephyr native drivers" default n diff --git a/zephyr/lib/alloc.c b/zephyr/lib/alloc.c index b9d970e34b8c..fc27adbf8a4b 100644 --- a/zephyr/lib/alloc.c +++ b/zephyr/lib/alloc.c @@ -18,6 +18,7 @@ #include #include #include + #if CONFIG_VIRTUAL_HEAP #include @@ -27,7 +28,7 @@ struct vmh_heap *virtual_buffers_heap; #undef HEAPMEM_SIZE /* Buffers are allocated from virtual space so we can safely reduce the heap size. */ -#define HEAPMEM_SIZE 0x40000 +#define HEAPMEM_SIZE CONFIG_SOF_ZEPHYR_VIRTUAL_HEAP_SIZE #endif /* CONFIG_VIRTUAL_HEAP */ From fb8bf11aa0c66a78ef8fd2d46b0e646c814d9a6f Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 9 May 2025 17:02:35 +0100 Subject: [PATCH 3/7] userspace: Add a Kconfig to be used for SOF userspace processing Add a Kconfig to be used for SOF userspace support. This will be a staging feature at the moment until its stable upstream. Signed-off-by: Liam Girdwood --- zephyr/Kconfig | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/zephyr/Kconfig b/zephyr/Kconfig index ce0765c6d256..d29ad00ad180 100644 --- a/zephyr/Kconfig +++ b/zephyr/Kconfig @@ -11,6 +11,14 @@ config SOF_STAGING rsource "../Kconfig.sof" +config SOF_USERSPACE + bool "Enable SOF support for userspace modules" + default n + help + SOF userspace modules support will enable modules to run in DP + processing mode as userspace code and data. This feature is WIP + and is not yet ready for production, for developers only. + config SOF_ZEPHYR_HEAP_CACHED bool "Cached Zephyr heap for SOF memory non-shared zones" default y if CAVS || ACE From 3fb80e0f3d9774877598190b43b38eeacd90a84d Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Sat, 12 Jul 2025 15:31:55 +0100 Subject: [PATCH 4/7] buffer: logging: use correct term for memory flags. Fix the log messages to show flags instead of type. Signed-off-by: Liam Girdwood --- src/audio/buffers/comp_buffer.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/audio/buffers/comp_buffer.c b/src/audio/buffers/comp_buffer.c index 8ed862ac8e29..454f5f0a49e3 100644 --- a/src/audio/buffers/comp_buffer.c +++ b/src/audio/buffers/comp_buffer.c @@ -330,7 +330,7 @@ int buffer_set_size(struct comp_buffer *buffer, uint32_t size, uint32_t alignmen audio_stream_get_size(&buffer->stream), alignment); /* we couldn't allocate bigger chunk */ if (!new_ptr && size > audio_stream_get_size(&buffer->stream)) { - buf_err(buffer, "resize can't alloc %u bytes type 0x%x", + buf_err(buffer, "resize can't alloc %u bytes of flags 0x%x", audio_stream_get_size(&buffer->stream), buffer->flags); return -ENOMEM; } @@ -388,7 +388,8 @@ int buffer_set_size_range(struct comp_buffer *buffer, size_t preferred_size, siz /* we couldn't allocate bigger chunk */ if (!new_ptr && new_size > actual_size) { - buf_err(buffer, "resize can't alloc %zu bytes type 0x%x", new_size, buffer->flags); + buf_err(buffer, "resize can't alloc %zu bytes of flags 0x%x", new_size, + buffer->flags); return -ENOMEM; } From d813dcc04acbb9cb4296405bf744c2481ff47444 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Sat, 12 Jul 2025 15:34:38 +0100 Subject: [PATCH 5/7] rtc: mock: clean up memory failure handling in Setup() Currently the caller checks for allocation failure in the callee. Make the correct function check for allocation failure. Signed-off-by: Liam Girdwood --- src/audio/google/google_rtc_audio_processing_mock.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/audio/google/google_rtc_audio_processing_mock.c b/src/audio/google/google_rtc_audio_processing_mock.c index 77b390452205..b931c3a85c7a 100644 --- a/src/audio/google/google_rtc_audio_processing_mock.c +++ b/src/audio/google/google_rtc_audio_processing_mock.c @@ -27,7 +27,7 @@ struct GoogleRtcAudioProcessingState { float *aec_reference; }; -static void SetFormats(GoogleRtcAudioProcessingState *const state, +static int SetFormats(GoogleRtcAudioProcessingState *const state, int capture_sample_rate_hz, int num_capture_input_channels, int num_capture_output_channels, @@ -45,6 +45,9 @@ static void SetFormats(GoogleRtcAudioProcessingState *const state, sizeof(state->aec_reference[0]) * state->num_frames * state->num_aec_reference_channels); + if (!state->aec_reference) + return -ENOMEM; + return 0; } void GoogleRtcAudioProcessingAttachMemoryBuffer(uint8_t *const buffer, @@ -64,20 +67,21 @@ GoogleRtcAudioProcessingState *GoogleRtcAudioProcessingCreateWithConfig(int capt const uint8_t *const config, int config_size) { + int err; struct GoogleRtcAudioProcessingState *s = rballoc(SOF_MEM_FLAG_USER, sizeof(GoogleRtcAudioProcessingState)); if (!s) return NULL; s->aec_reference = NULL; - SetFormats(s, + err = SetFormats(s, capture_sample_rate_hz, num_capture_input_channels, num_capture_output_channels, render_sample_rate_hz, num_render_channels); - if (!s->aec_reference) { + if (err) { rfree(s); return NULL; } From 245ab9e614960a3f95680d44c3d0fd91c814328f Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Sat, 12 Jul 2025 16:07:36 +0100 Subject: [PATCH 6/7] alloc: update comments and log string to align with new API Zone was mentioned in many comments and one log message. Fix this to remove zone references. Signed-off-by: Liam Girdwood --- posix/include/rtos/alloc.h | 17 ++--------------- src/include/sof/audio/component.h | 2 -- src/include/sof/lib/dai-legacy.h | 2 +- zephyr/include/rtos/alloc.h | 18 +++--------------- zephyr/lib/alloc.c | 10 ++-------- 5 files changed, 8 insertions(+), 41 deletions(-) diff --git a/posix/include/rtos/alloc.h b/posix/include/rtos/alloc.h index ad82b74d6ce7..294b27eaee9b 100644 --- a/posix/include/rtos/alloc.h +++ b/posix/include/rtos/alloc.h @@ -51,29 +51,20 @@ /** * Allocates memory block. - * @param zone Zone to allocate memory from, see enum mem_zone. * @param flags Flags, see SOF_MEM_FLAG_... - * @param caps Capabilities, see SOF_MEM_CAPS_... * @param bytes Size in bytes. * @return Pointer to the allocated memory or NULL if failed. - * - * @note Do not use for buffers (SOF_MEM_ZONE_BUFFER zone). - * Use rballoc(), rballoc_align() to allocate memory for buffers. */ void *rmalloc(uint32_t flags, size_t bytes); /** * Similar to rmalloc(), guarantees that returned block is zeroed. - * - * @note Do not use for buffers (SOF_MEM_ZONE_BUFFER zone). - * rballoc(), rballoc_align() to allocate memory for buffers. */ void *rzalloc(uint32_t flags, size_t bytes); /** - * Allocates memory block from SOF_MEM_ZONE_BUFFER. + * Allocates memory block. * @param flags Flags, see SOF_MEM_FLAG_... - * @param caps Capabilities, see SOF_MEM_CAPS_... * @param bytes Size in bytes. * @param alignment Alignment in bytes. * @return Pointer to the allocated memory or NULL if failed. @@ -90,10 +81,9 @@ static inline void *rballoc(uint32_t flags, size_t bytes) } /** - * Changes size of the memory block allocated from SOF_MEM_ZONE_BUFFER. + * Changes size of the memory block allocated. * @param ptr Address of the block to resize. * @param flags Flags, see SOF_MEM_FLAG_... - * @param caps Capabilities, see SOF_MEM_CAPS_... * @param bytes New size in bytes. * @param old_bytes Old size in bytes. * @param alignment Alignment in bytes. @@ -116,9 +106,6 @@ static inline void *rbrealloc(void *ptr, uint32_t flags, /** * Frees the memory block. * @param ptr Pointer to the memory block. - * - * @note Blocks from SOF_MEM_ZONE_SYS cannot be freed, such a call causes - * panic. */ void rfree(void *ptr); diff --git a/src/include/sof/audio/component.h b/src/include/sof/audio/component.h index a768dd93a3a6..ef2c0fac108a 100644 --- a/src/include/sof/audio/component.h +++ b/src/include/sof/audio/component.h @@ -321,8 +321,6 @@ struct comp_ops { * @param spec Pointer to initialization data * @return Pointer to the new component device. * - * All required data objects should be allocated from the run-time - * heap (SOF_MEM_ZONE_RUNTIME). * Any component-specific private data is allocated separately and * pointer is connected to the comp_dev::private by using * comp_set_drvdata() and later retrieved by comp_get_drvdata(). diff --git a/src/include/sof/lib/dai-legacy.h b/src/include/sof/lib/dai-legacy.h index 9408b6c27414..44142412334b 100644 --- a/src/include/sof/lib/dai-legacy.h +++ b/src/include/sof/lib/dai-legacy.h @@ -76,7 +76,7 @@ struct sof_ipc_stream_params; * If a single DAI instance can have multiple DMA links and/or there is * some other possibility of the same instance being used in multiple * contexts at the same time, the private data should be allocated in the - * SOF_MEM_ZONE_SHARED. + * SOF_MEM_FLAG_COHERENT. */ struct dai_ops { int (*set_config)(struct dai *dai, struct ipc_config_dai *config, diff --git a/zephyr/include/rtos/alloc.h b/zephyr/include/rtos/alloc.h index 6166ae6de449..600852418693 100644 --- a/zephyr/include/rtos/alloc.h +++ b/zephyr/include/rtos/alloc.h @@ -42,29 +42,21 @@ /** * Allocates memory block. - * @param zone Zone to allocate memory from, see enum mem_zone. - * @param flags Flags, see SOF_MEM_FLAG_... - * @param caps Capabilities, see SOF_MEM_CAPS_... + * @param flags Flags, see SOF_MEM_FLAG_.... * @param bytes Size in bytes. * @return Pointer to the allocated memory or NULL if failed. * - * @note Do not use for buffers (SOF_MEM_ZONE_BUFFER zone). - * Use rballoc(), rballoc_align() to allocate memory for buffers. */ void *rmalloc(uint32_t flags, size_t bytes); /** * Similar to rmalloc(), guarantees that returned block is zeroed. - * - * @note Do not use for buffers (SOF_MEM_ZONE_BUFFER zone). - * rballoc(), rballoc_align() to allocate memory for buffers. */ void *rzalloc(uint32_t flags, size_t bytes); /** - * Allocates memory block from SOF_MEM_ZONE_BUFFER. + * Allocates memory block. * @param flags Flags, see SOF_MEM_FLAG_... - * @param caps Capabilities, see SOF_MEM_CAPS_... * @param bytes Size in bytes. * @param alignment Alignment in bytes. * @return Pointer to the allocated memory or NULL if failed. @@ -81,10 +73,9 @@ static inline void *rballoc(uint32_t flags, size_t bytes) } /** - * Changes size of the memory block allocated from SOF_MEM_ZONE_BUFFER. + * Changes size of the memory block allocated. * @param ptr Address of the block to resize. * @param flags Flags, see SOF_MEM_FLAG_... - * @param caps Capabilities, see SOF_MEM_CAPS_... * @param bytes New size in bytes. * @param old_bytes Old size in bytes. * @param alignment Alignment in bytes. @@ -107,9 +98,6 @@ static inline void *rbrealloc(void *ptr, uint32_t flags, /** * Frees the memory block. * @param ptr Pointer to the memory block. - * - * @note Blocks from SOF_MEM_ZONE_SYS cannot be freed, such a call causes - * panic. */ void rfree(void *ptr); diff --git a/zephyr/lib/alloc.c b/zephyr/lib/alloc.c index fc27adbf8a4b..77c5aaeccf3e 100644 --- a/zephyr/lib/alloc.c +++ b/zephyr/lib/alloc.c @@ -385,7 +385,7 @@ void *rmalloc(uint32_t flags, size_t bytes) heap = &l3_heap; /* Uncached L3_HEAP should not be used */ if (flags & SOF_MEM_FLAG_COHERENT) { - tr_err(&zephyr_tr, "L3_HEAP available for cached zones only!"); + tr_err(&zephyr_tr, "L3_HEAP available for cached addresses only!"); return NULL; } ptr = (__sparse_force void *)l3_heap_alloc_aligned(heap, 0, bytes); @@ -412,14 +412,12 @@ void *rmalloc(uint32_t flags, size_t bytes) } EXPORT_SYMBOL(rmalloc); -/* Use SOF_MEM_ZONE_BUFFER at the moment */ void *rbrealloc_align(void *ptr, uint32_t flags, size_t bytes, size_t old_bytes, uint32_t alignment) { void *new_ptr; if (!ptr) { - /* TODO: Use correct zone */ return rballoc_align(flags, bytes, alignment); } @@ -446,9 +444,6 @@ void *rbrealloc_align(void *ptr, uint32_t flags, size_t bytes, /** * Similar to rmalloc(), guarantees that returned block is zeroed. - * - * @note Do not use for buffers (SOF_MEM_ZONE_BUFFER zone). - * rballoc(), rballoc_align() to allocate memory for buffers. */ void *rzalloc(uint32_t flags, size_t bytes) { @@ -462,9 +457,8 @@ void *rzalloc(uint32_t flags, size_t bytes) EXPORT_SYMBOL(rzalloc); /** - * Allocates memory block from SOF_MEM_ZONE_BUFFER. + * Allocates memory block. * @param flags see SOF_MEM_FLAG_... - * @param caps Capabilities, see SOF_MEM_CAPS_... * @param bytes Size in bytes. * @param align Alignment in bytes. * @return Pointer to the allocated memory or NULL if failed. From 67b8ba21e629b3e0792accdb3d9a959cf9e97678 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Sat, 12 Jul 2025 16:09:07 +0100 Subject: [PATCH 7/7] alloc: fix last two users of SOF_MEM_ZONE_ Trace and probes were using ZONE macro instead of flags. Fix. Signed-off-by: Liam Girdwood --- src/probe/probe.c | 2 +- src/trace/dma-trace.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/probe/probe.c b/src/probe/probe.c index 5c75eae8210b..6421d4b473c3 100644 --- a/src/probe/probe.c +++ b/src/probe/probe.c @@ -176,7 +176,7 @@ static int probe_dma_init(struct probe_dma_ext *dma, uint32_t direction) dma->config.dest_width = sizeof(uint32_t); dma->config.cyclic = 0; - err = dma_sg_alloc(&dma->config.elem_array, SOF_MEM_ZONE_RUNTIME, + err = dma_sg_alloc(&dma->config.elem_array, SOF_MEM_FLAG_USER, dma->config.direction, elem_num, elem_size, elem_addr, 0); if (err < 0) return err; diff --git a/src/trace/dma-trace.c b/src/trace/dma-trace.c index 82f3cbd1e46f..f30007a89b6b 100644 --- a/src/trace/dma-trace.c +++ b/src/trace/dma-trace.c @@ -324,7 +324,7 @@ static int dma_trace_buffer_init(struct dma_trace_data *d) config->dest_width = sizeof(uint32_t); config->cyclic = 0; - ret = dma_sg_alloc(&config->elem_array, SOF_MEM_ZONE_SYS, + ret = dma_sg_alloc(&config->elem_array, SOF_MEM_FLAG_USER, config->direction, elem_num, elem_size, elem_addr, 0); if (ret < 0) {