Skip to content

Commit e86eb4d

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 efbe395 commit e86eb4d

File tree

165 files changed

+528
-1703
lines changed

Some content is hidden

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

165 files changed

+528
-1703
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(void);
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: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -181,24 +181,25 @@ static const struct audio_buffer_ops audio_buffer_ops = {
181181
.set_alignment_constants = comp_buffer_set_alignment_constants
182182
};
183183

184-
static struct comp_buffer *buffer_alloc_struct(void *stream_addr, size_t size, uint32_t caps,
184+
static struct comp_buffer *buffer_alloc_struct(void *stream_addr, size_t size,
185185
uint32_t flags, bool is_shared)
186186
{
187187
struct comp_buffer *buffer;
188188

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

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

194-
buffer = rzalloc(zone, 0, SOF_MEM_CAPS_RAM, sizeof(*buffer));
195+
buffer = rzalloc(flags, sizeof(*buffer));
195196

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

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

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

222-
struct comp_buffer *buffer_alloc(size_t size, uint32_t caps, uint32_t flags, uint32_t align,
223+
struct comp_buffer *buffer_alloc(size_t size, uint32_t flags, uint32_t align,
223224
bool is_shared)
224225
{
225226
struct comp_buffer *buffer;
@@ -233,14 +234,14 @@ struct comp_buffer *buffer_alloc(size_t size, uint32_t caps, uint32_t flags, uin
233234
return NULL;
234235
}
235236

236-
stream_addr = rballoc_align(0, caps, size, align);
237+
stream_addr = rballoc_align(flags, size, align);
237238
if (!stream_addr) {
238-
tr_err(&buffer_tr, "buffer_alloc(): could not alloc size = %zu bytes of type = %u",
239-
size, caps);
239+
tr_err(&buffer_tr, "buffer_alloc(): could not alloc size = %zu bytes of flags = 0x%x",
240+
size, flags);
240241
return NULL;
241242
}
242243

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

252-
struct comp_buffer *buffer_alloc_range(size_t preferred_size, size_t minimum_size, uint32_t caps,
253+
struct comp_buffer *buffer_alloc_range(size_t preferred_size, size_t minimum_size,
253254
uint32_t flags, uint32_t align, bool is_shared)
254255
{
255256
struct comp_buffer *buffer;
@@ -270,20 +271,20 @@ struct comp_buffer *buffer_alloc_range(size_t preferred_size, size_t minimum_siz
270271
preferred_size += minimum_size - preferred_size % minimum_size;
271272

272273
for (size = preferred_size; size >= minimum_size; size -= minimum_size) {
273-
stream_addr = rballoc_align(0, caps, size, align);
274+
stream_addr = rballoc_align(flags, size, align);
274275
if (stream_addr)
275276
break;
276277
}
277278

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

280281
if (!stream_addr) {
281-
tr_err(&buffer_tr, "buffer_alloc_range(): could not alloc size = %zu bytes of type = %u",
282-
minimum_size, caps);
282+
tr_err(&buffer_tr, "buffer_alloc_range(): could not alloc size = %zu bytes of type = 0x%x",
283+
minimum_size, flags);
283284
return NULL;
284285
}
285286

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

300301
bzero(audio_stream_get_addr(&buffer->stream), audio_stream_get_size(&buffer->stream));
301-
if (buffer->caps & SOF_MEM_CAPS_DMA)
302+
if (buffer->flags & SOF_MEM_FLAG_DMA)
302303
dcache_writeback_region((__sparse_force void __sparse_cache *)
303304
audio_stream_get_addr(&buffer->stream),
304305
audio_stream_get_size(&buffer->stream));
@@ -320,16 +321,17 @@ int buffer_set_size(struct comp_buffer *buffer, uint32_t size, uint32_t alignmen
320321
return 0;
321322

322323
if (!alignment)
323-
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+
new_ptr = rbrealloc(audio_stream_get_addr(&buffer->stream),
325+
buffer->flags | SOF_MEM_FLAG_NO_COPY,
326+
size, audio_stream_get_size(&buffer->stream));
325327
else
326328
new_ptr = rbrealloc_align(audio_stream_get_addr(&buffer->stream),
327-
SOF_MEM_FLAG_NO_COPY, buffer->caps, size,
329+
buffer->flags | SOF_MEM_FLAG_NO_COPY, size,
328330
audio_stream_get_size(&buffer->stream), alignment);
329331
/* we couldn't allocate bigger chunk */
330332
if (!new_ptr && size > audio_stream_get_size(&buffer->stream)) {
331-
buf_err(buffer, "resize can't alloc %u bytes type %u",
332-
audio_stream_get_size(&buffer->stream), buffer->caps);
333+
buf_err(buffer, "resize can't alloc %u bytes type 0x%x",
334+
audio_stream_get_size(&buffer->stream), buffer->flags);
333335
return -ENOMEM;
334336
}
335337

@@ -369,24 +371,24 @@ int buffer_set_size_range(struct comp_buffer *buffer, size_t preferred_size, siz
369371
if (!alignment) {
370372
for (new_size = preferred_size; new_size >= minimum_size;
371373
new_size -= minimum_size) {
372-
new_ptr = rbrealloc(ptr, SOF_MEM_FLAG_NO_COPY, buffer->caps, new_size,
373-
actual_size);
374+
new_ptr = rbrealloc(ptr, buffer->flags | SOF_MEM_FLAG_NO_COPY,
375+
new_size, actual_size);
374376
if (new_ptr)
375377
break;
376378
}
377379
} else {
378380
for (new_size = preferred_size; new_size >= minimum_size;
379381
new_size -= minimum_size) {
380-
new_ptr = rbrealloc_align(ptr, SOF_MEM_FLAG_NO_COPY, buffer->caps, new_size,
381-
actual_size, alignment);
382+
new_ptr = rbrealloc_align(ptr, buffer->flags | SOF_MEM_FLAG_NO_COPY,
383+
new_size, actual_size, alignment);
382384
if (new_ptr)
383385
break;
384386
}
385387
}
386388

387389
/* we couldn't allocate bigger chunk */
388390
if (!new_ptr && new_size > actual_size) {
389-
buf_err(buffer, "resize can't alloc %zu bytes type %u", new_size, buffer->caps);
391+
buf_err(buffer, "resize can't alloc %zu bytes type 0x%x", new_size, buffer->flags);
390392
return -ENOMEM;
391393
}
392394

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

0 commit comments

Comments
 (0)