Skip to content

Commit 1fe992f

Browse files
committed
lib_manager: modules: move native lib support to lib_manager
Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
1 parent 6b99a95 commit 1fe992f

File tree

5 files changed

+136
-73
lines changed

5 files changed

+136
-73
lines changed

src/audio/module_adapter/module/modules.c

Lines changed: 18 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@
99
#include <sof/audio/module_adapter/module/modules.h>
1010
#include <utilities/array.h>
1111
#include <system_agent.h>
12-
#include <native_system_agent.h>
13-
#include <api_version.h>
1412
#include <sof/lib_manager.h>
1513
#include <sof/audio/module_adapter/module/module_interface.h>
16-
#include <module/module/api_ver.h>
1714

1815
/* Intel module adapter is an extension to SOF module adapter component that allows to integrate
1916
* modules developed under IADK (Intel Audio Development Kit) Framework. IADK modules uses uniform
@@ -60,91 +57,45 @@ static int modules_init(struct processing_module *mod)
6057
{
6158
uint32_t module_entry_point;
6259
struct module_data *md = &mod->priv;
63-
struct comp_dev *dev = mod->dev;
64-
struct comp_driver *drv = (struct comp_driver *)dev->drv;
60+
const struct comp_dev *dev = mod->dev;
61+
const struct comp_driver *const drv = dev->drv;
6562
const struct ipc4_base_module_cfg *src_cfg = &md->cfg.base_cfg;
6663
byte_array_t mod_cfg;
6764
void *system_agent;
68-
bool is_native_sof = false;
6965

7066
mod_cfg.data = (uint8_t *)md->cfg.init_data;
7167
/* Intel modules expects DW size here */
7268
mod_cfg.size = md->cfg.size >> 2;
7369

74-
struct comp_ipc_config *config = &(dev->ipc_config);
70+
const struct comp_ipc_config *config = &dev->ipc_config;
7571

7672
/* At this point module resources are allocated and it is moved to L2 memory. */
7773
module_entry_point = lib_manager_allocate_module(dev->drv, config, src_cfg);
7874
if (module_entry_point == 0) {
7975
comp_err(dev, "modules_init(), lib_manager_allocate_module() failed!");
8076
return -EINVAL;
8177
}
82-
8378
comp_info(dev, "modules_init() start");
8479

85-
uint32_t module_id = IPC4_MOD_ID(dev->ipc_config.id);
86-
uint32_t instance_id = IPC4_INST_ID(dev->ipc_config.id);
87-
uint32_t log_handle = (uint32_t) dev->drv->tctx;
88-
/* Connect loadable module interfaces with module adapter entity. */
89-
/* Check if native Zephyr lib is loaded */
80+
const uint32_t module_id = IPC4_MOD_ID(dev->ipc_config.id);
81+
const uint32_t instance_id = IPC4_INST_ID(dev->ipc_config.id);
82+
const uint32_t log_handle = (const uint32_t)drv->tctx;
9083

91-
const struct sof_man_module *const module_entry =
92-
lib_manager_get_library_manifest(module_id);
84+
system_agent = system_agent_start(module_entry_point, module_id,
85+
instance_id, 0, log_handle,
86+
&mod_cfg);
9387

94-
if (!module_entry) {
95-
comp_err(dev, "modules_init(): Failed to load manifest");
96-
return -ENOMEM;
88+
if (!system_agent) {
89+
lib_manager_free_module(module_id);
90+
comp_err(dev, "modules_init(), system_agent_start failed!");
91+
return -EFAULT;
9792
}
9893

99-
const struct sof_module_api_build_info *const mod_buildinfo =
100-
(struct sof_module_api_build_info *)
101-
(module_entry->segment[SOF_MAN_SEGMENT_TEXT].v_base_addr);
102-
103-
/* Check if module is FDK */
104-
if (mod_buildinfo->format == IADK_MODULE_API_BUILD_INFO_FORMAT &&
105-
mod_buildinfo->api_version_number.full == IADK_MODULE_API_CURRENT_VERSION) {
106-
system_agent = system_agent_start(module_entry_point, module_id,
107-
instance_id, 0, log_handle,
108-
&mod_cfg);
109-
110-
module_set_private_data(mod, system_agent);
111-
} else
112-
/* Check if module is native */
113-
if (mod_buildinfo->format == SOF_MODULE_API_BUILD_INFO_FORMAT &&
114-
mod_buildinfo->api_version_number.full == SOF_MODULE_API_CURRENT_VERSION) {
115-
/* If start agent for sof loadable */
116-
is_native_sof = true;
117-
drv->adapter_ops = native_system_agent_start(module_entry_point, module_id,
118-
instance_id, 0, log_handle, &mod_cfg);
119-
} else
120-
return -ENOEXEC;
121-
94+
module_set_private_data(mod, system_agent);
12295
md->mpd.in_buff_size = src_cfg->ibs;
12396
md->mpd.out_buff_size = src_cfg->obs;
12497

125-
int ret;
126-
127-
/* Call module specific init function if exists. */
128-
if (is_native_sof) {
129-
const struct module_interface *mod_in = drv->adapter_ops;
130-
131-
/* The order of preference */
132-
if (mod_in->process)
133-
mod->proc_type = MODULE_PROCESS_TYPE_SOURCE_SINK;
134-
else if (mod_in->process_audio_stream)
135-
mod->proc_type = MODULE_PROCESS_TYPE_STREAM;
136-
else if (mod_in->process_raw_data)
137-
mod->proc_type = MODULE_PROCESS_TYPE_RAW;
138-
else
139-
return -EINVAL;
140-
141-
ret = mod_in->init(mod);
142-
} else {
143-
mod->proc_type = MODULE_PROCESS_TYPE_SOURCE_SINK;
144-
ret = iadk_wrapper_init(system_agent);
145-
}
146-
147-
return ret;
98+
return iadk_wrapper_init(system_agent);
14899
}
149100

150101
/**
@@ -193,11 +144,13 @@ static int modules_free(struct processing_module *mod)
193144

194145
comp_info(dev, "modules_free()");
195146
ret = iadk_wrapper_free(module_get_private_data(mod));
147+
if (ret)
148+
comp_err(dev, "modules_free(): iadk_wrapper_free failed with error: %d", ret);
196149

197150
/* Free module resources allocated in L2 memory. */
198151
ret = lib_manager_free_module(dev->ipc_config.id);
199152
if (ret < 0)
200-
comp_err(dev, "modules_free(), lib_manager_free_module() failed!");
153+
comp_err(dev, "modules_free(), lib_manager_free_module failed with error: %d", ret);
201154

202155
return ret;
203156
}

src/audio/module_adapter/module_adapter.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ LOG_MODULE_REGISTER(module_adapter, CONFIG_SOF_LOG_LEVEL);
3333
* \brief Create a module adapter component.
3434
* \param[in] drv - component driver pointer.
3535
* \param[in] config - component ipc descriptor pointer.
36+
* \param[in] spec - passdowned data from driver.
3637
*
3738
* \return: a pointer to newly created module adapter component on success. NULL on error.
3839
*/

src/include/sof/lib_manager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ void lib_manager_init(void);
136136
/*
137137
* \brief Register module on driver list
138138
*
139-
* param[in] desc - module manifest descriptor
139+
* param[in] component_id - component id
140140
*
141141
* Creates new comp_driver_info and initialize it for module from library
142142
* Adds new module to sof_get()->comp_drivers list
143143
*/
144-
int lib_manager_register_module(const struct sof_man_module *const mod);
144+
int lib_manager_register_module(const uint32_t component_id);
145145

146146
/*
147147
* \brief Allocate module

src/ipc/ipc4/helper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ const struct comp_driver *ipc4_get_comp_drv(int module_id)
10021002
#if CONFIG_LIBRARY_MANAGER
10031003
if (!drv) {
10041004
/* New module not registered yet. */
1005-
lib_manager_register_module(mod);
1005+
lib_manager_register_module(module_id);
10061006
drv = ipc4_get_drv(mod->uuid);
10071007
}
10081008
#endif

src/library_manager/lib_manager.c

Lines changed: 114 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include <sof/llext_manager.h>
2323
#include <sof/audio/module_adapter/module/generic.h>
2424
#include <sof/audio/module_adapter/module/modules.h>
25+
#include <utilities/array.h>
26+
#include <native_system_agent.h>
27+
#include <api_version.h>
28+
#include <module/module/api_ver.h>
2529

2630
#include <zephyr/cache.h>
2731
#include <zephyr/drivers/mm/system_mm.h>
@@ -477,7 +481,6 @@ const struct sof_man_module *lib_manager_get_library_manifest(const uint32_t mod
477481
return NULL;
478482

479483
desc = (const struct sof_man_fw_desc *)((const char *)ctx->desc + SOF_MAN_ELF_TEXT_OFFSET);
480-
481484
if (entry_index >= desc->header.num_module_entries) {
482485
tr_err(&lib_manager_tr, "Entry index %d out of bounds.", entry_index);
483486
return NULL;
@@ -488,13 +491,91 @@ const struct sof_man_module *lib_manager_get_library_manifest(const uint32_t mod
488491
}
489492

490493
#if CONFIG_INTEL_MODULES
491-
int lib_manager_register_module(const struct sof_man_module *const mod)
494+
/*
495+
* \brief Load module code, allocate its instance and create a module adapter component.
496+
* \param[in] drv - component driver pointer.
497+
* \param[in] config - component ipc descriptor pointer.
498+
* \param[in] spec - passdowned data from driver.
499+
*
500+
* \return: a pointer to newly created module adapter component on success. NULL on error.
501+
*/
502+
static struct comp_dev *lib_manager_module_create(const struct comp_driver *drv,
503+
const struct comp_ipc_config *config,
504+
const void *spec)
505+
{
506+
const struct ipc_config_process *args = (struct ipc_config_process *)spec;
507+
const uint32_t module_id = IPC4_MOD_ID(config->id);
508+
const uint32_t instance_id = IPC4_INST_ID(config->id);
509+
const uint32_t log_handle = (uint32_t)drv->tctx;
510+
byte_array_t mod_cfg;
511+
512+
/* At this point module resources are allocated and it is moved to L2 memory. */
513+
const uint32_t module_entry_point = lib_manager_allocate_module(drv, config, args->data);
514+
if (!module_entry_point) {
515+
tr_err(&lib_manager_tr,
516+
"lib_manager_module_create(), lib_manager_allocate_module() failed!");
517+
return NULL;
518+
}
519+
tr_info(&lib_manager_tr, "lib_manager_module_create() start");
520+
521+
mod_cfg.data = (uint8_t *)args->data;
522+
/* Intel modules expects DW size here */
523+
mod_cfg.size = args->size >> 2;
524+
525+
((struct comp_driver *)drv)->adapter_ops = native_system_agent_start(module_entry_point,
526+
module_id, instance_id,
527+
0, log_handle,
528+
&mod_cfg);
529+
530+
if (!drv->adapter_ops) {
531+
lib_manager_free_module(module_id);
532+
tr_err(&lib_manager_tr,
533+
"lib_manager_module_create(), native_system_agent_start failed!");
534+
return NULL;
535+
}
536+
537+
return module_adapter_new(drv, config, spec);
538+
}
539+
540+
541+
static void lib_manager_module_free(struct comp_dev *dev)
542+
{
543+
struct processing_module *mod = comp_get_drvdata(dev);
544+
const struct comp_ipc_config *const config = &(mod->dev->ipc_config);
545+
const uint32_t module_id = config->id;
546+
int ret;
547+
548+
/* This call invalidates dev, mod and config pointers! */
549+
module_adapter_free(dev);
550+
551+
/* Free module resources allocated in L2 memory. */
552+
ret = lib_manager_free_module(module_id);
553+
if (ret < 0)
554+
comp_err(dev, "modules_free(), lib_manager_free_module() failed!");
555+
}
556+
557+
int lib_manager_register_module(const uint32_t component_id)
492558
{
493-
/* allocate new comp_driver_info */
559+
const struct lib_manager_mod_ctx *const ctx = lib_manager_get_mod_ctx(component_id);
560+
const uint32_t entry_index = LIB_MANAGER_GET_MODULE_INDEX(component_id);
561+
const struct sof_module_api_build_info *build_info;
494562
struct comp_driver_info *new_drv_info;
563+
const struct sof_man_fw_desc *desc;
564+
const struct sof_man_module *mod;
495565
struct comp_driver *drv = NULL;
496566
int ret;
497567

568+
/* Get library manifest based on component_id */
569+
if (!ctx || !ctx->desc)
570+
return -ENOENT;
571+
572+
desc = (const struct sof_man_fw_desc *)((const char *)ctx->desc + SOF_MAN_ELF_TEXT_OFFSET);
573+
if (entry_index >= desc->header.num_module_entries) {
574+
tr_err(&lib_manager_tr, "Entry index %d out of bounds.", entry_index);
575+
return -ENOENT;
576+
}
577+
578+
/* allocate new comp_driver_info */
498579
new_drv_info = rmalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0,
499580
SOF_MEM_CAPS_RAM | SOF_MEM_FLAG_COHERENT,
500581
sizeof(struct comp_driver_info));
@@ -515,13 +596,41 @@ int lib_manager_register_module(const struct sof_man_module *const mod)
515596
ret = -ENOMEM;
516597
goto cleanup;
517598
}
599+
600+
mod = (const struct sof_man_module *)((const char *)desc +
601+
SOF_MAN_MODULE_OFFSET(entry_index));
518602

519-
/* Fill the new_drv_info structure with already known parameters */
520-
/* Check already registered components */
521603
const struct sof_uuid *const uid = (const struct sof_uuid *)&mod->uuid[0];
522604

523605
declare_dynamic_module_adapter(drv, SOF_COMP_MODULE_ADAPTER, uid, &lib_manager_tr);
524606

607+
build_info = (const struct sof_module_api_build_info *)((const char *)ctx->desc +
608+
mod->segment[SOF_MAN_SEGMENT_TEXT].file_offset);
609+
610+
tr_info(&lib_manager_tr,
611+
"lib_manager_register_module(): Module API version: %u.%u.%u, format: 0x%x",
612+
build_info->api_version_number.fields.major,
613+
build_info->api_version_number.fields.middle,
614+
build_info->api_version_number.fields.minor,
615+
build_info->format);
616+
617+
/* Check if module is native */
618+
if (build_info->format == SOF_MODULE_API_BUILD_INFO_FORMAT &&
619+
build_info->api_version_number.full == SOF_MODULE_API_CURRENT_VERSION) {
620+
/* Use lib_manager shim functions */
621+
drv->ops.create = &lib_manager_module_create;
622+
drv->ops.free = &lib_manager_module_free;
623+
} else {
624+
/* Check if module is not FDK */
625+
if (build_info->format != IADK_MODULE_API_BUILD_INFO_FORMAT ||
626+
build_info->api_version_number.full != IADK_MODULE_API_CURRENT_VERSION) {
627+
tr_err(&lib_manager_tr,
628+
"lib_manager_register_module(): Unsupported module API version");
629+
return -ENOEXEC;
630+
}
631+
}
632+
633+
/* Fill the new_drv_info structure with already known parameters */
525634
new_drv_info->drv = drv;
526635

527636
/* Register new driver in the list */

0 commit comments

Comments
 (0)