Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 88 additions & 3 deletions src/audio/module_adapter/module/generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,83 @@ int z_impl_mod_free(struct processing_module *mod, const void *ptr)
EXPORT_SYMBOL(z_impl_mod_free);

#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION)
#if CONFIG_SOF_USERSPACE_LL
/* an LL module whose resources the LL and IPC user threads use in user mode */
struct mod_res_user_entry {
struct list_item list;
struct processing_module *mod;
};

/* the LL modules registered with mod_res_user_add(), under mod_res_ulock */
static APP_SYSUSER_DATA struct list_item mod_res_user_list = LIST_INIT(mod_res_user_list);

int mod_res_user_add(struct processing_module *mod)
{
struct mod_res_user_entry *entry;

if (!k_is_user_context())
return -EPERM;

/* the IPC user threads of all cores walk the list */
entry = sof_heap_alloc(sof_sys_user_heap_get(), SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT,
sizeof(*entry), 0);
if (!entry)
return -ENOMEM;

entry->mod = mod;

sys_mutex_lock(&mod_res_ulock, K_FOREVER);
list_item_append(&entry->list, &mod_res_user_list);
sys_mutex_unlock(&mod_res_ulock);

return 0;
}

void mod_res_user_remove(struct processing_module *mod)
{
struct mod_res_user_entry *entry = NULL;
struct list_item *item;

if (!k_is_user_context())
return;

sys_mutex_lock(&mod_res_ulock, K_FOREVER);
list_for_item(item, &mod_res_user_list) {
entry = container_of(item, struct mod_res_user_entry, list);
if (entry->mod == mod) {
list_item_del(item);
break;
}
entry = NULL;
}
sys_mutex_unlock(&mod_res_ulock);

sof_heap_free(sof_sys_user_heap_get(), entry);
}

static bool mod_res_user_listed(const struct processing_module *mod)
{
struct mod_res_user_entry *entry;
struct list_item *item;
bool found = false;

/* not an LL or IPC user thread */
if (sys_mutex_lock(&mod_res_ulock, K_FOREVER))
return false;

list_for_item(item, &mod_res_user_list) {
entry = container_of(item, struct mod_res_user_entry, list);
if (entry->mod == mod) {
found = true;
break;
}
}
sys_mutex_unlock(&mod_res_ulock);

return found;
}
#endif

/*
* With CONFIG_SOF_USERSPACE_LL, the LL and IPC user threads use the resources
* of LL modules in user mode: the module memory, its resource pool and the
Expand All @@ -614,14 +691,22 @@ EXPORT_SYMBOL(z_impl_mod_free);
* kernel structures behind their vregions. In kernel mode the system call
* wrappers call the implementation directly.
*
* The processing domain is user-writable, so a user thread can only pick the
* wrong path for itself: the system calls reject LL modules.
* The user-mode path is taken only for the LL modules registered in LL
* memory when they were created, not by the processing domain or other
* module data: the comp_dev and module objects of a DP module are in DP
* memory, so a DP thread could make them look like an LL module, or hand
* over a made-up module, whose resource pool the IPC user thread would then
* walk in user mode. Any other module goes to the system calls, which reject
* LL modules and modules without a live vregion. The processing domain is
* only checked first, so that DP threads, which have no access to the list,
* use the system calls for their own module.
*/
static bool mod_res_user(struct processing_module *mod)
{
#if CONFIG_SOF_USERSPACE_LL
return k_is_user_context() &&
mod->dev->ipc_config.proc_domain != COMP_PROCESSING_DOMAIN_DP;
mod->dev->ipc_config.proc_domain != COMP_PROCESSING_DOMAIN_DP &&
mod_res_user_listed(mod);
#else
return false;
#endif
Expand Down
45 changes: 35 additions & 10 deletions src/audio/module_adapter/module_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ __cold static struct vregion *module_adapter_dp_heap_new(const struct comp_ipc_c
LOG_INF("%zu byte heap size requested in IPC for %#x", buf_size, config->id);
}
#endif
return vregion_create_map(&vreg_start, &buf_size);
return vregion_create_map(&vreg_start, &buf_size, 0);
}

__cold static
struct processing_module *module_adapter_mem_alloc(const struct comp_driver *drv,
const struct comp_ipc_config *config,
const struct module_ext_init_data *ext_init,
struct mod_alloc_ctx *ppl_alloc)
struct mod_alloc_ctx *ppl_alloc,
struct vregion **dp_vreg)
{
struct k_heap *mod_heap;
struct vregion *mod_vreg;
Expand Down Expand Up @@ -130,9 +131,9 @@ struct processing_module *module_adapter_mem_alloc(const struct comp_driver *drv
if (!mod_vreg)
mod = sof_heap_alloc(mod_heap, flags, sizeof(*mod), 0);
else if (flags & SOF_MEM_FLAG_COHERENT)
mod = vregion_alloc_coherent(mod_vreg, sizeof(*mod));
mod = vregion_user_alloc_coherent_align(mod_vreg, sizeof(*mod), 0);
else
mod = vregion_alloc(mod_vreg, sizeof(*mod));
mod = vregion_user_alloc_align(mod_vreg, sizeof(*mod), 0);

if (!mod) {
comp_cl_err(drv, "failed to allocate memory for module");
Expand All @@ -150,9 +151,9 @@ struct processing_module *module_adapter_mem_alloc(const struct comp_driver *drv
if (!mod_vreg)
alloc = sof_heap_alloc(mod_heap, flags, sizeof(*alloc), 0);
else if (flags & SOF_MEM_FLAG_COHERENT)
alloc = vregion_alloc_coherent(mod_vreg, sizeof(*alloc));
alloc = vregion_user_alloc_coherent_align(mod_vreg, sizeof(*alloc), 0);
else
alloc = vregion_alloc(mod_vreg, sizeof(*alloc));
alloc = vregion_user_alloc_align(mod_vreg, sizeof(*alloc), 0);
if (!alloc)
goto ealloc;

Expand All @@ -171,7 +172,7 @@ struct processing_module *module_adapter_mem_alloc(const struct comp_driver *drv
* single-core configurations.
*/
if (mod_vreg)
dev = vregion_alloc_coherent(mod_vreg, sizeof(*dev));
dev = vregion_user_alloc_coherent_align(mod_vreg, sizeof(*dev), 0);
else
dev = sof_heap_alloc(mod_heap, SOF_MEM_FLAG_COHERENT, sizeof(*dev), 0);

Expand All @@ -186,14 +187,33 @@ struct processing_module *module_adapter_mem_alloc(const struct comp_driver *drv
mod->dev = dev;
dev->mod = mod;

#if CONFIG_SOF_USERSPACE_LL
/* an LL module in LL memory, its resources are used in user mode */
if (k_is_user_context() && config->proc_domain != COMP_PROCESSING_DOMAIN_DP &&
mod_res_user_add(mod) < 0) {
comp_cl_err(drv, "failed to register the module resources");
goto eres;
}
#endif

/* the region of a DP module, from here and not from module memory */
*dp_vreg = use_ppl_alloc ? NULL : mod_vreg;

return mod;

#if CONFIG_SOF_USERSPACE_LL
eres:
if (mod_vreg)
vregion_user_free(mod_vreg, dev);
else
sof_heap_free(mod_heap, dev);
#endif
edev:
if (!use_ppl_alloc && !mod_vreg)
sof_heap_free(mod_heap, alloc);
ealloc:
if (mod_vreg)
vregion_free(mod_vreg, mod);
vregion_user_free(mod_vreg, mod);
else
sof_heap_free(mod_heap, mod);
emod:
Expand All @@ -210,6 +230,9 @@ __cold static void module_adapter_mem_free(struct processing_module *mod)

assert_can_be_cold();

/* after the last module resource call */
mod_res_user_remove(mod);

/*
* In principle it shouldn't even be needed to free individual objects
* on the module heap since we're freeing the heap itself too
Expand Down Expand Up @@ -296,7 +319,9 @@ __cold struct comp_dev *module_adapter_new_ext(const struct comp_driver *drv,
ppl_alloc = ipc_pipe->pipeline->alloc;
#endif

struct processing_module *mod = module_adapter_mem_alloc(drv, config, ext_init, ppl_alloc);
struct vregion *dp_vreg;
struct processing_module *mod = module_adapter_mem_alloc(drv, config, ext_init, ppl_alloc,
&dp_vreg);
if (!mod)
return NULL;

Expand Down Expand Up @@ -338,7 +363,7 @@ __cold struct comp_dev *module_adapter_new_ext(const struct comp_driver *drv,
/* create a task for DP processing */
if (config->proc_domain == COMP_PROCESSING_DOMAIN_DP) {
/* All data allocated, create a thread */
ret = pipeline_comp_dp_task_init(dev);
ret = pipeline_comp_dp_task_init(dev, dp_vreg);
if (ret) {
comp_cl_err(drv, "DP task creation failed with error %d.", ret);
goto err;
Expand Down
19 changes: 15 additions & 4 deletions src/audio/pipeline/pipeline-graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,18 @@ __cold struct pipeline *pipeline_new(struct k_heap *heap, uint32_t pipeline_id,
memset(alloc, 0, sizeof(*alloc));
alloc->heap = heap;

/* Create vregion for pipeline and its modules if size info is available */
/*
* Create vregion for pipeline and its modules if size info is available.
* With user-space LL, the IPC user threads run its interim heap.
*/
if (IS_ENABLED(CONFIG_SOF_VREGIONS) &&
pparams && pparams->mem_data && pparams->mem_data->heap_bytes) {
uint32_t vreg_flags = IS_ENABLED(CONFIG_SOF_VREGION_USER_HEAP) ?
VREGION_FLAG_USER_HEAP : 0;
size_t buf_size = pparams->mem_data->heap_bytes;
uintptr_t vreg_start;

alloc->vreg = vregion_create_map(&vreg_start, &buf_size);
alloc->vreg = vregion_create_map(&vreg_start, &buf_size, vreg_flags);
if (!alloc->vreg)
pipe_cl_err("Failed to create pipeline vregion of %u bytes, using heap",
pparams->mem_data->heap_bytes);
Expand Down Expand Up @@ -378,7 +383,8 @@ __cold int pipeline_free(struct pipeline *p)
/* now free the pipeline */
sof_ctx_free(alloc, p);

/* free alloc context and vregion */
/* free alloc context and vregion, the modules are gone */
vregion_user_heap_remove(alloc->vreg);
if (vregion_put(alloc->vreg))
pipe_cl_warn("pipeline vregion still in use");
sof_heap_free(alloc->heap, alloc);
Expand Down Expand Up @@ -495,9 +501,14 @@ int pipeline_complete(struct pipeline *p, struct comp_dev *source,
p->source_comp = source;
p->sink_comp = sink;

if (p->alloc->vreg)
if (p->alloc->vreg) {
vregion_set_interim(p->alloc->vreg);

/* like a region too small for a kernel interim heap, not fatal here */
if (vregion_user_heap_add(p->alloc->vreg) < 0)
pipe_warn(p, "no interim heap");
Comment on lines +507 to +509
}

p->status = COMP_STATE_READY;

/* show heap status */
Expand Down
4 changes: 2 additions & 2 deletions src/audio/pipeline/pipeline-schedule.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ static enum task_state dp_task_run(void *data)
return SOF_TASK_STATE_RESCHEDULE;
}

int pipeline_comp_dp_task_init(struct comp_dev *comp)
int pipeline_comp_dp_task_init(struct comp_dev *comp, struct vregion *vreg)
{
/* DP tasks are guaranteed to have a module_adapter */
struct processing_module *mod = comp_mod(comp);
Expand Down Expand Up @@ -620,7 +620,7 @@ int pipeline_comp_dp_task_init(struct comp_dev *comp)
CONFIG_ZEPHYR_DP_SCHEDULER_MIN_STACK_SIZE);
}

return scheduler_dp_task_init(&comp->task, SOF_UUID(dp_task_uuid), &ops, mod,
return scheduler_dp_task_init(&comp->task, SOF_UUID(dp_task_uuid), &ops, mod, vreg,
comp->ipc_config.core, stack_size, flags);
}
#endif /* CONFIG_ZEPHYR_DP_SCHEDULER */
Expand Down
28 changes: 28 additions & 0 deletions src/include/sof/audio/module_adapter/module/generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,34 @@ void *z_impl_mod_balloc_align(struct processing_module *mod, size_t size, size_t
*/
void mod_resource_init(struct processing_module *mod);

#if CONFIG_SOF_USERSPACE_LL
/**
* @brief Let the LL and IPC user threads use the resources of an LL module.
*
* In user mode only, for a module whose memory and resource pool are in LL
* memory. The module resource functions then run in user mode for @p mod,
* see mod_res_user() in generic.c, and use the system calls for any other
* module.
*
* @param mod Pointer to processing module.
* @return 0 on success or a negative error code.
*/
int mod_res_user_add(struct processing_module *mod);

/**
* @brief Undo mod_res_user_add(), before the module memory is freed.
* @param mod Pointer to processing module.
*/
void mod_res_user_remove(struct processing_module *mod);
#else
static inline int mod_res_user_add(struct processing_module *mod)
{
return 0;
}

static inline void mod_res_user_remove(struct processing_module *mod) {}
#endif

/**
* @brief Query module memory heap boundary and size.
* @param mod Pointer to processing module.
Expand Down
4 changes: 3 additions & 1 deletion src/include/sof/audio/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
struct comp_buffer;
struct comp_dev;
struct ipc;
struct vregion;
struct ipc_msg;
struct k_heap;
struct mod_alloc_ctx;
Expand Down Expand Up @@ -411,9 +412,10 @@ void pipeline_comp_ll_task_free(struct pipeline *p);
/**
* \brief Initializes a data processing (DP) task for a component.
* \param[in,out] comp Component device for which the task is created.
* \param[in] vreg Region of the module created by the kernel, or NULL.
* \return 0 on success, or negative error code on failure.
*/
int pipeline_comp_dp_task_init(struct comp_dev *comp);
int pipeline_comp_dp_task_init(struct comp_dev *comp, struct vregion *vreg);

/**
* \brief Schedules a pipeline copy operation to run after a delay.
Expand Down
Loading
Loading