Skip to content
5 changes: 4 additions & 1 deletion posix/include/rtos/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ static inline void k_heap_init(struct k_heap *heap, void *mem, size_t bytes)
void *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes,
size_t alignment);
void sof_heap_free(struct k_heap *heap, void *addr);
struct k_heap *sof_sys_heap_get(void);
static inline struct k_heap *sof_sys_heap_get(void)
{
return NULL;
}

/**
* Calculates length of the null-terminated string.
Expand Down
117 changes: 76 additions & 41 deletions src/audio/module_adapter/module/generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/

#include <rtos/symbol.h>
#include <sof/compiler_attributes.h>
#include <sof/objpool.h>
#include <sof/audio/module_adapter/module/generic.h>
#include <sof/audio/data_blob.h>
#include <sof/lib/fast-get.h>
Expand Down Expand Up @@ -83,10 +81,10 @@ int module_load_config(struct comp_dev *dev, const void *cfg, size_t size)
void mod_resource_init(struct processing_module *mod)
{
struct module_data *md = &mod->priv;

/* Init memory list */
list_init(&md->resources.objpool.list);
md->resources.objpool.heap = md->resources.heap;
list_init(&md->resources.res_list);
list_init(&md->resources.free_cont_list);
list_init(&md->resources.cont_chunk_list);
md->resources.heap_usage = 0;
md->resources.heap_high_water_mark = 0;
}
Expand Down Expand Up @@ -143,14 +141,43 @@ int module_init(struct processing_module *mod)
return 0;
}

struct container_chunk {
struct list_item chunk_list;
struct module_resource containers[CONFIG_MODULE_MEMORY_API_CONTAINER_CHUNK_SIZE];
};

static struct module_resource *container_get(struct processing_module *mod)
{
return objpool_alloc(&mod->priv.resources.objpool, sizeof(struct module_resource), 0);
struct module_resources *res = &mod->priv.resources;
struct k_heap *mod_heap = res->heap;
struct module_resource *container;

if (list_is_empty(&res->free_cont_list)) {
struct container_chunk *chunk = sof_heap_alloc(mod_heap, 0, sizeof(*chunk), 0);
int i;

if (!chunk) {
comp_err(mod->dev, "allocating more containers failed");
return NULL;
}

memset(chunk, 0, sizeof(*chunk));

list_item_append(&chunk->chunk_list, &res->cont_chunk_list);
for (i = 0; i < ARRAY_SIZE(chunk->containers); i++)
list_item_append(&chunk->containers[i].list, &res->free_cont_list);
}

container = list_first_item(&res->free_cont_list, struct module_resource, list);
list_item_del(&container->list);
return container;
}

static void container_put(struct processing_module *mod, struct module_resource *container)
{
objpool_free(&mod->priv.resources.objpool, container);
struct module_resources *res = &mod->priv.resources;

list_item_append(&container->list, &res->free_cont_list);
}

#if CONFIG_USERSPACE
Expand Down Expand Up @@ -208,6 +235,7 @@ void *mod_balloc_align(struct processing_module *mod, size_t size, size_t alignm
container->ptr = ptr;
container->size = size;
container->type = MOD_RES_HEAP;
list_item_prepend(&container->list, &res->res_list);

res->heap_usage += size;
if (res->heap_usage > res->heap_high_water_mark)
Expand Down Expand Up @@ -258,6 +286,7 @@ void *z_impl_mod_alloc_ext(struct processing_module *mod, uint32_t flags, size_t
container->ptr = ptr;
container->size = size;
container->type = MOD_RES_HEAP;
list_item_prepend(&container->list, &res->res_list);

res->heap_usage += size;
if (res->heap_usage > res->heap_high_water_mark)
Expand All @@ -277,7 +306,7 @@ EXPORT_SYMBOL(z_impl_mod_alloc_ext);
#if CONFIG_COMP_BLOB
struct comp_data_blob_handler *mod_data_blob_handler_new(struct processing_module *mod)
{
struct module_resources * __maybe_unused res = &mod->priv.resources;
struct module_resources *res = &mod->priv.resources;
struct comp_data_blob_handler *bhp;
struct module_resource *container;

Expand All @@ -296,6 +325,7 @@ struct comp_data_blob_handler *mod_data_blob_handler_new(struct processing_modul
container->bhp = bhp;
container->size = 0;
container->type = MOD_RES_BLOB_HANDLER;
list_item_prepend(&container->list, &res->res_list);

return bhp;
}
Expand Down Expand Up @@ -332,6 +362,7 @@ const void *z_impl_mod_fast_get(struct processing_module *mod, const void * cons
container->sram_ptr = ptr;
container->size = 0;
container->type = MOD_RES_FAST_GET;
list_item_prepend(&container->list, &res->res_list);

return ptr;
}
Expand Down Expand Up @@ -363,29 +394,6 @@ static int free_contents(struct processing_module *mod, struct module_resource *
return -EINVAL;
}

struct mod_res_cb_arg {
struct processing_module *mod;
const void *ptr;
};

static bool mod_res_free(void *data, void *arg)
{
struct mod_res_cb_arg *cb_arg = arg;
struct module_resource *container = data;

if (cb_arg->ptr && container->ptr != cb_arg->ptr)
return false;

int ret = free_contents(cb_arg->mod, container);

if (ret < 0)
comp_err(cb_arg->mod->dev, "Cannot free allocation %p", cb_arg->ptr);

container_put(cb_arg->mod, container);

return true;
}

/**
* Frees the memory block removes it from module's book keeping.
* @param mod Pointer to module this memory block was allocated for.
Expand All @@ -394,19 +402,28 @@ static bool mod_res_free(void *data, void *arg)
int z_impl_mod_free(struct processing_module *mod, const void *ptr)
{
struct module_resources *res = &mod->priv.resources;
struct module_resource *container;
struct list_item *res_list;

MEM_API_CHECK_THREAD(res);
if (!ptr)
return 0;

/* Find which container holds this memory */
struct mod_res_cb_arg cb_arg = {mod, ptr};
int ret = objpool_iterate(&res->objpool, mod_res_free, &cb_arg);
/* Find which container keeps this memory */
list_for_item(res_list, &res->res_list) {
container = container_of(res_list, struct module_resource, list);
if (container->ptr == ptr) {
int ret = free_contents(mod, container);

list_item_del(&container->list);
container_put(mod, container);
return ret;
}
}

if (ret < 0)
comp_err(mod->dev, "error: could not find memory pointed by %p", ptr);
comp_err(mod->dev, "error: could not find memory pointed by %p", ptr);

return ret;
return -EINVAL;
}
EXPORT_SYMBOL(z_impl_mod_free);

Expand Down Expand Up @@ -667,14 +684,32 @@ int module_reset(struct processing_module *mod)
void mod_free_all(struct processing_module *mod)
{
struct module_resources *res = &mod->priv.resources;
struct k_heap *mod_heap = res->heap;
struct list_item *list;
struct list_item *_list;

MEM_API_CHECK_THREAD(res);

/* Free all contents found in used containers */
struct mod_res_cb_arg cb_arg = {mod, NULL};
list_for_item(list, &res->res_list) {
struct module_resource *container =
container_of(list, struct module_resource, list);

free_contents(mod, container);
}

objpool_iterate(&res->objpool, mod_res_free, &cb_arg);
objpool_prune(&res->objpool);
/*
* We do not need to remove the containers from res_list in
* the loop above or go through free_cont_list as all the
* containers are anyway freed in the loop below, and the list
* heads are reinitialized when mod_resource_init() is called.
*/
list_for_item_safe(list, _list, &res->cont_chunk_list) {
struct container_chunk *chunk =
container_of(list, struct container_chunk, chunk_list);

list_item_del(&chunk->chunk_list);
sof_heap_free(mod_heap, chunk);
}

/* Make sure resource lists and accounting are reset */
mod_resource_init(mod);
Expand Down
2 changes: 1 addition & 1 deletion src/audio/module_adapter/module_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct comp_dev *module_adapter_new(const struct comp_driver *drv,
#if CONFIG_MM_DRV
#define PAGE_SZ CONFIG_MM_DRV_PAGE_SIZE
#else
#include <platform/platform.h>
#include <sof/platform.h>
#define PAGE_SZ HOST_PAGE_SIZE
#endif

Expand Down
3 changes: 2 additions & 1 deletion src/audio/module_adapter/module_adapter_ipc4.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ int module_adapter_init_data(struct comp_dev *dev,
}
}

if (!config->ipc_extended_init) {
if (!config->ipc_extended_init ||
(!dst->ext_data->dp_data && !dst->ext_data->module_data)) {
dst->init_data = cfg; /* legacy API */
dst->avail = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/include/sof/audio/component_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ static inline int comp_trigger_local(struct comp_dev *dev, int cmd)
/* schedule or cancel task */
switch (cmd) {
case COMP_TRIGGER_START:
case COMP_TRIGGER_RELEASE:
ret = schedule_task(dev->task, 0, dev->period);
break;
case COMP_TRIGGER_RELEASE:
case COMP_TRIGGER_XRUN:
case COMP_TRIGGER_PAUSE:
case COMP_TRIGGER_STOP:
Expand Down
7 changes: 4 additions & 3 deletions src/include/sof/audio/module_adapter/module/generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
#ifndef __SOF_AUDIO_MODULE_GENERIC__
#define __SOF_AUDIO_MODULE_GENERIC__

#include <sof/objpool.h>
#include <sof/ut.h>
#include <sof/audio/component.h>
#include <sof/ut.h>
#include <sof/audio/sink_api.h>
#include <sof/audio/source_api.h>
#include "module_interface.h"
Expand Down Expand Up @@ -129,7 +128,9 @@ struct module_param {
* when the module unloads.
*/
struct module_resources {
struct objpool_head objpool;
struct list_item res_list; /**< Allocad resource containers */
struct list_item free_cont_list; /**< Unused memory containers */
struct list_item cont_chunk_list; /**< Memory container chunks */
size_t heap_usage;
size_t heap_high_water_mark;
struct k_heap *heap;
Expand Down
14 changes: 2 additions & 12 deletions src/include/sof/lib/fast-get.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,7 @@

struct k_heap;

#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION)
#include <zephyr/toolchain.h>

__syscall const void *fast_get(struct k_heap *heap, const void * const dram_ptr, size_t size);
__syscall void fast_put(struct k_heap *heap, const void *sram_ptr);
#include <zephyr/syscalls/fast-get.h>
#else
const void *z_impl_fast_get(struct k_heap *heap, const void * const dram_ptr, size_t size);
void z_impl_fast_put(struct k_heap *heap, const void *sram_ptr);
#define fast_get z_impl_fast_get
#define fast_put z_impl_fast_put
#endif /* __ZEPHYR__ */
const void *fast_get(struct k_heap *heap, const void * const dram_ptr, size_t size);
void fast_put(struct k_heap *heap, const void *sram_ptr);

#endif /* __SOF_LIB_FAST_GET_H__ */
1 change: 1 addition & 0 deletions src/lib/objpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ void *objpool_alloc(struct objpool_head *head, size_t size, uint32_t flags)
unsigned int new_n;

if (list_is_empty(&head->list)) {
head->flags = flags;
new_n = 2;
} else {
/* Check the last one */
Expand Down
5 changes: 0 additions & 5 deletions src/platform/library/lib/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,3 @@ void heap_trace_all(int force)
{
heap_trace(NULL, 0);
}

struct k_heap *sof_sys_heap_get(void)
{
return NULL;
}
2 changes: 2 additions & 0 deletions src/schedule/zephyr_dp_schedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ struct scheduler_dp_data {

enum sof_dp_part_type {
SOF_DP_PART_HEAP,
SOF_DP_PART_HEAP_CACHE,
SOF_DP_PART_CFG,
SOF_DP_PART_CFG_CACHE,
SOF_DP_PART_TYPE_COUNT,
};

Expand Down
19 changes: 17 additions & 2 deletions src/schedule/zephyr_dp_schedule_application.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,9 @@ static void scheduler_dp_domain_free(struct task_dp_pdata *pdata)
llext_manager_rm_domain(pmod->dev->ipc_config.id, mdom);

k_mem_domain_remove_partition(mdom, pdata->mpart + SOF_DP_PART_HEAP);
k_mem_domain_remove_partition(mdom, pdata->mpart + SOF_DP_PART_HEAP_CACHE);
k_mem_domain_remove_partition(mdom, pdata->mpart + SOF_DP_PART_CFG);
k_mem_domain_remove_partition(mdom, pdata->mpart + SOF_DP_PART_CFG_CACHE);

pmod->mdom = NULL;
objpool_free(&dp_mdom_head, mdom);
Expand Down Expand Up @@ -516,8 +518,11 @@ int scheduler_dp_task_init(struct task **task, const struct sof_uuid_entry *uid,
struct k_mem_domain *mdom = objpool_alloc(&dp_mdom_head, sizeof(*mdom),
SOF_MEM_FLAG_COHERENT);

if (!mdom)
if (!mdom) {
tr_err(&dp_tr, "objpool allocation failed");
ret = -ENOMEM;
goto e_thread;
}

mod->mdom = mdom;

Expand All @@ -534,12 +539,22 @@ int scheduler_dp_task_init(struct task **task, const struct sof_uuid_entry *uid,
.size = size,
.attr = K_MEM_PARTITION_P_RW_U_RW,
};
pdata->mpart[SOF_DP_PART_HEAP_CACHE] = (struct k_mem_partition){
.start = (uintptr_t)sys_cache_cached_ptr_get((void *)start),
.size = size,
.attr = K_MEM_PARTITION_P_RW_U_RW | XTENSA_MMU_CACHED_WB,
};
/* Host mailbox partition for additional IPC parameters: read-only */
pdata->mpart[SOF_DP_PART_CFG] = (struct k_mem_partition){
.start = (uintptr_t)MAILBOX_HOSTBOX_BASE,
.start = (uintptr_t)sys_cache_uncached_ptr_get((void *)MAILBOX_HOSTBOX_BASE),
.size = 4096,
.attr = K_MEM_PARTITION_P_RO_U_RO,
};
pdata->mpart[SOF_DP_PART_CFG_CACHE] = (struct k_mem_partition){
.start = (uintptr_t)MAILBOX_HOSTBOX_BASE,
.size = 4096,
.attr = K_MEM_PARTITION_P_RO_U_RO | XTENSA_MMU_CACHED_WB,
};

for (pidx = 0; pidx < SOF_DP_PART_TYPE_COUNT; pidx++) {
ret = k_mem_domain_add_partition(mdom, pdata->mpart + pidx);
Expand Down
1 change: 0 additions & 1 deletion test/cmocka/src/audio/eq_fir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ add_library(audio_for_eq_fir STATIC
${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c
${PROJECT_SOURCE_DIR}/src/lib/objpool.c
${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c
Expand Down
1 change: 0 additions & 1 deletion test/cmocka/src/audio/eq_iir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ add_library(audio_for_eq_iir STATIC
${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c
${PROJECT_SOURCE_DIR}/src/lib/objpool.c
${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c
Expand Down
1 change: 0 additions & 1 deletion test/cmocka/src/audio/mixer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ cmocka_test(mixer
${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c
${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c
${PROJECT_SOURCE_DIR}/src/lib/objpool.c
${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module_adapter.c
${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module_adapter_ipc3.c
${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module/generic.c
Expand Down
Loading