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 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/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..0627e9c67226 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 @@ -19,6 +27,42 @@ 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 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 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 899730e312e0..5d058635d7e1 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 */ @@ -97,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) @@ -124,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; @@ -400,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; @@ -503,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); @@ -540,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