Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/audio/module_adapter/module_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ __cold static struct vregion *module_adapter_dp_heap_new(const struct comp_ipc_c
LOG_INF("%zu byte heap size requested in IPC for %#x", buf_size, config->id);
}
#endif
return vregion_create_map(&vreg_start, &buf_size);
return vregion_create_map(&vreg_start, &buf_size, 0);
}

__cold static
Expand Down Expand Up @@ -130,9 +130,9 @@ struct processing_module *module_adapter_mem_alloc(const struct comp_driver *drv
if (!mod_vreg)
mod = sof_heap_alloc(mod_heap, flags, sizeof(*mod), 0);
else if (flags & SOF_MEM_FLAG_COHERENT)
mod = vregion_alloc_coherent(mod_vreg, sizeof(*mod));
mod = vregion_user_alloc_coherent_align(mod_vreg, sizeof(*mod), 0);
else
mod = vregion_alloc(mod_vreg, sizeof(*mod));
mod = vregion_user_alloc_align(mod_vreg, sizeof(*mod), 0);

if (!mod) {
comp_cl_err(drv, "failed to allocate memory for module");
Expand All @@ -150,9 +150,9 @@ struct processing_module *module_adapter_mem_alloc(const struct comp_driver *drv
if (!mod_vreg)
alloc = sof_heap_alloc(mod_heap, flags, sizeof(*alloc), 0);
else if (flags & SOF_MEM_FLAG_COHERENT)
alloc = vregion_alloc_coherent(mod_vreg, sizeof(*alloc));
alloc = vregion_user_alloc_coherent_align(mod_vreg, sizeof(*alloc), 0);
else
alloc = vregion_alloc(mod_vreg, sizeof(*alloc));
alloc = vregion_user_alloc_align(mod_vreg, sizeof(*alloc), 0);
if (!alloc)
goto ealloc;

Expand All @@ -171,7 +171,7 @@ struct processing_module *module_adapter_mem_alloc(const struct comp_driver *drv
* single-core configurations.
*/
if (mod_vreg)
dev = vregion_alloc_coherent(mod_vreg, sizeof(*dev));
dev = vregion_user_alloc_coherent_align(mod_vreg, sizeof(*dev), 0);
else
dev = sof_heap_alloc(mod_heap, SOF_MEM_FLAG_COHERENT, sizeof(*dev), 0);

Expand All @@ -193,7 +193,7 @@ struct processing_module *module_adapter_mem_alloc(const struct comp_driver *drv
sof_heap_free(mod_heap, alloc);
ealloc:
if (mod_vreg)
vregion_free(mod_vreg, mod);
vregion_user_free(mod_vreg, mod);
else
sof_heap_free(mod_heap, mod);
emod:
Expand Down
19 changes: 15 additions & 4 deletions src/audio/pipeline/pipeline-graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,18 @@ __cold struct pipeline *pipeline_new(struct k_heap *heap, uint32_t pipeline_id,
memset(alloc, 0, sizeof(*alloc));
alloc->heap = heap;

/* Create vregion for pipeline and its modules if size info is available */
/*
* Create vregion for pipeline and its modules if size info is available.
* With user-space LL, the IPC user threads run its interim heap.
*/
if (IS_ENABLED(CONFIG_SOF_VREGIONS) &&
pparams && pparams->mem_data && pparams->mem_data->heap_bytes) {
uint32_t vreg_flags = IS_ENABLED(CONFIG_SOF_VREGION_USER_HEAP) ?
VREGION_FLAG_USER_HEAP : 0;
size_t buf_size = pparams->mem_data->heap_bytes;
uintptr_t vreg_start;

alloc->vreg = vregion_create_map(&vreg_start, &buf_size);
alloc->vreg = vregion_create_map(&vreg_start, &buf_size, vreg_flags);
if (!alloc->vreg)
pipe_cl_err("Failed to create pipeline vregion of %u bytes, using heap",
pparams->mem_data->heap_bytes);
Expand Down Expand Up @@ -378,7 +383,8 @@ __cold int pipeline_free(struct pipeline *p)
/* now free the pipeline */
sof_ctx_free(alloc, p);

/* free alloc context and vregion */
/* free alloc context and vregion, the modules are gone */
vregion_user_heap_remove(alloc->vreg);
if (vregion_put(alloc->vreg))
pipe_cl_warn("pipeline vregion still in use");
sof_heap_free(alloc->heap, alloc);
Expand Down Expand Up @@ -495,9 +501,14 @@ int pipeline_complete(struct pipeline *p, struct comp_dev *source,
p->source_comp = source;
p->sink_comp = sink;

if (p->alloc->vreg)
if (p->alloc->vreg) {
vregion_set_interim(p->alloc->vreg);

/* like a region too small for a kernel interim heap, not fatal here */
if (vregion_user_heap_add(p->alloc->vreg) < 0)
pipe_warn(p, "no interim heap");
}

p->status = COMP_STATE_READY;

/* show heap status */
Expand Down
178 changes: 176 additions & 2 deletions src/include/sof/lib/vregion.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ enum vregion_mem_type {
VREGION_MEM_TYPE_INVALID,
};

/**
* @brief The interim heap of the region is run by the user threads that use it.
*
* vregion_set_interim() then only reserves the interim range, and the kernel
* neither allocates from nor frees to that range. The users get the range
* with vregion_interim_get() and run their own allocator on it, see
* vregion_user_heap_add(). Only with CONFIG_SOF_VREGION_USER_HEAP.
*/
#define VREGION_FLAG_USER_HEAP (1U << 0)

/**
* @brief Usage counters of a user-mode interim heap.
*
* At the start of the interim range of a region with VREGION_FLAG_USER_HEAP.
* Maintained by the user threads, so the kernel only copies them as numbers.
*/
struct vregion_user_heap_stats {
size_t allocated_bytes; /**< usable bytes allocated now */
size_t max_allocated_bytes; /**< peak of allocated_bytes */
unsigned int fail_count; /**< failed allocations */
};

#if CONFIG_SOF_VREGIONS

/**
Expand All @@ -64,9 +86,11 @@ struct vregion *vregion_create(size_t memsize);
* @param[out] vreg_start Output pointer receiving the virtual start address of the region.
* @param[in,out] vreg_size Input pointer specifying the requested size in bytes (must be non-zero);
* updated on return with the actual mapped size in bytes.
* @param[in] flags VREGION_FLAG_* flags.
* @return Pointer to the allocated virtual region instance, or NULL on failure.
*/
__syscall struct vregion *vregion_create_map(uintptr_t *vreg_start, size_t *vreg_size);
__syscall struct vregion *vregion_create_map(uintptr_t *vreg_start, size_t *vreg_size,
uint32_t flags);

/**
* @brief Switch virtual region allocations to interim mode.
Expand All @@ -79,6 +103,18 @@ __syscall struct vregion *vregion_create_map(uintptr_t *vreg_start, size_t *vreg
*/
__syscall void vregion_set_interim(struct vregion *vr);

/**
* @brief Get the interim range of a virtual region with VREGION_FLAG_USER_HEAP.
*
* @param[in] vr Pointer to the virtual region instance.
* @param[out] start Start address of the interim range (cached alias).
* @param[out] size Size of the interim range in bytes.
* @return 0 on success, -EAGAIN before vregion_set_interim(), -ENOMEM if no
* interim range was left, -EPERM if the interim heap of vr is run by
* the kernel.
*/
__syscall int vregion_interim_get(struct vregion *vr, uintptr_t *start, size_t *size);

/**
* @brief Increment virtual region's user count.
*
Expand Down Expand Up @@ -213,6 +249,119 @@ bool vregion_pinned_get(struct vregion *vr);
*/
int vregion_pinned_put(struct vregion *vr);

#if CONFIG_SOF_VREGION_USER_HEAP

/**
* @brief Set up the user-mode interim heap of a region.
*
* For a region with VREGION_FLAG_USER_HEAP, after vregion_set_interim(). In
* the LL or IPC user thread only: initialises the heap in the interim range
* and makes vregion_user_alloc_align() and the other vregion_user_*()
* functions use it in user mode.
*
* @param[in] vr Pointer to the virtual region instance.
* @return 0 on success or a negative error code.
*/
int vregion_user_heap_add(struct vregion *vr);

/**
* @brief Stop using the user-mode interim heap of a region.
*
* Call before dropping the last reference that allocates from the region.
* Later frees of its interim memory go to the kernel, which refuses them.
*
* @param[in] vr Pointer to the virtual region instance.
*/
void vregion_user_heap_remove(struct vregion *vr);

/**
* @brief Get the usage counters of the user-mode interim heap of a region.
*
* @param[in] vr Pointer to the virtual region instance.
* @param[out] stats Usage counters.
* @return 0 on success, -ENOENT if vr has no user-mode heap.
*/
int vregion_user_heap_stats_get(struct vregion *vr, struct vregion_user_heap_stats *stats);

/**
* @brief Allocate aligned memory from a region.
*
* In user mode, from the user-mode interim heap of vr, if it has one. Else
* the same as vregion_alloc_align().
*
* @param[in] vr Pointer to the virtual region instance.
* @param[in] size Size of memory to allocate in bytes.
* @param[in] alignment Alignment of memory to allocate in bytes.
* @return Pointer to the allocated memory, or NULL on failure.
*/
void *vregion_user_alloc_align(struct vregion *vr, size_t size, size_t alignment);

/**
* @brief Allocate aligned coherent (uncached) memory from a region.
*
* In user mode, from the user-mode interim heap of vr, if it has one. Else
* the same as vregion_alloc_coherent_align().
*
* @param[in] vr Pointer to the virtual region instance.
* @param[in] size Size of memory to allocate in bytes.
* @param[in] alignment Alignment of memory to allocate in bytes.
* @return Pointer to the allocated coherent memory, or NULL on failure.
*/
void *vregion_user_alloc_coherent_align(struct vregion *vr, size_t size, size_t alignment);

/**
* @brief Free memory allocated from a region.
*
* In user mode, to the user-mode interim heap of vr, if ptr is in it. Else
* the same as vregion_free().
*
* @param[in] vr Pointer to the virtual region instance.
* @param[in] ptr Pointer to the memory to free.
*/
void vregion_user_free(struct vregion *vr, void *ptr);

#if CONFIG_SOF_BOOT_TEST_STANDALONE
struct sys_heap;
struct sys_mutex;

/**
* @brief Get the allocator state of the user-mode heap of a region, for tests.
* @param[in] vr Pointer to the virtual region instance.
* @param[out] heap The heap, in the region memory, or NULL if vr has no heap.
* @param[out] lock The lock of all user-mode region heaps, in LL memory.
*/
void vregion_user_heap_test_get(struct vregion *vr, struct sys_heap **heap,
struct sys_mutex **lock);
#endif

#else

static inline int vregion_user_heap_add(struct vregion *vr)
{
return 0;
}
static inline void vregion_user_heap_remove(struct vregion *vr) {}
static inline int vregion_user_heap_stats_get(struct vregion *vr,
struct vregion_user_heap_stats *stats)
{
return -ENOENT;
}
static inline void *vregion_user_alloc_align(struct vregion *vr, size_t size, size_t alignment)
{
return vregion_alloc_align(vr, size, alignment);
}
static inline void *vregion_user_alloc_coherent_align(struct vregion *vr, size_t size,
size_t alignment)
{
return vregion_alloc_coherent_align(vr, size, alignment);
}
static inline void vregion_user_free(struct vregion *vr, void *ptr)
{
vregion_free(vr, ptr);
}

#endif /* CONFIG_SOF_VREGION_USER_HEAP */

#include <zephyr/syscalls/vregion.h>

#else
Expand All @@ -225,11 +374,16 @@ static inline struct vregion *vregion_create(size_t memsize)
{
return NULL;
}
static inline struct vregion *vregion_create_map(uintptr_t *vreg_start, size_t *vreg_size)
static inline struct vregion *vregion_create_map(uintptr_t *vreg_start, size_t *vreg_size,
uint32_t flags)
{
return NULL;
}
static inline void vregion_set_interim(struct vregion *vr) {}
static inline int vregion_interim_get(struct vregion *vr, uintptr_t *start, size_t *size)
{
return -EPERM;
}
static inline struct vregion *vregion_get(struct vregion *vr)
{
return vr;
Expand Down Expand Up @@ -262,6 +416,26 @@ static inline void vregion_mem_info(struct vregion *vr, size_t *size, uintptr_t
if (size)
*size = 0;
}
static inline int vregion_user_heap_add(struct vregion *vr)
{
return 0;
}
static inline void vregion_user_heap_remove(struct vregion *vr) {}
static inline int vregion_user_heap_stats_get(struct vregion *vr,
struct vregion_user_heap_stats *stats)
{
return -ENOENT;
}
static inline void *vregion_user_alloc_align(struct vregion *vr, size_t size, size_t alignment)
{
return NULL;
}
static inline void *vregion_user_alloc_coherent_align(struct vregion *vr, size_t size,
size_t alignment)
{
return NULL;
}
static inline void vregion_user_free(struct vregion *vr, void *ptr) {}
static inline bool vregion_verify_pin(struct vregion *vr) { return false; }
static inline void vregion_unpin(struct vregion *vr) {}
static inline bool vregion_pinned_get(struct vregion *vr) { return false; }
Expand Down
19 changes: 18 additions & 1 deletion src/include/sof/userspace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ syscall by subsystem:
| Subsystem | Syscalls | Declared in | Validator (`z_vrfy_`) / logic (`z_impl_`) |
|-----------|----------|-------------|-------------------------------------------|
| IPC | `ipc_msg_send`, `ipc_msg_list_remove`, `ipc_msg_reply`, `ipc_compound_pre_start`, `ipc_compound_post_start`, `ipc_wait_for_compound_msg`, `send_resource_notif` | `sof/ipc/msg.h`, `sof/ipc/ipc_reply.h`, `ipc4/handler.h`, `ipc4/notification.h` | scattered: `ipc/ipc-common.c`, `kernel/ipc/ipc3/helper.c`, `kernel/ipc/ipc4/handler-kernel.c`, `kernel/ipc/ipc4/notification.c` |
| Virtual regions | `vregion_create_map`, `vregion_set_interim`, `vregion_get`, `vregion_put`, `vregion_alloc_align`, `vregion_alloc_coherent_align`, `vregion_free` | `sof/lib/vregion.h` | `zephyr/syscall/vregion.c` / `zephyr/lib/vregion.c` |
| Virtual regions | `vregion_create_map`, `vregion_set_interim`, `vregion_interim_get`, `vregion_get`, `vregion_put`, `vregion_alloc_align`, `vregion_alloc_coherent_align`, `vregion_free` | `sof/lib/vregion.h` | `zephyr/syscall/vregion.c` / `zephyr/lib/vregion.c` |
| DMA | `sof_dma_get`, `sof_dma_put`, `sof_dma_get_attribute`, `sof_dma_request_channel`, `sof_dma_release_channel`, `sof_dma_config`, `sof_dma_start`, `sof_dma_stop`, `sof_dma_get_status`, `sof_dma_reload`, `sof_dma_suspend`, `sof_dma_resume` | `sof/lib/sof_dma.h` | `zephyr/syscall/sof_dma.c` / `kernel/lib/dma-kernel.c` (get/put); remaining 10 are `static inline z_impl_*` in `sof/lib/sof_dma.h` |
| DAI | `dai_get_device` | `sof/lib/dai-zephyr.h` | `zephyr/syscall/dai.c` / `kernel/lib/dai-kernel.c` |
| CPU | `cpu_get_id` | `sof/lib/cpu.h` | `zephyr/syscall/cpu.c` / `zephyr/lib/cpu.c` |
Expand Down Expand Up @@ -240,6 +240,23 @@ heap. A user thread that corrupts the heap can only make LL and IPC user threads
`zephyr_ll_user_heap()` returns a kernel-only `struct k_heap` that is never initialised: its
address only selects the LL user heap.

Each pipeline also has a virtual region (vregion) for itself and its LL modules, mapped
read-write into the LL memory domain. Allocations made before the pipeline is complete come
from the lifetime allocator of the region, whose state is kernel-only, through the `vregion_*`
syscalls. The interim heap, used after that, keeps its `sys_heap` metadata in the region, so it
runs in user mode too ([`lib/vregion_user.c`](../../../lib/vregion_user.c)). The pipeline
regions are created with `VREGION_FLAG_USER_HEAP`: `vregion_set_interim()` then only reserves
the rest of the region for the interim heap, and kernel code, including the `vregion_*`
syscalls, refuses to allocate from or free to that range and logs the caller. When the
pipeline is complete, the IPC user thread gets the range with the `vregion_interim_get()`
syscall, which only the LL and IPC user threads may call, and initialises the heap in it. The
heap header at the start of the range holds the usage counters, which telemetry reads as
numbers, and the `sys_heap` object. The user threads find the header of a region in a table
in the system user partition, under a `sys_mutex` there, so `sof_ctx_alloc()`,
`sof_ctx_free()` and the object pools allocate from the heap with no syscall, through
`vregion_user_alloc_align()` and `vregion_user_free()`. The heap is removed when the pipeline
is freed, after its modules. The kernel still runs the interim heap of the DP module regions.

The fast_get area holds the `fast_get()` copies of the LL modules, such as SRC coefficients.
LL and IPC user threads get them with the `fast_get_ll()` and `fast_put_ll()` syscalls, and all
users of the same data share a copy. The allocator is a bitmap of 256-byte blocks in kernel
Expand Down
1 change: 1 addition & 0 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ is_zephyr(zephyr)
if(zephyr) ### Zephyr ###

add_local_sources(sof ${common_files})
add_local_sources_ifdef(CONFIG_SOF_VREGION_USER_HEAP sof vregion_user.c)

endif()
10 changes: 5 additions & 5 deletions src/lib/objpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ static int objpool_add(struct objpool_head *head, unsigned int n, size_t size, u
pobjpool = sof_heap_alloc(head->heap, flags,
aligned_size + sizeof(*pobjpool), 0);
else if (flags & SOF_MEM_FLAG_COHERENT)
pobjpool = vregion_alloc_coherent(head->vreg,
aligned_size + sizeof(*pobjpool));
pobjpool = vregion_user_alloc_coherent_align(head->vreg,
aligned_size + sizeof(*pobjpool), 0);
else
pobjpool = vregion_alloc(head->vreg,
aligned_size + sizeof(*pobjpool));
pobjpool = vregion_user_alloc_align(head->vreg,
aligned_size + sizeof(*pobjpool), 0);

if (!pobjpool)
return -ENOMEM;
Expand Down Expand Up @@ -164,7 +164,7 @@ void objpool_prune(struct objpool_head *head)

list_item_del(next);
if (head->vreg)
vregion_free(head->vreg, pool);
vregion_user_free(head->vreg, pool);
else
sof_heap_free(head->heap, pool);
}
Expand Down
Loading
Loading