Skip to content

Commit 7026cda

Browse files
committed
lib_manager: modules: Unification of loadable module initialization functions
Move module allocation and system agent call from modules_init to lib_manager_module_create to unify the module loading process. Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
1 parent 446a5d6 commit 7026cda

4 files changed

Lines changed: 87 additions & 120 deletions

File tree

src/audio/module_adapter/module/modules.c

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <sof/audio/module_adapter/module/modules.h>
1010
#include <utilities/array.h>
1111
#include <iadk_module_adapter.h>
12-
#include <system_agent.h>
1312
#include <sof/lib_manager.h>
1413
#include <sof/audio/module_adapter/module/module_interface.h>
1514

@@ -56,46 +55,16 @@ static int modules_init(struct processing_module *mod)
5655
{
5756
struct module_data *md = &mod->priv;
5857
struct comp_dev *dev = mod->dev;
59-
const struct comp_driver *const drv = dev->drv;
6058
const struct ipc4_base_module_cfg *src_cfg = &md->cfg.base_cfg;
61-
const struct comp_ipc_config *config = &dev->ipc_config;
62-
void *adapter;
63-
int ret;
64-
65-
uintptr_t module_entry_point = lib_manager_allocate_module(config, src_cfg);
66-
67-
if (module_entry_point == 0) {
68-
comp_err(dev, "modules_init(), lib_manager_allocate_module() failed!");
69-
return -EINVAL;
70-
}
7159

7260
/* At this point module resources are allocated and it is moved to L2 memory. */
7361
comp_info(dev, "modules_init() start");
7462

75-
const uint32_t module_id = IPC4_MOD_ID(config->id);
76-
const uint32_t instance_id = IPC4_INST_ID(config->id);
77-
const uint32_t log_handle = (uint32_t)drv->tctx;
78-
79-
byte_array_t mod_cfg = {
80-
.data = (uint8_t *)md->cfg.init_data,
81-
/* Intel modules expects DW size here */
82-
.size = md->cfg.size >> 2,
83-
};
84-
85-
ret = system_agent_start(module_entry_point, module_id, instance_id, 0, log_handle,
86-
&mod_cfg, &adapter);
87-
if (ret) {
88-
comp_info(dev, "System agent failed");
89-
return ret;
90-
}
91-
92-
module_set_private_data(mod, adapter);
93-
9463
md->mpd.in_buff_size = src_cfg->ibs;
9564
md->mpd.out_buff_size = src_cfg->obs;
9665

9766
mod->proc_type = MODULE_PROCESS_TYPE_SOURCE_SINK;
98-
return iadk_wrapper_init(adapter);
67+
return iadk_wrapper_init(module_get_private_data(mod));
9968
}
10069

10170
/**

src/include/sof/audio/module_adapter/library/native_system_agent.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include <sof/audio/module_adapter/module/module_interface.h>
1212
#include <native_system_service.h>
1313

14+
typedef int (*system_agent_start_fn)(uintptr_t entry_point, uint32_t module_id,
15+
uint32_t instance_id, uint32_t core_id, uint32_t log_handle,
16+
void *mod_cfg, void **adapter);
17+
1418
struct native_system_agent {
1519
struct system_service system_service;
1620
uint32_t log_handle;

src/include/sof/lib_manager.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -186,21 +186,6 @@ int lib_manager_register_module(const uint32_t component_id);
186186
*/
187187
const struct sof_man_fw_desc *lib_manager_get_library_manifest(int module_id);
188188

189-
struct processing_module;
190-
struct comp_ipc_config;
191-
/*
192-
* \brief Allocate module
193-
*
194-
* param[in] drv - component driver
195-
* param[in] ipc_config - audio component base configuration from IPC at creation
196-
* param[in] ipc_specific_config - ipc4 base configuration
197-
*
198-
* Function is responsible to allocate module in available free memory and assigning proper address.
199-
* (WIP) These feature will contain module validation and proper memory management.
200-
*/
201-
uintptr_t lib_manager_allocate_module(const struct comp_ipc_config *ipc_config,
202-
const void *ipc_specific_config);
203-
204189
/*
205190
* \brief Free module
206191
*

src/library_manager/lib_manager.c

Lines changed: 82 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <sof/audio/module_adapter/module/generic.h>
2525
#include <sof/audio/module_adapter/module/modules.h>
2626
#include <utilities/array.h>
27+
#include <system_agent.h>
2728
#include <native_system_agent.h>
2829
#include <api_version.h>
2930
#include <module/module/api_ver.h>
@@ -142,8 +143,7 @@ static int lib_manager_load_data_from_storage(void __sparse_cache *vma, void *s_
142143
return sys_mm_drv_update_region_flags((__sparse_force void *)vma, size, flags);
143144
}
144145

145-
static int lib_manager_load_module(const uint32_t module_id,
146-
const struct sof_man_module *const mod)
146+
static int lib_manager_load_module(const uint32_t module_id, const struct sof_man_module *const mod)
147147
{
148148
struct lib_manager_mod_ctx *ctx = lib_manager_get_mod_ctx(module_id);
149149
const uintptr_t load_offset = POINTER_TO_UINT(ctx->base_addr);
@@ -325,22 +325,25 @@ static int lib_manager_free_module_instance(uint32_t instance_id, const struct s
325325
return sys_mm_drv_unmap_region((__sparse_force void *)va_base, bss_size);
326326
}
327327

328-
uintptr_t lib_manager_allocate_module(const struct comp_ipc_config *ipc_config,
329-
const void *ipc_specific_config)
328+
/*
329+
* \brief Allocate module
330+
*
331+
* param[in] mod - module manifest
332+
* param[in] ipc_config - audio component base configuration from IPC at creation
333+
* param[in] ipc_specific_config - ipc4 base configuration
334+
*
335+
* Function is responsible to allocate module in available free memory and assigning proper address.
336+
*/
337+
static uintptr_t lib_manager_allocate_module(const struct sof_man_module *mod,
338+
const struct comp_ipc_config *ipc_config,
339+
const void *ipc_specific_config)
330340
{
331-
const struct sof_man_module *mod;
332341
const struct ipc4_base_module_cfg *base_cfg = ipc_specific_config;
333342
const uint32_t module_id = IPC4_MOD_ID(ipc_config->id);
334343
int ret;
335344

336345
tr_dbg(&lib_manager_tr, "mod_id: %#x", ipc_config->id);
337346

338-
mod = lib_manager_get_module_manifest(module_id);
339-
if (!mod) {
340-
tr_err(&lib_manager_tr, "failed to get module descriptor");
341-
return 0;
342-
}
343-
344347
if (module_is_llext(mod))
345348
return llext_manager_allocate_module(ipc_config, ipc_specific_config);
346349

@@ -488,45 +491,94 @@ static struct comp_dev *lib_manager_module_create(const struct comp_driver *drv,
488491
const struct comp_ipc_config *config,
489492
const void *spec)
490493
{
494+
const struct sof_man_fw_desc *const desc = lib_manager_get_library_manifest(config->id);
491495
const struct ipc_config_process *args = (struct ipc_config_process *)spec;
496+
const struct sof_module_api_build_info *build_info;
497+
const uint32_t entry_index = LIB_MANAGER_GET_MODULE_INDEX(config->id);
492498
const uint32_t module_id = IPC4_MOD_ID(config->id);
493499
const uint32_t instance_id = IPC4_INST_ID(config->id);
494500
const uint32_t log_handle = (uint32_t)drv->tctx;
501+
const struct sof_man_module *mod;
502+
system_agent_start_fn agent;
503+
void **agent_iface;
504+
void *adapter_priv = NULL;
495505
byte_array_t mod_cfg;
506+
struct comp_dev *dev;
496507
int ret;
497508

509+
tr_dbg(&lib_manager_tr, "start");
510+
if (!desc) {
511+
tr_err(&lib_manager_tr, "Error: Couldn't find loadable module with id %u.",
512+
config->id);
513+
return NULL;
514+
}
515+
516+
if (entry_index >= desc->header.num_module_entries) {
517+
tr_err(&lib_manager_tr, "Entry index %u out of bounds.", entry_index);
518+
return NULL;
519+
}
520+
521+
mod = (struct sof_man_module *)((const uint8_t *)desc + SOF_MAN_MODULE_OFFSET(entry_index));
522+
498523
/*
499-
* Variable used by llext_manager to temporary store llext handle before creation
500-
* a instance of processing_module.
501-
*/
502-
struct comp_dev *dev;
524+
* llext modules store build info structure in separate section which is not accessible now.
525+
*/
526+
agent = &native_system_agent_start;
527+
agent_iface = (void **)&drv->adapter_ops;
528+
if (!module_is_llext(mod)) {
529+
build_info = (const struct sof_module_api_build_info *)((const char *)desc -
530+
SOF_MAN_ELF_TEXT_OFFSET +
531+
mod->segment[SOF_MAN_SEGMENT_TEXT].file_offset);
503532

504-
/* At this point module resources are allocated and it is moved to L2 memory. */
505-
const uint32_t module_entry_point = lib_manager_allocate_module(config,
506-
args->data);
533+
tr_info(&lib_manager_tr, "Module API version: %u.%u.%u, format: 0x%x",
534+
build_info->api_version_number.fields.major,
535+
build_info->api_version_number.fields.middle,
536+
build_info->api_version_number.fields.minor,
537+
build_info->format);
507538

539+
/* Check if module is IADK */
540+
if (IS_ENABLED(CONFIG_INTEL_MODULES) &&
541+
build_info->format == IADK_MODULE_API_BUILD_INFO_FORMAT &&
542+
build_info->api_version_number.full == IADK_MODULE_API_CURRENT_VERSION) {
543+
/* IADK module */
544+
agent = &system_agent_start;
545+
/* processing_module_adapter_interface is already assigned
546+
* to drv->adapter_ops by the lib_manager_prepare_module_adapter function.
547+
*/
548+
agent_iface = &adapter_priv;
549+
} else {
550+
/* Check if module is NOT native */
551+
if (build_info->format != SOF_MODULE_API_BUILD_INFO_FORMAT ||
552+
build_info->api_version_number.full != SOF_MODULE_API_CURRENT_VERSION) {
553+
tr_err(&lib_manager_tr, "Unsupported module API version");
554+
return NULL;
555+
}
556+
}
557+
}
558+
559+
const uint32_t module_entry_point = lib_manager_allocate_module(mod, config, args->data);
508560
if (!module_entry_point) {
509561
tr_err(&lib_manager_tr, "lib_manager_allocate_module() failed!");
510562
return NULL;
511563
}
512-
tr_dbg(&lib_manager_tr, "start");
564+
565+
/* At this point module resources are allocated and it is moved to L2 memory. */
513566

514567
mod_cfg.data = (uint8_t *)args->data;
515568
/* Intel modules expects DW size here */
516569
mod_cfg.size = args->size >> 2;
517570

518-
ret = native_system_agent_start(module_entry_point, module_id, instance_id, 0, log_handle,
519-
&mod_cfg, (void**)&((struct comp_driver *)drv)->adapter_ops);
520-
571+
ret = agent(module_entry_point, module_id, instance_id, 0, log_handle, &mod_cfg,
572+
agent_iface);
521573
if (ret) {
522-
lib_manager_free_module(module_id);
523-
tr_err(&lib_manager_tr, "native_system_agent_start failed!");
574+
tr_err(&lib_manager_tr, "System agent start failed %d!", ret);
575+
lib_manager_free_module(config->id);
524576
return NULL;
525577
}
526578

527-
dev = module_adapter_new(drv, config, spec);
579+
dev = module_adapter_new_ext(drv, config, spec, adapter_priv);
528580
if (!dev)
529-
lib_manager_free_module(module_id);
581+
lib_manager_free_module(config->id);
530582

531583
return dev;
532584
}
@@ -581,34 +633,26 @@ static void lib_manager_prepare_module_adapter(struct comp_driver *drv, const st
581633

582634
int lib_manager_register_module(const uint32_t component_id)
583635
{
584-
const struct sof_man_fw_desc *const desc = lib_manager_get_library_manifest(component_id);
585-
const uint32_t entry_index = LIB_MANAGER_GET_MODULE_INDEX(component_id);
586-
const struct sof_module_api_build_info *build_info;
587636
struct comp_driver_info *new_drv_info;
588637
struct comp_driver *drv = NULL;
589-
const struct sof_man_module *mod;
638+
const struct sof_man_module *mod = lib_manager_get_module_manifest(component_id);
639+
const struct sof_uuid *uid = (struct sof_uuid *)&mod->uuid;
590640
int ret;
591641

592-
if (!desc) {
642+
if (!mod) {
593643
tr_err(&lib_manager_tr, "Error: Couldn't find loadable module with id %u.",
594644
component_id);
595645
return -ENOENT;
596646
}
597647

598-
if (entry_index >= desc->header.num_module_entries) {
599-
tr_err(&lib_manager_tr, "Entry index %u out of bounds.", entry_index);
600-
return -ENOENT;
601-
}
602-
603648
/* allocate new comp_driver_info */
604649
new_drv_info = rmalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0,
605650
SOF_MEM_CAPS_RAM | SOF_MEM_FLAG_COHERENT,
606651
sizeof(struct comp_driver_info));
607652

608653
if (!new_drv_info) {
609654
tr_err(&lib_manager_tr, "failed to allocate comp_driver_info");
610-
ret = -ENOMEM;
611-
goto cleanup;
655+
return -ENOMEM;
612656
}
613657

614658
drv = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0,
@@ -620,43 +664,8 @@ int lib_manager_register_module(const uint32_t component_id)
620664
goto cleanup;
621665
}
622666

623-
mod = (const struct sof_man_module *)((const uint8_t *)desc +
624-
SOF_MAN_MODULE_OFFSET(entry_index));
625-
const struct sof_uuid *uid = (const struct sof_uuid *)&mod->uuid;
626-
627667
lib_manager_prepare_module_adapter(drv, uid);
628668

629-
/*
630-
* llext modules store build info structure in separate section which is not accessible now.
631-
*/
632-
if (!module_is_llext(mod)) {
633-
build_info = (const struct sof_module_api_build_info *)((const char *)desc -
634-
SOF_MAN_ELF_TEXT_OFFSET +
635-
mod->segment[SOF_MAN_SEGMENT_TEXT].file_offset);
636-
637-
tr_info(&lib_manager_tr, "Module API version: %u.%u.%u, format: 0x%x",
638-
build_info->api_version_number.fields.major,
639-
build_info->api_version_number.fields.middle,
640-
build_info->api_version_number.fields.minor,
641-
build_info->format);
642-
643-
/* Check if module is IADK */
644-
if (IS_ENABLED(CONFIG_INTEL_MODULES) &&
645-
build_info->format == IADK_MODULE_API_BUILD_INFO_FORMAT &&
646-
build_info->api_version_number.full == IADK_MODULE_API_CURRENT_VERSION) {
647-
/* Use module_adapter functions */
648-
drv->ops.create = module_adapter_new;
649-
} else {
650-
/* Check if module is NOT native */
651-
if (build_info->format != SOF_MODULE_API_BUILD_INFO_FORMAT ||
652-
build_info->api_version_number.full != SOF_MODULE_API_CURRENT_VERSION) {
653-
tr_err(&lib_manager_tr, "Unsupported module API version");
654-
ret = -ENOEXEC;
655-
goto cleanup;
656-
}
657-
}
658-
}
659-
660669
/* Fill the new_drv_info structure with already known parameters */
661670
new_drv_info->drv = drv;
662671

0 commit comments

Comments
 (0)