-
Notifications
You must be signed in to change notification settings - Fork 350
alloc: virtual regions and vpage allocator. #10281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we really hard-specify the size?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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__ */ | ||
| 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. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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__ */ | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
superfluous line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed