Skip to content

Commit bfa39d7

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 7203e53 commit bfa39d7

File tree

4 files changed

+87
-121
lines changed

4 files changed

+87
-121
lines changed

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 & 74 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,46 +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,
520-
(void **)&((struct comp_driver *)drv)->adapter_ops);
521-
571+
ret = agent(module_entry_point, module_id, instance_id, 0, log_handle, &mod_cfg,
572+
agent_iface);
522573
if (ret) {
523-
lib_manager_free_module(module_id);
524-
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);
525576
return NULL;
526577
}
527578

528-
dev = module_adapter_new(drv, config, spec);
579+
dev = module_adapter_new_ext(drv, config, spec, adapter_priv);
529580
if (!dev)
530-
lib_manager_free_module(module_id);
581+
lib_manager_free_module(config->id);
531582

532583
return dev;
533584
}
@@ -582,34 +633,26 @@ static void lib_manager_prepare_module_adapter(struct comp_driver *drv, const st
582633

583634
int lib_manager_register_module(const uint32_t component_id)
584635
{
585-
const struct sof_man_fw_desc *const desc = lib_manager_get_library_manifest(component_id);
586-
const uint32_t entry_index = LIB_MANAGER_GET_MODULE_INDEX(component_id);
587-
const struct sof_module_api_build_info *build_info;
588636
struct comp_driver_info *new_drv_info;
589637
struct comp_driver *drv = NULL;
590-
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;
591640
int ret;
592641

593-
if (!desc) {
642+
if (!mod) {
594643
tr_err(&lib_manager_tr, "Error: Couldn't find loadable module with id %u.",
595644
component_id);
596645
return -ENOENT;
597646
}
598647

599-
if (entry_index >= desc->header.num_module_entries) {
600-
tr_err(&lib_manager_tr, "Entry index %u out of bounds.", entry_index);
601-
return -ENOENT;
602-
}
603-
604648
/* allocate new comp_driver_info */
605649
new_drv_info = rmalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0,
606650
SOF_MEM_CAPS_RAM | SOF_MEM_FLAG_COHERENT,
607651
sizeof(struct comp_driver_info));
608652

609653
if (!new_drv_info) {
610654
tr_err(&lib_manager_tr, "failed to allocate comp_driver_info");
611-
ret = -ENOMEM;
612-
goto cleanup;
655+
return -ENOMEM;
613656
}
614657

615658
drv = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0,
@@ -621,43 +664,8 @@ int lib_manager_register_module(const uint32_t component_id)
621664
goto cleanup;
622665
}
623666

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

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

0 commit comments

Comments
 (0)