Skip to content

Commit 286da62

Browse files
committed
userspace context
1 parent 114cb34 commit 286da62

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

src/include/module/module/base.h

Lines changed: 2 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,7 @@ struct processing_module {
180181
uint32_t max_sinks;
181182

182183
enum module_processing_type proc_type;
184+
struct userspace_context *user_ctx;
183185
#endif /* SOF_MODULE_PRIVATE */
184186
};
185187

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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#define DRV_HEAP_SIZE ALIGN_UP(CONFIG_SOF_ZEPHYR_USERSPACE_MODULE_HEAP_SIZE, \
1717
CONFIG_MM_DRV_PAGE_SIZE)
1818

19+
struct processing_module;
20+
struct userspace_context;
21+
1922
/**
2023
* Initialize private processing module heap.
2124
* @param N/A.
@@ -29,8 +32,47 @@
2932
*/
3033
struct sys_heap *module_driver_heap_init(void);
3134

35+
/**
36+
* Add DP scheduler created thread to module memory domain.
37+
* @param thread_id - id of thread to be added to memory domain.
38+
* @param module - processing module structure
39+
*
40+
* @return 0 for success, error otherwise.
41+
*
42+
* @note
43+
* Function used only when CONFIG_USERSPACE is set.
44+
*/
45+
int user_memory_init_shared(k_tid_t thread_id, struct processing_module *mod);
46+
3247
#endif
3348

49+
/**
50+
* Allocates thread stack memory.
51+
* @param stack_size Required stack size.
52+
* @param options Stack configuration options
53+
* K_USER - when creating user thread
54+
* 0 - when creating kernel thread
55+
* @return pointer to the stack or NULL if not created.
56+
*
57+
* When CONFIG_USERSPACE not set function calls rballoc_align(),
58+
* otherwise it uses k_thread_stack_alloc() routine.
59+
*
60+
*/
61+
void *user_stack_allocate(size_t stack_size, uint32_t options);
62+
63+
/**
64+
* Free thread stack memory.
65+
* @param p_stack Pointer to the stack.
66+
*
67+
* @return 0 for success, error otherwise.
68+
*
69+
* @note
70+
* When CONFIG_USERSPACE not set function calls rfree(),
71+
* otherwise it uses k_thread_stack_free() routine.
72+
*
73+
*/
74+
int user_stack_free(void *p_stack);
75+
3476
/**
3577
* Allocates memory block from private module sys_heap if exists, otherwise call rballoc_align().
3678
* @param sys_heap - pointer to the sys_heap structure

zephyr/lib/userspace_helper.c

Lines changed: 50 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,8 +131,49 @@ void module_driver_heap_remove(struct sys_heap *mod_drv_heap)
122131
}
123132
}
124133

134+
void *user_stack_allocate(size_t stack_size, uint32_t options)
135+
{
136+
return (__sparse_force void __sparse_cache *)
137+
k_thread_stack_alloc(stack_size, options & K_USER);
138+
}
139+
140+
int user_stack_free(void *p_stack)
141+
{
142+
if (!p_stack)
143+
return 0;
144+
return k_thread_stack_free((__sparse_force void *)p_stack);
145+
}
146+
147+
int user_memory_init_shared(k_tid_t thread_id, struct processing_module *mod)
148+
{
149+
struct k_mem_domain *comp_dom = mod->user_ctx->comp_dom;
150+
int ret;
151+
152+
ret = k_mem_domain_add_partition(comp_dom, &common_partition);
153+
if (ret < 0)
154+
return ret;
155+
156+
return k_mem_domain_add_thread(comp_dom, thread_id);
157+
}
158+
125159
#else /* CONFIG_USERSPACE */
126160

161+
void *user_stack_allocate(size_t stack_size, uint32_t options)
162+
{
163+
/* allocate stack - must be aligned and cached so a separate alloc */
164+
stack_size = K_KERNEL_STACK_LEN(stack_size);
165+
void *p_stack = (__sparse_force void __sparse_cache *)
166+
rballoc_align(SOF_MEM_FLAG_USER, stack_size, Z_KERNEL_STACK_OBJ_ALIGN);
167+
168+
return p_stack;
169+
}
170+
171+
int user_stack_free(void *p_stack)
172+
{
173+
rfree((__sparse_force void *)p_stack);
174+
return 0;
175+
}
176+
127177
void *module_driver_heap_rmalloc(struct sys_heap *mod_drv_heap, uint32_t flags, size_t bytes)
128178
{
129179
return rmalloc(flags, bytes);

0 commit comments

Comments
 (0)