Sound Open Firmware
Loading...
Searching...
No Matches
alloc.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-3-Clause
2 *
3 * Copyright(c) 2022 Intel Corporation. All rights reserved.
4 */
5
12#ifndef __ZEPHYR_RTOS_ALLOC_H__
13#define __ZEPHYR_RTOS_ALLOC_H__
14
15#include <rtos/bit.h>
16#include <rtos/string.h>
17#include <sof/lib/memory.h> /* PLATFORM_DCACHE_ALIGN */
18#include <sof/trace/trace.h>
19#include <user/trace.h>
20
21#include <stddef.h>
22#include <stdint.h>
23
28/*
29 * for compatibility with the initial `flags` meaning
30 * SOF_MEM_FLAG_ should start at BIT(2)
31 * the first two positions are reserved for SOF_BUF_ flags
32 */
33
39#define SOF_MEM_FLAG_DMA BIT(2)
45#define SOF_MEM_FLAG_NO_COPY BIT(3)
53#define SOF_MEM_FLAG_COHERENT BIT(4)
60#define SOF_MEM_FLAG_L3 BIT(5)
65#define SOF_MEM_FLAG_LOW_POWER BIT(6)
67#define SOF_MEM_FLAG_KERNEL BIT(7)
69#define SOF_MEM_FLAG_USER BIT(8)
71#define SOF_MEM_FLAG_USER_SHARED_BUFFER BIT(9)
73#define SOF_MEM_FLAG_LARGE_BUFFER BIT(10)
74
95void *rmalloc_align(uint32_t flags, size_t bytes, uint32_t alignment);
96
106void *rmalloc(uint32_t flags, size_t bytes);
107
118void *rzalloc(uint32_t flags, size_t bytes);
119
135void *rballoc_align(uint32_t flags, size_t bytes,
136 uint32_t alignment);
137
147static inline void *rballoc(uint32_t flags, size_t bytes)
148{
149 return rballoc_align(flags, bytes, PLATFORM_DCACHE_ALIGN);
150}
151
162void rfree(void *ptr);
163
170void l3_heap_save(void);
171
180#ifdef CONFIG_SOF_USERSPACE_INTERFACE_ALLOC
181__syscall void *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes,
182 size_t alignment);
188__syscall void sof_heap_free(struct k_heap *heap, void *addr);
189#include <zephyr/syscalls/alloc.h>
190#else
191void *sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes,
192 size_t alignment);
193void sof_heap_free(struct k_heap *heap, void *addr);
194#endif
195
200#if CONFIG_SOF_FULL_ZEPHYR_APPLICATION
201struct k_heap *sof_sys_heap_get(void);
202#else
203/* for unit-testing */
204static inline struct k_heap *sof_sys_heap_get(void)
205{
206 return NULL;
207}
208#endif
209
220struct k_heap *sof_sys_user_heap_get(void);
221
229static inline void heap_trace_all(int force) {}
230
231#if CONFIG_SOF_USERSPACE_USE_SHARED_HEAP
237
243
244#endif
245
246#include <sof/lib/vregion.h>
247
252 struct k_heap *heap;
253 struct vregion *vreg;
254};
255
270static inline void *sof_ctx_alloc(struct mod_alloc_ctx *ctx, uint32_t flags,
271 size_t size, size_t alignment)
272{
273 if (!ctx || !ctx->vreg)
274 return sof_heap_alloc(ctx ? ctx->heap : NULL, flags, size, alignment);
275
276 if (flags & SOF_MEM_FLAG_COHERENT)
277 return vregion_alloc_coherent_align(ctx->vreg, size, alignment);
278
279 return vregion_alloc_align(ctx->vreg, size, alignment);
280}
281
293static inline void *sof_ctx_zalloc(struct mod_alloc_ctx *ctx, uint32_t flags,
294 size_t size, size_t alignment)
295{
296 void *ptr = sof_ctx_alloc(ctx, flags, size, alignment);
297
298 if (ptr)
299 memset(ptr, 0, size);
300
301 return ptr;
302}
303
313static inline void sof_ctx_free(struct mod_alloc_ctx *ctx, void *ptr)
314{
315 if (!ptr)
316 return;
317
318 if (ctx && ctx->vreg)
319 vregion_free(ctx->vreg, ptr);
320 else
321 sof_heap_free(ctx ? ctx->heap : NULL, ptr);
322}
323
326#endif /* __ZEPHYR_RTOS_ALLOC_H__ */
void vregion_free(struct vregion *vr, void *ptr)
Free memory allocated from the specified virtual region.
void * rmalloc_align(uint32_t flags, size_t bytes, uint32_t alignment)
Allocate memory block with explicit alignment and heap selection flags.
size_t get_shared_buffer_heap_size(void)
Returns the total size of shared memory heap for buffers.
static void heap_trace_all(int force)
Trace and log memory allocation statistics across all active heaps.
Definition alloc.h:229
void l3_heap_save(void)
Save and flush L3 heap state and metadata across DSP reset or power gating.
void * rzalloc(uint32_t flags, size_t bytes)
Allocate zero-initialized memory block with default natural alignment.
void * sof_heap_alloc(struct k_heap *heap, uint32_t flags, size_t bytes, size_t alignment)
Allocate memory from a specified Zephyr heap or default system heap.
static void sof_ctx_free(struct mod_alloc_ctx *ctx, void *ptr)
Free memory allocated from an allocation context.
Definition alloc.h:313
void * vregion_alloc_align(struct vregion *vr, size_t size, size_t alignment)
Allocate aligned memory from the specified virtual region.
void rfree(void *ptr)
Free memory block allocated by SOF RTOS allocators.
void * rballoc_align(uint32_t flags, size_t bytes, uint32_t alignment)
Allocate runtime stream buffer with explicit alignment.
struct k_heap * sof_sys_heap_get(void)
Get the system kernel heap descriptor.
uintptr_t get_shared_buffer_heap_start(void)
Returns the start address of shared memory heap for buffers.
static void * rballoc(uint32_t flags, size_t bytes)
Allocate runtime stream buffer aligned to platform data cache line size.
Definition alloc.h:147
void * rmalloc(uint32_t flags, size_t bytes)
Allocate memory block with default natural alignment.
void * vregion_alloc_coherent_align(struct vregion *vr, size_t size, size_t alignment)
Allocate aligned coherent (uncached) memory from the specified virtual region.
static void * sof_ctx_zalloc(struct mod_alloc_ctx *ctx, uint32_t flags, size_t size, size_t alignment)
Allocate zero-initialized memory from an allocation context.
Definition alloc.h:293
static void * sof_ctx_alloc(struct mod_alloc_ctx *ctx, uint32_t flags, size_t size, size_t alignment)
Allocate memory from an allocation context.
Definition alloc.h:270
struct k_heap * sof_sys_user_heap_get(void)
Get the heap object to use for audio application allocations.
#define SOF_MEM_FLAG_COHERENT
Allocate uncached / coherent memory address.
Definition alloc.h:53
void sof_heap_free(struct k_heap *heap, void *addr)
Free memory allocated via sof_heap_alloc().
Memory attributes, cold/hot execution paths, and platform memory inclusions.
User-space ABI log levels and binary trace packet header definitions.
Module and pipeline allocation context aggregating a Zephyr heap and optional virtual region.
Definition alloc.h:251
struct vregion * vreg
Optional virtual region for interim/lifetime partitioning.
Definition alloc.h:253
struct k_heap * heap
Underlying Zephyr heap descriptor.
Definition alloc.h:252
Pre-allocated contiguous virtual memory region allocator (lifetime vs interim).