Skip to content
2 changes: 2 additions & 0 deletions app/os_linux_overlay.conf
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ CONFIG_LOG_RUNTIME_DEFAULT_LEVEL=3

# Disable additional VMH buffers to use memory for virtual regions.
CONFIG_VIRTUAL_HEAP_EXTENDED=n

CONFIG_MTRACE_LOG_CACHE_BUF_SIZE=32768
2 changes: 1 addition & 1 deletion app/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ CONFIG_LOG_MODE_DEFERRED=y
CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD=1
# Frontend buffer must be large enough to cover all
# typical bursts of log messages.
CONFIG_LOG_BUFFER_SIZE=4096
CONFIG_LOG_BUFFER_SIZE=16384
# Use stack that is sufficient for SOF backends
CONFIG_LOG_PROCESS_THREAD_STACK_SIZE=4096
# Use Linux kernel style timestamp format
Expand Down
4 changes: 4 additions & 0 deletions src/audio/module_adapter/module/generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ __cold int module_init(struct processing_module *mod)
ret = scheduler_dp_thread_ipc(mod, SOF_IPC4_MOD_INIT_INSTANCE, &param);
} else
#endif
{
comp_info(dev, "module_init: calling module init callback");
ret = interface->init(mod);
comp_info(dev, "module_init: module init callback returned %d", ret);
}

if (ret) {
comp_err(dev, "error %d: module specific init failed", ret);
Expand Down
4 changes: 4 additions & 0 deletions src/audio/module_adapter/module_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ __cold struct comp_dev *module_adapter_new_ext(const struct comp_driver *drv,
goto err;
}

comp_info(dev, "module adapter: proc_domain=%u period=%u",
(unsigned int)dev->ipc_config.proc_domain,
(unsigned int)dev->period);

module_adapter_reset_data(dst);

dev->state = COMP_STATE_READY;
Expand Down
33 changes: 31 additions & 2 deletions src/audio/volume/volume_ipc4.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ LOG_MODULE_DECLARE(volume, CONFIG_SOF_LOG_LEVEL);

#include "volume.h"

/* Debug only: remove to disable the deliberate fault in volume_init() */
#define VOLUME_INJECT_FAULT 1

void set_volume_process(struct vol_data *cd, struct comp_dev *dev, bool source_or_sink)
{
cd->scale_vol = vol_get_processing_function(dev, cd);
Expand Down Expand Up @@ -115,8 +118,17 @@ int volume_init(struct processing_module *mod)
uint8_t channel_cfg;
uint8_t channel;
bool all_channels;


#ifdef VOLUME_INJECT_FAULT
/* Debug only: deliberately write to NULL to raise a DSP exception */
comp_err(dev, "volume_init: injecting NULL pointer write fault");
*(volatile uint32_t *)NULL = 0xdeadbeef;
#endif

uint32_t instance_id = IPC4_INST_ID(dev_comp_id(dev));


if (instance_id >= IPC4_MAX_PEAK_VOL_REG_SLOTS) {
comp_err(dev, "instance_id %u out of array bounds.", instance_id);
return -EINVAL;
Expand Down Expand Up @@ -147,34 +159,46 @@ int volume_init(struct processing_module *mod)
return -EINVAL;
}

comp_info(dev, "volume_init: config validated, channels %u all_channels %u",
channels_count, (unsigned int)all_channels);

comp_info(dev, "volume_init: allocating vol_data, size %zu", sizeof(struct vol_data));
cd = mod_zalloc(mod, sizeof(struct vol_data));
if (!cd)
if (!cd) {
comp_err(dev, "volume_init: vol_data allocation failed");
return -ENOMEM;
}
comp_info(dev, "volume_init: vol_data allocated %p", cd);

/*
* malloc memory to store current volume 4 times to ensure the address
* is 8-byte aligned for multi-way xtensa intrinsic operations.
*/
comp_info(dev, "volume_init: allocating volume buffer, size %zu", vol_size);
cd->vol = mod_alloc_align(mod, vol_size, SOF_FRAME_BYTE_ALIGN);
if (!cd->vol) {
mod_free(mod, cd);
comp_err(dev, "Failed to allocate %d", vol_size);
return -ENOMEM;
}
comp_info(dev, "volume_init: volume buffer allocated %p", cd->vol);

/* malloc memory to store temp peak volume 4 times to ensure the address
* is 8-byte aligned for multi-way xtensa intrinsic operations.
*/
comp_info(dev, "volume_init: allocating peak buffer, size %zu", vol_size);
cd->peak_vol = mod_zalloc(mod, vol_size);
if (!cd->peak_vol) {
mod_free(mod, cd->vol);
mod_free(mod, cd);
comp_err(dev, "Failed to allocate %d for peak_vol", vol_size);
return -ENOMEM;
}
comp_info(dev, "volume_init: peak buffer allocated %p", cd->peak_vol);

md->private = cd;

comp_info(dev, "volume_init: initializing channels");
for (channel = 0; channel < channels_count; channel++) {
channel_cfg = all_channels ? 0 : channel;

Expand All @@ -188,20 +212,26 @@ int volume_init(struct processing_module *mod)

/* set muted as false*/
cd->muted[channel] = false;
comp_info(dev, "volume_init: channel %u initialized, target volume %#x",
(unsigned int)channel, target_volume[channel]);
}

comp_info(dev, "volume_init: initializing ramp");
init_ramp(cd, vol->config[0].curve_duration, target_volume[0]);

volume_set_ramp_channel_counter(cd, channels_count);
comp_info(dev, "volume_init: ramp initialized");

cd->mailbox_offset = offsetof(struct ipc4_fw_registers, peak_vol_regs);
cd->mailbox_offset += instance_id * sizeof(struct ipc4_peak_volume_regs);

cd->attenuation = 0;
cd->is_passthrough = false;

comp_info(dev, "volume_init: mailbox offset %#x, resetting state", cd->mailbox_offset);
volume_reset_state(cd);

comp_info(dev, "volume_init: completed");
return 0;
}

Expand Down Expand Up @@ -477,4 +507,3 @@ int volume_peak_prepare(struct vol_data *cd, struct processing_module *mod)

return 0;
}

8 changes: 8 additions & 0 deletions src/include/sof/ipc/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ struct ipc_user_xcore {

struct ipc_user {
struct k_thread *thread[IPC_DSP_CORE_COUNT];
/** @brief Per-core helper thread objects used for INIT_INSTANCE processing */
struct k_thread *init_thread[IPC_DSP_CORE_COUNT];
/** @brief Per-core stacks for the INIT_INSTANCE helper threads */
k_thread_stack_t *init_stack[IPC_DSP_CORE_COUNT];
/** @brief Core the currently forwarded command is executed on */
unsigned int cmd_core;
/** @brief Result of the INIT_INSTANCE helper thread */
int init_result;
struct k_sem *sem;
struct k_event *event[IPC_DSP_CORE_COUNT];
/** @brief Copy of IPC4 message primary word forwarded to user thread */
Expand Down
2 changes: 1 addition & 1 deletion src/ipc/ipc-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ void ipc_user_thread_fn(void *p1, void *p2, void *p3)
IPC_USER_EVENT_XCORE,
false, K_FOREVER);

LOG_DBG("IPC user wake, mask %u", mask);
LOG_INF("IPC user wake, mask %u", mask);

/* idc_cmd_user() is only built with SMP, there are no requests without */
if (IS_ENABLED(CONFIG_SMP) && (mask & IPC_USER_EVENT_XCORE)) {
Expand Down
103 changes: 87 additions & 16 deletions src/ipc/ipc4/handler-user.c
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,64 @@ __cold int ipc4_process_large_config_set(struct ipc4_message_request *ipc4)
}

#ifdef CONFIG_SOF_USERSPACE_LL
/**
* @brief Helper thread entry performing SOF_IPC4_MOD_INIT_INSTANCE.
*
* Runs in user space on the same core as the IPC user thread that spawned
* it. The IPC message words and driver pointer are taken from the shared
* @c ipc_user context (@p p1); the result is stored in
* @c ipc_user->init_result.
*
* @param p1 struct ipc_user pointer
* @param p2 unused
* @param p3 unused
*/
static void ipc_user_init_instance_fn(void *p1, void *p2, void *p3)
{
struct ipc_user *ipc_user = p1;
struct ipc4_message_request msg;
const struct comp_driver *drv = ipc_user->init_drv;
struct comp_dev *dev;
int result;

ARG_UNUSED(p2);
ARG_UNUSED(p3);

msg.primary.dat = ipc_user->ipc_msg_pri;
msg.extension.dat = ipc_user->ipc_msg_ext;

/* User thread creates the component: drv->ops.create() runs in
* user-space so untrusted module code does not execute with kernel
* privileges. init_drv is the original kernel pointer.
*/
tr_info(&ipc_tr, "INIT_INSTANCE: driver received %p", drv);
ipc_user->init_drv = NULL;
if (!drv) {
tr_err(&ipc_tr, "INIT_INSTANCE: driver missing");
ipc_user->init_result = IPC4_MOD_NOT_INITIALIZED;
return;
}

dev = comp_new_ipc4_user(&msg, drv);
if (!dev) {
tr_err(&ipc_tr, "INIT_INSTANCE: component creation failed");
ipc_user->init_result = IPC4_MOD_NOT_INITIALIZED;
return;
}

result = ipc4_add_comp_dev(dev);
if (result != IPC4_SUCCESS) {
tr_err(&ipc_tr, "INIT_INSTANCE: component registration failed %d", result);
ipc_user->init_result = result;
return;
}

comp_update_ibs_obs_cpc(dev);

ipc_user->init_result = 0;
tr_info(&ipc_tr, "INIT_INSTANCE: completed");
}

/*
* IPC4 implementation of the user-thread dispatch hook. Runs in the
* user-space IPC thread; reconstructs the IPC4 message from the forwarded
Expand All @@ -1064,6 +1122,8 @@ int ipc_user_thread_dispatch(struct ipc_user *ipc_user)
msg.primary.dat = ipc_user->ipc_msg_pri;
msg.extension.dat = ipc_user->ipc_msg_ext;

tr_info(&ipc_tr, "user dispatch: message primary type %#x", msg.primary.r.type);

ipc_user->reply_ext = 0;

if (msg.primary.r.msg_tgt == SOF_IPC4_MESSAGE_TARGET_MODULE_MSG) {
Expand Down Expand Up @@ -1097,34 +1157,45 @@ int ipc_user_thread_dispatch(struct ipc_user *ipc_user)
break;
}
case SOF_IPC4_MOD_INIT_INSTANCE: {
/* User thread creates the component —
* drv->ops.create() runs in user-space so untrusted
* module code does not execute with kernel privileges.
*
* init_drv = original kernel pointer
/*
* Run the instance creation in a helper thread pinned to
* the same core as this IPC user thread, then join it.
* Message words and init_drv are already in ipc_user.
*/
const struct comp_driver *drv = ipc_user->init_drv;
unsigned int core = ipc_user->cmd_core;
struct k_thread *thread;

ipc_user->init_drv = NULL;
if (!drv) {
if (core >= CONFIG_CORE_COUNT || !ipc_user->init_thread[core]) {
result = IPC4_MOD_NOT_INITIALIZED;
break;
}

struct comp_dev *dev = comp_new_ipc4_user(&msg, drv);

if (!dev) {
thread = ipc_user->init_thread[core];
ipc_user->init_result = IPC4_MOD_NOT_INITIALIZED;

k_thread_create(thread, ipc_user->init_stack[core],
CONFIG_SOF_IPC_USER_THREAD_STACK_SIZE,
ipc_user_init_instance_fn, ipc_user, NULL, NULL,
-1, K_USER | K_INHERIT_PERMS, K_FOREVER);
result = sof_thread_cpu_pin(thread, core);
if (result) {
tr_err(&ipc_tr, "INIT_INSTANCE: cpu pin failed %d", result);
/* never started: abort so the objects can be reused */
k_thread_abort(thread);
result = IPC4_MOD_NOT_INITIALIZED;
break;
}
k_thread_name_set(thread, "ipc_init");
k_thread_start(thread);

result = ipc4_add_comp_dev(dev);
if (result != IPC4_SUCCESS)
result = k_thread_join(thread, K_FOREVER);
if (result) {
tr_err(&ipc_tr, "INIT_INSTANCE: join failed %d", result);
result = IPC4_MOD_NOT_INITIALIZED;
break;
}

comp_update_ibs_obs_cpc(dev);

result = 0;
result = ipc_user->init_result;
break;
}
case SOF_IPC4_MOD_DELETE_INSTANCE: {
Expand Down
21 changes: 18 additions & 3 deletions src/kernel/ipc/ipc-common-kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,10 @@ SOF_DEFINE_REG_UUID(sec_core_init);
static K_THREAD_STACK_ARRAY_DEFINE(ipc_user_stack, CONFIG_CORE_COUNT,
CONFIG_SOF_IPC_USER_THREAD_STACK_SIZE);

/* Stacks for the per-core helper threads spawned by the IPC user thread */
static K_THREAD_STACK_ARRAY_DEFINE(ipc_init_stack, CONFIG_CORE_COUNT,
CONFIG_SOF_IPC_USER_THREAD_STACK_SIZE);

/*
* Kernel-only copies of the IPC user thread state. struct ipc_user and the
* ipc->ipc_user_pdata pointer are both writable by user threads, so kernel
Expand Down Expand Up @@ -513,7 +517,7 @@ int ipc_user_forward_cmd(uint32_t primary, uint32_t extension, unsigned int core
pdata->ipc_msg_pri = primary;
pdata->ipc_msg_ext = extension;
pdata->ipc = ipc;

pdata->cmd_core = core;
/* Prevent host completion until user thread finishes */
key = k_spin_lock(&ipc_ks(ipc)->lock);
ipc_ks(ipc)->task_mask |= IPC_TASK_IN_THREAD;
Expand Down Expand Up @@ -545,6 +549,7 @@ __cold static int ipc_user_init_thread(struct ipc_user *ipc_user, unsigned int c
{
char thread_name[] = "ll_user0";
struct k_thread *thread;
struct k_thread *init_thread;
struct k_event *event;
int ret;

Expand All @@ -570,6 +575,13 @@ __cold static int ipc_user_init_thread(struct ipc_user *ipc_user, unsigned int c
goto e_event;
}

init_thread = k_object_alloc(K_OBJ_THREAD);
if (!init_thread) {
LOG_ERR("user IPC init thread alloc failed");
ret = -ENOMEM;
goto e_thread;
}

k_thread_create(thread, ipc_user_stack[core],
CONFIG_SOF_IPC_USER_THREAD_STACK_SIZE,
ipc_user_thread_fn, ipc_user, UINT_TO_POINTER(core), NULL,
Expand All @@ -584,6 +596,7 @@ __cold static int ipc_user_init_thread(struct ipc_user *ipc_user, unsigned int c
* and signal completion on the primary core semaphore
*/
k_thread_access_grant(thread, ipc_user_ksem, event);
k_thread_access_grant(thread, init_thread, ipc_init_stack[core]);
user_grant_dai_access_all(thread);
user_grant_dma_access_all(thread);
k_mem_domain_add_thread(zephyr_ll_mem_domain(), thread);
Expand All @@ -597,9 +610,11 @@ __cold static int ipc_user_init_thread(struct ipc_user *ipc_user, unsigned int c
/* for ipc_user_thread_fn() and ipc_thread_user() in user context */
ipc_user->event[core] = event;
ipc_user->thread[core] = thread;

ipc_user->init_thread[core] = init_thread;
ipc_user->init_stack[core] = ipc_init_stack[core];
return 0;

e_thread:
k_object_free(thread);
e_event:
k_object_free(event);

Expand Down
15 changes: 15 additions & 0 deletions zephyr/include/sof/lib/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ void cpu_notify_state_exit(enum pm_state state);
*/
#if defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION)
__syscall int cpu_get_id(void);

struct k_thread;

/**
* @brief Pin a (not yet started) thread to a single DSP core.
*
* k_thread_cpu_pin() is a plain kernel function that takes the scheduler
* lock and therefore cannot be called from user mode. This system call
* lets a user-mode thread pin a helper thread it has just created.
*
* @param thread Thread object the caller has access to
* @param cpu Target core id
* @return 0 on success, negative error code otherwise
*/
__syscall int sof_thread_cpu_pin(struct k_thread *thread, int cpu);
#endif

/* let the compiler optimise when in single core mode */
Expand Down
Loading