Skip to content

Commit de5eec1

Browse files
committed
userspace context
1 parent a5b4d7d commit de5eec1

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-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.
@@ -30,8 +33,47 @@
3033
*/
3134
struct sys_heap *drv_heap_init(void);
3235

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

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

zephyr/lib/userspace_helper.c

Lines changed: 49 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 DRV_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 *drv_heap_init(void)
2433
{
2534
struct sys_heap *drv_heap = rballoc(SOF_MEM_FLAG_USER, sizeof(struct sys_heap));
@@ -121,8 +130,48 @@ void drv_heap_remove(struct sys_heap *drv_heap)
121130
}
122131
}
123132

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

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

0 commit comments

Comments
 (0)