From 1b42a40c8d33e3089ec96874f73a3381330d8ea2 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Wed, 18 Mar 2026 18:35:47 +0200 Subject: [PATCH 1/2] rtos: alloc.h: add sof_sys_user_heap_get() Add sof_sys_user_heap_get() to get heap object to use for SOF audio application heap allocations. When SOF is built with CONFIG_SOF_USERSPACE_LL, this will return a heap that can be used for user-space code. Signed-off-by: Kai Vehmanen --- posix/include/rtos/alloc.h | 1 + src/platform/library/lib/alloc.c | 5 +++++ zephyr/include/rtos/alloc.h | 16 ++++++++++++++++ zephyr/lib/alloc.c | 11 +++++++++++ 4 files changed, 33 insertions(+) diff --git a/posix/include/rtos/alloc.h b/posix/include/rtos/alloc.h index b8efac0d3112..60dc951876e0 100644 --- a/posix/include/rtos/alloc.h +++ b/posix/include/rtos/alloc.h @@ -141,6 +141,7 @@ void *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes, size_t alignment); 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); /** * Calculates length of the null-terminated string. diff --git a/src/platform/library/lib/alloc.c b/src/platform/library/lib/alloc.c index 74cb926e4aff..804c7a2f4f6e 100644 --- a/src/platform/library/lib/alloc.c +++ b/src/platform/library/lib/alloc.c @@ -75,3 +75,8 @@ struct k_heap *sof_sys_heap_get(void) { return NULL; } + +struct k_heap *sof_sys_user_heap_get(void) +{ + return NULL; +} diff --git a/zephyr/include/rtos/alloc.h b/zephyr/include/rtos/alloc.h index 116789ea6ead..71dc85d93cbc 100644 --- a/zephyr/include/rtos/alloc.h +++ b/zephyr/include/rtos/alloc.h @@ -127,6 +127,22 @@ void *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes, void sof_heap_free(struct k_heap *heap, void *addr); struct k_heap *sof_sys_heap_get(void); +/** + * Returns heap object to use for SOF heap allocations + * for audio application code. + * + * The returned value may be NULL. This matches with semantics + * of sof_heap_alloc() that allows passing NULL as the 'heap'. + * In this case, the heap implementation will choose the heap to + * use. + * + * The function should not be used for heap allocations for objects that + * are only used in SOF kernel space. + * + * Note: audio modules should use mod_alloc() instead! + */ +struct k_heap *sof_sys_user_heap_get(void); + /* TODO: remove - debug only - only needed for linking */ static inline void heap_trace_all(int force) {} diff --git a/zephyr/lib/alloc.c b/zephyr/lib/alloc.c index 4079e01d1891..8192c2caff0f 100644 --- a/zephyr/lib/alloc.c +++ b/zephyr/lib/alloc.c @@ -13,6 +13,7 @@ #include #include #include +#include /* for zephyr_ll_user_heap() */ #include #include #include @@ -380,6 +381,16 @@ struct k_heap *sof_sys_heap_get(void) return &sof_heap; } +struct k_heap *sof_sys_user_heap_get(void) +{ +#ifdef CONFIG_SOF_USERSPACE_LL + return zephyr_ll_user_heap(); +#else + /* let sof_heap_alloc() pick */ + return NULL; +#endif +} + static void *heap_alloc_aligned(struct k_heap *h, size_t min_align, size_t bytes) { k_spinlock_key_t key; From af0787aed75f8857e20657f437a281904238b562 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Thu, 26 Mar 2026 15:20:06 +0200 Subject: [PATCH 2/2] zephyr: rtos: userspace_helper.h: add sysuser memory partition Add APP_SYSUSER_BSS() and APP_SYSUSER_DATA() macros to put global objects to a memory partition that is available to the user context used to run main SOF application. If SOF is run in kernel space, these are no-ops, but if CONFIG_SOF_USERSPACE_LL is set, a dedicated memory partition is used. Signed-off-by: Kai Vehmanen --- zephyr/include/rtos/userspace_helper.h | 31 ++++++++++++++++++++++++++ zephyr/lib/userspace_helper.c | 8 +++++++ 2 files changed, 39 insertions(+) diff --git a/zephyr/include/rtos/userspace_helper.h b/zephyr/include/rtos/userspace_helper.h index 29635fb942ad..7d8f7919d865 100644 --- a/zephyr/include/rtos/userspace_helper.h +++ b/zephyr/include/rtos/userspace_helper.h @@ -26,6 +26,14 @@ #define APP_TASK_BSS K_APP_BMEM(common_partition) #define APP_TASK_DATA K_APP_DMEM(common_partition) +#ifdef CONFIG_SOF_USERSPACE_LL +#define APP_SYSUSER_BSS K_APP_BMEM(sysuser_partition) +#define APP_SYSUSER_DATA K_APP_DMEM(sysuser_partition) +#else +#define APP_SYSUSER_BSS +#define APP_SYSUSER_DATA +#endif + struct processing_module; struct userspace_context; @@ -137,4 +145,27 @@ static inline int user_access_to_mailbox(struct k_mem_domain *domain, k_tid_t th #endif /* CONFIG_USERSPACE */ +#ifdef CONFIG_SOF_USERSPACE_LL + +int user_memory_attach_system_user_partition(struct k_mem_domain *dom); + +#else + +/** + * Attach SOF system user memory partition to a memory domain. + * @param dom - memory domain to attach the sysuser partition to. + * + * @return 0 for success, error otherwise. + * + * @note + * Function used only when CONFIG_USERSPACE is set. + * The sysuser partition contains shared objects required by user-space modules. + */ +static inline int user_memory_attach_system_user_partition(struct k_mem_domain *dom) +{ + return 0; +} + +#endif /* CONFIG_SOF_USERSPACE_LL */ + #endif /* __ZEPHYR_LIB_USERSPACE_HELPER_H__ */ diff --git a/zephyr/lib/userspace_helper.c b/zephyr/lib/userspace_helper.c index 8c4aef423e15..c7c361295269 100644 --- a/zephyr/lib/userspace_helper.c +++ b/zephyr/lib/userspace_helper.c @@ -36,6 +36,10 @@ LOG_MODULE_REGISTER(userspace_helper, CONFIG_SOF_LOG_LEVEL); K_APPMEM_PARTITION_DEFINE(common_partition); +#ifdef CONFIG_SOF_USERSPACE_LL +K_APPMEM_PARTITION_DEFINE(sysuser_partition); +#endif + struct k_heap *module_driver_heap_init(void) { struct k_heap *mod_drv_heap = rballoc(SOF_MEM_FLAG_USER, sizeof(*mod_drv_heap)); @@ -83,6 +87,10 @@ int user_memory_attach_common_partition(struct k_mem_domain *dom) } #ifdef CONFIG_SOF_USERSPACE_LL +int user_memory_attach_system_user_partition(struct k_mem_domain *dom) +{ + return k_mem_domain_add_partition(dom, &sysuser_partition); +} int user_access_to_mailbox(struct k_mem_domain *domain, k_tid_t thread_id) {