Skip to content
Merged
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
18 changes: 10 additions & 8 deletions posix/include/rtos/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,24 @@
* the first two positions are reserved for SOF_BUF_ flags
*/

/** \brief Indicates we should return DMA-able memory. */
/** \brief Allocate DMA-able memory. */
#define SOF_MEM_FLAG_DMA BIT(2)
/** \brief Indicates that original content should not be copied by realloc. */
/** \brief realloc() skips copying the original content. */
#define SOF_MEM_FLAG_NO_COPY BIT(3)
/** \brief Indicates that if we should return uncached address. */
/** \brief Allocate uncached address. */
#define SOF_MEM_FLAG_COHERENT BIT(4)
/** \brief Indicates that if we should return L3 address. */
/** \brief Allocate L3 address. */
#define SOF_MEM_FLAG_L3 BIT(5)
/** \brief Indicates that if we should return Low power memory address. */
/** \brief Allocate Low power memory address. */
#define SOF_MEM_FLAG_LOW_POWER BIT(6)
/** \brief Indicates that if we should return kernel memory address. */
/** \brief Allocate kernel memory address. */
#define SOF_MEM_FLAG_KERNEL BIT(7)
/** \brief Indicates that if we should return user memory address. */
/** \brief Allocate user memory address. */
#define SOF_MEM_FLAG_USER BIT(8)
/** \brief Indicates that if we should return shared user memory address. */
/** \brief Allocate shared user memory address. */
#define SOF_MEM_FLAG_USER_SHARED_BUFFER BIT(9)
/** \brief Use allocation method for large buffers. */
#define SOF_MEM_FLAG_LARGE_BUFFER BIT(10)

/** @} */

Expand Down
63 changes: 3 additions & 60 deletions posix/include/rtos/userspace_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,77 +26,20 @@ struct sys_heap;
/**
* Initialize private processing module heap.
* @param N/A.
* @return pointer to the sys_heap structure.
* @return pointer to the k_heap structure.
*
* @note
* Function used only when CONFIG_USERSPACE is set.
* The private heap is used only for non-privileged modules for all processing module allocations
* that should be isolated. The heap helps to accumulate all dynamic allocations in single memory
* region which is then added to modules memory domain.
*/
static inline struct sys_heap *module_driver_heap_init(void)
static inline struct k_heap *module_driver_heap_init(void)
{
return NULL;
}

#endif

/**
* Allocates memory block from private module sys_heap if exists, otherwise call rballoc_align().
* @param sys_heap - pointer to the sys_heap structure
* @param flags - Flags, see SOF_MEM_FLAG_...
* @param bytes - Size in bytes.
* @param alignment - Alignment in bytes.
* @return Pointer to the allocated memory or NULL if failed.
*
* @note When CONFIG_USERSPACE not set function calls rballoc_align()
*/
static inline void *module_driver_heap_aligned_alloc(struct sys_heap *mod_drv_heap, uint32_t flags,
size_t bytes, uint32_t align)
{
return rballoc_align(flags, bytes, align);
}

/**
* Allocates memory block from private module sys_heap if exists, otherwise call rmalloc.
* @param sys_heap - pointer to the sys_heap structure
* @param flags - Flags, see SOF_MEM_FLAG_...
* @param bytes - Size in bytes.
* @return - Pointer to the allocated memory or NULL if failed.
*
* * @note When CONFIG_USERSPACE not set function calls rmalloc()
*/
static inline void *module_driver_heap_rmalloc(struct sys_heap *mod_drv_heap, uint32_t flags,
size_t bytes)
{
return rmalloc(flags, bytes);
}

/**
* Similar to user_rmalloc(), guarantees that returned block is zeroed.
*
* @note When CONFIG_USERSPACE not set function calls rzalloc()
*/
static inline void *module_driver_heap_rzalloc(struct sys_heap *mod_drv_heap, uint32_t flags,
size_t bytes)
{
return rzalloc(flags, bytes);
}

/**
* Frees the memory block from private module sys_heap if exists. Otherwise call rfree.
* @param ptr Pointer to the memory block.
*
* @note User should take care to not free memory allocated from sys_heap
* with module_driver_heap set to NULL. It will cause exception.
*
* When CONFIG_USERSPACE not set function calls rfree()
*/
static inline void module_driver_heap_free(struct sys_heap *mod_drv_heap, void *mem)
{
rfree(mem);
}

/**
* Free private processing module heap.
* @param sys_heap pointer to the sys_heap structure.
Expand All @@ -105,7 +48,7 @@ static inline void module_driver_heap_free(struct sys_heap *mod_drv_heap, void *
* Function used only when CONFIG_USERSPACE is set.
* Frees private module heap.
*/
static inline void module_driver_heap_remove(struct sys_heap *mod_drv_heap)
static inline void module_driver_heap_remove(struct k_heap *mod_drv_heap)
{ }

#endif /* __RTOS_USERSPACE_HELPER_H__ */
15 changes: 8 additions & 7 deletions src/audio/module_adapter/library/userspace_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ static int userspace_proxy_memory_init(struct userspace_context *user,
{
/* Add module private heap to memory partitions */
struct k_mem_partition heap_part = { .attr = K_MEM_PARTITION_P_RW_U_RW };
struct sys_heap *heap = &drv->user_heap->heap;

k_mem_region_align(&heap_part.start, &heap_part.size,
POINTER_TO_UINT(drv->user_heap->init_mem),
drv->user_heap->init_bytes, CONFIG_MM_DRV_PAGE_SIZE);
POINTER_TO_UINT(heap->init_mem),
heap->init_bytes, CONFIG_MM_DRV_PAGE_SIZE);

tr_dbg(&userspace_proxy_tr, "Heap partition %p + %zx, attr = %u",
UINT_TO_POINTER(heap_part.start), heap_part.size, heap_part.attr);
Expand All @@ -63,8 +64,8 @@ static int userspace_proxy_memory_init(struct userspace_context *user,
struct k_mem_partition heap_cached_part = { .attr = K_MEM_PARTITION_P_RW_U_RW };

k_mem_region_align(&heap_cached_part.start, &heap_cached_part.size,
POINTER_TO_UINT(sys_cache_cached_ptr_get(drv->user_heap->init_mem)),
drv->user_heap->init_bytes, CONFIG_MM_DRV_PAGE_SIZE);
POINTER_TO_UINT(sys_cache_cached_ptr_get(heap->init_mem)),
heap->init_bytes, CONFIG_MM_DRV_PAGE_SIZE);

tr_dbg(&userspace_proxy_tr, "Cached heap partition %p + %zx, attr = %u",
UINT_TO_POINTER(heap_cached_part.start), heap_cached_part.size,
Expand Down Expand Up @@ -135,7 +136,7 @@ int userspace_proxy_create(struct userspace_context **user_ctx, const struct com

tr_dbg(&userspace_proxy_tr, "userspace create");

user = sys_heap_alloc(drv->user_heap, sizeof(struct userspace_context));
user = k_heap_alloc(drv->user_heap, sizeof(struct userspace_context), K_FOREVER);
if (!user)
return -ENOMEM;

Expand Down Expand Up @@ -184,15 +185,15 @@ int userspace_proxy_create(struct userspace_context **user_ctx, const struct com
error_dom:
rfree(domain);
error:
sys_heap_free(drv->user_heap, user);
k_heap_free(drv->user_heap, user);
return ret;
}

void userspace_proxy_destroy(const struct comp_driver *drv, struct userspace_context *user_ctx)
{
tr_dbg(&userspace_proxy_tr, "userspace proxy destroy");
rfree(user_ctx->comp_dom);
sys_heap_free(drv->user_heap, user_ctx);
k_heap_free(drv->user_heap, user_ctx);
}

/**
Expand Down
20 changes: 11 additions & 9 deletions src/audio/module_adapter/module/generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,20 @@ struct container_chunk {
static struct module_resource *container_get(struct processing_module *mod)
{
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 = rzalloc(SOF_MEM_FLAG_USER, sizeof(*chunk));
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);
Expand Down Expand Up @@ -180,7 +183,6 @@ void *mod_balloc_align(struct processing_module *mod, size_t size, size_t alignm
{
struct module_resources *res = &mod->priv.resources;
struct module_resource *container;
void *ptr;

MEM_API_CHECK_THREAD(res);

Expand All @@ -195,7 +197,8 @@ void *mod_balloc_align(struct processing_module *mod, size_t size, size_t alignm
}

/* Allocate buffer memory for module */
ptr = rballoc_align(SOF_MEM_FLAG_USER, size, alignment);
void *ptr = sof_heap_alloc(res->heap, SOF_MEM_FLAG_USER | SOF_MEM_FLAG_LARGE_BUFFER,
size, alignment);

if (!ptr) {
comp_err(mod->dev, "Failed to alloc %zu bytes %zu alignment for comp %#x.",
Expand Down Expand Up @@ -231,7 +234,6 @@ void *mod_alloc_ext(struct processing_module *mod, uint32_t flags, size_t size,
{
struct module_resources *res = &mod->priv.resources;
struct module_resource *container;
void *ptr;

MEM_API_CHECK_THREAD(res);

Expand All @@ -246,7 +248,7 @@ void *mod_alloc_ext(struct processing_module *mod, uint32_t flags, size_t size,
}

/* Allocate memory for module */
ptr = rmalloc_align(flags, size, alignment);
void *ptr = sof_heap_alloc(res->heap, flags, size, alignment);

if (!ptr) {
comp_err(mod->dev, "Failed to alloc %zu bytes %zu alignment for comp %#x.",
Expand Down Expand Up @@ -276,8 +278,7 @@ EXPORT_SYMBOL(mod_alloc_ext);
* Like comp_data_blob_handler_new() but the handler is automatically freed.
*/
#if CONFIG_COMP_BLOB
struct comp_data_blob_handler *
mod_data_blob_handler_new(struct processing_module *mod)
struct comp_data_blob_handler *mod_data_blob_handler_new(struct processing_module *mod)
{
struct module_resources *res = &mod->priv.resources;
struct comp_data_blob_handler *bhp;
Expand Down Expand Up @@ -347,7 +348,7 @@ static int free_contents(struct processing_module *mod, struct module_resource *

switch (container->type) {
case MOD_RES_HEAP:
rfree(container->ptr);
sof_heap_free(res->heap, container->ptr);
res->heap_usage -= container->size;
return 0;
#if CONFIG_COMP_BLOB
Expand Down Expand Up @@ -588,6 +589,7 @@ 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;

Expand All @@ -611,7 +613,7 @@ void mod_free_all(struct processing_module *mod)
container_of(list, struct container_chunk, chunk_list);

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

/* Make sure resource lists and accounting are reset */
Expand Down
Loading
Loading