-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathuserspace_helper.c
More file actions
172 lines (134 loc) · 4.18 KB
/
userspace_helper.c
File metadata and controls
172 lines (134 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2025 Intel Corporation. All rights reserved.
//
// Author: Jaroslaw Stelter <jaroslaw.stelter@intel.com>
// Adrian Warecki <adrian.warecki@intel.com>
/**
* \file
* \brief Zephyr userspace helper functions
* \authors Jaroslaw Stelter <jaroslaw.stelter@intel.com>
* \authors Adrian Warecki <adrian.warecki@intel.com>
*/
#include <stdint.h>
#include <rtos/alloc.h>
#include <rtos/sof.h>
#include <rtos/userspace_helper.h>
#include <sof/audio/module_adapter/module/generic.h>
#include <sof/audio/module_adapter/library/userspace_proxy.h>
#include <sof/lib/mailbox.h>
#include <sof/lib/dai.h>
#include <sof/lib/dma.h>
/* Zephyr includes */
#include <zephyr/kernel.h>
#include <zephyr/app_memory/app_memdomain.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(userspace_helper, CONFIG_SOF_LOG_LEVEL);
#if CONFIG_USERSPACE
K_APPMEM_PARTITION_DEFINE(common_partition);
struct k_heap *module_driver_heap_init(void)
{
struct k_heap *mod_drv_heap = rballoc(SOF_MEM_FLAG_USER, sizeof(*mod_drv_heap));
if (!mod_drv_heap)
return NULL;
void *mem = rballoc_align(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT, USER_MOD_HEAP_SIZE,
CONFIG_MM_DRV_PAGE_SIZE);
if (!mem) {
rfree(mod_drv_heap);
return NULL;
}
k_heap_init(mod_drv_heap, mem, USER_MOD_HEAP_SIZE);
mod_drv_heap->heap.init_mem = mem;
mod_drv_heap->heap.init_bytes = USER_MOD_HEAP_SIZE;
return mod_drv_heap;
}
void module_driver_heap_remove(struct k_heap *mod_drv_heap)
{
if (mod_drv_heap) {
rfree(mod_drv_heap->heap.init_mem);
rfree(mod_drv_heap);
}
}
void *user_stack_allocate(size_t stack_size, uint32_t options)
{
return k_thread_stack_alloc(stack_size, options & K_USER);
}
int user_stack_free(void *p_stack)
{
if (!p_stack)
return 0;
return k_thread_stack_free(p_stack);
}
int user_memory_attach_common_partition(struct k_mem_domain *dom)
{
return k_mem_domain_add_partition(dom, &common_partition);
}
#ifdef CONFIG_SOF_USERSPACE_LL
int user_access_to_mailbox(struct k_mem_domain *domain, k_tid_t thread_id)
{
struct k_mem_partition mem_partition;
int ret;
/*
* Start with mailbox_swregs. This is aligned with mailbox.h
* implementation with uncached addressed used for register I/O.
*/
mem_partition.start =
(uintptr_t)sys_cache_uncached_ptr_get((void __sparse_cache *)MAILBOX_SW_REG_BASE);
BUILD_ASSERT(MAILBOX_SW_REG_SIZE == CONFIG_MMU_PAGE_SIZE);
mem_partition.size = CONFIG_MMU_PAGE_SIZE;
mem_partition.attr = K_MEM_PARTITION_P_RW_U_RW;
ret = k_mem_domain_add_partition(domain, &mem_partition);
if (ret < 0)
return ret;
#ifndef CONFIG_IPC_MAJOR_4
/*
* Next mailbox_stream (not available in IPC4). Stream access is cached,
* so different mapping this time.
*/
mem_partition.start =
(uintptr_t)sys_cache_cached_ptr_get((void *)SRAM_STREAM_BASE);
BUILD_ASSERT(MAILBOX_STREAM_SIZE == CONFIG_MMU_PAGE_SIZE);
/* size and attr the same as for mailbox_swregs */
ret = k_mem_domain_add_partition(domain, &mem_partition);
if (ret < 0)
return ret;
#endif
k_mem_domain_add_thread(domain, thread_id);
return 0;
}
void user_grant_dai_access_all(struct k_thread *thread)
{
const struct device **devices;
size_t count;
size_t i;
devices = dai_get_device_list(&count);
for (i = 0; i < count; i++)
k_thread_access_grant(thread, devices[i]);
LOG_DBG("Granted DAI access to thread %p for %zu devices", thread, count);
}
void user_grant_dma_access_all(struct k_thread *thread)
{
const struct dma_info *info = dma_info_get();
struct sof_dma *d;
for (d = info->dma_array; d < info->dma_array + info->num_dmas; d++) {
k_thread_access_grant(thread, d->z_dev);
LOG_DBG("Granted DMA device access: %s to thread %p", d->z_dev->name, thread);
}
}
#endif /* CONFIG_SOF_USERSPACE_LL */
#else /* CONFIG_USERSPACE */
void *user_stack_allocate(size_t stack_size, uint32_t options)
{
/* allocate stack - must be aligned and cached so a separate alloc */
stack_size = K_KERNEL_STACK_LEN(stack_size);
void *p_stack = rballoc_align(SOF_MEM_FLAG_USER, stack_size, Z_KERNEL_STACK_OBJ_ALIGN);
return p_stack;
}
int user_stack_free(void *p_stack)
{
rfree(p_stack);
return 0;
}
void module_driver_heap_remove(struct k_heap *mod_drv_heap)
{ }
#endif /* CONFIG_USERSPACE */