Skip to content

Commit 41a8d17

Browse files
committed
userspace context
1 parent a59af49 commit 41a8d17

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,33 @@ struct sys_heap *module_driver_heap_init(void);
3131

3232
#endif
3333

34+
/**
35+
* Allocates thread stack memory.
36+
* @param stack_size Required stack size.
37+
* @param options Stack configuration options
38+
* K_USER - when creating user thread
39+
* 0 - when creating kernel thread
40+
* @return pointer to the stack or NULL if not created.
41+
*
42+
* When CONFIG_USERSPACE not set function calls rballoc_align(),
43+
* otherwise it uses k_thread_stack_alloc() routine.
44+
*
45+
*/
46+
void *user_stack_allocate(size_t stack_size, uint32_t options);
47+
48+
/**
49+
* Free thread stack memory.
50+
* @param p_stack Pointer to the stack.
51+
*
52+
* @return 0 for success, error otherwise.
53+
*
54+
* @note
55+
* When CONFIG_USERSPACE not set function calls rfree(),
56+
* otherwise it uses k_thread_stack_free() routine.
57+
*
58+
*/
59+
int user_stack_free(void *p_stack);
60+
3461
/**
3562
* Allocates memory block from private module sys_heap if exists, otherwise call rballoc_align().
3663
* @param sys_heap - pointer to the sys_heap structure

zephyr/lib/userspace_helper.c

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

1717
#include <rtos/alloc.h>
1818
#include <rtos/userspace_helper.h>
19+
#include <sof/audio/module_adapter/module/generic.h>
1920

2021
#define MODULE_DRIVER_HEAP_CACHED CONFIG_SOF_ZEPHYR_HEAP_CACHED
2122

23+
/* Zephyr includes */
24+
#include <zephyr/kernel.h>
25+
#include <zephyr/app_memory/app_memdomain.h>
26+
2227
#if CONFIG_USERSPACE
28+
2329
struct sys_heap *module_driver_heap_init(void)
2430
{
2531
struct sys_heap *mod_drv_heap = rballoc(SOF_MEM_FLAG_USER, sizeof(struct sys_heap));
@@ -122,8 +128,37 @@ void module_driver_heap_remove(struct sys_heap *mod_drv_heap)
122128
}
123129
}
124130

131+
void *user_stack_allocate(size_t stack_size, uint32_t options)
132+
{
133+
return (__sparse_force void __sparse_cache *)
134+
k_thread_stack_alloc(stack_size, options & K_USER);
135+
}
136+
137+
int user_stack_free(void *p_stack)
138+
{
139+
if (!p_stack)
140+
return 0;
141+
return k_thread_stack_free((__sparse_force void *)p_stack);
142+
}
143+
125144
#else /* CONFIG_USERSPACE */
126145

146+
void *user_stack_allocate(size_t stack_size, uint32_t options)
147+
{
148+
/* allocate stack - must be aligned and cached so a separate alloc */
149+
stack_size = K_KERNEL_STACK_LEN(stack_size);
150+
void *p_stack = (__sparse_force void __sparse_cache *)
151+
rballoc_align(SOF_MEM_FLAG_USER, stack_size, Z_KERNEL_STACK_OBJ_ALIGN);
152+
153+
return p_stack;
154+
}
155+
156+
int user_stack_free(void *p_stack)
157+
{
158+
rfree((__sparse_force void *)p_stack);
159+
return 0;
160+
}
161+
127162
void *module_driver_heap_rmalloc(struct sys_heap *mod_drv_heap, uint32_t flags, size_t bytes)
128163
{
129164
return rmalloc(flags, bytes);

0 commit comments

Comments
 (0)