Skip to content

Commit 92bcbef

Browse files
committed
multi core heap
1 parent 28c622b commit 92bcbef

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

zephyr/lib/alloc.c

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#if CONFIG_VIRTUAL_HEAP
2222
#include <sof/lib/regions_mm.h>
2323

24-
struct vmh_heap *virtual_buffers_heap;
24+
struct vmh_heap *virtual_buffers_heap[CONFIG_MP_MAX_NUM_CPUS];
2525
struct k_spinlock vmh_lock;
2626

2727
#undef HEAPMEM_SIZE
@@ -211,10 +211,10 @@ static void *virtual_heap_alloc(struct vmh_heap *heap, uint32_t flags, uint32_t
211211
{
212212
void *mem;
213213

214-
K_SPINLOCK(&vmh_lock) {
215-
heap->core_id = cpu_get_id();
214+
//K_SPINLOCK(&vmh_lock) {
215+
// heap->core_id = cpu_get_id();
216216
mem = vmh_alloc(heap, bytes);
217-
}
217+
//}
218218

219219
if (!mem)
220220
return NULL;
@@ -247,14 +247,15 @@ static bool is_virtual_heap_pointer(void *ptr)
247247

248248
static void virtual_heap_free(void *ptr)
249249
{
250+
struct vmh_heap *const heap = virtual_buffers_heap[cpu_get_id()];
250251
int ret;
251252

252253
ptr = (__sparse_force void *)sys_cache_cached_ptr_get(ptr);
253254

254-
K_SPINLOCK(&vmh_lock) {
255-
virtual_buffers_heap->core_id = cpu_get_id();
256-
ret = vmh_free(virtual_buffers_heap, ptr);
257-
}
255+
//K_SPINLOCK(&vmh_lock) {
256+
//virtual_buffers_heap->core_id = cpu_get_id();
257+
ret = vmh_free(heap, ptr);
258+
//}
258259

259260
if (ret)
260261
tr_err(&zephyr_tr, "Unable to free %p! %d", ptr, ret);
@@ -276,12 +277,18 @@ static const struct vmh_heap_config static_hp_buffers = {
276277

277278
static int virtual_heap_init(void)
278279
{
280+
int core;
281+
279282
k_spinlock_init(&vmh_lock);
280283

281-
virtual_buffers_heap = vmh_init_heap(&static_hp_buffers, MEM_REG_ATTR_SHARED_HEAP, 0,
282-
false);
283-
if (!virtual_buffers_heap)
284-
tr_err(&zephyr_tr, "Unable to init virtual buffers heap!");
284+
for (core = 0; core < CONFIG_MP_MAX_NUM_CPUS; core++) {
285+
struct vmh_heap *heap = vmh_init_heap(&static_hp_buffers, MEM_REG_ATTR_CORE_HEAP,
286+
core, false);
287+
if (!heap)
288+
tr_err(&zephyr_tr, "Unable to init virtual heap for core %d!", core);
289+
290+
virtual_buffers_heap[core] = heap;
291+
}
285292

286293
return 0;
287294
}
@@ -485,6 +492,9 @@ EXPORT_SYMBOL(rzalloc);
485492
void *rballoc_align(uint32_t flags, uint32_t caps, size_t bytes,
486493
uint32_t align)
487494
{
495+
#if CONFIG_VIRTUAL_HEAP
496+
struct vmh_heap *virtual_heap;
497+
#endif
488498
struct k_heap *heap;
489499
void *ret;
490500

@@ -507,8 +517,9 @@ void *rballoc_align(uint32_t flags, uint32_t caps, size_t bytes,
507517

508518
#if CONFIG_VIRTUAL_HEAP
509519
/* Use virtual heap if it is available */
510-
if (virtual_buffers_heap) {
511-
ret = virtual_heap_alloc(virtual_buffers_heap, flags, caps, bytes, align);
520+
virtual_heap = virtual_buffers_heap[cpu_get_id()];
521+
if (virtual_heap) {
522+
ret = virtual_heap_alloc(virtual_heap, flags, caps, bytes, align);
512523
if (!ret)
513524
tr_err(&zephyr_tr, "!virtual_heap_alloc");
514525
return ret;

0 commit comments

Comments
 (0)