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
53 changes: 11 additions & 42 deletions src/audio/buffers/comp_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <rtos/alloc.h>
#include <rtos/cache.h>
#include <sof/lib/vregion.h>
#include <sof/ctx_alloc.h>
#include <sof/list.h>
#include <sof/schedule/dp_schedule.h>
#include <rtos/spinlock.h>
Expand Down Expand Up @@ -158,10 +159,7 @@ static void comp_buffer_free(struct sof_audio_buffer *audio_buffer)

#ifdef CONFIG_SOF_USERSPACE_LL
assert(alloc);
if (alloc->vreg)
vregion_free(alloc->vreg, buffer->stream.addr);
else
sof_heap_free(alloc->heap, buffer->stream.addr);
sof_ctx_free(alloc, buffer->stream.addr);
#else
rfree(buffer->stream.addr);
#endif
Expand Down Expand Up @@ -214,12 +212,7 @@ static struct comp_buffer *buffer_alloc_struct(struct mod_alloc_ctx *alloc,
if (is_shared)
flags |= SOF_MEM_FLAG_COHERENT;

if (!alloc || !alloc->vreg)
buffer = sof_heap_alloc(alloc ? alloc->heap : NULL, flags, sizeof(*buffer), 0);
else if (is_shared)
buffer = vregion_alloc_coherent(alloc->vreg, VREGION_MEM_TYPE_INTERIM, sizeof(*buffer));
else
buffer = vregion_alloc(alloc->vreg, VREGION_MEM_TYPE_INTERIM, sizeof(*buffer));
buffer = sof_ctx_alloc(alloc, flags, sizeof(*buffer), 0);
if (!buffer) {
tr_err(&buffer_tr, "could not alloc structure");
return NULL;
Expand Down Expand Up @@ -265,10 +258,7 @@ struct comp_buffer *buffer_alloc(struct mod_alloc_ctx *alloc, size_t size, uint3

#ifdef CONFIG_SOF_USERSPACE_LL
assert(alloc);
if (alloc->vreg)
stream_addr = vregion_alloc_align(alloc->vreg, VREGION_MEM_TYPE_INTERIM, size, align);
else
stream_addr = sof_heap_alloc(alloc->heap, flags, size, align);
stream_addr = sof_ctx_alloc(alloc, flags, size, align);
#else
stream_addr = rballoc_align(flags, size, align);
#endif
Expand All @@ -283,10 +273,7 @@ struct comp_buffer *buffer_alloc(struct mod_alloc_ctx *alloc, size_t size, uint3
tr_err(&buffer_tr, "could not alloc buffer structure");
#ifdef CONFIG_SOF_USERSPACE_LL
assert(alloc);
if (alloc->vreg)
vregion_free(alloc->vreg, stream_addr);
else
sof_heap_free(alloc->heap, stream_addr);
sof_ctx_free(alloc, stream_addr);
#else
rfree(stream_addr);
#endif
Expand Down Expand Up @@ -319,10 +306,7 @@ struct comp_buffer *buffer_alloc_range(struct mod_alloc_ctx *alloc, size_t prefe
for (size = preferred_size; size >= minimum_size; size -= minimum_size) {
#ifdef CONFIG_SOF_USERSPACE_LL
assert(alloc);
if (alloc->vreg)
stream_addr = vregion_alloc_align(alloc->vreg, VREGION_MEM_TYPE_INTERIM, size, align);
else
stream_addr = sof_heap_alloc(alloc->heap, flags, size, align);
stream_addr = sof_ctx_alloc(alloc, flags, size, align);
#else
stream_addr = rballoc_align(flags, size, align);
#endif
Expand All @@ -343,10 +327,7 @@ struct comp_buffer *buffer_alloc_range(struct mod_alloc_ctx *alloc, size_t prefe
tr_err(&buffer_tr, "could not alloc buffer structure");
#ifdef CONFIG_SOF_USERSPACE_LL
assert(alloc);
if (alloc->vreg)
vregion_free(alloc->vreg, stream_addr);
else
sof_heap_free(alloc->heap, stream_addr);
sof_ctx_free(alloc, stream_addr);
#else
rfree(stream_addr);
#endif
Expand Down Expand Up @@ -387,10 +368,7 @@ int buffer_set_size(struct comp_buffer *buffer, uint32_t size, uint32_t alignmen

#ifdef CONFIG_SOF_USERSPACE_LL
assert(alloc);
if (alloc->vreg)
new_ptr = vregion_alloc_align(alloc->vreg, VREGION_MEM_TYPE_INTERIM, size, alignment);
else
new_ptr = sof_heap_alloc(alloc->heap, buffer->flags, size, alignment);
new_ptr = sof_ctx_alloc(alloc, buffer->flags, size, alignment);
#else
new_ptr = rballoc_align(buffer->flags, size, alignment);
#endif
Expand All @@ -406,10 +384,7 @@ int buffer_set_size(struct comp_buffer *buffer, uint32_t size, uint32_t alignmen
if (new_ptr) {
#ifdef CONFIG_SOF_USERSPACE_LL
assert(alloc);
if (alloc->vreg)
vregion_free(alloc->vreg, audio_stream_get_addr(&buffer->stream));
else
sof_heap_free(alloc->heap, audio_stream_get_addr(&buffer->stream));
sof_ctx_free(alloc, audio_stream_get_addr(&buffer->stream));
#else
rfree(audio_stream_get_addr(&buffer->stream));
#endif
Expand Down Expand Up @@ -451,10 +426,7 @@ int buffer_set_size_range(struct comp_buffer *buffer, size_t preferred_size, siz
new_size -= minimum_size) {
#ifdef CONFIG_SOF_USERSPACE_LL
assert(alloc);
if (alloc->vreg)
new_ptr = vregion_alloc_align(alloc->vreg, VREGION_MEM_TYPE_INTERIM, new_size, alignment);
else
new_ptr = sof_heap_alloc(alloc->heap, buffer->flags, new_size, alignment);
new_ptr = sof_ctx_alloc(alloc, buffer->flags, new_size, alignment);
#else
new_ptr = rballoc_align(buffer->flags, new_size, alignment);
#endif
Expand All @@ -473,10 +445,7 @@ int buffer_set_size_range(struct comp_buffer *buffer, size_t preferred_size, siz
if (new_ptr) {
#ifdef CONFIG_SOF_USERSPACE_LL
assert(alloc);
if (alloc->vreg)
vregion_free(alloc->vreg, audio_stream_get_addr(&buffer->stream));
else
sof_heap_free(alloc->heap, audio_stream_get_addr(&buffer->stream));
sof_ctx_free(alloc, audio_stream_get_addr(&buffer->stream));
#else
rfree(audio_stream_get_addr(&buffer->stream));
#endif
Expand Down
31 changes: 7 additions & 24 deletions src/audio/buffers/ring_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <sof/trace/trace.h>
#include <sof/lib/uuid.h>
#include <sof/lib/vregion.h>
#include <sof/ctx_alloc.h>

#include <sof/audio/module_adapter/module/generic.h>
#include <sof/audio/ring_buffer.h>
Expand Down Expand Up @@ -99,13 +100,8 @@ static void ring_buffer_free(struct sof_audio_buffer *audio_buffer)
struct ring_buffer, audio_buffer);
struct mod_alloc_ctx *alloc = audio_buffer->alloc;

if (alloc->vreg) {
vregion_free(alloc->vreg, (__sparse_force void *)ring_buffer->_data_buffer);
vregion_free(alloc->vreg, ring_buffer);
} else {
sof_heap_free(alloc->heap, (__sparse_force void *)ring_buffer->_data_buffer);
sof_heap_free(alloc->heap, ring_buffer);
}
sof_ctx_free(alloc, (__sparse_force void *)ring_buffer->_data_buffer);
sof_ctx_free(alloc, ring_buffer);
}

static void ring_buffer_reset(struct sof_audio_buffer *audio_buffer)
Expand Down Expand Up @@ -296,17 +292,11 @@ struct ring_buffer *ring_buffer_create(struct comp_dev *dev, size_t min_availabl
struct ring_buffer *ring_buffer;
struct mod_alloc_ctx *alloc = dev->mod->priv.resources.alloc;
struct k_heap *heap = alloc->heap;
struct vregion *vreg = alloc->vreg;
int memory_flags = (is_shared ? SOF_MEM_FLAG_COHERENT : 0) |
user_get_buffer_memory_region(dev->drv);

/* allocate ring_buffer structure */
if (!vreg)
ring_buffer = sof_heap_alloc(heap, memory_flags, sizeof(*ring_buffer), 0);
else if (is_shared)
ring_buffer = vregion_alloc_coherent(vreg, VREGION_MEM_TYPE_INTERIM, sizeof(*ring_buffer));
else
ring_buffer = vregion_alloc(vreg, VREGION_MEM_TYPE_INTERIM, sizeof(*ring_buffer));
ring_buffer = sof_ctx_alloc(alloc, memory_flags, sizeof(*ring_buffer), 0);
if (!ring_buffer)
return NULL;

Expand Down Expand Up @@ -382,12 +372,8 @@ struct ring_buffer *ring_buffer_create(struct comp_dev *dev, size_t min_availabl

void *data_buf;

if (vreg)
data_buf = vregion_alloc_align(vreg, VREGION_MEM_TYPE_INTERIM, ring_buffer->data_buffer_size,
PLATFORM_DCACHE_ALIGN);
else
data_buf = sof_heap_alloc(heap, user_get_buffer_memory_region(dev->drv),
ring_buffer->data_buffer_size, PLATFORM_DCACHE_ALIGN);
data_buf = sof_ctx_alloc(alloc, user_get_buffer_memory_region(dev->drv),
ring_buffer->data_buffer_size, PLATFORM_DCACHE_ALIGN);

if (!data_buf)
goto err;
Expand All @@ -402,9 +388,6 @@ struct ring_buffer *ring_buffer_create(struct comp_dev *dev, size_t min_availabl
return ring_buffer;
err:
tr_err(&ring_buffer_tr, "Ring buffer creation failure");
if (vreg)
vregion_free(vreg, ring_buffer);
else
sof_heap_free(heap, ring_buffer);
sof_ctx_free(alloc, ring_buffer);
return NULL;
}
22 changes: 6 additions & 16 deletions src/audio/module_adapter/module/generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <sof/audio/data_blob.h>
#include <sof/lib/fast-get.h>
#include <sof/lib/vregion.h>
#include <sof/ctx_alloc.h>
#include <sof/schedule/dp_schedule.h>
#if CONFIG_IPC_MAJOR_4
#include <ipc4/header.h>
Expand All @@ -28,8 +29,9 @@
/* The __ZEPHYR__ condition is to keep cmocka tests working */
#if CONFIG_MODULE_MEMORY_API_DEBUG && defined(__ZEPHYR__)
#define MEM_API_CHECK_THREAD(res) do { \
if ((res)->rsrc_mngr != k_current_get()) \
LOG_WRN("mngr %p != cur %p", (res)->rsrc_mngr, k_current_get()); \
k_tid_t tid = k_current_get(); \
if ((res)->rsrc_mngr != tid && (res)->dp_thread != tid) \
LOG_WRN("cur %p != mngr %p or dp %p", tid, (res)->rsrc_mngr, (res)->dp_thread); \
} while (0)
#else
#define MEM_API_CHECK_THREAD(res)
Expand Down Expand Up @@ -252,16 +254,7 @@ void *z_impl_mod_alloc_ext(struct processing_module *mod, uint32_t flags, size_t
}

/* Allocate memory for module */
void *ptr;

if (!res->alloc->vreg)
ptr = sof_heap_alloc(res->alloc->heap, flags, size, alignment);
else if (flags & SOF_MEM_FLAG_COHERENT)
ptr = vregion_alloc_coherent_align(res->alloc->vreg, VREGION_MEM_TYPE_INTERIM,
size, alignment);
else
ptr = vregion_alloc_align(res->alloc->vreg, VREGION_MEM_TYPE_INTERIM,
size, alignment);
void *ptr = sof_ctx_alloc(res->alloc, flags, size, alignment);

if (!ptr) {
comp_err(mod->dev, "Failed to alloc %zu bytes %zu alignment for comp %#x.",
Expand Down Expand Up @@ -362,10 +355,7 @@ static int free_contents(struct processing_module *mod, struct module_resource *

switch (container->type) {
case MOD_RES_HEAP:
if (res->alloc->vreg)
vregion_free(res->alloc->vreg, container->ptr);
else
sof_heap_free(res->alloc->heap, container->ptr);
sof_ctx_free(res->alloc, container->ptr);
res->heap_usage -= container->size;
return 0;
#if CONFIG_COMP_BLOB
Expand Down
Loading