Skip to content
Draft
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
1 change: 0 additions & 1 deletion app/boards/intel_adsp_ace40_nvls.conf
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ CONFIG_LLEXT_STORAGE_WRITABLE=y
CONFIG_LLEXT_EXPERIMENTAL=y
CONFIG_LLEXT_EDK=n
CONFIG_MODULES=y
CONFIG_VIRTUAL_HEAP=n

# Zephyr / device drivers
CONFIG_DAI_INIT_PRIORITY=70
Expand Down
1 change: 0 additions & 1 deletion app/ll_userspace_overlay.conf
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ CONFIG_XTENSA_MMU_NUM_L2_TABLES=70
# via vregions.
CONFIG_SOF_ZEPHYR_SYS_USER_HEAP_SIZE=0x40000
CONFIG_SOF_ZEPHYR_HEAP_SIZE=0x60000
CONFIG_VIRTUAL_HEAP=n

# e.g. sof-ptl-nocodec.tplg requires more than 64 partitions for DP
# (vregion is created for each pipeline)
Expand Down
8 changes: 8 additions & 0 deletions src/kernel/include/sof/kernel/lib/vpage.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ void *vpage_alloc(unsigned int pages);
*/
void vpage_free(void *ptr);

/**
* @brief Check whether memory is in the vpage region.
*
* @param[in] ptr Cached or uncached pointer to check.
* @return True if ptr is in the vpage region, false otherwise.
*/
bool vpage_is_ptr(void *ptr);

/** @}*/

#ifdef __cplusplus
Expand Down
18 changes: 16 additions & 2 deletions zephyr/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ config SOF_NATIVE_SIM_HOST_HEAP
config SOF_ZEPHYR_HEAP_SIZE
hex "Size of the Zephyr heap for SOF"
default 0x40000 if VIRTUAL_HEAP
default 0x60000 if SOF_VREGIONS
default 0xF0000 if SOC_ACE15_MTPM || SOC_ACE20_LNL
default 0x100000 if SOC_ACE30 || SOC_ACE40
default 0x0
Expand Down Expand Up @@ -260,6 +261,20 @@ config SOF_VPAGE_MAX_ALLOCS
this number allows for more simultaneous allocations, but also
increases the memory overhead for tracking these allocations.

config SOF_VPAGE_LLEXT_SIZE
hex "Physical L2 kept out of the vpage region for LLEXT modules"
depends on SOF_VREGIONS
default 0x0 if !LIBRARY_MANAGER
default 0x40000
help
Size of the physical L2 memory that the virtual page allocator leaves
free for the pages of loadable (LLEXT) modules. The library region
itself is virtual address space at LIBRARY_BASE_ADDRESS, but its
modules are backed by the same physical L2 pages as the vpages.
Builds without the library manager need no reserve. The loadable
modules of the default Intel ACE builds need about 110kB, all ACE
platforms keep 256kB, and the vpages get the rest.

config ZEPHYR_NATIVE_DRIVERS
bool "Use Zephyr native drivers"
help
Expand Down Expand Up @@ -371,7 +386,7 @@ config SOF_ZEPHYR_NO_SOF_CLOCK

config SOF_VREGIONS
bool "Enable virtual memory regions"
default y if ACE && !ACE_VERSION_1_5 && !ACE_VERSION_2_0
default y if ACE
depends on ACE
help
Enable the virtual regions memory allocator for pipeline resource management.
Expand All @@ -382,7 +397,6 @@ config SOF_VREGIONS

config VIRTUAL_HEAP
bool "Use virtual memory heap to allocate buffers"
default y if ACE
depends on ACE
help
Enabling this option will use the virtual memory heap allocator to allocate buffers.
Expand Down
1,121 changes: 1,121 additions & 0 deletions zephyr/MemoryMap.md

Large diffs are not rendered by default.

67 changes: 65 additions & 2 deletions zephyr/lib/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ struct vmh_heap;
struct vmh_heap *virtual_buffers_heap;
#endif /* CONFIG_VIRTUAL_HEAP */

#if CONFIG_SOF_VREGIONS
#include <sof/kernel/lib/vpage.h>
#include <sof/lib/cold.h>
#endif


/* Zephyr includes */
#include <zephyr/init.h>
Expand Down Expand Up @@ -432,6 +437,41 @@ SYS_INIT(virtual_heap_init, POST_KERNEL, 1);

#endif /* CONFIG_VIRTUAL_HEAP */

#if CONFIG_SOF_VREGIONS
/*
* Back a system heap allocation that did not fit with whole vpages. This
* takes the place of the virtual heap fallback for allocations that are
* not in a pipeline vregion, e.g. the KPB history buffer.
*/
static void *vpage_heap_alloc(uint32_t flags, size_t bytes, uint32_t align)
{
void *mem;

/* vpages are only page aligned, and are cold code for the LL thread */
if (align > CONFIG_MM_DRV_PAGE_SIZE || ll_sch_is_current())
return NULL;

mem = vpage_alloc(DIV_ROUND_UP(bytes, CONFIG_MM_DRV_PAGE_SIZE));
if (!mem)
return NULL;

tr_warn(&zephyr_tr, "system heap allocation of %zu bytes backed by vpages", bytes);

if (flags & SOF_MEM_FLAG_COHERENT)
mem = sys_cache_uncached_ptr_get((__sparse_force void __sparse_cache *)mem);

return mem;
}

static void vpage_heap_free(void *ptr)
{
if (sys_cache_is_ptr_uncached(ptr))
ptr = (__sparse_force void *)sys_cache_cached_ptr_get(ptr);

vpage_free(ptr);
}
#endif /* CONFIG_SOF_VREGIONS */

#if CONFIG_SOF_FULL_ZEPHYR_APPLICATION
/* unit-test builds get a static inline NULL stub from rtos/alloc.h */
struct k_heap *sof_sys_heap_get(void)
Expand Down Expand Up @@ -701,6 +741,10 @@ void *rmalloc_align(uint32_t flags, size_t bytes, uint32_t alignment)
ptr = virtual_heap_alloc(virtual_buffers_heap, flags, bytes, alignment);
}
#endif
#if CONFIG_SOF_VREGIONS
if (!ptr && heap == &sof_heap)
ptr = vpage_heap_alloc(flags, bytes, alignment);
#endif

return ptr;
}
Expand Down Expand Up @@ -766,6 +810,7 @@ EXPORT_SYMBOL(rzalloc);
void *rballoc_align(uint32_t flags, size_t bytes, uint32_t align)
{
struct k_heap *heap;
void *ptr;

/* choose a heap */
if (flags & SOF_MEM_FLAG_L3) {
Expand All @@ -791,9 +836,16 @@ void *rballoc_align(uint32_t flags, size_t bytes, uint32_t align)
}

if (flags & SOF_MEM_FLAG_COHERENT)
return heap_alloc_aligned(heap, align, bytes);
ptr = heap_alloc_aligned(heap, align, bytes);
else
ptr = (__sparse_force void *)heap_alloc_aligned_cached(heap, align, bytes);

return (__sparse_force void *)heap_alloc_aligned_cached(heap, align, bytes);
#if CONFIG_SOF_VREGIONS
if (!ptr && heap == &sof_heap)
ptr = vpage_heap_alloc(flags, bytes, align);
#endif

return ptr;
}
EXPORT_SYMBOL(rballoc_align);

Expand Down Expand Up @@ -826,6 +878,13 @@ void rfree(void *ptr)
}
#endif

#if CONFIG_SOF_VREGIONS
if (vpage_is_ptr(ptr)) {
vpage_heap_free(ptr);
return;
}
#endif

heap_free(&sof_heap, ptr);
}
EXPORT_SYMBOL(rfree);
Expand Down Expand Up @@ -904,6 +963,10 @@ static void *sof_heap_alloc_impl(struct k_heap *heap, uint32_t flags, size_t byt
ptr = virtual_heap_alloc(virtual_buffers_heap, flags, bytes, alignment);
}
#endif
#if CONFIG_SOF_VREGIONS
if (!ptr && heap == &sof_heap)
ptr = vpage_heap_alloc(flags, bytes, alignment);
#endif

return ptr;
}
Expand Down
62 changes: 36 additions & 26 deletions zephyr/lib/vpage.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,22 @@ void vpage_free(void *ptr)
}
}

bool vpage_is_ptr(void *ptr)
{
uintptr_t start;

if (!vpage_ctx.virtual_region)
return false;

if (sys_cache_is_ptr_uncached(ptr))
ptr = (__sparse_force void *)sys_cache_cached_ptr_get(ptr);

start = POINTER_TO_UINT(vpage_ctx.virtual_region->addr);

return POINTER_TO_UINT(ptr) >= start &&
POINTER_TO_UINT(ptr) < start + vpage_ctx.virtual_region->size;
}

#ifdef CONFIG_SOF_TELEMETRY_VREGION
void telemetry_collect_vpage_stats(struct sof_tel_vregion_section *vsec)
{
Expand Down Expand Up @@ -341,31 +357,23 @@ __cold static int vpage_init(void)
uintptr_t l2_end = L2_SRAM_BASE + L2_SRAM_SIZE;
uintptr_t start = adsp_mm_get_unused_l2_start_aligned() +
CONFIG_SOF_ZEPHYR_VIRTUAL_HEAP_REGION_SIZE;
uintptr_t end;
uintptr_t end = L2_VIRTUAL_SRAM_BASE + L2_VIRTUAL_SRAM_SIZE;
size_t phys_left;
size_t remaining_ram;

assert_can_be_cold();

k_mutex_init(&vpage_ctx.lock);

if (start + CONFIG_LIBRARY_REGION_SIZE + CONFIG_MM_DRV_PAGE_SIZE <= l2_end) {
/* virtual heap and library regions fit in L2, vpages get the rest */
end = l2_end - CONFIG_LIBRARY_REGION_SIZE;
} else {
/*
* Small L2 (e.g. NVL-S): the virtual heap and library regions are only
* virtual address space backed on demand, so they need not fit in L2
* together with the vpages. Use the virtual space up to the library
* region (or the end of virtual memory) and cap it below by the
* physical L2 left after the image, as vpages can never use more.
* Using pages beyond physical L2 needs the Zephyr TLB driver to map
* the uncached alias too, see
* https://github.com/zephyrproject-rtos/zephyr/pull/122801
*/
end = L2_VIRTUAL_SRAM_BASE + L2_VIRTUAL_SRAM_SIZE;
if (IS_ENABLED(CONFIG_LIBRARY_MANAGER) && CONFIG_LIBRARY_BASE_ADDRESS < end)
end = CONFIG_LIBRARY_BASE_ADDRESS;
}
/*
* The vpages are virtual address space after the image and the virtual
* heap region, up to the library region (or the end of virtual memory).
* Using pages beyond physical L2 needs the Zephyr TLB driver to map
* the uncached alias too, see
* https://github.com/zephyrproject-rtos/zephyr/pull/122801
*/
if (IS_ENABLED(CONFIG_LIBRARY_MANAGER) && CONFIG_LIBRARY_BASE_ADDRESS < end)
end = CONFIG_LIBRARY_BASE_ADDRESS;

/* the image or virtual heap may leave no room, don't let this underflow */
if (end < start + CONFIG_MM_DRV_PAGE_SIZE) {
Expand All @@ -374,13 +382,15 @@ __cold static int vpage_init(void)
return -ENOMEM;
}

remaining_ram = end - start;
if (end > l2_end) {
uintptr_t image_end = adsp_mm_get_unused_l2_start_aligned();
size_t phys_left = image_end < l2_end ? l2_end - image_end : 0;

remaining_ram = MIN(remaining_ram, phys_left);
}
/*
* The vpages, the virtual heap and the loadable modules all map their
* pages from the physical L2 left after the image. The vpages get what
* is left after the virtual heap region and the LLEXT reserve.
*/
phys_left = start < l2_end ? l2_end - start : 0;
phys_left = phys_left > CONFIG_SOF_VPAGE_LLEXT_SIZE ?
phys_left - CONFIG_SOF_VPAGE_LLEXT_SIZE : 0;
remaining_ram = MIN(end - start, phys_left);

/* revalidate, the physical cap can leave less than a page */
if (remaining_ram < CONFIG_MM_DRV_PAGE_SIZE) {
Expand Down