Skip to content

Commit 2b71b0c

Browse files
committed
userspace: Add userspace_context and common_partition memory domain
Introduce the userspace_context structure to store data required by modules operating in userspace. Add the common_partition memory domain to share SOF components that need to be accessible from userspace. Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
1 parent dbe7168 commit 2b71b0c

6 files changed

Lines changed: 80 additions & 3 deletions

File tree

posix/include/rtos/userspace_helper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
#include <rtos/alloc.h>
1919

20+
#define APP_TASK_BSS
21+
#define APP_TASK_DATA
22+
2023
struct sys_heap;
2124

2225
#ifdef CONFIG_USERSPACE

src/audio/buffers/comp_buffer.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <sof/audio/sink_api.h>
1212
#include <sof/audio/source_api.h>
1313
#include <sof/audio/sink_source_utils.h>
14+
#include <rtos/userspace_helper.h>
1415
#include <sof/common.h>
1516
#include <rtos/interrupt.h>
1617
#include <rtos/alloc.h>
@@ -161,7 +162,7 @@ static void comp_buffer_free(struct sof_audio_buffer *audio_buffer)
161162
rfree(buffer);
162163
}
163164

164-
static const struct source_ops comp_buffer_source_ops = {
165+
APP_TASK_DATA static const struct source_ops comp_buffer_source_ops = {
165166
.get_data_available = comp_buffer_get_data_available,
166167
.get_data = comp_buffer_get_data,
167168
.release_data = comp_buffer_release_data,
@@ -170,7 +171,7 @@ static const struct source_ops comp_buffer_source_ops = {
170171
.set_alignment_constants = audio_buffer_source_set_alignment_constants,
171172
};
172173

173-
static const struct sink_ops comp_buffer_sink_ops = {
174+
APP_TASK_DATA static const struct sink_ops comp_buffer_sink_ops = {
174175
.get_free_size = comp_buffer_get_free_size,
175176
.get_buffer = comp_buffer_get_buffer,
176177
.commit_buffer = comp_buffer_commit_buffer,

src/include/module/module/base.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "interface.h"
1717
#include "../ipc4/base-config.h"
18+
#include <rtos/userspace_helper.h>
1819

1920
#define module_get_private_data(mod) ((mod)->priv.private)
2021
#define module_set_private_data(mod, data) ((mod)->priv.private = data)
@@ -180,6 +181,9 @@ struct processing_module {
180181
uint32_t max_sinks;
181182

182183
enum module_processing_type proc_type;
184+
#if CONFIG_USERSPACE
185+
struct userspace_context *user_ctx;
186+
#endif /* CONFIG_USERSPACE */
183187
#endif /* SOF_MODULE_PRIVATE */
184188
};
185189

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* SPDX-License-Identifier: BSD-3-Clause
2+
*
3+
* Copyright(c) 2025 Intel Corporation. All rights reserved.
4+
*
5+
* Author: Jaroslaw Stelter <jaroslaw.stelter@intel.com>
6+
* Author: Adrian Warecki <adrian.warecki@intel.com>
7+
*/
8+
9+
#ifndef __SOF_AUDIO_USERSPACE_PROXY_H__
10+
#define __SOF_AUDIO_USERSPACE_PROXY_H__
11+
12+
#if CONFIG_USERSPACE
13+
#include <stdint.h>
14+
#include <stdbool.h>
15+
16+
#include <zephyr/kernel.h>
17+
18+
/* Processing module structure fields needed for user mode */
19+
struct userspace_context {
20+
struct k_mem_domain *comp_dom; /* Module specific memory domain */
21+
};
22+
23+
#endif /* CONFIG_USERSPACE */
24+
25+
#endif /* __SOF_AUDIO_USERSPACE_PROXY_H__ */

zephyr/include/rtos/userspace_helper.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,20 @@
1212
#ifndef __ZEPHYR_LIB_USERSPACE_HELPER_H__
1313
#define __ZEPHYR_LIB_USERSPACE_HELPER_H__
1414

15-
#ifdef CONFIG_USERSPACE
15+
#ifndef CONFIG_USERSPACE
16+
#define APP_TASK_BSS
17+
#define APP_TASK_DATA
18+
#else
19+
20+
#include <zephyr/app_memory/app_memdomain.h>
21+
1622
#define DRV_HEAP_SIZE ALIGN_UP(CONFIG_SOF_ZEPHYR_USERSPACE_MODULE_HEAP_SIZE, \
1723
CONFIG_MM_DRV_PAGE_SIZE)
24+
#define APP_TASK_BSS K_APP_BMEM(common_partition)
25+
#define APP_TASK_DATA K_APP_DMEM(common_partition)
26+
27+
struct processing_module;
28+
struct userspace_context;
1829

1930
/**
2031
* Initialize private processing module heap.
@@ -29,6 +40,18 @@
2940
*/
3041
struct sys_heap *module_driver_heap_init(void);
3142

43+
/**
44+
* Add DP scheduler created thread to module memory domain.
45+
* @param thread_id - id of thread to be added to memory domain.
46+
* @param module - processing module structure
47+
*
48+
* @return 0 for success, error otherwise.
49+
*
50+
* @note
51+
* Function used only when CONFIG_USERSPACE is set.
52+
*/
53+
int user_memory_init_shared(k_tid_t thread_id, struct processing_module *mod);
54+
3255
#endif
3356

3457
/**

zephyr/lib/userspace_helper.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,19 @@
1616

1717
#include <rtos/alloc.h>
1818
#include <rtos/userspace_helper.h>
19+
#include <sof/audio/module_adapter/module/generic.h>
20+
#include <sof/audio/module_adapter/library/userspace_proxy.h>
1921

2022
#define MODULE_DRIVER_HEAP_CACHED CONFIG_SOF_ZEPHYR_HEAP_CACHED
2123

24+
/* Zephyr includes */
25+
#include <zephyr/kernel.h>
26+
#include <zephyr/app_memory/app_memdomain.h>
27+
2228
#if CONFIG_USERSPACE
29+
30+
K_APPMEM_PARTITION_DEFINE(common_partition);
31+
2332
struct sys_heap *module_driver_heap_init(void)
2433
{
2534
struct sys_heap *mod_drv_heap = rballoc(SOF_MEM_FLAG_USER, sizeof(struct sys_heap));
@@ -122,6 +131,18 @@ void module_driver_heap_remove(struct sys_heap *mod_drv_heap)
122131
}
123132
}
124133

134+
int user_memory_init_shared(k_tid_t thread_id, struct processing_module *mod)
135+
{
136+
struct k_mem_domain *comp_dom = mod->user_ctx->comp_dom;
137+
int ret;
138+
139+
ret = k_mem_domain_add_partition(comp_dom, &common_partition);
140+
if (ret < 0)
141+
return ret;
142+
143+
return k_mem_domain_add_thread(comp_dom, thread_id);
144+
}
145+
125146
#else /* CONFIG_USERSPACE */
126147

127148
void *module_driver_heap_rmalloc(struct sys_heap *mod_drv_heap, uint32_t flags, size_t bytes)

0 commit comments

Comments
 (0)