Skip to content

Commit 7549fc2

Browse files
committed
zephyr: userspace_helper: add user_access_to_mailbox()
Add helper function to make memory regions needed for sof/mailbox.h available to a user thread. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
1 parent 7b50fa3 commit 7549fc2

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

zephyr/include/rtos/userspace_helper.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,23 @@ int user_stack_free(void *p_stack);
9191
*/
9292
void module_driver_heap_remove(struct k_heap *mod_drv_heap);
9393

94+
#ifdef CONFIG_USERSPACE
95+
96+
/**
97+
* Add access to mailbox.h interface to a user-space thread.
98+
*
99+
* @param domain memory domain to add the mailbox partitions to
100+
* @param thread_id user-space thread for which access is added
101+
*/
102+
int user_access_to_mailbox(struct k_mem_domain *domain, k_tid_t thread_id);
103+
104+
#else
105+
106+
static inline int user_access_to_mailbox(struct k_mem_domain *domain, k_tid_t thread_id)
107+
{
108+
return 0;
109+
}
110+
111+
#endif /* CONFIG_USERSPACE */
112+
94113
#endif /* __ZEPHYR_LIB_USERSPACE_HELPER_H__ */

zephyr/lib/userspace_helper.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <rtos/userspace_helper.h>
1919
#include <sof/audio/module_adapter/module/generic.h>
2020
#include <sof/audio/module_adapter/library/userspace_proxy.h>
21+
#include <sof/lib/mailbox.h>
2122

2223
#define MODULE_DRIVER_HEAP_CACHED CONFIG_SOF_ZEPHYR_HEAP_CACHED
2324

@@ -82,6 +83,46 @@ int user_memory_init_shared(k_tid_t thread_id, struct processing_module *mod)
8283
return k_mem_domain_add_thread(comp_dom, thread_id);
8384
}
8485

86+
int user_access_to_mailbox(struct k_mem_domain *domain, k_tid_t thread_id)
87+
{
88+
struct k_mem_partition mem_partition;
89+
int ret;
90+
91+
/*
92+
* Start with mailbox_swregs. This is aligned with mailbox.h
93+
* implementation with uncached addressed used for register I/O.
94+
*/
95+
mem_partition.start =
96+
(uintptr_t)sys_cache_uncached_ptr_get((void __sparse_cache *)MAILBOX_SW_REG_BASE);
97+
98+
BUILD_ASSERT(MAILBOX_SW_REG_SIZE == CONFIG_MMU_PAGE_SIZE);
99+
mem_partition.size = CONFIG_MMU_PAGE_SIZE;
100+
mem_partition.attr = K_MEM_PARTITION_P_RW_U_RW;
101+
102+
ret = k_mem_domain_add_partition(domain, &mem_partition);
103+
if (ret < 0)
104+
return ret;
105+
106+
#ifndef CONFIG_IPC_MAJOR_4
107+
/*
108+
* Next mailbox_stream (not available in IPC4). Stream access is cached,
109+
* so different mapping this time.
110+
*/
111+
mem_partition.start =
112+
(uintptr_t)sys_cache_cached_ptr_get((void *)SRAM_STREAM_BASE);
113+
BUILD_ASSERT(MAILBOX_STREAM_SIZE == CONFIG_MMU_PAGE_SIZE);
114+
/* size and attr the same as for mailbox_swregs */
115+
116+
ret = k_mem_domain_add_partition(domain, &mem_partition);
117+
if (ret < 0)
118+
return ret;
119+
#endif
120+
121+
k_mem_domain_add_thread(domain, thread_id);
122+
123+
return 0;
124+
}
125+
85126
#else /* CONFIG_USERSPACE */
86127

87128
void *user_stack_allocate(size_t stack_size, uint32_t options)

0 commit comments

Comments
 (0)