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
27 changes: 21 additions & 6 deletions src/audio/module_adapter/module/generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,13 @@ const void *z_impl_mod_fast_get(struct processing_module *mod, const void * cons
return NULL;
}

ptr = fast_get(res->alloc, dram_ptr, size);
#if CONFIG_SOF_USERSPACE_LL
/* only LL modules use their resources in user mode, see mod_res_user() */
if (k_is_user_context())
ptr = fast_get_ll(dram_ptr, size);
else
#endif
ptr = fast_get(res->alloc, dram_ptr, size);
if (!ptr) {
container_put(mod, container);
mod_res_unlock();
Expand Down Expand Up @@ -525,6 +531,12 @@ static int free_contents(struct processing_module *mod, struct module_resource *
#endif
#if CONFIG_FAST_GET
case MOD_RES_FAST_GET:
#if CONFIG_SOF_USERSPACE_LL
if (k_is_user_context()) {
fast_put_ll(container->sram_ptr);
return 0;
}
#endif
#if CONFIG_USERSPACE
mdom = mod->mdom;
#else
Expand Down Expand Up @@ -678,9 +690,9 @@ EXPORT_SYMBOL(mod_data_blob_set);
#if CONFIG_FAST_GET
#if CONFIG_SOF_USERSPACE_LL
/*
* fast_get() keeps its state in kernel memory, so LL modules in user mode
* get a private copy in the module memory instead of a shared one. The
* copy is a heap resource, so mod_fast_put() frees it with mod_free().
* When the fast_get area of the LL memory region is full, LL modules get a
* private copy in the module memory instead of a shared one. The copy is a
* heap resource, so mod_fast_put() frees it with mod_free().
*/
static const void *mod_fast_get_copy(struct processing_module *mod,
const void * const dram_ptr, size_t size)
Expand All @@ -701,8 +713,11 @@ const void *mod_fast_get(struct processing_module *mod, const void * const dram_
size_t size)
{
#if CONFIG_SOF_USERSPACE_LL
if (mod_res_user(mod))
return mod_fast_get_copy(mod, dram_ptr, size);
if (mod_res_user(mod)) {
const void *ptr = z_impl_mod_fast_get(mod, dram_ptr, size);

return ptr ? ptr : mod_fast_get_copy(mod, dram_ptr, size);
}
#endif

return mod_dp_fast_get(mod, dram_ptr, size);
Expand Down
28 changes: 28 additions & 0 deletions src/include/sof/lib/fast-get.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,29 @@ const void *fast_get(struct mod_alloc_ctx *alloc, const void * const dram_ptr, s
* @param[in,out] sram_ptr Pointer to staged SRAM memory to release.
*/
void fast_put(struct mod_alloc_ctx *alloc, struct k_mem_domain *mdom, const void *sram_ptr);

#if CONFIG_SOF_USERSPACE_LL
/**
* @brief Get a read-only copy of DRAM data for an LL user thread.
*
* The copy is in the fast_get area of the LL memory region and shared by
* all users of the same data. Only threads in the LL memory domain may
* call this.
* @param[in] dram_ptr Pointer to source data resident in DRAM.
* @param[in] size Size in bytes to copy.
* @return Pointer to the cached alias of the copy, or NULL on failure,
* for example when the area is full.
*/
__syscall const void *fast_get_ll(const void *dram_ptr, size_t size);

/**
* @brief Release a copy from fast_get_ll().
* @param[in] sram_ptr Pointer returned by fast_get_ll().
*/
__syscall void fast_put_ll(const void *sram_ptr);

#include <zephyr/syscalls/fast-get.h>
#endif
#else
static inline const void *fast_get(struct mod_alloc_ctx *alloc, const void * const dram_ptr,
size_t size)
Expand All @@ -61,6 +84,11 @@ static inline const void *fast_get(struct mod_alloc_ctx *alloc, const void * con
}
static inline void fast_put(struct mod_alloc_ctx *alloc, struct k_mem_domain *mdom,
const void *sram_ptr) {}
static inline const void *fast_get_ll(const void *dram_ptr, size_t size)
{
return dram_ptr;
}
static inline void fast_put_ll(const void *sram_ptr) {}
#endif

/** @}*/
Expand Down
34 changes: 34 additions & 0 deletions src/include/sof/schedule/ll_schedule_domain.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,40 @@ void *zephyr_ll_karea_alloc(enum zephyr_ll_karea area, uint32_t flags, size_t by
*/
bool zephyr_ll_karea_contains(enum zephyr_ll_karea area, const void *ptr, size_t bytes);

/**
* @brief Allocate from the fast_get area of the LL user memory region.
*
* For the fast_get() copies of the LL modules. The allocator state is
* kernel-only. LL user threads may only read the area, through the cached
* alias. Kernel code writes it through the returned uncached alias, which
* works also in system calls from LL threads. Kernel context only.
*
* @param[in] bytes Size in bytes.
* @return Uncached pointer to the memory, or NULL if the area is full.
*/
void *zephyr_ll_fg_alloc(size_t bytes);

/**
* @brief Free memory from zephyr_ll_fg_alloc(). Kernel context only.
* @param[in] ptr Start of the memory, cached or uncached alias.
* @param[in] bytes Size in bytes, as allocated.
*/
void zephyr_ll_fg_free(void *ptr, size_t bytes);

/**
* @brief Check that memory lies in the fast_get area of the LL region.
* @param[in] ptr Start of the memory, cached or uncached alias.
* @param[in] bytes Size in bytes.
* @return true if [ptr, ptr + bytes) lies in the area.
*/
bool zephyr_ll_fg_contains(const void *ptr, size_t bytes);

/**
* @brief Get the allocated size of the fast_get area. Kernel context only.
* @return Allocated bytes, in whole allocation units.
*/
size_t zephyr_ll_fg_used(void);

/**
* @brief Grant thread access to per-core Low-Latency mutexes.
*
Expand Down
26 changes: 20 additions & 6 deletions src/include/sof/userspace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ syscall by subsystem:
| Scheduling | `zephyr_ll_task_sem_alloc`, `zephyr_ll_task_sem_free`, `scheduler_dp_internal_free`, `scheduler_dp_ll_tick`, `scheduler_dp_kick` | `sof/schedule/ll_schedule_domain.h`, `sof/schedule/dp_schedule.h` | scattered: `kernel/schedule/zephyr_ll-kernel.c`, `kernel/schedule/zephyr_dp_schedule*-kernel.c` |
| Debug | `debug_stream_slot_send_record` | `user/debug_stream_slot.h` | `kernel/debug/debug_stream/debug_stream_slot.c` |
| KPB | `kpb_notifier_schedule` | `sof/audio/kpb.h` | `zephyr/syscall/kpb.c` / `audio/kpb.c` |
| fast_get | `fast_get_ll`, `fast_put_ll` | `sof/lib/fast-get.h` | scattered: `zephyr/lib/fast-get.c` |

### Allowed callers

Expand Down Expand Up @@ -156,8 +157,9 @@ syscalls or to vregion, DMA, DAI or scheduler syscalls.
Modules call the module resource functions without the `dp` prefix (`mod_alloc_ext()`,
`mod_free()`, `mod_fast_get()` and so on), which are plain functions. For an LL module in the
LL or IPC user thread they run in user mode, on the module's resource pool and the LL user
heap, under a `sys_mutex` in LL memory. `mod_fast_get()` then makes a private copy, as the
shared copies are kernel state. For DP modules, and in kernel code, they call the `mod_dp_*`
heap, under a `sys_mutex` in LL memory. `mod_fast_get()` then gets a shared read-only copy
with the `fast_get_ll()` syscall, see below, or makes a private copy when the fast_get area is
full. For DP modules, and in kernel code, they call the `mod_dp_*`
syscalls. Those validators reject LL modules and DP modules whose allocation context has no
live vregion, and pin the vregion during the call. They still read the context from DP memory,
so a DP thread that rewrites it during the call is not stopped.
Expand Down Expand Up @@ -186,6 +188,7 @@ With `CONFIG_SOF_USERSPACE_LL`, the memory of the LL and IPC user threads is one
| Part | Mapping in the LL memory domain | Allocator |
|------|---------------------------------|-----------|
| Kernel RO area (`CONFIG_SOF_ZEPHYR_LL_KAREA_RO_SIZE`) | read-only | `zephyr_ll_karea_alloc(ZEPHYR_LL_KAREA_RO, ...)` |
| fast_get area (`CONFIG_SOF_ZEPHYR_LL_FG_AREA_SIZE`) | read-only, cached alias only | `zephyr_ll_fg_alloc()`, `zephyr_ll_fg_free()` |
| Kernel RW area (`CONFIG_SOF_ZEPHYR_LL_KAREA_RW_SIZE`) | read-write | `zephyr_ll_karea_alloc(ZEPHYR_LL_KAREA_RW, ...)` |
| LL user heap (the rest) | read-write | `sof_heap_alloc(sof_sys_user_heap_get(), ...)`, in user mode only |

Expand Down Expand Up @@ -237,10 +240,21 @@ 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.

Large `fast_get()` copies for DP modules are taken from `vpage_alloc()` instead of the heap of
the module allocation context, which is the LL user heap, and mapped read-only into the domain
of each user. Kernel code still follows some pointers stored in user-writable memory, for
example `task->sch` and the domain `ops`.
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
memory, and the entries of the copies are in kernel memory too, marked as LL copies, which
`fast_get()` and `fast_put()` never use. Only the cached alias of the area is mapped into the
LL memory domain, so kernel code writes a copy through the uncached alias, also in the
syscall, and invalidates the cached alias on the calling core at every get, as the core may
still hold lines of an earlier copy at the same address. LL threads cannot change a shared
copy. A user thread can release copies that other LL modules use, which only affects LL
modules.

DP modules keep their copies in their own memory: small copies in the module vregion, and
large copies from `vpage_alloc()` instead of the heap of the module allocation context, which
is the LL user heap, mapped read-only into the domain of each user. Kernel code still follows
some pointers stored in user-writable memory, for example `task->sch` and the domain `ops`.

## Memory-partition markers

Expand Down
131 changes: 123 additions & 8 deletions src/kernel/schedule/zephyr_ll_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <zephyr/cache.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/bitarray.h>

#include <rtos/alloc.h>
#include <rtos/cache.h>
Expand All @@ -18,7 +19,7 @@ LOG_MODULE_DECLARE(ll_schedule, CONFIG_SOF_LOG_LEVEL);
/*
* Layout of the LL user memory region, each part page aligned:
*
* | kernel RO area | kernel RW area | LL user heap |
* | kernel RO area | fast_get area | kernel RW area | LL user heap |
*
* The heap and the kernel RW area are mapped read-write into the LL memory
* domain, the kernel RO area read-only. Kernel code allocates objects that
Expand All @@ -27,17 +28,37 @@ LOG_MODULE_DECLARE(ll_schedule, CONFIG_SOF_LOG_LEVEL);
* ll_mem_resources, which is kernel-only. The heap allocator runs in user
* mode, see src/schedule/zephyr_ll_user_heap.c: the kernel initialises the
* heap at boot and does not parse its metadata after that.
*
* The fast_get area holds the fast_get() copies of the LL modules, see
* zephyr_ll_fg_alloc(). Only its cached alias is mapped into the LL memory
* domain, read-only, so kernel code can write copies through the uncached
* alias also in system calls from LL threads.
*/
#define LL_REGION_SIZE CONFIG_SOF_ZEPHYR_SYS_USER_HEAP_SIZE
#define LL_KAREA_RO_SIZE CONFIG_SOF_ZEPHYR_LL_KAREA_RO_SIZE
#define LL_KAREA_RW_SIZE CONFIG_SOF_ZEPHYR_LL_KAREA_RW_SIZE
#define LL_HEAP_SIZE (LL_REGION_SIZE - LL_KAREA_RO_SIZE - LL_KAREA_RW_SIZE)
#define LL_FG_SIZE CONFIG_SOF_ZEPHYR_LL_FG_AREA_SIZE
#define LL_HEAP_SIZE (LL_REGION_SIZE - LL_KAREA_RO_SIZE - LL_FG_SIZE - \
LL_KAREA_RW_SIZE)

/* fast_get area allocation unit, a multiple of the cache line size */
#define LL_FG_BLOCK_SIZE 256
#define LL_FG_BLOCKS (LL_FG_SIZE / LL_FG_BLOCK_SIZE)

BUILD_ASSERT(LL_REGION_SIZE % CONFIG_MM_DRV_PAGE_SIZE == 0);
BUILD_ASSERT(LL_KAREA_RO_SIZE % CONFIG_MM_DRV_PAGE_SIZE == 0);
BUILD_ASSERT(LL_KAREA_RW_SIZE % CONFIG_MM_DRV_PAGE_SIZE == 0);
BUILD_ASSERT(LL_KAREA_RO_SIZE + LL_KAREA_RW_SIZE < LL_REGION_SIZE,
BUILD_ASSERT(LL_FG_SIZE % CONFIG_MM_DRV_PAGE_SIZE == 0);
BUILD_ASSERT(LL_FG_BLOCK_SIZE % PLATFORM_DCACHE_ALIGN == 0);
BUILD_ASSERT(LL_KAREA_RO_SIZE + LL_FG_SIZE + LL_KAREA_RW_SIZE < LL_REGION_SIZE,
"kernel areas do not leave room for the LL user heap");
BUILD_ASSERT(!LL_FG_SIZE || IS_ENABLED(CONFIG_CACHE_HAS_MIRRORED_MEMORY_REGIONS),
"the kernel writes the fast_get area through the uncached alias");

#if LL_FG_SIZE
/* allocation bitmap of the fast_get area, a bit per block */
SYS_BITARRAY_DEFINE_STATIC(ll_fg_blocks, LL_FG_BLOCKS);
#endif

/* A kernel-managed area, addresses are the uncached alias */
struct zephyr_ll_karea_state {
Expand All @@ -61,11 +82,12 @@ struct zephyr_ll_mem_resources {
*/
struct k_heap heap;
struct zephyr_ll_karea_state karea[ZEPHYR_LL_KAREA_COUNT]; /**< Kernel-managed areas */
uint8_t *fg_base; /**< fast_get area, uncached alias */
};

static struct zephyr_ll_mem_resources ll_mem_resources;

static void ll_region_map(uint8_t *start, size_t size, k_mem_partition_attr_t attr)
static void ll_region_map_cached(uint8_t *start, size_t size, k_mem_partition_attr_t attr)
{
struct k_mem_partition part;
int ret;
Expand All @@ -78,9 +100,18 @@ static void ll_region_map(uint8_t *start, size_t size, k_mem_partition_attr_t at
tr_dbg(&ll_tr, "map %p size %#zx (cached), ret %d", (void *)part.start, size, ret);
if (ret)
k_panic();
}

static void ll_region_map(uint8_t *start, size_t size, k_mem_partition_attr_t attr)
{
ll_region_map_cached(start, size, attr);

#ifdef CONFIG_CACHE_HAS_MIRRORED_MEMORY_REGIONS
struct k_mem_partition part;
int ret;

part.start = (uintptr_t)sys_cache_uncached_ptr_get(start);
part.size = size;
part.attr = attr;

ret = k_mem_domain_add_partition(&ll_mem_resources.mem_domain, &part);
Expand All @@ -105,21 +136,32 @@ static void ll_region_init(void)

ro->base = mem;
ro->size = LL_KAREA_RO_SIZE;
rw->base = mem + LL_KAREA_RO_SIZE;
ll_mem_resources.fg_base = mem + LL_KAREA_RO_SIZE;
rw->base = ll_mem_resources.fg_base + LL_FG_SIZE;
rw->size = LL_KAREA_RW_SIZE;

mem += LL_KAREA_RO_SIZE + LL_KAREA_RW_SIZE;
mem = rw->base + LL_KAREA_RW_SIZE;
/* the only time the kernel writes the heap metadata */
zephyr_ll_user_heap_init(mem, LL_HEAP_SIZE);

if (ro->size)
ll_region_map(ro->base, ro->size, K_MEM_PARTITION_P_RO_U_RO);

if (LL_FG_SIZE) {
void __sparse_cache *fg = sys_cache_cached_ptr_get(ll_mem_resources.fg_base);

/* no dirty lines may be written back over the copies */
sys_cache_data_flush_and_invd_range((__sparse_force void *)fg, LL_FG_SIZE);
ll_region_map_cached(ll_mem_resources.fg_base, LL_FG_SIZE,
K_MEM_PARTITION_P_RO_U_RO);
}

/* the kernel RW area and the heap are adjacent, map them together */
ll_region_map(rw->base, rw->size + LL_HEAP_SIZE, K_MEM_PARTITION_P_RW_U_RW);

tr_info(&ll_tr, "LL region: RO area %p %#zx, RW area %p %#zx, heap %p %#zx",
(void *)ro->base, ro->size, (void *)rw->base, rw->size, (void *)mem,
tr_info(&ll_tr, "LL region: RO area %p %#zx, fast_get area %p %#zx, RW area %p %#zx, heap %p %#zx",
(void *)ro->base, ro->size, (void *)ll_mem_resources.fg_base,
(size_t)LL_FG_SIZE, (void *)rw->base, rw->size, (void *)mem,
(size_t)LL_HEAP_SIZE);
}

Expand Down Expand Up @@ -190,6 +232,79 @@ bool zephyr_ll_karea_contains(enum zephyr_ll_karea area, const void *ptr, size_t
start - (uintptr_t)ka->base <= ka->size - bytes;
}

bool zephyr_ll_fg_contains(const void *ptr, size_t bytes)
{
uintptr_t base = (uintptr_t)ll_mem_resources.fg_base;
uintptr_t start;

start = (uintptr_t)sys_cache_uncached_ptr_get((__sparse_force void __sparse_cache *)ptr);

return LL_FG_SIZE && start >= base && bytes <= LL_FG_SIZE &&
start - base <= LL_FG_SIZE - bytes;
}

#if LL_FG_SIZE
void *zephyr_ll_fg_alloc(size_t bytes)
{
size_t offset;
int ret;

__ASSERT(!k_is_user_context(), "kernel context only");

if (!bytes || bytes > LL_FG_SIZE)
return NULL;

ret = sys_bitarray_alloc(&ll_fg_blocks, DIV_ROUND_UP(bytes, LL_FG_BLOCK_SIZE), &offset);
if (ret) {
tr_warn(&ll_tr, "fast_get area full: %zu bytes requested, %zu of %zu used", bytes,
zephyr_ll_fg_used(), (size_t)LL_FG_SIZE);
return NULL;
}

return ll_mem_resources.fg_base + offset * LL_FG_BLOCK_SIZE;
}

void zephyr_ll_fg_free(void *ptr, size_t bytes)
{
uintptr_t start;
int ret = -EINVAL;

__ASSERT(!k_is_user_context(), "kernel context only");

start = (uintptr_t)sys_cache_uncached_ptr_get((__sparse_force void __sparse_cache *)ptr);
start -= (uintptr_t)ll_mem_resources.fg_base;

if (bytes && zephyr_ll_fg_contains(ptr, bytes) && !(start % LL_FG_BLOCK_SIZE))
ret = sys_bitarray_free(&ll_fg_blocks, DIV_ROUND_UP(bytes, LL_FG_BLOCK_SIZE),
start / LL_FG_BLOCK_SIZE);
if (ret)
tr_err(&ll_tr, "bad fast_get area free of %zu bytes at %p: %d", bytes, ptr, ret);
}

size_t zephyr_ll_fg_used(void)
{
uint32_t count = 0;

sys_bitarray_popcount_region(&ll_fg_blocks, LL_FG_BLOCKS, 0, &count);

return (size_t)count * LL_FG_BLOCK_SIZE;
}
#else
void *zephyr_ll_fg_alloc(size_t bytes)
{
return NULL;
}

void zephyr_ll_fg_free(void *ptr, size_t bytes)
{
}

size_t zephyr_ll_fg_used(void)
{
return 0;
}
#endif

void zephyr_ll_user_resources_init(void)
{
int ret;
Expand Down
Loading
Loading