diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec.c b/src/audio/ffmpeg_dec/ffmpeg_dec.c index 81eaab628441..99ea3f3af8ff 100644 --- a/src/audio/ffmpeg_dec/ffmpeg_dec.c +++ b/src/audio/ffmpeg_dec/ffmpeg_dec.c @@ -9,6 +9,7 @@ // ffmpeg_dec-stub.c and ffmpeg_dec-ffmpeg.c). The input is a compressed audio // elementary stream (raw data), the output is interleaved PCM. +#include #include #include #include @@ -967,6 +968,71 @@ static const struct module_interface ffmpeg_dec_interface = { .free = ffmpeg_dec_free }; +static const uint32_t ffmpeg_dec_codec_info_items[] +#if CONFIG_COMP_FFMPEG_DEC_MODULE + __section(SOF_CODEC_INFO_SECTION) __used +#endif + = { +#if defined(CONFIG_FFMPEG_FILTER) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_PCM, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_DEC_FLAC) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_FLAC, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_DEC_ALAC) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_ALAC, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_DEC_AAC) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_AAC, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_DEC_MP3) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_MP3, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_ENC_MP3) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_MP3, SOF_IPC_STREAM_CAPTURE), +#endif +#if defined(CONFIG_FFMPEG_ENC_AAC) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_AAC, SOF_IPC_STREAM_CAPTURE), +#endif +#if defined(CONFIG_FFMPEG_ENC_FLAC) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_FLAC, SOF_IPC_STREAM_CAPTURE), +#endif +#if defined(CONFIG_FFMPEG_ENC_ALAC) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_ALAC, SOF_IPC_STREAM_CAPTURE), +#endif +#if defined(CONFIG_FFMPEG_DEC_SBC) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_SBC, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_ENC_SBC) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_SBC, SOF_IPC_STREAM_CAPTURE), +#endif +#if defined(CONFIG_FFMPEG_DEC_G722) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_G722, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_ENC_G722) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_G722, SOF_IPC_STREAM_CAPTURE), +#endif +#if defined(CONFIG_FFMPEG_DEC_APTX) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_APTX, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_ENC_APTX) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_APTX, SOF_IPC_STREAM_CAPTURE), +#endif +#if defined(CONFIG_FFMPEG_ENC_SBC) || defined(CONFIG_FFMPEG_ENC_G722) || \ + defined(CONFIG_FFMPEG_ENC_APTX) + /* Also advertise BESPOKE capture for kernels with older SND_AUDIOCODEC_MAX. */ + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_BESPOKE, SOF_IPC_STREAM_CAPTURE), +#endif +#if defined(CONFIG_FFMPEG_DEC_OPUS) || defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_OPUS_RAW, SOF_IPC_STREAM_PLAYBACK), +#endif +#if defined(CONFIG_FFMPEG_DEC_OPUS) || defined(CONFIG_FFMPEG_DEC_SBC) || \ + defined(CONFIG_FFMPEG_DEC_G722) || defined(CONFIG_FFMPEG_DEC_APTX) || \ + defined(CONFIG_COMP_FFMPEG_DEC_STUB) + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_BESPOKE, SOF_IPC_STREAM_PLAYBACK), +#endif +}; + /* If COMP_FFMPEG_DEC is =m in Kconfig this is built as a loadable LLEXT module. */ #if CONFIG_COMP_FFMPEG_DEC_MODULE @@ -985,6 +1051,14 @@ SOF_LLEXT_BUILDINFO; /* Only used for the module adapter trace context, soon to be deprecated */ DECLARE_TR_CTX(ffmpeg_dec_tr, SOF_UUID(ffmpeg_dec_uuid), LOG_LEVEL_INFO); DECLARE_MODULE_ADAPTER(ffmpeg_dec_interface, ffmpeg_dec_uuid, ffmpeg_dec_tr); -SOF_MODULE_INIT(ffmpeg_dec, sys_comp_module_ffmpeg_dec_interface_init); + +static void sys_comp_ffmpeg_dec_init(void) +{ + sys_comp_module_ffmpeg_dec_interface_init(); + sof_codec_info_register(&ffmpeg_dec_interface, ffmpeg_dec_codec_info_items, + ARRAY_SIZE(ffmpeg_dec_codec_info_items)); +} + +SOF_MODULE_INIT(ffmpeg_dec, sys_comp_ffmpeg_dec_init); #endif diff --git a/src/audio/mfcc/mfcc.c b/src/audio/mfcc/mfcc.c index 945c77c6d613..ae4b014f8d69 100644 --- a/src/audio/mfcc/mfcc.c +++ b/src/audio/mfcc/mfcc.c @@ -4,6 +4,7 @@ // // Author: Seppo Ingalsuo +#include #include #include #include @@ -21,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -321,6 +323,14 @@ static const struct module_interface mfcc_interface = { .reset = mfcc_reset, }; +static const uint32_t mfcc_codec_info_items[] +#if CONFIG_COMP_MFCC_MODULE + __section(SOF_CODEC_INFO_SECTION) __used +#endif + = { + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_BESPOKE, SOF_IPC_STREAM_CAPTURE), +}; + #if CONFIG_COMP_MFCC_MODULE /* modular: llext dynamic link */ @@ -338,6 +348,14 @@ SOF_LLEXT_BUILDINFO; /* unused with Zephyr, generates no output */ DECLARE_TR_CTX(mfcc_tr, SOF_UUID(mfcc_uuid), LOG_LEVEL_INFO); DECLARE_MODULE_ADAPTER(mfcc_interface, mfcc_uuid, mfcc_tr); -SOF_MODULE_INIT(mfcc, sys_comp_module_mfcc_interface_init); + +static void sys_comp_mfcc_init(void) +{ + sys_comp_module_mfcc_interface_init(); + sof_codec_info_register(&mfcc_interface, mfcc_codec_info_items, + ARRAY_SIZE(mfcc_codec_info_items)); +} + +SOF_MODULE_INIT(mfcc, sys_comp_mfcc_init); #endif diff --git a/src/audio/module_adapter/module/cadence_ipc4.c b/src/audio/module_adapter/module/cadence_ipc4.c index 3a4489e24dcf..66fea7a881ae 100644 --- a/src/audio/module_adapter/module/cadence_ipc4.c +++ b/src/audio/module_adapter/module/cadence_ipc4.c @@ -3,6 +3,7 @@ // Copyright(c) 2025-2026 Intel Corporation. All rights reserved. // +#include #include #include #include @@ -644,4 +645,38 @@ static const struct module_interface cadence_codec_interface = { }; DECLARE_MODULE_ADAPTER(cadence_codec_interface, cadence_codec_uuid, cadence_codec_tr); -SOF_MODULE_INIT(cadence_codec, sys_comp_module_cadence_codec_interface_init); + +#if defined(CONFIG_CADENCE_CODEC_AAC_DEC) || defined(CONFIG_CADENCE_CODEC_MP3_DEC) || \ + defined(CONFIG_CADENCE_CODEC_MP3_ENC) || defined(CONFIG_CADENCE_CODEC_VORBIS_DEC) || \ + defined(CONFIG_SOF_COMPRESS_CODEC_PCM_DEC) +static const uint32_t cadence_codec_info_items[] = { +#ifdef CONFIG_CADENCE_CODEC_AAC_DEC + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_AAC, SOF_IPC_STREAM_PLAYBACK), +#endif +#ifdef CONFIG_CADENCE_CODEC_MP3_DEC + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_MP3, SOF_IPC_STREAM_PLAYBACK), +#endif +#ifdef CONFIG_CADENCE_CODEC_MP3_ENC + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_MP3, SOF_IPC_STREAM_CAPTURE), +#endif +#ifdef CONFIG_CADENCE_CODEC_VORBIS_DEC + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_VORBIS, SOF_IPC_STREAM_PLAYBACK), +#endif +#ifdef CONFIG_SOF_COMPRESS_CODEC_PCM_DEC + SOF_CODEC_INFO_ITEM(SND_AUDIOCODEC_PCM, SOF_IPC_STREAM_PLAYBACK), +#endif +}; +#endif + +static void sys_comp_cadence_codec_init(void) +{ + sys_comp_module_cadence_codec_interface_init(); +#if defined(CONFIG_CADENCE_CODEC_AAC_DEC) || defined(CONFIG_CADENCE_CODEC_MP3_DEC) || \ + defined(CONFIG_CADENCE_CODEC_MP3_ENC) || defined(CONFIG_CADENCE_CODEC_VORBIS_DEC) || \ + defined(CONFIG_SOF_COMPRESS_CODEC_PCM_DEC) + sof_codec_info_register(&cadence_codec_interface, cadence_codec_info_items, + ARRAY_SIZE(cadence_codec_info_items)); +#endif +} + +SOF_MODULE_INIT(cadence_codec, sys_comp_cadence_codec_init); diff --git a/src/include/ipc4/base_fw.h b/src/include/ipc4/base_fw.h index 6583365e755a..f7958d5efb2c 100644 --- a/src/include/ipc4/base_fw.h +++ b/src/include/ipc4/base_fw.h @@ -190,6 +190,9 @@ enum ipc4_basefw_params { /** Set policy mask for microphone privacy in firmware-managed mode. */ IPC4_SET_MIC_PRIVACY_FW_MANAGED_POLICY_MASK = 36, + + /** Discover SOF specific runtime capabilities/information in TLV format via LARGE_CONFIG_GET. */ + IPC4_SOF_INFO_GET = 37, }; /** @@ -289,6 +292,8 @@ enum ipc4_fw_config_params { enum ipc4_fw_sof_info_params { /** Decoder/encoder codec information. */ IPC4_SOF_CODEC_INFO = 0, + /** Bitmask of runtime SOF info capabilities (IPC4_SOF_CAP_*). */ + IPC4_SOF_CAPS = 1, /** Total number of SOF config parameters. */ IPC4_SOF_CFG_PARAMS_COUNT, @@ -296,6 +301,9 @@ enum ipc4_fw_sof_info_params { IPC4_MAX_SOF_CFG_PARAM = IPC4_SOF_CFG_PARAMS_COUNT - 1, }; +/** BaseFW supports runtime query via IPC4_SOF_INFO_GET. */ +#define IPC4_SOF_CAP_RUNTIME_SOF_INFO (1U << 0) + /** * \brief Hardware configuration parameter TLV IDs returned by IPC4_HW_CONFIG_GET. * \ingroup ipc4_base_fw_api diff --git a/src/include/sof/audio/codec_info.h b/src/include/sof/audio/codec_info.h new file mode 100644 index 000000000000..560609c64cf3 --- /dev/null +++ b/src/include/sof/audio/codec_info.h @@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Copyright(c) 2026 Intel Corporation. All rights reserved. + */ + +#ifndef __SOF_AUDIO_CODEC_INFO_H__ +#define __SOF_AUDIO_CODEC_INFO_H__ + +#include + +/** + * \brief Encodes codec and direction information into a single 32-bit value. + * \param codec Codec type (bits 0-7, e.g. SND_AUDIOCODEC_*) + * \param dir Stream direction (bits 8-11, e.g. SOF_IPC_STREAM_*) + * \return Encoded 32-bit codec capability item + */ +#define SOF_CODEC_INFO_ITEM(codec, dir) \ + (((uint32_t)(codec) & 0xffU) | (((uint32_t)(dir) & 0xfU) << 8)) + +/** ELF section name holding LLEXT module codec capability items. */ +#define SOF_CODEC_INFO_SECTION ".sof_codec_info" + +#if CONFIG_COMP_BASEFW_IPC4 && !CONFIG_LIBRARY +/** + * \brief Register runtime codec audio capabilities for a module or library. + * \param[in] owner Opaque owner token (e.g. module interface or library module ctx). + * \param[in] items Array of SOF_CODEC_INFO_ITEM(codec, dir) values. + * \param[in] count Number of items in @p items. + * \return 0 on success, negative error code on failure. + */ +int sof_codec_info_register(const void *owner, const uint32_t *items, uint32_t count); + +/** + * \brief Unregister runtime codec audio capabilities for an owner token. + * \param[in] owner Opaque owner token previously passed to sof_codec_info_register(). + */ +void sof_codec_info_unregister(const void *owner); +#else +static inline int sof_codec_info_register(const void *owner, const uint32_t *items, + uint32_t count) +{ + (void)owner; + (void)items; + (void)count; + return 0; +} + +static inline void sof_codec_info_unregister(const void *owner) +{ + (void)owner; +} +#endif + +#endif /* __SOF_AUDIO_CODEC_INFO_H__ */ diff --git a/src/include/sof/lib_manager.h b/src/include/sof/lib_manager.h index 70ca5f97102c..ed5395000117 100644 --- a/src/include/sof/lib_manager.h +++ b/src/include/sof/lib_manager.h @@ -221,6 +221,16 @@ void lib_manager_get_instance_bss_address(uint32_t instance_id, */ int lib_manager_load_library(uint32_t dma_id, uint32_t lib_id, uint32_t type); +/* + * \brief Unload library + * + * param[in] lib_id - library ID to unload (1..LIB_MANAGER_MAX_LIBS - 1) + * + * Unregisters codec capabilities, unloads any cached LLEXT contexts, and frees + * library storage when no module instance is active. + */ +int lib_manager_unload_library(uint32_t lib_id); + struct userspace_context; /* * \brief Allocate the module and start the agent if needed diff --git a/src/include/sof/llext_manager.h b/src/include/sof/llext_manager.h index 525fa50b1506..a39dc9b9ee47 100644 --- a/src/include/sof/llext_manager.h +++ b/src/include/sof/llext_manager.h @@ -29,6 +29,13 @@ uintptr_t llext_manager_allocate_module(const struct comp_ipc_config *ipc_config int llext_manager_free_module(const uint32_t component_id); int llext_manager_add_library(uint32_t module_id); +int llext_manager_remove_library(uint32_t module_id); + +struct lib_manager_mod_ctx; +struct sof_man_fw_desc; +void llext_manager_register_lib_codecs(const struct lib_manager_mod_ctx *ctx, + const struct sof_man_fw_desc *desc); +void llext_manager_unregister_lib_codecs(const struct lib_manager_mod_ctx *ctx); int llext_manager_add_domain(const uint32_t component_id, struct k_mem_domain *domain); int llext_manager_rm_domain(const uint32_t component_id, struct k_mem_domain *domain); @@ -39,6 +46,7 @@ bool comp_is_llext(struct comp_dev *comp); #define llext_manager_allocate_module(ipc_config, ipc_specific_config) 0 #define llext_manager_free_module(component_id) 0 #define llext_manager_add_library(module_id) 0 +#define llext_manager_remove_library(module_id) 0 #define llext_manager_add_domain(component_id, domain) 0 #define comp_is_llext(comp) false #endif diff --git a/src/kernel/audio/base_fw.c b/src/kernel/audio/base_fw.c index 880306a4a03b..251cb5db1b2f 100644 --- a/src/kernel/audio/base_fw.c +++ b/src/kernel/audio/base_fw.c @@ -5,7 +5,9 @@ #include +#include #include +#include #include #include #include @@ -14,15 +16,17 @@ #include #include #include -#include #include #include #include #include #include #include +#include #include #include +#include +#include #include #include #include @@ -73,143 +77,140 @@ __cold static uint32_t get_host_buffer_size(void) return periods; } +#define SOF_IPC4_CODEC_INFO_MAX_ITEMS 64 + struct sof_ipc4_codec_info_data { uint32_t count; - uint32_t items[32]; + uint32_t items[SOF_IPC4_CODEC_INFO_MAX_ITEMS]; } __packed __aligned(4); -/** - * Encodes codec and direction information into a single 32-bit value. - * @param codec Codec type (bits 0-7) - * @param dir Stream direction (bits 8-11) - * @return Encoded 32-bit value - */ -#define SET_CODEC_INFO_ITEM(codec, dir) (((codec) & 0xff) | (((dir) & 0xf) << 8)) +struct sof_codec_info_provider { + struct list_item list; + const void *owner; + uint32_t count; + uint32_t items[]; +}; + +static struct list_item codec_info_providers = LIST_INIT(codec_info_providers); +static struct k_spinlock codec_info_lock; + +static inline void add_codec_info_item(struct sof_ipc4_codec_info_data *info, + uint32_t item) +{ + uint32_t i; + + if (!item) + return; + + for (i = 0; i < info->count; i++) { + if (info->items[i] == item) + return; + } + + if (info->count < ARRAY_SIZE(info->items)) + info->items[info->count++] = item; +} + +__cold int sof_codec_info_register(const void *owner, const uint32_t *items, uint32_t count) +{ + struct sof_codec_info_provider *entry; + struct sof_codec_info_provider *old_entry = NULL; + struct list_item *clist; + k_spinlock_key_t key; + size_t items_bytes; + int ret; + + assert_can_be_cold(); + + if (!owner || !items || !count) + return -EINVAL; + + if (count > (SIZE_MAX - sizeof(*entry)) / sizeof(uint32_t)) + return -EINVAL; + + items_bytes = count * sizeof(uint32_t); + entry = rmalloc(SOF_MEM_FLAG_KERNEL, sizeof(*entry) + items_bytes); + if (!entry) + return -ENOMEM; + + entry->owner = owner; + entry->count = count; + ret = memcpy_s(entry->items, items_bytes, items, items_bytes); + if (ret < 0) { + rfree(entry); + return ret; + } + + key = k_spin_lock(&codec_info_lock); + list_for_item(clist, &codec_info_providers) { + struct sof_codec_info_provider *cur = + container_of(clist, struct sof_codec_info_provider, list); + + if (cur->owner == owner) { + list_item_del(&cur->list); + old_entry = cur; + break; + } + } + list_item_append(&entry->list, &codec_info_providers); + k_spin_unlock(&codec_info_lock, key); + + if (old_entry) + rfree(old_entry); + + return 0; +} + +__cold void sof_codec_info_unregister(const void *owner) +{ + struct list_item *clist, *tmp; + struct list_item to_free = LIST_INIT(to_free); + k_spinlock_key_t key; + + assert_can_be_cold(); -static void get_codec_info(struct sof_tlv **tuple) + if (!owner) + return; + + key = k_spin_lock(&codec_info_lock); + list_for_item_safe(clist, tmp, &codec_info_providers) { + struct sof_codec_info_provider *cur = + container_of(clist, struct sof_codec_info_provider, list); + + if (cur->owner == owner) { + list_item_del(&cur->list); + list_item_append(&cur->list, &to_free); + } + } + k_spin_unlock(&codec_info_lock, key); + + list_for_item_safe(clist, tmp, &to_free) { + struct sof_codec_info_provider *cur = + container_of(clist, struct sof_codec_info_provider, list); + + list_item_del(&cur->list); + rfree(cur); + } +} + +static void get_codec_info(struct sof_tlv **tuple, bool include_empty) { struct sof_ipc4_codec_info_data codec_info = { 0 }; + struct list_item *clist; + k_spinlock_key_t key; + uint32_t i; -#ifdef CONFIG_CADENCE_CODEC_AAC_DEC - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_AAC, SOF_IPC_STREAM_PLAYBACK); -#endif -#ifdef CONFIG_CADENCE_CODEC_MP3_DEC - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_MP3, SOF_IPC_STREAM_PLAYBACK); -#endif -#ifdef CONFIG_CADENCE_CODEC_MP3_ENC - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_MP3, SOF_IPC_STREAM_CAPTURE); -#endif -#ifdef CONFIG_CADENCE_CODEC_VORBIS_DEC - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_VORBIS, SOF_IPC_STREAM_PLAYBACK); -#endif -#ifdef CONFIG_COMP_MFCC - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_BESPOKE, SOF_IPC_STREAM_CAPTURE); -#endif -#if defined(CONFIG_SOF_COMPRESS_CODEC_PCM_DEC) || defined(CONFIG_FFMPEG_FILTER) - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_PCM, SOF_IPC_STREAM_PLAYBACK); -#endif -#ifdef CONFIG_FFMPEG_DEC_FLAC - /* FLAC decode is provided by the ffmpeg_dec (libavcodec) module. */ - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_FLAC, SOF_IPC_STREAM_PLAYBACK); -#endif -#ifdef CONFIG_FFMPEG_DEC_ALAC - /* ALAC decode is provided by the ffmpeg_dec (libavcodec) module. */ - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_ALAC, SOF_IPC_STREAM_PLAYBACK); -#endif -#if defined(CONFIG_FFMPEG_DEC_AAC) && !defined(CONFIG_CADENCE_CODEC_AAC_DEC) - /* AAC-LC decode is provided by the ffmpeg_dec (libavcodec) module (guarded so - * it is not advertised twice when the Cadence AAC decoder is also present). - */ - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_AAC, SOF_IPC_STREAM_PLAYBACK); -#endif -#if defined(CONFIG_FFMPEG_DEC_MP3) && !defined(CONFIG_CADENCE_CODEC_MP3_DEC) - /* MP3 decode is provided by the ffmpeg_dec (libavcodec) module (guarded so - * it is not advertised twice when the Cadence MP3 decoder is also present). - */ - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_MP3, SOF_IPC_STREAM_PLAYBACK); -#endif -#if defined(CONFIG_FFMPEG_ENC_MP3) && !defined(CONFIG_CADENCE_CODEC_MP3_ENC) - /* MP3 encode (compress capture) via ffmpeg_dec (libshine), guarded so it is - * not advertised twice when the Cadence MP3 encoder is also present. - */ - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_MP3, SOF_IPC_STREAM_CAPTURE); -#endif -#if defined(CONFIG_FFMPEG_ENC_AAC) - /* AAC-LC encode (compress capture) via ffmpeg_dec (vo-aacenc). */ - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_AAC, SOF_IPC_STREAM_CAPTURE); -#endif -#if defined(CONFIG_FFMPEG_ENC_FLAC) - /* FLAC encode (compress capture) via ffmpeg_enc (native libavcodec). */ - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_FLAC, SOF_IPC_STREAM_CAPTURE); -#endif -#if defined(CONFIG_FFMPEG_ENC_ALAC) - /* ALAC encode (compress capture) via ffmpeg_enc (native libavcodec). */ - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_ALAC, SOF_IPC_STREAM_CAPTURE); -#endif -#if defined(CONFIG_FFMPEG_DEC_SBC) - /* Bluetooth SBC / mSBC decode via ffmpeg_dec (native libavcodec). */ - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_SBC, SOF_IPC_STREAM_PLAYBACK); -#endif -#if defined(CONFIG_FFMPEG_ENC_SBC) - /* Bluetooth SBC / mSBC encode via ffmpeg_enc (native libavcodec). */ - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_SBC, SOF_IPC_STREAM_CAPTURE); -#endif -#if defined(CONFIG_FFMPEG_DEC_G722) - /* ITU-T G.722 wideband speech decode via ffmpeg_dec (native libavcodec). */ - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_G722, SOF_IPC_STREAM_PLAYBACK); -#endif -#if defined(CONFIG_FFMPEG_ENC_G722) - /* ITU-T G.722 wideband speech encode via ffmpeg_enc (native libavcodec). */ - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_G722, SOF_IPC_STREAM_CAPTURE); -#endif -#if defined(CONFIG_FFMPEG_DEC_APTX) - /* Bluetooth aptX decode via ffmpeg_dec (native libavcodec). */ - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_APTX, SOF_IPC_STREAM_PLAYBACK); -#endif -#if defined(CONFIG_FFMPEG_ENC_APTX) - /* Bluetooth aptX encode via ffmpeg_enc (native libavcodec). */ - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_APTX, SOF_IPC_STREAM_CAPTURE); -#endif -#if (defined(CONFIG_FFMPEG_ENC_SBC) || defined(CONFIG_FFMPEG_ENC_G722) || \ - defined(CONFIG_FFMPEG_ENC_APTX)) && !defined(CONFIG_COMP_MFCC) - /* Also advertise BESPOKE capture for kernels with older SND_AUDIOCODEC_MAX. */ - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_BESPOKE, SOF_IPC_STREAM_CAPTURE); -#endif -#ifdef CONFIG_FFMPEG_DEC_OPUS - /* Opus decode is provided by the ffmpeg_dec (libavcodec) module. - * Advertise OPUS_RAW and BESPOKE. - */ - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_OPUS_RAW, SOF_IPC_STREAM_PLAYBACK); -#endif -#if defined(CONFIG_FFMPEG_DEC_OPUS) || defined(CONFIG_FFMPEG_DEC_SBC) || \ - defined(CONFIG_FFMPEG_DEC_G722) || defined(CONFIG_FFMPEG_DEC_APTX) - codec_info.items[codec_info.count++] = - SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_BESPOKE, SOF_IPC_STREAM_PLAYBACK); -#endif + key = k_spin_lock(&codec_info_lock); + list_for_item(clist, &codec_info_providers) { + struct sof_codec_info_provider *cur = + container_of(clist, struct sof_codec_info_provider, list); + + for (i = 0; i < cur->count; i++) + add_codec_info_item(&codec_info, cur->items[i]); + } + k_spin_unlock(&codec_info_lock, key); - if (!codec_info.count) + if (!codec_info.count && !include_empty) return; tlv_value_set(*tuple, IPC4_SOF_CODEC_INFO, sizeof(codec_info.count) + @@ -220,16 +221,28 @@ static void get_codec_info(struct sof_tlv **tuple) #define SOF_CONFIG_MEMBER_SIZE(struct_name) (sizeof(struct sof_tlv) + \ sizeof(struct struct_name)) -#define SOF_CONFIG_SIZE_MAX (SOF_CONFIG_MEMBER_SIZE(sof_ipc4_codec_info_data)) +#define SOF_CONFIG_SIZE_MAX (SOF_CONFIG_MEMBER_SIZE(sof_ipc4_codec_info_data) + \ + sizeof(struct sof_tlv) + sizeof(uint32_t)) + +static uint32_t build_sof_info_tlvs(char *buf, bool include_empty_codec_info) +{ + struct sof_tlv *sof_config_tuple = (struct sof_tlv *)buf; + + get_codec_info(&sof_config_tuple, include_empty_codec_info); + + tlv_value_uint32_set(sof_config_tuple, IPC4_SOF_CAPS, + IPC4_SOF_CAP_RUNTIME_SOF_INFO); + sof_config_tuple = tlv_next(sof_config_tuple); + + return (uint32_t)((char *)sof_config_tuple - buf); +} static void base_fw_sof_config(struct sof_tlv **tuple) { char sof_config_data[SOF_CONFIG_SIZE_MAX] = { 0 }; - struct sof_tlv *sof_config_tuple = (struct sof_tlv *)sof_config_data; uint32_t sof_config_size; - get_codec_info(&sof_config_tuple); - sof_config_size = (uint32_t)((char *)sof_config_tuple - sof_config_data); + sof_config_size = build_sof_info_tlvs(sof_config_data, false); if (sof_config_size == 0) return; @@ -238,6 +251,27 @@ static void base_fw_sof_config(struct sof_tlv **tuple) *tuple = tlv_next(*tuple); } +__cold static int basefw_sof_info_get(uint32_t *data_offset, char *data) +{ + char sof_config_data[SOF_CONFIG_SIZE_MAX] = { 0 }; + uint32_t sof_config_size; + int ret; + + assert_can_be_cold(); + + sof_config_size = build_sof_info_tlvs(sof_config_data, true); + if (*data_offset < sof_config_size) + return IPC4_INVALID_CONFIG_DATA_LEN; + + ret = memcpy_s(data, *data_offset, sof_config_data, sof_config_size); + if (ret < 0) + return IPC4_OUT_OF_MEMORY; + + *data_offset = sof_config_size; + + return IPC4_SUCCESS; +} + __cold static int basefw_config(uint32_t *data_offset, char *data) { uint16_t version[4] = {SOF_MAJOR, SOF_MINOR, SOF_MICRO, SOF_BUILD}; @@ -825,6 +859,8 @@ __cold static int basefw_get_large_config(struct comp_dev *dev, uint32_t param_i return io_global_perf_state_get(data_offset, data); case IPC4_IO_GLOBAL_PERF_DATA: return io_global_perf_data_get(data_offset, data); + case IPC4_SOF_INFO_GET: + return basefw_sof_info_get(data_offset, data); /* TODO: add more support */ case IPC4_DSP_RESOURCE_STATE: diff --git a/src/kernel/library_manager/lib_manager.c b/src/kernel/library_manager/lib_manager.c index f2b25edd7ea2..de8f11bf73bd 100644 --- a/src/kernel/library_manager/lib_manager.c +++ b/src/kernel/library_manager/lib_manager.c @@ -1370,3 +1370,38 @@ __cold int lib_manager_load_library(uint32_t dma_id, uint32_t lib_id, uint32_t t return ret; } + +__cold int lib_manager_unload_library(uint32_t lib_id) +{ + struct ext_library *_ext_lib = ext_lib_get(); + struct lib_manager_mod_ctx *ctx; + uint32_t module_id; + int ret; + + assert_can_be_cold(); + + if (lib_id == 0 || lib_id >= LIB_MANAGER_MAX_LIBS || !_ext_lib) + return -EINVAL; + + ctx = _ext_lib->desc[lib_id]; + if (!ctx) + return -ENOENT; + + module_id = LIB_MANAGER_PACK_LIB_ID(lib_id); + if (ctx->load_type == SOF_MAN_MOD_TYPE_LLEXT || + ctx->load_type == SOF_MAN_MOD_TYPE_LLEXT_AUX) { + ret = llext_manager_remove_library(module_id); + if (ret < 0) + return ret; + } + + if (ctx->base_addr) + rfree(ctx->base_addr); + + rfree(ctx); + _ext_lib->desc[lib_id] = NULL; + + tr_info(&lib_manager_tr, "unloaded library id: %u", lib_id); + + return 0; +} diff --git a/src/kernel/library_manager/llext_manager.c b/src/kernel/library_manager/llext_manager.c index 267955040cce..26e4a7df4031 100644 --- a/src/kernel/library_manager/llext_manager.c +++ b/src/kernel/library_manager/llext_manager.c @@ -10,6 +10,7 @@ */ #include +#include #include #include #include @@ -1170,6 +1171,140 @@ __cold int llext_manager_free_module(const uint32_t component_id) return llext_manager_unload_module(mctx); } +__cold static const void *llext_manager_elf_find_section(const uint8_t *elf, size_t elf_size, + const char *name, size_t *sec_size) +{ + const elf_ehdr_t *ehdr; + const elf_shdr_t *shdrs; + const elf_shdr_t *shstr; + const char *strtab; + unsigned int i; + + assert_can_be_cold(); + + if (!elf || elf_size < sizeof(*ehdr)) + return NULL; + + ehdr = (const elf_ehdr_t *)elf; + if (ehdr->e_ident[0] != 0x7f || ehdr->e_ident[1] != 'E' || + ehdr->e_ident[2] != 'L' || ehdr->e_ident[3] != 'F') + return NULL; + + if (ehdr->e_shentsize != sizeof(*shdrs) || !ehdr->e_shnum) + return NULL; + + if (ehdr->e_shoff > elf_size || + ehdr->e_shnum > (elf_size - ehdr->e_shoff) / sizeof(*shdrs)) + return NULL; + + if (ehdr->e_shstrndx >= ehdr->e_shnum) + return NULL; + + shdrs = (const elf_shdr_t *)(elf + ehdr->e_shoff); + shstr = &shdrs[ehdr->e_shstrndx]; + if (shstr->sh_offset > elf_size || shstr->sh_size > elf_size - shstr->sh_offset) + return NULL; + + strtab = (const char *)(elf + shstr->sh_offset); + + for (i = 0; i < ehdr->e_shnum; i++) { + const char *sec_name; + size_t max_len; + + if (shdrs[i].sh_name >= shstr->sh_size) + continue; + + sec_name = strtab + shdrs[i].sh_name; + max_len = shstr->sh_size - shdrs[i].sh_name; + if (strnlen(sec_name, max_len) == max_len) + continue; + + if (strcmp(sec_name, name) != 0) + continue; + + if (shdrs[i].sh_type == SHT_NOBITS || !shdrs[i].sh_size) + return NULL; + + if (shdrs[i].sh_offset > elf_size || + shdrs[i].sh_size > elf_size - shdrs[i].sh_offset) + return NULL; + + *sec_size = shdrs[i].sh_size; + return elf + shdrs[i].sh_offset; + } + + return NULL; +} + +__cold void llext_manager_register_lib_codecs(const struct lib_manager_mod_ctx *ctx, + const struct sof_man_fw_desc *desc) +{ + const struct sof_man_module *mod_array; + size_t lib_size; + unsigned int k; + + assert_can_be_cold(); + + if (!ctx || !ctx->mod || !desc) + return; + + mod_array = (const struct sof_man_module *)((const uint8_t *)desc + + SOF_MAN_MODULE_OFFSET(0)); + if (size_mul_overflow(desc->header.preload_page_count, PAGE_SZ, &lib_size)) + return; + + for (k = 0; k < ctx->n_mod; k++) { + const struct lib_manager_module *mctx = &ctx->mod[k]; + uint32_t entry_index = mctx->start_idx; + uint32_t next_idx = entry_index + mctx->n_mod; + size_t mod_offset; + size_t mod_size; + const void *sec; + size_t sec_size = 0; + int ret; + + if (entry_index >= desc->header.num_module_entries) + continue; + + mod_offset = mod_array[entry_index].segment[LIB_MANAGER_TEXT].file_offset; + if (mod_offset >= lib_size) + continue; + + if (next_idx >= desc->header.num_module_entries) + mod_size = lib_size - mod_offset; + else + mod_size = ALIGN_UP(mod_array[next_idx].segment[LIB_MANAGER_TEXT].file_offset - + mod_offset, PAGE_SZ); + + if (mod_size > lib_size - mod_offset) + continue; + + sec = llext_manager_elf_find_section((const uint8_t *)desc - + SOF_MAN_ELF_TEXT_OFFSET + mod_offset, + mod_size, SOF_CODEC_INFO_SECTION, &sec_size); + if (!sec || sec_size < sizeof(uint32_t) || (sec_size % sizeof(uint32_t)) != 0) + continue; + + ret = sof_codec_info_register(mctx, sec, sec_size / sizeof(uint32_t)); + if (ret < 0) + tr_warn(&lib_manager_tr, "failed to register codec info for mod %u: %d", + k, ret); + } +} + +__cold void llext_manager_unregister_lib_codecs(const struct lib_manager_mod_ctx *ctx) +{ + unsigned int k; + + assert_can_be_cold(); + + if (!ctx || !ctx->mod) + return; + + for (k = 0; k < ctx->n_mod; k++) + sof_codec_info_unregister(&ctx->mod[k]); +} + /* An auxiliary library has been loaded, need to read in its exported symbols */ __cold int llext_manager_add_library(uint32_t module_id) { @@ -1207,6 +1342,51 @@ __cold int llext_manager_add_library(uint32_t module_id) } ctx->load_type = SOF_MAN_MOD_TYPE_LLEXT; + llext_manager_register_lib_codecs(ctx, desc); + + return 0; +} + +__cold int llext_manager_remove_library(uint32_t module_id) +{ + struct lib_manager_mod_ctx *const ctx = lib_manager_get_mod_ctx(module_id); + unsigned int k; + + assert_can_be_cold(); + + if (!ctx || !ctx->mod) + return -ENOENT; + + for (k = 0; k < ctx->n_mod; k++) { + const struct lib_manager_module *mctx = &ctx->mod[k]; + + if (mctx->mapped || mctx->n_dependent || + (mctx->llext && mctx->llext->use_count > 1)) + return -EBUSY; + } + + llext_manager_unregister_lib_codecs(ctx); + + for (k = 0; k < ctx->n_mod; k++) { + struct lib_manager_module *mctx = &ctx->mod[k]; + + if (mctx->llext) { + int ret = llext_unload(&mctx->llext); + + if (ret < 0) + tr_warn(&lib_manager_tr, "llext_unload(%u) failed: %d", k, ret); + mctx->llext = NULL; + } + if (mctx->ebl) { + rfree(mctx->ebl); + mctx->ebl = NULL; + } + } + + rfree(ctx->mod); + ctx->mod = NULL; + ctx->n_mod = 0; + return 0; } diff --git a/src/kernel/library_manager/llext_manager_dram.c b/src/kernel/library_manager/llext_manager_dram.c index 2f6cff2b3501..35430d7784f1 100644 --- a/src/kernel/library_manager/llext_manager_dram.c +++ b/src/kernel/library_manager/llext_manager_dram.c @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -274,6 +275,12 @@ int llext_manager_restore_from_dram(void) } _ext_lib->desc[i] = ctx; + if (ctx->base_addr) { + const struct sof_man_fw_desc *desc = (const struct sof_man_fw_desc *) + ((const uint8_t *)ctx->base_addr + SOF_MAN_ELF_TEXT_OFFSET); + + llext_manager_register_lib_codecs(ctx, desc); + } } if (lib_manager_dram.n_llext) { @@ -325,6 +332,8 @@ int llext_manager_restore_from_dram(void) struct lib_manager_module *mod = ctx->mod; + llext_manager_unregister_lib_codecs(ctx); + if (!mod) continue;