Skip to content

Commit 76252e7

Browse files
committed
zephyr: alloc: ace: calculate L3 heap size based on actual IMR size
Updates the L3 heap management to dynamically calculate heap size based on the actual IMR size reported by hardware registers instead of using hardcoded values. Only initializes the L3 heap when the IMR is actually available and being used, as determined by the ace_imr_used() function, improving robustness by preventing the initialization of unavailable memory regions. Adds proper memory mapping when MMU is enabled, which maps the physical L3 heap memory to a virtual address with appropriate permissions (read/write with write-back caching). MMU mapping is required because it is no longer a fixed region with fixed mapping in Zephyr. This change makes the L3 heap allocation more flexible and adaptable to different hardware configurations. Signed-off-by: Adrian Bonislawski <adrian.bonislawski@intel.com>
1 parent 15d0fdf commit 76252e7

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

zephyr/lib/alloc.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121

2222
#define SHARED_BUFFER_HEAP_MEM_SIZE 0
2323

24+
#if CONFIG_L3_HEAP && CONFIG_MMU
25+
#include <kernel_arch_interface.h>
26+
#endif
27+
2428
#if CONFIG_VIRTUAL_HEAP
2529
#include <sof/lib/regions_mm.h>
2630
#include <zephyr/drivers/mm/mm_drv_intel_adsp_mtl_tlb.h>
@@ -217,7 +221,9 @@ static inline size_t get_l3_heap_size(void)
217221
* - IMR base address
218222
* - actual IMR heap start
219223
*/
220-
return ROUND_DOWN(IMR_L3_HEAP_SIZE, L3_MEM_PAGE_SIZE);
224+
size_t offset = IMR_L3_HEAP_BASE - L3_MEM_BASE_ADDR;
225+
226+
return ROUND_DOWN(ace_imr_get_mem_size() - offset, L3_MEM_PAGE_SIZE);
221227
}
222228

223229
void l3_heap_save(void)
@@ -651,11 +657,19 @@ static int heap_init(void)
651657
#endif
652658

653659
#if CONFIG_L3_HEAP
654-
if (l3_heap_copy.heap.heap)
660+
if (l3_heap_copy.heap.heap) {
655661
l3_heap = l3_heap_copy;
656-
else
657-
sys_heap_init(&l3_heap.heap, UINT_TO_POINTER(get_l3_heap_start()),
658-
get_l3_heap_size());
662+
} else if (ace_imr_used()) {
663+
void *l3_heap_start = UINT_TO_POINTER(get_l3_heap_start());
664+
size_t l3_heap_size = get_l3_heap_size();
665+
#if CONFIG_MMU
666+
void *cached_ptr = sys_cache_cached_ptr_get(l3_heap_start);
667+
uintptr_t va = POINTER_TO_UINT(cached_ptr);
668+
669+
arch_mem_map(l3_heap_start, va, l3_heap_size, K_MEM_PERM_RW | K_MEM_CACHE_WB);
670+
#endif
671+
sys_heap_init(&l3_heap.heap, l3_heap_start, l3_heap_size);
672+
}
659673
#endif
660674

661675
return 0;

0 commit comments

Comments
 (0)