Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions zephyr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,18 @@ if (CONFIG_SOC_SERIES_INTEL_ADSP_ACE)
${SOF_PLATFORM_PATH}/novalake/lib/clk.c
)

# Sources for virtual heap management
zephyr_library_sources(
lib/regions_mm.c
)
# Virtual memory support is required and can be enabled with
# either VMH or Virtual pages and regions.
if (CONFIG_SOF_VREGIONS)
zephyr_library_sources(
lib/vpages.c
lib/vregion.c
)
else()
zephyr_library_sources(
lib/regions_mm.c
)
endif()

zephyr_library_sources_ifdef(CONFIG_CAVS_LPS
${SOF_PLATFORM_PATH}/intel/ace/lps_wait.c
Expand Down
25 changes: 24 additions & 1 deletion zephyr/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,29 @@ config SOF_USERSPACE_APPLICATION
Not manually settable. This is effectively a shortcut to replace numerous
checks for (CONFIG_USERSPACE && !CONFIG_SOF_USERSPACE_PROXY)

config SOF_VPAGE_MAX_ALLOCS
int "Number of virtual memory page allocation elements"
default 128
help
This setting defines the maximum number of virtual memory page allocation
elements that can be tracked. Each allocation element represents a
contiguous block of virtual memory allocated from the virtual memory
region. Increasing this number allows for more simultaneous page allocations,
but also increases the memory overhead for tracking these allocations.

config SOF_VREGIONS
bool "Enable virtual memory regions"
default n
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

superfluous line

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

depends on ACE
help
Enable the virtual regions memory allocator for pipeline resource management.
This provides a way to manage memory resources for audio pipelines,
including
1) multiple pipeline static lifetime allocations.
2) runtime pipeline allocations.
3) pipeline shared memory allocations.
4) module text allocation.

config ZEPHYR_NATIVE_DRIVERS
bool "Use Zephyr native drivers"
help
Expand Down Expand Up @@ -232,7 +255,7 @@ config SOF_ZEPHYR_NO_SOF_CLOCK

config VIRTUAL_HEAP
bool "Use virtual memory heap to allocate a buffers"
default y if ACE
default n
depends on ACE
help
Enabling this option will use the virtual memory heap allocator to allocate buffers.
Expand Down
49 changes: 49 additions & 0 deletions zephyr/include/sof/lib/vpages.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright(c) 2025 Intel Corporation.

/* Virtual Page Allocator API */
#ifndef __SOF_LIB_VPAGE_H__
#define __SOF_LIB_VPAGE_H__

#include <zephyr/kernel.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Allocate virtual pages
* Allocates a specified number of contiguous virtual memory pages by mapping
* physical pages.
*
* @param[in] pages Number of 4kB pages to allocate.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we really hard-specify the size?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a runtime decision based on topology so yes the size must be aligned.

*
* @return Pointer to the allocated virtual memory region, or NULL on failure.
*/
void *alloc_vpages(uint32_t pages);

/**
* @brief Free virtual pages
* Frees previously allocated virtual memory pages and unmaps them.
*
* @param[in] ptr Pointer to the memory pages to free.
*/
void free_vpages(void *ptr);

/**
* @brief Initialize virtual page allocator
*
* Initializes a virtual page allocator that manages a virtual memory region
* using a page table and block structures.
*
* @retval 0 if successful.
* @retval -ENOMEM on creation failure.
*/
int init_vpages(void);

#ifdef __cplusplus
}
#endif

#endif /* __SOF_LIB_VPAGE_H__ */
106 changes: 106 additions & 0 deletions zephyr/include/sof/lib/vregion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright(c) 2025 Intel Corporation.

/* Pre Allocated Contiguous Virtual Region */
#ifndef __SOF_LIB_VREGION_H__
#define __SOF_LIB_VREGION_H__

#include <zephyr/kernel.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

struct vregion;

/**
* @brief Create a new virtual region instance.
*
* Create a new virtual region instance with specified static, dynamic, and shared static sizes
* plus an optional read-only text partition and optional shared static partition.
* Total size is the sum of static, dynamic, shared static, and text sizes.
*
* @param[in] lifetime_size Size of the virtual region lifetime partition.
* @param[in] interim_size Size of the virtual region interim partition.
* @param[in] lifetime_shared_size Size of the virtual region shared lifetime partition.
* @param[in] interim_shared_size Size of the virtual region shared interim partition.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"shared across cores" means "coherent" / "uncached." But how are you proposing to share with (all?) other domains? And why? Every time a new domain is created it has to be added to all these regions? And every time a new such allocator is created it has to be added to all already running domains?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use case is untrusted DP module - shared here means buffers/objects it has to share with userspace pipeline infra. Once vregion is created for DP, this shared region will be shared with userspace pipeline infra for buffer IO.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, so not shared with all, but among 2 possibly 3 parties: audio buffer memory would be shared between (accessible to) source and sink and buffer objects might need to be additionally accessible to the (user-space) framework?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DP can be shared across cores too, hence we need to support (in future updates) cross domain permissions and cross core coherency.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct, so that memory has to be uncached and accessible to source, sink and framework user-space threads, right?

* @param[in] text_size Size of the optional read-only text partition.
* @return struct vregion* Pointer to the new virtual region instance, or NULL on failure.
*/
struct vregion *vregion_create(size_t lifetime_size, size_t interim_size,
size_t lifetime_shared_size, size_t interim_shared_size,
size_t text_size);

/**
* @brief Destroy a virtual region instance.
*
* Free all associated resources and deallocate the virtual region instance.
*
* @param[in] vr Pointer to the virtual region instance to destroy.
*/
void vregion_destroy(struct vregion *vr);

/**
* @brief Memory types for virtual region allocations.
* Used to specify the type of memory allocation within a virtual region.
*
* @note
* - interim: allocation that can be freed i.e. get/set large config, kcontrols.
* - lifetime: allocation that cannot be freed i.e. init data, pipeline data.
* - shared: allocation that can be shared between multiple virtual regions and
* - with different cores.
*/
enum vregion_mem_type {
VREGION_MEM_TYPE_INTERIM, /* interim allocation that can be freed */
VREGION_MEM_TYPE_LIFETIME, /* lifetime allocation */
VREGION_MEM_TYPE_INTERIM_SHARED, /* shared interim allocation */
VREGION_MEM_TYPE_LIFETIME_SHARED /* shared lifetime allocation */
};

/**
* @brief Allocate memory from the specified virtual region.
*
* @param[in] vr Pointer to the virtual region instance.
* @param[in] type Type of memory to allocate (static, dynamic, or shared static).
* @param[in] size Size of memory to allocate in bytes.
* @return void* Pointer to the allocated memory, or NULL on failure.
*/
void *vregion_alloc(struct vregion *vr, enum vregion_mem_type type, size_t size);

/**
* @brief Allocate aligned memory from the specified virtual region.
*
* Allocate aligned memory from the specified virtual region based on the memory type.
*
* @param[in] vr Pointer to the virtual region instance.
* @param[in] type Type of memory to allocate (static, dynamic, or shared static).
* @param[in] size Size of memory to allocate in bytes.
* @param[in] alignment Alignment of memory to allocate in bytes.
* @return void* Pointer to the allocated memory, or NULL on failure.
*/
void *vregion_alloc_align(struct vregion *vr, enum vregion_mem_type type,
size_t size, size_t alignment);

/**
* @brief Free memory allocated from the specified virtual region.
*
* Free memory previously allocated from the specified virtual region.
*
* @param[in] vr Pointer to the virtual region instance.
* @param[in] ptr Pointer to the memory to free.
*/
void vregion_free(struct vregion *vr, void *ptr);

/**
* @brief Log virtual region memory usage.
*
* @param[in] vr Pointer to the virtual region instance.
*/
void vregion_info(struct vregion *vr);

#ifdef __cplusplus
}
#endif

#endif /* __SOF_LIB_VREGION_H__ */
Loading
Loading