From f87d643b3d0a9d207f2eb3c685cd59b7328cf231 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Thu, 8 May 2025 18:07:52 +0100 Subject: [PATCH 1/5] 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 dab9d94c062a..52d7fe121763 100644 --- a/src/platform/ace30/include/platform/lib/memory.h +++ b/src/platform/ace30/include/platform/lib/memory.h @@ -56,7 +56,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 64206eb5e60309cba859cb24f41c14202d5c0f97 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 9 May 2025 16:42:22 +0100 Subject: [PATCH 2/5] 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 | 10 ++++++++++ zephyr/lib/alloc.c | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/zephyr/Kconfig b/zephyr/Kconfig index 43a0115495c0..05f5d92d9805 100644 --- a/zephyr/Kconfig +++ b/zephyr/Kconfig @@ -33,6 +33,16 @@ 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 if SOC_INTEL_ACE30 + default 0x0 + 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 899730e312e0..546d125fa0f4 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 70ad318a3f6eb6b7739fb60fdaf15eaaf0eb44bf Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 9 May 2025 17:02:35 +0100 Subject: [PATCH 3/5] 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 05f5d92d9805..778360d7ff6e 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 yest 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 f8c93776b4fb5d9aa4d062791a47e77eaf0ac8b7 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Fri, 9 May 2025 17:06:14 +0100 Subject: [PATCH 4/5] userspace: add a userspace heap Add a userspace heap, memory type and associated helper APIs. Signed-off-by: Liam Girdwood --- src/include/ipc/topology.h | 1 + zephyr/Kconfig | 12 +++++++++ zephyr/include/rtos/alloc.h | 40 ++++++++++++++++++++++++++++++ zephyr/lib/alloc.c | 49 ++++++++++++++++++++++++++++++++++++- 4 files changed, 101 insertions(+), 1 deletion(-) diff --git a/src/include/ipc/topology.h b/src/include/ipc/topology.h index a83b806a3467..5e0f82f2bf50 100644 --- a/src/include/ipc/topology.h +++ b/src/include/ipc/topology.h @@ -89,6 +89,7 @@ struct sof_ipc_comp { #define SOF_MEM_CAPS_L3 BIT(8) /**< L3 memory */ /* Don't forget to update when adding a new bit to the ABI. */ #define SOF_MEM_CAPS_LOWEST_INVALID BIT(9) /**< Used for input validation */ +#define SOF_MEM_CAPS_MMU_SHD BIT(10) /**< MMU shared memory */ /* * overrun will cause ring buffer overwrite, instead of XRUN. diff --git a/zephyr/Kconfig b/zephyr/Kconfig index 778360d7ff6e..0627e9c67226 100644 --- a/zephyr/Kconfig +++ b/zephyr/Kconfig @@ -51,6 +51,18 @@ config SOF_ZEPHYR_VIRTUAL_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_USER_HEAP_SIZE + hex "Size of the Zephyr userspace heap for SOF" + depends on SOF_STAGING + depends on SOF_USERSPACE + default 0x20000 if SOC_INTEL_ACE30 + default 0x0 + help + Support scaling of the userspace 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 + alongside the default SOF heap size). + config ZEPHYR_NATIVE_DRIVERS bool "Use Zephyr native drivers" default n diff --git a/zephyr/include/rtos/alloc.h b/zephyr/include/rtos/alloc.h index 69228ea8f9c2..0d705dcd0dd6 100644 --- a/zephyr/include/rtos/alloc.h +++ b/zephyr/include/rtos/alloc.h @@ -139,6 +139,46 @@ void rfree(void *ptr); /* TODO: remove - debug only - only needed for linking */ static inline void heap_trace_all(int force) {} +#if CONFIG_SOF_USERSPACE + +/** + * Returns whether pointer is in userspace heap. + * @return True is userspace pointer. + */ +bool s_heap_user_is_pointer(void *ptr); + +/** + * Returns the start of user memory heap. + * @return Pointer to the user memory heap start address. + */ +uintptr_t s_heap_user_get_start(void); + +/** + * Returns the size of user memory heap. + * @return Size of the user memory region which can be used for user heap. + */ +static inline size_t s_get_heap_get_user_size(void) +{ + return ROUND_DOWN(CONFIG_SOF_ZEPHYR_USER_HEAP_SIZE, CONFIG_MMU_PAGE_SIZE); +} +#else + +static inline bool s_heap_user_is_pointer(void *ptr) +{ + return false; +} + +static inline uintptr_t s_heap_user_get_start(void) +{ + return 0; +} + +static inline size_t s_get_heap_get_user_size(void) +{ + return 0; +} +#endif + /** @}*/ #endif /* __ZEPHYR_RTOS_ALLOC_H__ */ diff --git a/zephyr/lib/alloc.c b/zephyr/lib/alloc.c index 546d125fa0f4..5d058635d7e1 100644 --- a/zephyr/lib/alloc.c +++ b/zephyr/lib/alloc.c @@ -98,7 +98,13 @@ static uint8_t __aligned(PLATFORM_DCACHE_ALIGN) heapmem[HEAPMEM_SIZE]; * to allow memory management driver to control unused * memory pages. */ -__section(".heap_mem") static uint8_t __aligned(PLATFORM_DCACHE_ALIGN) heapmem[HEAPMEM_SIZE]; + +__section(".heap_mem") static uint8_t __aligned(HOST_PAGE_SIZE) heapmem[HEAPMEM_SIZE]; + +#if CONFIG_SOF_USERSPACE +#define USER_HEAP_MEM_SIZE CONFIG_SOF_ZEPHYR_USER_HEAP_SIZE +__section(".heap_mem") static uint8_t __aligned(HOST_PAGE_SIZE) user_heapmem[USER_HEAP_MEM_SIZE]; +#endif #elif defined(CONFIG_ARCH_POSIX) @@ -125,6 +131,35 @@ extern char _end[], _heap_sentry[]; static struct k_heap sof_heap; +#if CONFIG_SOF_USERSPACE +static struct k_heap sof_user_heap; + +/** + * Returns the start of user memory heap. + * @return Pointer to the user memory heap start address. + */ +bool s_heap_user_is_pointer(void *ptr) +{ + if (is_cached(ptr)) + ptr = sys_cache_uncached_ptr_get((__sparse_force void __sparse_cache *)ptr); + + if ((POINTER_TO_UINT(ptr) >= (uintptr_t)&user_heapmem[0]) && + (POINTER_TO_UINT(ptr) < (uintptr_t)&user_heapmem[CONFIG_SOF_ZEPHYR_USER_HEAP_SIZE])) + return true; + + return false; +} + +/** + * Returns the start of user memory heap. + * @return Pointer to the user memory heap start address. + */ +uintptr_t s_heap_user_get_start(void) +{ + return (uintptr_t)ROUND_UP(&user_heapmem[0], CONFIG_MMU_PAGE_SIZE); +} +#endif + #if CONFIG_L3_HEAP static struct k_heap l3_heap; @@ -401,6 +436,10 @@ void *rmalloc(enum mem_zone zone, uint32_t flags, uint32_t caps, size_t bytes) return ptr; #else k_panic(); +#endif +#if CONFIG_USERSPACE + } else if (caps & SOF_MEM_CAPS_MMU_SHD) { + heap = &sof_user_heap; #endif } else { heap = &sof_heap; @@ -504,6 +543,11 @@ void *rballoc_align(uint32_t flags, uint32_t caps, size_t bytes, return virtual_heap_alloc(virtual_buffers_heap, flags, caps, bytes, align); #endif /* CONFIG_VIRTUAL_HEAP */ +#if CONFIG_SOF_USERSPACE + if (caps & SOF_MEM_CAPS_MMU_SHD) + heap = &sof_user_heap; +#endif + if (flags & SOF_MEM_FLAG_COHERENT) return heap_alloc_aligned(heap, align, bytes); @@ -541,6 +585,9 @@ static int heap_init(void) { sys_heap_init(&sof_heap.heap, heapmem, HEAPMEM_SIZE); +#if CONFIG_SOF_USERSPACE + sys_heap_init(&sof_user_heap.heap, user_heapmem, USER_HEAP_MEM_SIZE); +#endif #if CONFIG_L3_HEAP sys_heap_init(&l3_heap.heap, UINT_TO_POINTER(get_l3_heap_start()), get_l3_heap_size()); #endif From 8322eda98bff235b80da5f8bd6c60611357e2844 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Tue, 20 May 2025 13:33:17 +0100 Subject: [PATCH 5/5] scripts: gen-doc: fix sof workspace venv Workspace venv should be in top level SOF workspace directory since it also has to be used for pip installation of SOF docs dependecies and not just zephyr build python packages. Signed-off-by: Liam Girdwood --- scripts/gen-doc.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/gen-doc.sh b/scripts/gen-doc.sh index 7b36c0da96d6..98d7a69bca83 100755 --- a/scripts/gen-doc.sh +++ b/scripts/gen-doc.sh @@ -15,7 +15,7 @@ if [ ! -z "$ZEPHYR_BASE" ]; then VENV_DIR="$ZEPHYR_BASE/.venv" echo "Using Zephyr environment at $ZEPHYR_BASE" elif [ ! -z "$SOF_WORKSPACE" ]; then - VENV_DIR="$SOF_WORKSPACE/zephyr/.venv" + VENV_DIR="$SOF_WORKSPACE/.venv" echo "Using SOF/Zephyr environment at $SOF_WORKSPACE" else # fallback to the zephyr default from the getting started guide