Skip to content
Merged
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: 1 addition & 0 deletions app/boards/intel_adsp_ace15_mtpm.conf
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ CONFIG_WATCHDOG=y
CONFIG_TIMESLICE_PER_THREAD=y
CONFIG_THREAD_RUNTIME_STATS=y
CONFIG_SCHED_THREAD_USAGE=y
CONFIG_MM_DRV_INTEL_VIRTUAL_REGION_COUNT=2

# Zephyr / device drivers
CONFIG_CLOCK_CONTROL=y
Expand Down
1 change: 1 addition & 0 deletions app/boards/intel_adsp_ace20_lnl.conf
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ CONFIG_LLEXT_STORAGE_WRITABLE=y
CONFIG_LLEXT_EXPERIMENTAL=y
CONFIG_MODULES=y
CONFIG_TIMING_FUNCTIONS=y
CONFIG_MM_DRV_INTEL_VIRTUAL_REGION_COUNT=2

# Zephyr / device drivers
CONFIG_CLOCK_CONTROL=y
Expand Down
1 change: 1 addition & 0 deletions app/boards/intel_adsp_ace30_ptl.conf
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ CONFIG_LLEXT=y
CONFIG_LLEXT_STORAGE_WRITABLE=y
CONFIG_LLEXT_EXPERIMENTAL=y
CONFIG_MODULES=y
CONFIG_MM_DRV_INTEL_VIRTUAL_REGION_COUNT=2

# Zephyr / device drivers
CONFIG_CLOCK_CONTROL=y
Expand Down
1 change: 1 addition & 0 deletions app/boards/intel_adsp_ace30_wcl.conf
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ CONFIG_LLEXT=y
CONFIG_LLEXT_STORAGE_WRITABLE=y
CONFIG_LLEXT_EXPERIMENTAL=y
CONFIG_MODULES=y
CONFIG_MM_DRV_INTEL_VIRTUAL_REGION_COUNT=2

# Zephyr / device drivers
CONFIG_CLOCK_CONTROL=y
Expand Down
6 changes: 6 additions & 0 deletions src/library_manager/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,10 @@ config LIBRARY_BASE_ADDRESS
automatically but the beginning of that area is platform-specific and
should be set by this option.

config LIBRARY_REGION_SIZE
hex "Size of memory region dedicated to loadable modules"
default 0x100000
help
Size of the virtual memory region dedicated for loadable modules

endmenu
51 changes: 42 additions & 9 deletions src/library_manager/llext_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <rtos/spinlock.h>
#include <sof/lib/cpu-clk-manager.h>
#include <sof/lib_manager.h>
#include <sof/lib/regions_mm.h>
#include <sof/llext_manager.h>
#include <sof/audio/module_adapter/module/generic.h>
#include <sof/audio/module_adapter/module/modules.h>
Expand All @@ -35,6 +36,7 @@
#include <rimage/sof/user/manifest.h>
#include <module/module/api_ver.h>

#include <adsp_memory_regions.h>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the west update needs to come before this, or bisect is broken.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so it does...
image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the west update needs to come before this, or bisect is broken.

@kv2019i I think it's the opposite - moving to the new Zephyr option without updating SOF to it seems to break bisection to me #10124 (comment)

#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
Expand All @@ -55,13 +57,13 @@ static int llext_manager_update_flags(void __sparse_cache *vma, size_t size, uin
ALIGN_UP(pre_pad_size + size, PAGE_SZ), flags);
}

static int llext_manager_align_map(void __sparse_cache *vma, size_t size, uint32_t flags)
static int llext_manager_align_map(const struct sys_mm_drv_region *virtual_region,
void __sparse_cache *vma, size_t size, uint32_t flags)
{
size_t pre_pad_size = (uintptr_t)vma & (PAGE_SZ - 1);
void *aligned_vma = (__sparse_force uint8_t *)vma - pre_pad_size;

return sys_mm_drv_map_region(aligned_vma, POINTER_TO_UINT(NULL),
ALIGN_UP(pre_pad_size + size, PAGE_SZ), flags);
return sys_mm_drv_map_region_safe(virtual_region, aligned_vma, POINTER_TO_UINT(NULL),
ALIGN_UP(pre_pad_size + size, PAGE_SZ), flags);
}

static int llext_manager_align_unmap(void __sparse_cache *vma, size_t size)
Expand Down Expand Up @@ -91,15 +93,21 @@ static void llext_manager_detached_update_flags(void __sparse_cache *vma,
* sections that belong to the specified 'region' and are contained in the
* memory range, then remap the same area according to the 'flags' parameter.
*/
static int llext_manager_load_data_from_storage(const struct llext_loader *ldr,
static int llext_manager_load_data_from_storage(const struct sys_mm_drv_region *virtual_region,
const struct llext_loader *ldr,
const struct llext *ext,
enum llext_mem region,
void __sparse_cache *vma,
size_t size, uint32_t flags)
{
unsigned int i;
const void *region_addr;
int ret = llext_manager_align_map(vma, size, SYS_MM_MEM_PERM_RW);

/* check if there region to be mapped exists */
if (size == 0)
return 0;

int ret = llext_manager_align_map(virtual_region, vma, size, SYS_MM_MEM_PERM_RW);

if (ret < 0) {
tr_err(&lib_manager_tr, "cannot map %u of %p", size, (__sparse_force void *)vma);
Expand Down Expand Up @@ -238,14 +246,25 @@ static int llext_manager_load_module(struct lib_manager_module *mctx)
const struct llext_loader *ldr = &mctx->ebl->loader;
const struct llext *ext = mctx->llext;

/* find dedicated virtual memory zone */
const struct sys_mm_drv_region *virtual_memory_regions = sys_mm_drv_query_memory_regions();
const struct sys_mm_drv_region *virtual_region;

SYS_MM_DRV_MEMORY_REGION_FOREACH(virtual_memory_regions, virtual_region) {
if (virtual_region->attr == VIRTUAL_REGION_LLEXT_LIBRARIES_ATTR)
break;
}
if (!virtual_region || !virtual_region->size)
return -EFAULT;

/* Copy Code */
ret = llext_manager_load_data_from_storage(ldr, ext, LLEXT_MEM_TEXT,
ret = llext_manager_load_data_from_storage(virtual_region, ldr, ext, LLEXT_MEM_TEXT,
va_base_text, text_size, SYS_MM_MEM_PERM_EXEC);
if (ret < 0)
return ret;

/* Copy read-only data */
ret = llext_manager_load_data_from_storage(ldr, ext, LLEXT_MEM_RODATA,
ret = llext_manager_load_data_from_storage(virtual_region, ldr, ext, LLEXT_MEM_RODATA,
va_base_rodata, rodata_size, 0);
if (ret < 0)
goto e_text;
Expand All @@ -256,7 +275,7 @@ static int llext_manager_load_module(struct lib_manager_module *mctx)
* spans over the BSS area as well, so the mapping will cover
* both, but only LLEXT_MEM_DATA sections will be copied.
*/
ret = llext_manager_load_data_from_storage(ldr, ext, LLEXT_MEM_DATA,
ret = llext_manager_load_data_from_storage(virtual_region, ldr, ext, LLEXT_MEM_DATA,
va_base_data, data_size, SYS_MM_MEM_PERM_RW);
if (ret < 0)
goto e_rodata;
Expand Down Expand Up @@ -806,3 +825,17 @@ bool comp_is_llext(struct comp_dev *comp)

return mod && module_is_llext(mod);
}

static int llext_memory_region_init(void)
{
int ret;

/* add a region for loadable libraries */
ret = adsp_add_virtual_memory_region(CONFIG_LIBRARY_BASE_ADDRESS,
CONFIG_LIBRARY_REGION_SIZE,
VIRTUAL_REGION_LLEXT_LIBRARIES_ATTR);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return adsp_add_virtual_memory_region(...);;-)


return ret;
}

SYS_INIT(llext_memory_region_init, POST_KERNEL, 1);
2 changes: 1 addition & 1 deletion west.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ manifest:

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could have the usual commit style for west.yml updates.

- name: zephyr
repo-path: zephyr
revision: c99605126cd9cce5684ddd9ad56aed5292004867
revision: 8843fd9fe1800164fbb5221d95d1eba7d0699458
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this breaking bisection? If so, it should be merged with the changes that switch SOF over to the new API

remote: zephyrproject

# Import some projects listed in zephyr/west.yml@revision
Expand Down
1 change: 1 addition & 0 deletions zephyr/include/sof/lib/regions_mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

/* Attributes for memory regions */
#define VIRTUAL_REGION_SHARED_HEAP_ATTR 1U /*< region dedicated for shared virtual heap */
#define VIRTUAL_REGION_LLEXT_LIBRARIES_ATTR 2U /*< region dedicated for LLEXT libraries */

/* Dependency on ipc/topology.h created due to memory capability definitions
* that are defined there
Expand Down
19 changes: 0 additions & 19 deletions zephyr/lib/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,25 +289,6 @@ static const struct vmh_heap_config static_hp_buffers = {
},
};

/* WA Stubs begin
*
* in order to merge a PR that moves initialization of virtual regions from Zephyr to SOF,
* we need to create weak stubs for 2 functions that will need to be called once the PR is merged
*/

__weak
uintptr_t adsp_mm_get_unused_l2_start_aligned(void)
{
return 0;
}

__weak
int adsp_add_virtual_memory_region(uintptr_t region_address, uint32_t region_size, uint32_t attr)
{
return 0;
}
/* WA Stubs end */

static int virtual_heap_init(void)
{
int ret;
Expand Down
26 changes: 16 additions & 10 deletions zephyr/lib/regions_mm.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ struct vmh_heap {
*/
struct vmh_heap *vmh_init_heap(const struct vmh_heap_config *cfg, bool allocating_continuously)
{
const struct sys_mm_drv_region *virtual_memory_regions =
sys_mm_drv_query_memory_regions();
int i;

struct vmh_heap *new_heap =
rzalloc(SOF_MEM_FLAG_KERNEL | SOF_MEM_FLAG_COHERENT, sizeof(*new_heap));

Expand All @@ -58,13 +61,14 @@ struct vmh_heap *vmh_init_heap(const struct vmh_heap_config *cfg, bool allocatin

k_mutex_init(&new_heap->lock);
struct vmh_heap_config new_config = {0};
const struct sys_mm_drv_region *region;

/* Workaround - use the very first virtual memory region because of virt addresses
* collision.
* Fix will be provided ASAP, but removing MEM_REG_ATTR_SHARED_HEAP from SOF is required
* to merge Zephyr changes
*/
new_heap->virtual_region = sys_mm_drv_query_memory_regions();
SYS_MM_DRV_MEMORY_REGION_FOREACH(virtual_memory_regions, region) {
if (region->attr == VIRTUAL_REGION_SHARED_HEAP_ATTR) {
new_heap->virtual_region = region;
break;
}
}
if (!new_heap->virtual_region || !new_heap->virtual_region->size)
goto fail;

Expand Down Expand Up @@ -323,7 +327,8 @@ static void vmh_get_mapped_size(void *addr, size_t *size)
*
* @retval 0 on success, error code otherwise.
*/
static int vmh_map_region(struct sys_mem_blocks *region, void *ptr, size_t size)
static int vmh_map_region(const struct sys_mm_drv_region *virtual_region,
struct sys_mem_blocks *region, void *ptr, size_t size)
{
const size_t block_size = 1 << region->info.blk_sz_shift;
uintptr_t begin;
Expand All @@ -336,8 +341,8 @@ static int vmh_map_region(struct sys_mem_blocks *region, void *ptr, size_t size)
if (!vmh_get_map_region_boundaries(region, ptr, size, &begin, &size))
return 0;
}

ret = sys_mm_drv_map_region(UINT_TO_POINTER(begin), 0, size, SYS_MM_MEM_PERM_RW);
ret = sys_mm_drv_map_region_safe(virtual_region, UINT_TO_POINTER(begin), 0, size,
SYS_MM_MEM_PERM_RW);

/* In case of an error, the pages that were successfully mapped must be manually released */
if (ret)
Expand Down Expand Up @@ -491,7 +496,8 @@ static void *_vmh_alloc(struct vmh_heap *heap, uint32_t alloc_size)
if (!ptr)
return NULL;

allocation_error_code = vmh_map_region(heap->physical_blocks_allocators[mem_block_iterator],
allocation_error_code = vmh_map_region(heap->virtual_region,
heap->physical_blocks_allocators[mem_block_iterator],
ptr, alloc_size);
if (allocation_error_code) {
sys_mem_blocks_free_contiguous(heap->physical_blocks_allocators[mem_block_iterator],
Expand Down
Loading