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
61 changes: 19 additions & 42 deletions posix/include/rtos/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,24 @@
* @{
*/

/**
* \brief Heap Memory Zones
*
* The heap has three different zones from where memory can be allocated :-
*
* 1) System Zone. Fixed size heap where alloc always succeeds and is never
* freed. Used by any init code that will never give up the memory.
*
* 2) System Runtime Zone. Heap zone intended for runtime objects allocated
* by the kernel part of the code.
*
* 3) Runtime Zone. Main and larger heap zone where allocs are not guaranteed to
* succeed. Memory can be freed here.
*
* 4) Buffer Zone. Largest heap zone intended for audio buffers.
*
* 5) Runtime Shared Zone. Similar to Runtime Zone, but content may be used and
* fred from any enabled core.
*
* 6) System Shared Zone. Similar to System Zone, but content may be used from
* any enabled core.
*
* See platform/memory.h for heap size configuration and mappings.
*/
enum mem_zone {
SOF_MEM_ZONE_SYS = 0, /**< System zone */
SOF_MEM_ZONE_SYS_RUNTIME, /**< System-runtime zone */
SOF_MEM_ZONE_RUNTIME, /**< Runtime zone */
SOF_MEM_ZONE_BUFFER, /**< Buffer zone */
SOF_MEM_ZONE_RUNTIME_SHARED, /**< Runtime shared zone */
SOF_MEM_ZONE_SYS_SHARED, /**< System shared zone */
};

/** \name Heap zone flags
* @{
*/

/** \brief Indicates we should return DMA-able memory. */
#define SOF_MEM_FLAG_DMA BIT(0)
/** \brief Indicates that original content should not be copied by realloc. */
#define SOF_MEM_FLAG_NO_COPY BIT(1)
/** \brief Indicates that if we should return uncached address. */
#define SOF_MEM_FLAG_COHERENT BIT(2)
#define SOF_MEM_FLAG_COHERENT BIT(2)
/** \brief Indicates that if we should return L3 address. */
#define SOF_MEM_FLAG_L3 BIT(3)
/** \brief Indicates that if we should return Low power memory address. */
#define SOF_MEM_FLAG_LOW_POWER BIT(4)
/** \brief Indicates that if we should return kernel memory address. */
#define SOF_MEM_FLAG_KERNEL BIT(5)
/** \brief Indicates that if we should return user memory address. */
#define SOF_MEM_FLAG_USER BIT(6)

/** @} */

Expand All @@ -83,15 +60,15 @@ enum mem_zone {
* @note Do not use for buffers (SOF_MEM_ZONE_BUFFER zone).
* Use rballoc(), rballoc_align() to allocate memory for buffers.
*/
void *rmalloc(enum mem_zone zone, uint32_t flags, uint32_t caps, size_t bytes);
void *rmalloc(uint32_t flags, size_t bytes);

/**
* Similar to rmalloc(), guarantees that returned block is zeroed.
*
* @note Do not use for buffers (SOF_MEM_ZONE_BUFFER zone).
* rballoc(), rballoc_align() to allocate memory for buffers.
*/
void *rzalloc(enum mem_zone zone, uint32_t flags, uint32_t caps, size_t bytes);
void *rzalloc(uint32_t flags, size_t bytes);

/**
* Allocates memory block from SOF_MEM_ZONE_BUFFER.
Expand All @@ -101,15 +78,15 @@ void *rzalloc(enum mem_zone zone, uint32_t flags, uint32_t caps, size_t bytes);
* @param alignment Alignment in bytes.
* @return Pointer to the allocated memory or NULL if failed.
*/
void *rballoc_align(uint32_t flags, uint32_t caps, size_t bytes,
void *rballoc_align(uint32_t flags, size_t bytes,
uint32_t alignment);

/**
* Similar to rballoc_align(), returns buffer aligned to PLATFORM_DCACHE_ALIGN.
*/
static inline void *rballoc(uint32_t flags, uint32_t caps, size_t bytes)
static inline void *rballoc(uint32_t flags, size_t bytes)
{
return rballoc_align(flags, caps, bytes, PLATFORM_DCACHE_ALIGN);
return rballoc_align(flags, bytes, PLATFORM_DCACHE_ALIGN);
}

/**
Expand All @@ -122,17 +99,17 @@ static inline void *rballoc(uint32_t flags, uint32_t caps, size_t bytes)
* @param alignment Alignment in bytes.
* @return Pointer to the resized memory of NULL if failed.
*/
void *rbrealloc_align(void *ptr, uint32_t flags, uint32_t caps, size_t bytes,
void *rbrealloc_align(void *ptr, uint32_t flags, size_t bytes,
size_t old_bytes, uint32_t alignment);

/**
* Similar to rballoc_align(), returns resized buffer aligned to
* PLATFORM_DCACHE_ALIGN.
*/
static inline void *rbrealloc(void *ptr, uint32_t flags, uint32_t caps,
static inline void *rbrealloc(void *ptr, uint32_t flags,
size_t bytes, size_t old_bytes)
{
return rbrealloc_align(ptr, flags, caps, bytes, old_bytes,
return rbrealloc_align(ptr, flags, bytes, old_bytes,
PLATFORM_DCACHE_ALIGN);
}

Expand Down
2 changes: 1 addition & 1 deletion posix/include/sof/lib/dma.h
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ static inline void dma_sg_init(struct dma_sg_elem_array *ea)
}

int dma_sg_alloc(struct dma_sg_elem_array *ea,
enum mem_zone zone,
uint32_t flags,
uint32_t direction,
uint32_t buffer_count, uint32_t buffer_bytes,
uintptr_t dma_buffer_addr, uintptr_t external_addr);
Expand Down
4 changes: 2 additions & 2 deletions posix/include/sof/lib/mm_heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ uint32_t mm_pm_context_size(void);
void init_heap(struct sof *sof);

/* frees entire heap (supported for secondary core system heap atm) */
void free_heap(enum mem_zone zone);
void free_heap(void);

/* status */
void heap_trace_all(int force);
Expand All @@ -101,7 +101,7 @@ void heap_trace(struct mm_heap *heap, int size);
* @param out output variable
* @return error code or zero
*/
int heap_info(enum mem_zone zone, int index, struct mm_info *out);
int heap_info(int index, struct mm_info *out);
#endif

/* retrieve memory map pointer */
Expand Down
4 changes: 2 additions & 2 deletions src/audio/aria/aria.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ static int aria_init(struct processing_module *mod)
list_init(&dev->bsource_list);
list_init(&dev->bsink_list);

cd = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, sizeof(*cd));
cd = rzalloc(SOF_MEM_FLAG_USER, sizeof(*cd));
if (!cd) {
return -ENOMEM;
}
Expand All @@ -145,7 +145,7 @@ static int aria_init(struct processing_module *mod)
}
mod_data->private = cd;

buf = rballoc(0, SOF_MEM_CAPS_RAM, req_mem);
buf = rballoc(SOF_MEM_FLAG_USER, req_mem);

if (!buf) {
rfree(cd);
Expand Down
10 changes: 5 additions & 5 deletions src/audio/asrc/asrc.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ static int asrc_init(struct processing_module *mod)
return -EINVAL;
}

cd = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, sizeof(*cd));
cd = rzalloc(SOF_MEM_FLAG_USER, sizeof(*cd));
if (!cd)
return -ENOMEM;

Expand Down Expand Up @@ -261,7 +261,7 @@ static int asrc_initialize_buffers(struct asrc_farrow *src_obj)
buffer_size = src_obj->buffer_length * sizeof(int32_t);

for (ch = 0; ch < src_obj->num_channels; ch++) {
buf_32 = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, buffer_size);
buf_32 = rzalloc(SOF_MEM_FLAG_USER, buffer_size);

if (!buf_32)
return -ENOMEM;
Expand All @@ -272,7 +272,7 @@ static int asrc_initialize_buffers(struct asrc_farrow *src_obj)
buffer_size = src_obj->buffer_length * sizeof(int16_t);

for (ch = 0; ch < src_obj->num_channels; ch++) {
buf_16 = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, buffer_size);
buf_16 = rzalloc(SOF_MEM_FLAG_USER, buffer_size);

if (!buf_16)
return -ENOMEM;
Expand Down Expand Up @@ -614,7 +614,7 @@ static int asrc_prepare(struct processing_module *mod,
cd->buf_size = (cd->source_frames_max + cd->sink_frames_max) *
frame_bytes;

cd->buf = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM,
cd->buf = rzalloc(SOF_MEM_FLAG_USER,
cd->buf_size);
if (!cd->buf) {
cd->buf_size = 0;
Expand All @@ -640,7 +640,7 @@ static int asrc_prepare(struct processing_module *mod,
goto err_free_buf;
}

cd->asrc_obj = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM,
cd->asrc_obj = rzalloc(SOF_MEM_FLAG_USER,
cd->asrc_size);
if (!cd->asrc_obj) {
comp_err(dev, "asrc_prepare(), allocation fail for size %d",
Expand Down
6 changes: 5 additions & 1 deletion src/audio/base_fw_intel.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,11 @@ __cold static int basefw_mem_state_info(uint32_t *data_offset, char *data)
info.page_alloc_struct.page_alloc_count * sizeof(uint32_t);
size = ALIGN(size, 4);
/* size is also saved as tuple length */
tuple_data = rballoc(0, SOF_MEM_CAPS_RAM, size);
tuple_data = rballoc(SOF_MEM_FLAG_USER, size);
if (!tuple_data) {
LOG_ERR("basefw_mem_state_info(): allocation failed");
return IPC4_ERROR_INVALID_PARAM;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's an IPC4 error for "out of memory"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially thought that too, but no.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lgirdwood IPC4_OUT_OF_MEMORY

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lgirdwood should we update this one while fixing rfree()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll followup in another PR - I want to change it to IPC4_ERROR_ prefix, this was why it was not grep-able

}

/* save memory info in data array since info length is variable */
index = 0;
Expand Down
54 changes: 28 additions & 26 deletions src/audio/buffers/comp_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,24 +181,25 @@ static const struct audio_buffer_ops audio_buffer_ops = {
.set_alignment_constants = comp_buffer_set_alignment_constants
};

static struct comp_buffer *buffer_alloc_struct(void *stream_addr, size_t size, uint32_t caps,
static struct comp_buffer *buffer_alloc_struct(void *stream_addr, size_t size,
uint32_t flags, bool is_shared)
{
struct comp_buffer *buffer;

tr_dbg(&buffer_tr, "buffer_alloc_struct()");

/* allocate new buffer */
enum mem_zone zone = is_shared ? SOF_MEM_ZONE_RUNTIME_SHARED : SOF_MEM_ZONE_RUNTIME;
/* allocate new buffer, but add coherent if shared with other cores */
if (is_shared)
flags |= SOF_MEM_FLAG_COHERENT;

buffer = rzalloc(zone, 0, SOF_MEM_CAPS_RAM, sizeof(*buffer));
buffer = rzalloc(flags, sizeof(*buffer));

if (!buffer) {
tr_err(&buffer_tr, "buffer_alloc_struct(): could not alloc structure");
return NULL;
}

buffer->caps = caps;
buffer->flags = flags;
/* Force channels to 2 for init to prevent bad call to clz in buffer_init_stream */
buffer->stream.runtime_stream_params.channels = 2;

Expand All @@ -219,7 +220,7 @@ static struct comp_buffer *buffer_alloc_struct(void *stream_addr, size_t size, u
return buffer;
}

struct comp_buffer *buffer_alloc(size_t size, uint32_t caps, uint32_t flags, uint32_t align,
struct comp_buffer *buffer_alloc(size_t size, uint32_t flags, uint32_t align,
bool is_shared)
{
struct comp_buffer *buffer;
Expand All @@ -233,14 +234,14 @@ struct comp_buffer *buffer_alloc(size_t size, uint32_t caps, uint32_t flags, uin
return NULL;
}

stream_addr = rballoc_align(0, caps, size, align);
stream_addr = rballoc_align(flags, size, align);
if (!stream_addr) {
tr_err(&buffer_tr, "buffer_alloc(): could not alloc size = %zu bytes of type = %u",
size, caps);
tr_err(&buffer_tr, "buffer_alloc(): could not alloc size = %zu bytes of flags = 0x%x",
size, flags);
return NULL;
}

buffer = buffer_alloc_struct(stream_addr, size, caps, flags, is_shared);
buffer = buffer_alloc_struct(stream_addr, size, flags, is_shared);
if (!buffer) {
tr_err(&buffer_tr, "buffer_alloc(): could not alloc buffer structure");
rfree(stream_addr);
Expand All @@ -249,7 +250,7 @@ struct comp_buffer *buffer_alloc(size_t size, uint32_t caps, uint32_t flags, uin
return buffer;
}

struct comp_buffer *buffer_alloc_range(size_t preferred_size, size_t minimum_size, uint32_t caps,
struct comp_buffer *buffer_alloc_range(size_t preferred_size, size_t minimum_size,
uint32_t flags, uint32_t align, bool is_shared)
{
struct comp_buffer *buffer;
Expand All @@ -270,20 +271,20 @@ struct comp_buffer *buffer_alloc_range(size_t preferred_size, size_t minimum_siz
preferred_size += minimum_size - preferred_size % minimum_size;

for (size = preferred_size; size >= minimum_size; size -= minimum_size) {
stream_addr = rballoc_align(0, caps, size, align);
stream_addr = rballoc_align(flags, size, align);
if (stream_addr)
break;
}

tr_dbg(&buffer_tr, "buffer_alloc_range(): allocated %zu bytes", size);

if (!stream_addr) {
tr_err(&buffer_tr, "buffer_alloc_range(): could not alloc size = %zu bytes of type = %u",
minimum_size, caps);
tr_err(&buffer_tr, "buffer_alloc_range(): could not alloc size = %zu bytes of type = 0x%x",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'bytes of flags'

minimum_size, flags);
return NULL;
}

buffer = buffer_alloc_struct(stream_addr, size, caps, flags, is_shared);
buffer = buffer_alloc_struct(stream_addr, size, flags, is_shared);
if (!buffer) {
tr_err(&buffer_tr, "buffer_alloc_range(): could not alloc buffer structure");
rfree(stream_addr);
Expand All @@ -298,7 +299,7 @@ void buffer_zero(struct comp_buffer *buffer)
CORE_CHECK_STRUCT(&buffer->audio_buffer);

bzero(audio_stream_get_addr(&buffer->stream), audio_stream_get_size(&buffer->stream));
if (buffer->caps & SOF_MEM_CAPS_DMA)
if (buffer->flags & SOF_MEM_FLAG_DMA)
dcache_writeback_region((__sparse_force void __sparse_cache *)
audio_stream_get_addr(&buffer->stream),
audio_stream_get_size(&buffer->stream));
Expand All @@ -320,16 +321,17 @@ int buffer_set_size(struct comp_buffer *buffer, uint32_t size, uint32_t alignmen
return 0;

if (!alignment)
new_ptr = rbrealloc(audio_stream_get_addr(&buffer->stream), SOF_MEM_FLAG_NO_COPY,
buffer->caps, size, audio_stream_get_size(&buffer->stream));
new_ptr = rbrealloc(audio_stream_get_addr(&buffer->stream),
buffer->flags | SOF_MEM_FLAG_NO_COPY,
size, audio_stream_get_size(&buffer->stream));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need buffer->flags or buffer->dma

else
new_ptr = rbrealloc_align(audio_stream_get_addr(&buffer->stream),
SOF_MEM_FLAG_NO_COPY, buffer->caps, size,
buffer->flags | SOF_MEM_FLAG_NO_COPY, size,
audio_stream_get_size(&buffer->stream), alignment);
/* we couldn't allocate bigger chunk */
if (!new_ptr && size > audio_stream_get_size(&buffer->stream)) {
buf_err(buffer, "resize can't alloc %u bytes type %u",
audio_stream_get_size(&buffer->stream), buffer->caps);
buf_err(buffer, "resize can't alloc %u bytes type 0x%x",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'bytes of flags'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed all of these in a new PR.

audio_stream_get_size(&buffer->stream), buffer->flags);
return -ENOMEM;
}

Expand Down Expand Up @@ -369,24 +371,24 @@ int buffer_set_size_range(struct comp_buffer *buffer, size_t preferred_size, siz
if (!alignment) {
for (new_size = preferred_size; new_size >= minimum_size;
new_size -= minimum_size) {
new_ptr = rbrealloc(ptr, SOF_MEM_FLAG_NO_COPY, buffer->caps, new_size,
actual_size);
new_ptr = rbrealloc(ptr, buffer->flags | SOF_MEM_FLAG_NO_COPY,
new_size, actual_size);
if (new_ptr)
break;
}
} else {
for (new_size = preferred_size; new_size >= minimum_size;
new_size -= minimum_size) {
new_ptr = rbrealloc_align(ptr, SOF_MEM_FLAG_NO_COPY, buffer->caps, new_size,
actual_size, alignment);
new_ptr = rbrealloc_align(ptr, buffer->flags | SOF_MEM_FLAG_NO_COPY,
new_size, actual_size, alignment);
if (new_ptr)
break;
}
}

/* we couldn't allocate bigger chunk */
if (!new_ptr && new_size > actual_size) {
buf_err(buffer, "resize can't alloc %zu bytes type %u", new_size, buffer->caps);
buf_err(buffer, "resize can't alloc %zu bytes type 0x%x", new_size, buffer->flags);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'bytes of flags'

return -ENOMEM;
}

Expand Down
7 changes: 3 additions & 4 deletions src/audio/buffers/ring_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,10 @@ struct ring_buffer *ring_buffer_create(size_t min_available, size_t min_free_spa

/* allocate ring_buffer structure */
if (is_shared)
ring_buffer = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM,
ring_buffer = rzalloc(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT,
sizeof(*ring_buffer));
else
ring_buffer = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM,
sizeof(*ring_buffer));
ring_buffer = rzalloc(SOF_MEM_FLAG_USER, sizeof(*ring_buffer));
if (!ring_buffer)
return NULL;

Expand Down Expand Up @@ -354,7 +353,7 @@ struct ring_buffer *ring_buffer_create(size_t min_available, size_t min_free_spa
ring_buffer->data_buffer_size =
ALIGN_UP(ring_buffer->data_buffer_size, PLATFORM_DCACHE_ALIGN);
ring_buffer->_data_buffer = (__sparse_force __sparse_cache void *)
rballoc_align(0, 0, ring_buffer->data_buffer_size, PLATFORM_DCACHE_ALIGN);
rballoc_align(SOF_MEM_FLAG_USER, ring_buffer->data_buffer_size, PLATFORM_DCACHE_ALIGN);
if (!ring_buffer->_data_buffer)
goto err;

Expand Down
Loading
Loading