Skip to content

Commit 13d5445

Browse files
lrgirdwolyakh
authored andcommitted
vpages: Add a simple virtual continuous page allocator
Add a simple virtual page allocator that uses the Zephyr memory mapping infrastructure to allocate pages from a virtual region and map them to physical pages. Due to simplicity, the number of allocations is limited to CONFIG_SOF_VPAGE_MAX_ALLOCS Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com> Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
1 parent ac805f1 commit 13d5445

File tree

4 files changed

+402
-2
lines changed

4 files changed

+402
-2
lines changed

zephyr/Kconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ config SOF_USERSPACE_APPLICATION
145145
Not manually settable. This is effectively a shortcut to replace numerous
146146
checks for (CONFIG_USERSPACE && !CONFIG_SOF_USERSPACE_PROXY)
147147

148+
config SOF_VPAGE_MAX_ALLOCS
149+
int "Number of virtual memory page allocations"
150+
default 128
151+
help
152+
This setting defines the maximum number of virtual memory page
153+
allocations that can be tracked. Each allocation represents a
154+
contiguous block of virtual memory allocated from the virtual memory
155+
region. This API isn't intended for end-user allocations, instead it
156+
should be used by the framework to allocate memory, which is then
157+
used, e.g. to create one or multiple heap allocators in it. Increasing
158+
this number allows for more simultaneous allocations, but also
159+
increases the memory overhead for tracking these allocations.
160+
148161
config ZEPHYR_NATIVE_DRIVERS
149162
bool "Use Zephyr native drivers"
150163
help

zephyr/include/sof/lib/regions_mm.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
#include <zephyr/sys/mem_blocks.h>
1919

2020
/* Attributes for memory regions */
21-
#define VIRTUAL_REGION_SHARED_HEAP_ATTR 1U /*< region dedicated for shared virtual heap */
22-
#define VIRTUAL_REGION_LLEXT_LIBRARIES_ATTR 2U /*< region dedicated for LLEXT libraries */
21+
#define VIRTUAL_REGION_SHARED_HEAP_ATTR 1U /*< region for shared virtual heap */
22+
#define VIRTUAL_REGION_LLEXT_LIBRARIES_ATTR 2U /*< region for LLEXT libraries */
23+
#define VIRTUAL_REGION_VPAGES_ATTR 3U /*< region for virtual page allocator */
2324

2425
/* Dependency on ipc/topology.h created due to memory capability definitions
2526
* that are defined there

zephyr/include/sof/lib/vpages.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
// Copyright(c) 2025 Intel Corporation.
3+
4+
/* Virtual Page Allocator API */
5+
#ifndef __SOF_LIB_VPAGE_H__
6+
#define __SOF_LIB_VPAGE_H__
7+
8+
#include <zephyr/kernel.h>
9+
#include <stdint.h>
10+
11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif
14+
15+
/**
16+
* @brief Allocate virtual pages
17+
* Allocates a specified number of contiguous virtual memory pages by mapping
18+
* physical pages.
19+
*
20+
* @param[in] pages Number of 4kB pages to allocate.
21+
*
22+
* @return Pointer to the allocated virtual memory region, or NULL on failure.
23+
*/
24+
void *vpage_alloc(unsigned int pages);
25+
26+
/**
27+
* @brief Free virtual pages
28+
* Frees previously allocated virtual memory pages and unmaps them.
29+
*
30+
* @param[in] ptr Pointer to the memory pages to free.
31+
*/
32+
void vpage_free(void *ptr);
33+
34+
#ifdef __cplusplus
35+
}
36+
#endif
37+
38+
#endif /* __SOF_LIB_VPAGE_H__ */

0 commit comments

Comments
 (0)