Skip to content

Commit

Permalink
zephyr: lib: alloc: Use cached memory for L3 Heap
Browse files Browse the repository at this point in the history
This is recommended HW flow for Intel ADSP code.
L3 Heap should be accessed thought cached pointers
including its management data.

Signed-off-by: Jaroslaw Stelter <Jaroslaw.Stelter@intel.com>
  • Loading branch information
jxstelter committed Dec 12, 2023
1 parent 267d069 commit 19058ef
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions zephyr/lib/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ static inline uintptr_t get_l3_heap_start(void)
* - main_fw_load_offset
* - main fw size in manifest
*/
return (uintptr_t)z_soc_uncached_ptr((__sparse_force void __sparse_cache *)
ROUND_UP(IMR_L3_HEAP_BASE, L3_MEM_PAGE_SIZE));
return (uintptr_t)((__sparse_force void __sparse_cache *)
ROUND_UP(IMR_L3_HEAP_BASE, L3_MEM_PAGE_SIZE));
}

/**
Expand Down Expand Up @@ -144,14 +144,20 @@ static bool is_l3_heap_pointer(void *ptr)
uintptr_t l3_heap_start = get_l3_heap_start();
uintptr_t l3_heap_end = l3_heap_start + get_l3_heap_size();

if (is_cached(ptr))
ptr = z_soc_uncached_ptr((__sparse_force void __sparse_cache *)ptr);

if ((POINTER_TO_UINT(ptr) >= l3_heap_start) && (POINTER_TO_UINT(ptr) < l3_heap_end))
return true;

return false;
}
/**
* Flush cached first page of of L3 memory heap.
* It contains L3 heap management data.
*/
static inline void l3_heap_mng_flush(void)
{
sys_cache_data_flush_and_invd_range((void *)get_l3_heap_start(),
L3_MEM_PAGE_SIZE);
}
#endif

static void *heap_alloc_aligned(struct k_heap *h, size_t min_align, size_t bytes)
Expand All @@ -164,6 +170,10 @@ static void *heap_alloc_aligned(struct k_heap *h, size_t min_align, size_t bytes

key = k_spin_lock(&h->lock);
ret = sys_heap_aligned_alloc(&h->heap, min_align, bytes);
#if CONFIG_L3_HEAP
if (h == &l3_heap)
l3_heap_mng_flush();
#endif
k_spin_unlock(&h->lock, key);

#if CONFIG_SYS_HEAP_RUNTIME_STATS && CONFIG_IPC_MAJOR_4
Expand Down Expand Up @@ -220,7 +230,10 @@ static void heap_free(struct k_heap *h, void *mem)
#endif

sys_heap_free(&h->heap, mem);

#if CONFIG_L3_HEAP
if (h == &l3_heap)
l3_heap_mng_flush();
#endif
k_spin_unlock(&h->lock, key);
}

Expand Down Expand Up @@ -362,6 +375,7 @@ static int heap_init(void)

#if CONFIG_L3_HEAP
sys_heap_init(&l3_heap.heap, UINT_TO_POINTER(get_l3_heap_start()), get_l3_heap_size());
l3_heap_mng_flush();
#endif

return 0;
Expand Down

0 comments on commit 19058ef

Please sign in to comment.