Skip to content

Commit

Permalink
smart-amp-test: make a loadable module
Browse files Browse the repository at this point in the history
Convert the smart-amp-test in its IPC4 version to a loadable LLEXT
module.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
  • Loading branch information
lyakh committed Dec 7, 2023
1 parent a10a6bb commit 7e92c24
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 4 deletions.
21 changes: 21 additions & 0 deletions scripts/calc_elf_addresses.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

file=$1
text_addr=$2

# We need to take addresses of .text, .data, .rodata and .bss and calculate
# offsets, because the linker will keep everything between .data and .rodata and
# between .rodata and .bss in the final image too, when we specify section
# addresses. So just taking sizes of .text, .data and .rodata isn't enough.
# We use file offset from the objdump command output to avoid unused sections.

source <(
objdump -h $file | awk '/ .text / {print "text="toupper($6)} / .rodata / {print "rodata="toupper($6)} / .data / {print "data="toupper($6)}'
)

# round up to 4KiB
rodata_addr=$(echo "obase=16; ibase=16; upper=($rodata - $text + $text_addr + FFF); upper - (upper % 1000)" | bc)
# round up to 16 bytes
data_addr=$(echo "obase=16; ibase=16; upper=($data - $rodata + $rodata_addr + F); upper - (upper % 10)" | bc)

echo "-Wl,-Ttext=0x$text_addr -Wl,--section-start=.rodata=0x$rodata_addr -Wl,-Tdata=0x$data_addr"
2 changes: 1 addition & 1 deletion src/samples/audio/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
menu "Audio component samples"

config SAMPLE_SMART_AMP
bool "Smart amplifier test component"
tristate "Smart amplifier test component"
default y
help
Select for test smart amplifier component
Expand Down
71 changes: 71 additions & 0 deletions src/samples/audio/smart_amp_llext/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Copyright (c) 2023 Intel Corporation.
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(smart_amp_test)

SET_PROPERTY(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)

set(MODULE "smart_amp_test")
cmake_path(SET SOF_BASE NORMALIZE ${PROJECT_SOURCE_DIR}/../../../..)

add_library(${MODULE} SHARED)

target_sources(${MODULE} PRIVATE
${CMAKE_CURRENT_LIST_DIR}/../smart_amp_test_ipc4.c
)

sof_append_relative_path_definitions(${MODULE})

target_include_directories(${MODULE} PRIVATE
"${ZEPHYR_BASE}/include"
"${ZEPHYR_BASE}/soc/xtensa/intel_adsp/common/include"
"${ZEPHYR_BASE}/soc/xtensa/intel_adsp/ace/include/intel_ace15_mtpm"
"${ZEPHYR_BASE}/../modules/hal/xtensa/include"
"${ZEPHYR_BASE}/../modules/hal/xtensa/zephyr/soc/intel_ace15_mtpm"
"${SOF_BASE}/src/include"
"${SOF_BASE}/src/arch/xtensa/include"
"${SOF_BASE}/src/platform/meteorlake/include"
"${SOF_BASE}/src/platform/intel/ace/include"
"${SOF_BASE}/src/include/sof/audio/module_adapter/iadk"
"${SOF_BASE}/zephyr/include"
"${SOF_BASE}/xtos/include"
"${SOF_BASE}/rimage/src/include"
"${PROJECT_BINARY_DIR}/../include/generated"
)

set(MODULE_PROPERTIES HPSRAM_ADDR "0xa06c1000")
set_target_properties(${MODULE} PROPERTIES ${MODULE_PROPERTIES})

set(MODULE_COMPILE_DEF
__ZEPHYR__=1
__XTENSA__
KERNEL
MAJOR_IADSP_API_VERSION=5
MIDDLE_IADSP_API_VERSION=0
MINOR_IADSP_API_VERSION=0
)
target_compile_definitions(${MODULE} PRIVATE ${MODULE_COMPILE_DEF})

target_compile_options(${MODULE} PRIVATE
-imacros${PROJECT_BINARY_DIR}/../include/generated/autoconf.h
-save-temps -O2
)

set(MODULE_LINKER_PARAMS -nostdlib -nodefaultlibs)
target_link_options(${MODULE} PRIVATE
${MODULE_LINKER_PARAMS}
)

add_custom_target(${MODULE}_llext ${CMAKE_C_COMPILER}
${MODULE_LINKER_PARAMS}
"$$(${SOF_BASE}/scripts/calc_elf_addresses.sh" lib${MODULE}.so "A06CA000)"
-shared -fPIC
-o lib${MODULE}_llext.so $<TARGET_OBJECTS:${MODULE}>
COMMAND ${CMAKE_STRIP} -R .xt.* -o lib${MODULE}_out.so lib${MODULE}_llext.so
DEPENDS ${MODULE}
COMMAND_EXPAND_LISTS
)

add_dependencies(${MODULE} zephyr_interface)
80 changes: 80 additions & 0 deletions src/samples/audio/smart_amp_llext/smart_amp_test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
version = [3, 0]

[adsp]
name = "mtl"
image_size = "0x2C0000" # (22) bank * 128KB
alias_mask = "0xE0000000"

[[adsp.mem_zone]]
type = "ROM"
base = "0x1FF80000"
size = "0x400"
[[adsp.mem_zone]]
type = "IMR"
base = "0xA104A000"
size = "0x2000"
[[adsp.mem_zone]]
type = "SRAM"
base = "0xa00f0000"
size = "0x100000"

[[adsp.mem_alias]]
type = "uncached"
base = "0x40000000"
[[adsp.mem_alias]]
type = "cached"
base = "0xA0000000"

[cse]
partition_name = "ADSP"
[[cse.entry]]
name = "ADSP.man"
offset = "0x5c"
length = "0x464"
[[cse.entry]]
name = "ADSP.met"
offset = "0x4c0"
length = "0x70"
[[cse.entry]]
name = "ADSP"
offset = "0x540"
length = "0x0" # calculated by rimage

[css]

[signed_pkg]
name = "ADSP"
partition_usage = "0x23"
[[signed_pkg.module]]
name = "ADSP.met"

[adsp_file]
[[adsp_file.comp]]
base_offset = "0x2000"

[fw_desc.header]
name = "ADSPFW"
load_offset = "0x40000"

[module]
count = 1

[[module.entry]]
name = "SMATEST"
uuid = "167A961E-8AE4-11EA-89F1-000C29CE1635"
affinity_mask = "0x1"
instance_count = "1"
domain_types = "0"
load_type = "0"
init_config = "1"
module_type = "0xD"
auto_start = "0"
sched_caps = [1, 0x00008000]

# pin = [dir, type, sample rate, size, container, channel-cfg]
pin = [0, 0, 0xfeef, 0xf, 0xa, 0x45ff,
0, 0, 0xfeef, 0xf, 0xa, 0x45ff,
1, 0, 0xfeef, 0xf, 0xa, 0x45ff]

# mod_cfg [PAR_0 PAR_1 PAR_2 PAR_3 IS_BYTES CPS IBS OBS MOD_FLAGS CPC OBLS]
mod_cfg = [0, 0, 0, 0, 4096, 1000000, 128, 128, 0, 0, 0]
42 changes: 42 additions & 0 deletions src/samples/audio/smart_amp_test_ipc4.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ struct smart_amp_data {
uint32_t out_channels;
};

/* When building as a loadable module, we need .bss to avoid rimage errors */
static int keep_bss __attribute__((used));

static int smart_amp_init(struct processing_module *mod)
{
struct smart_amp_data *sad;
Expand All @@ -49,7 +52,13 @@ static int smart_amp_init(struct processing_module *mod)
int ret;
const struct ipc4_base_module_extended_cfg *base_cfg = mod_data->cfg.init_data;

if (!base_cfg) {
comp_err(dev, "smart_amp_init(): no module configuration");
return -EINVAL;
}

comp_dbg(dev, "smart_amp_init()");

sad = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, sizeof(*sad));
if (!sad)
return -ENOMEM;
Expand Down Expand Up @@ -393,3 +402,36 @@ DECLARE_MODULE_ADAPTER(smart_amp_test_interface, smart_amp_test_comp_uuid, smart
* "sys_comp_module_<smart_amp_test_interface>_init()" (and a lot more)
*/
SOF_MODULE_INIT(smart_amp_test, sys_comp_module_smart_amp_test_interface_init);

#ifdef MAJOR_IADSP_API_VERSION

#include <rimage/sof/user/manifest.h>
#include <module/module/api_ver.h>

static const void *loadable_module_main(void *mod_cfg, void *parent_ppl, void **mod_ptr)
{
return &smart_amp_test_interface;
}

static const struct sof_man_module_manifest main_manifest __section(".module") __attribute__((used)) = {
.module = {
.name = "SMATEST",
.uuid = {0x1E, 0x96, 0x7A, 0x16, 0xE4, 0x8A, 0xEA, 0x11,
0x89, 0xF1, 0x00, 0x0C, 0x29, 0xCE, 0x16, 0x35},
.entry_point = (uint32_t)loadable_module_main,
.type = {
.load_type = SOF_MAN_MOD_TYPE_MODULE,
.domain_ll = 1,
},
.affinity_mask = 1,
}
};

static const struct sof_module_api_build_info buildinfo __section(".mod_buildinfo") __attribute__((used)) = {
.format = SOF_MODULE_API_BUILD_INFO_FORMAT,
.api_version_number = {
.fields.major = SOF_MODULE_API_MAJOR_VERSION,
},
};

#endif
12 changes: 9 additions & 3 deletions zephyr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -764,9 +764,15 @@ if(CONFIG_IPC_MAJOR_3)
${SOF_AUDIO_PATH}/tdfb/tdfb_ipc3.c
)
elseif(CONFIG_IPC_MAJOR_4)
zephyr_library_sources_ifdef(CONFIG_SAMPLE_SMART_AMP
${SOF_SAMPLES_PATH}/audio/smart_amp_test_ipc4.c
)
if(CONFIG_SAMPLE_SMART_AMP STREQUAL "m")
add_subdirectory(${SOF_SAMPLES_PATH}/audio/smart_amp_llext
${PROJECT_BINARY_DIR}/smart_amp_llext)
add_dependencies(app smart_amp_test_llext)
elseif(CONFIG_SAMPLE_SMART_AMP)
zephyr_library_sources(
${SOF_SAMPLES_PATH}/audio/smart_amp_test_ipc4.c
)
endif()

zephyr_library_sources_ifdef(CONFIG_COMP_TDFB
${SOF_AUDIO_PATH}/tdfb/tdfb_ipc4.c
Expand Down

0 comments on commit 7e92c24

Please sign in to comment.