Skip to content

Commit 75aee7f

Browse files
committed
heap: simplify heap API.
Use a Linux kernel like heap API now that zephyr allocator deals with the complexity. This change removes the memory zone and memory capabilities fields from allocation APIs and rolls these into SOF_MEM_FLAG_ bitmask. This bitmask can be ORed to make any combination of memory needed. Additionally this change introduces the new SOF_MEM_FLAG_KERNEL and SOF_MEM_FLAG_USER flags that have no action today but are used as the base default allocation bits for all allocations. Signed-off-by: Liam Girdwood <liam.r.girdwood@intel.com>
1 parent ee58c00 commit 75aee7f

File tree

163 files changed

+489
-1679
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

163 files changed

+489
-1679
lines changed

posix/include/rtos/alloc.h

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,47 +28,24 @@
2828
* @{
2929
*/
3030

31-
/**
32-
* \brief Heap Memory Zones
33-
*
34-
* The heap has three different zones from where memory can be allocated :-
35-
*
36-
* 1) System Zone. Fixed size heap where alloc always succeeds and is never
37-
* freed. Used by any init code that will never give up the memory.
38-
*
39-
* 2) System Runtime Zone. Heap zone intended for runtime objects allocated
40-
* by the kernel part of the code.
41-
*
42-
* 3) Runtime Zone. Main and larger heap zone where allocs are not guaranteed to
43-
* succeed. Memory can be freed here.
44-
*
45-
* 4) Buffer Zone. Largest heap zone intended for audio buffers.
46-
*
47-
* 5) Runtime Shared Zone. Similar to Runtime Zone, but content may be used and
48-
* fred from any enabled core.
49-
*
50-
* 6) System Shared Zone. Similar to System Zone, but content may be used from
51-
* any enabled core.
52-
*
53-
* See platform/memory.h for heap size configuration and mappings.
54-
*/
55-
enum mem_zone {
56-
SOF_MEM_ZONE_SYS = 0, /**< System zone */
57-
SOF_MEM_ZONE_SYS_RUNTIME, /**< System-runtime zone */
58-
SOF_MEM_ZONE_RUNTIME, /**< Runtime zone */
59-
SOF_MEM_ZONE_BUFFER, /**< Buffer zone */
60-
SOF_MEM_ZONE_RUNTIME_SHARED, /**< Runtime shared zone */
61-
SOF_MEM_ZONE_SYS_SHARED, /**< System shared zone */
62-
};
63-
6431
/** \name Heap zone flags
6532
* @{
6633
*/
6734

35+
/** \brief Indicates we should return DMA-able memory. */
36+
#define SOF_MEM_FLAG_DMA BIT(0)
6837
/** \brief Indicates that original content should not be copied by realloc. */
6938
#define SOF_MEM_FLAG_NO_COPY BIT(1)
7039
/** \brief Indicates that if we should return uncached address. */
71-
#define SOF_MEM_FLAG_COHERENT BIT(2)
40+
#define SOF_MEM_FLAG_COHERENT BIT(2)
41+
/** \brief Indicates that if we should return L3 address. */
42+
#define SOF_MEM_FLAG_L3 BIT(3)
43+
/** \brief Indicates that if we should return Low power memory address. */
44+
#define SOF_MEM_FLAG_LOW_POWER BIT(4)
45+
/** \brief Indicates that if we should return kernel memory address. */
46+
#define SOF_MEM_FLAG_KERNEL BIT(5)
47+
/** \brief Indicates that if we should return user memory address. */
48+
#define SOF_MEM_FLAG_USER BIT(6)
7249

7350
/** @} */
7451

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

8865
/**
8966
* Similar to rmalloc(), guarantees that returned block is zeroed.
9067
*
9168
* @note Do not use for buffers (SOF_MEM_ZONE_BUFFER zone).
9269
* rballoc(), rballoc_align() to allocate memory for buffers.
9370
*/
94-
void *rzalloc(enum mem_zone zone, uint32_t flags, uint32_t caps, size_t bytes);
71+
void *rzalloc(uint32_t flags, size_t bytes);
9572

9673
/**
9774
* Allocates memory block from SOF_MEM_ZONE_BUFFER.
@@ -101,15 +78,15 @@ void *rzalloc(enum mem_zone zone, uint32_t flags, uint32_t caps, size_t bytes);
10178
* @param alignment Alignment in bytes.
10279
* @return Pointer to the allocated memory or NULL if failed.
10380
*/
104-
void *rballoc_align(uint32_t flags, uint32_t caps, size_t bytes,
81+
void *rballoc_align(uint32_t flags, size_t bytes,
10582
uint32_t alignment);
10683

10784
/**
10885
* Similar to rballoc_align(), returns buffer aligned to PLATFORM_DCACHE_ALIGN.
10986
*/
110-
static inline void *rballoc(uint32_t flags, uint32_t caps, size_t bytes)
87+
static inline void *rballoc(uint32_t flags, size_t bytes)
11188
{
112-
return rballoc_align(flags, caps, bytes, PLATFORM_DCACHE_ALIGN);
89+
return rballoc_align(flags, bytes, PLATFORM_DCACHE_ALIGN);
11390
}
11491

11592
/**
@@ -122,17 +99,17 @@ static inline void *rballoc(uint32_t flags, uint32_t caps, size_t bytes)
12299
* @param alignment Alignment in bytes.
123100
* @return Pointer to the resized memory of NULL if failed.
124101
*/
125-
void *rbrealloc_align(void *ptr, uint32_t flags, uint32_t caps, size_t bytes,
102+
void *rbrealloc_align(void *ptr, uint32_t flags, size_t bytes,
126103
size_t old_bytes, uint32_t alignment);
127104

128105
/**
129106
* Similar to rballoc_align(), returns resized buffer aligned to
130107
* PLATFORM_DCACHE_ALIGN.
131108
*/
132-
static inline void *rbrealloc(void *ptr, uint32_t flags, uint32_t caps,
109+
static inline void *rbrealloc(void *ptr, uint32_t flags,
133110
size_t bytes, size_t old_bytes)
134111
{
135-
return rbrealloc_align(ptr, flags, caps, bytes, old_bytes,
112+
return rbrealloc_align(ptr, flags, bytes, old_bytes,
136113
PLATFORM_DCACHE_ALIGN);
137114
}
138115

posix/include/sof/lib/dma.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ static inline void dma_sg_init(struct dma_sg_elem_array *ea)
512512
}
513513

514514
int dma_sg_alloc(struct dma_sg_elem_array *ea,
515-
enum mem_zone zone,
515+
uint32_t flags,
516516
uint32_t direction,
517517
uint32_t buffer_count, uint32_t buffer_bytes,
518518
uintptr_t dma_buffer_addr, uintptr_t external_addr);

posix/include/sof/lib/mm_heap.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ uint32_t mm_pm_context_size(void);
8888
void init_heap(struct sof *sof);
8989

9090
/* frees entire heap (supported for secondary core system heap atm) */
91-
void free_heap(enum mem_zone zone);
91+
void free_heap();
9292

9393
/* status */
9494
void heap_trace_all(int force);
@@ -101,7 +101,7 @@ void heap_trace(struct mm_heap *heap, int size);
101101
* @param out output variable
102102
* @return error code or zero
103103
*/
104-
int heap_info(enum mem_zone zone, int index, struct mm_info *out);
104+
int heap_info(int index, struct mm_info *out);
105105
#endif
106106

107107
/* retrieve memory map pointer */

src/audio/aria/aria.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ static int aria_init(struct processing_module *mod)
126126
list_init(&dev->bsource_list);
127127
list_init(&dev->bsink_list);
128128

129-
cd = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, sizeof(*cd));
129+
cd = rzalloc(SOF_MEM_FLAG_USER, sizeof(*cd));
130130
if (!cd) {
131131
return -ENOMEM;
132132
}
@@ -145,7 +145,7 @@ static int aria_init(struct processing_module *mod)
145145
}
146146
mod_data->private = cd;
147147

148-
buf = rballoc(0, SOF_MEM_CAPS_RAM, req_mem);
148+
buf = rballoc(SOF_MEM_FLAG_USER, req_mem);
149149

150150
if (!buf) {
151151
rfree(cd);

src/audio/asrc/asrc.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ static int asrc_init(struct processing_module *mod)
217217
return -EINVAL;
218218
}
219219

220-
cd = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, sizeof(*cd));
220+
cd = rzalloc(SOF_MEM_FLAG_USER, sizeof(*cd));
221221
if (!cd)
222222
return -ENOMEM;
223223

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

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

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

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

277277
if (!buf_16)
278278
return -ENOMEM;
@@ -614,7 +614,7 @@ static int asrc_prepare(struct processing_module *mod,
614614
cd->buf_size = (cd->source_frames_max + cd->sink_frames_max) *
615615
frame_bytes;
616616

617-
cd->buf = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM,
617+
cd->buf = rzalloc(SOF_MEM_FLAG_USER,
618618
cd->buf_size);
619619
if (!cd->buf) {
620620
cd->buf_size = 0;
@@ -640,7 +640,7 @@ static int asrc_prepare(struct processing_module *mod,
640640
goto err_free_buf;
641641
}
642642

643-
cd->asrc_obj = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM,
643+
cd->asrc_obj = rzalloc(SOF_MEM_FLAG_USER,
644644
cd->asrc_size);
645645
if (!cd->asrc_obj) {
646646
comp_err(dev, "asrc_prepare(), allocation fail for size %d",

src/audio/base_fw_intel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ __cold static int basefw_mem_state_info(uint32_t *data_offset, char *data)
182182
info.page_alloc_struct.page_alloc_count * sizeof(uint32_t);
183183
size = ALIGN(size, 4);
184184
/* size is also saved as tuple length */
185-
tuple_data = rballoc(0, SOF_MEM_CAPS_RAM, size);
185+
tuple_data = rballoc(SOF_MEM_FLAG_USER, size);
186186

187187
/* save memory info in data array since info length is variable */
188188
index = 0;

src/audio/buffers/comp_buffer.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@ static struct comp_buffer *buffer_alloc_struct(void *stream_addr, size_t size, u
189189
tr_dbg(&buffer_tr, "buffer_alloc_struct()");
190190

191191
/* allocate new buffer */
192-
enum mem_zone zone = is_shared ? SOF_MEM_ZONE_RUNTIME_SHARED : SOF_MEM_ZONE_RUNTIME;
192+
int alloc_flags = is_shared ? SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT : SOF_MEM_FLAG_USER;
193193

194-
buffer = rzalloc(zone, 0, SOF_MEM_CAPS_RAM, sizeof(*buffer));
194+
buffer = rzalloc(alloc_flags, sizeof(*buffer));
195195

196196
if (!buffer) {
197197
tr_err(&buffer_tr, "buffer_alloc_struct(): could not alloc structure");
@@ -233,7 +233,7 @@ struct comp_buffer *buffer_alloc(size_t size, uint32_t caps, uint32_t flags, uin
233233
return NULL;
234234
}
235235

236-
stream_addr = rballoc_align(0, caps, size, align);
236+
stream_addr = rballoc_align(SOF_MEM_FLAG_USER, size, align);
237237
if (!stream_addr) {
238238
tr_err(&buffer_tr, "buffer_alloc(): could not alloc size = %zu bytes of type = %u",
239239
size, caps);
@@ -270,7 +270,7 @@ struct comp_buffer *buffer_alloc_range(size_t preferred_size, size_t minimum_siz
270270
preferred_size += minimum_size - preferred_size % minimum_size;
271271

272272
for (size = preferred_size; size >= minimum_size; size -= minimum_size) {
273-
stream_addr = rballoc_align(0, caps, size, align);
273+
stream_addr = rballoc_align(SOF_MEM_FLAG_USER, size, align);
274274
if (stream_addr)
275275
break;
276276
}
@@ -321,10 +321,10 @@ int buffer_set_size(struct comp_buffer *buffer, uint32_t size, uint32_t alignmen
321321

322322
if (!alignment)
323323
new_ptr = rbrealloc(audio_stream_get_addr(&buffer->stream), SOF_MEM_FLAG_NO_COPY,
324-
buffer->caps, size, audio_stream_get_size(&buffer->stream));
324+
size, audio_stream_get_size(&buffer->stream));
325325
else
326326
new_ptr = rbrealloc_align(audio_stream_get_addr(&buffer->stream),
327-
SOF_MEM_FLAG_NO_COPY, buffer->caps, size,
327+
SOF_MEM_FLAG_NO_COPY, size,
328328
audio_stream_get_size(&buffer->stream), alignment);
329329
/* we couldn't allocate bigger chunk */
330330
if (!new_ptr && size > audio_stream_get_size(&buffer->stream)) {
@@ -369,15 +369,15 @@ int buffer_set_size_range(struct comp_buffer *buffer, size_t preferred_size, siz
369369
if (!alignment) {
370370
for (new_size = preferred_size; new_size >= minimum_size;
371371
new_size -= minimum_size) {
372-
new_ptr = rbrealloc(ptr, SOF_MEM_FLAG_NO_COPY, buffer->caps, new_size,
372+
new_ptr = rbrealloc(ptr, SOF_MEM_FLAG_NO_COPY, new_size,
373373
actual_size);
374374
if (new_ptr)
375375
break;
376376
}
377377
} else {
378378
for (new_size = preferred_size; new_size >= minimum_size;
379379
new_size -= minimum_size) {
380-
new_ptr = rbrealloc_align(ptr, SOF_MEM_FLAG_NO_COPY, buffer->caps, new_size,
380+
new_ptr = rbrealloc_align(ptr, SOF_MEM_FLAG_NO_COPY, new_size,
381381
actual_size, alignment);
382382
if (new_ptr)
383383
break;

src/audio/buffers/ring_buffer.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,10 @@ struct ring_buffer *ring_buffer_create(size_t min_available, size_t min_free_spa
280280

281281
/* allocate ring_buffer structure */
282282
if (is_shared)
283-
ring_buffer = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM,
283+
ring_buffer = rzalloc(SOF_MEM_FLAG_USER | SOF_MEM_FLAG_COHERENT,
284284
sizeof(*ring_buffer));
285285
else
286-
ring_buffer = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM,
287-
sizeof(*ring_buffer));
286+
ring_buffer = rzalloc(SOF_MEM_FLAG_USER, sizeof(*ring_buffer));
288287
if (!ring_buffer)
289288
return NULL;
290289

@@ -354,7 +353,7 @@ struct ring_buffer *ring_buffer_create(size_t min_available, size_t min_free_spa
354353
ring_buffer->data_buffer_size =
355354
ALIGN_UP(ring_buffer->data_buffer_size, PLATFORM_DCACHE_ALIGN);
356355
ring_buffer->_data_buffer = (__sparse_force __sparse_cache void *)
357-
rballoc_align(0, 0, ring_buffer->data_buffer_size, PLATFORM_DCACHE_ALIGN);
356+
rballoc_align(SOF_MEM_FLAG_USER, ring_buffer->data_buffer_size, PLATFORM_DCACHE_ALIGN);
358357
if (!ring_buffer->_data_buffer)
359358
goto err;
360359

src/audio/chain_dma.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ __cold static int chain_task_init(struct comp_dev *dev, uint8_t host_dma_id, uin
605605

606606
fifo_size = ALIGN_UP_INTERNAL(fifo_size, addr_align);
607607
/* allocate not shared buffer */
608-
cd->dma_buffer = buffer_alloc(fifo_size, SOF_MEM_CAPS_DMA, 0, addr_align, false);
608+
cd->dma_buffer = buffer_alloc(fifo_size, 0, SOF_MEM_FLAG_USER | SOF_MEM_FLAG_DMA, addr_align, false);
609609

610610
if (!cd->dma_buffer) {
611611
comp_err(dev, "chain_task_init(): failed to alloc dma buffer");
@@ -668,7 +668,7 @@ __cold static struct comp_dev *chain_task_create(const struct comp_driver *drv,
668668
if (!dev)
669669
return NULL;
670670

671-
cd = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, sizeof(*cd));
671+
cd = rzalloc(SOF_MEM_FLAG_USER, sizeof(*cd));
672672
if (!cd)
673673
goto error;
674674

src/audio/copier/copier.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static int mic_privacy_configure(struct comp_dev *dev, struct copier_data *cd)
8787
struct mic_privacy_data *mic_priv_data;
8888
int ret;
8989

90-
mic_priv_data = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM,
90+
mic_priv_data = rzalloc(SOF_MEM_FLAG_USER,
9191
sizeof(struct mic_privacy_data));
9292
if (!mic_priv_data)
9393
return -ENOMEM;
@@ -144,7 +144,7 @@ __cold static int copier_init(struct processing_module *mod)
144144

145145
assert_can_be_cold();
146146

147-
cd = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, sizeof(*cd));
147+
cd = rzalloc(SOF_MEM_FLAG_USER, sizeof(*cd));
148148
if (!cd)
149149
return -ENOMEM;
150150

@@ -166,7 +166,7 @@ __cold static int copier_init(struct processing_module *mod)
166166
*/
167167
if (copier->gtw_cfg.config_length) {
168168
gtw_cfg_size = copier->gtw_cfg.config_length << 2;
169-
gtw_cfg = rmalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM,
169+
gtw_cfg = rmalloc(SOF_MEM_FLAG_USER,
170170
gtw_cfg_size);
171171
if (!gtw_cfg) {
172172
ret = -ENOMEM;

0 commit comments

Comments
 (0)