Skip to content

Commit 51d3c7a

Browse files
mm: add external control to virtual memory regions
this commit removes creation of virtual memory regions from Zephyr, allowing the application to create required regions It is up the application to use virtual memory as needed, zephyr however is keeping the table and ensures no memory addresses overlaps Signed-off-by: Marcin Szkudlinski <[email protected]>
1 parent 7c0e4ae commit 51d3c7a

File tree

5 files changed

+49
-54
lines changed

5 files changed

+49
-54
lines changed

drivers/mm/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ config MM_DRV_INTEL_ADSP_TLB
4444
Driver for the translation lookup buffer on
4545
Intel Audio DSP hardware.
4646

47+
config MM_DRV_INTEL_VIRTUAL_REGION_COUNT
48+
int "Maximum number of regions defined in virtual memory"
49+
depends on MM_DRV_INTEL_ADSP_MTL_TLB
50+
default 1
51+
help
52+
This options defines a table size keeping all virtual memory region
53+
4754
config EXTERNAL_ADDRESS_TRANSLATION
4855
bool "Support for external address translation modules"
4956
depends on !MMU

drivers/mm/mm_drv_intel_adsp.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,4 @@ static inline uintptr_t tlb_entry_to_pa(uint16_t tlb_entry)
7979
CONFIG_MM_DRV_PAGE_SIZE) + TLB_PHYS_BASE);
8080
}
8181

82-
/**
83-
* Calculate virtual memory regions allocation based on
84-
* info from linker script.
85-
*
86-
* @param End address of staticaly allocated memory.
87-
* @return Error Code.
88-
*/
89-
int calculate_memory_regions(uintptr_t static_alloc_end_ptr);
90-
9182
#endif /* ZEPHYR_DRIVERS_SYSTEM_MM_DRV_INTEL_MTL_ */

drivers/mm/mm_drv_intel_adsp_mtl_tlb.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -738,10 +738,6 @@ static int sys_mm_drv_mm_init(const struct device *dev)
738738

739739
L2_PHYS_SRAM_REGION.info.num_blocks = avalible_memory_size / CONFIG_MM_DRV_PAGE_SIZE;
740740

741-
ret = calculate_memory_regions(UNUSED_L2_START_ALIGNED);
742-
CHECKIF(ret != 0) {
743-
return ret;
744-
}
745741
/*
746742
* Initialize memblocks that will store physical
747743
* page usage. Initially all physical pages are

drivers/mm/mm_drv_intel_adsp_regions.c

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,50 @@
1212

1313
#include "mm_drv_intel_adsp.h"
1414

15+
16+
/* virtual memory regions table, last item is an end marker */
1517
struct sys_mm_drv_region
16-
virtual_memory_regions[CONFIG_MP_MAX_NUM_CPUS + VIRTUAL_REGION_COUNT] = { {0} };
18+
virtual_memory_regions[CONFIG_MM_DRV_INTEL_VIRTUAL_REGION_COUNT+1] = { {0} };
1719

1820
const struct sys_mm_drv_region *sys_mm_drv_query_memory_regions(void)
1921
{
2022
return (const struct sys_mm_drv_region *) virtual_memory_regions;
2123
}
2224

23-
static inline void append_region(void *address, uint32_t mem_size,
24-
uint32_t attributes, uint32_t position, uint32_t *total_size)
25+
int adsp_add_virtual_memory_region(uintptr_t region_address, uint32_t region_size, uint32_t attr)
2526
{
26-
virtual_memory_regions[position].addr = address;
27-
virtual_memory_regions[position].size = mem_size;
28-
virtual_memory_regions[position].attr = attributes;
29-
total_size += mem_size;
30-
}
27+
struct sys_mm_drv_region *region;
28+
uint32_t pos = 0;
29+
uintptr_t new_region_end = region_address + region_size;
3130

32-
int calculate_memory_regions(uintptr_t static_alloc_end_ptr)
33-
{
34-
int i, total_size = 0;
31+
/* check if the region fits to virtual memory */
32+
if (region_address < L2_VIRTUAL_SRAM_BASE ||
33+
new_region_end > L2_VIRTUAL_SRAM_BASE + L2_VIRTUAL_SRAM_SIZE) {
34+
return -EINVAL;
35+
}
36+
37+
/* find an empty slot, verify if the region is not overlapping */
38+
SYS_MM_DRV_MEMORY_REGION_FOREACH(virtual_memory_regions, region) {
39+
uintptr_t region_start = (uintptr_t)region->addr;
40+
uintptr_t region_end = region_start + region->size;
3541

36-
for (i = 0; i < CONFIG_MP_MAX_NUM_CPUS; i++) {
37-
append_region((void *)(static_alloc_end_ptr + i * CORE_HEAP_SIZE),
38-
CORE_HEAP_SIZE, MEM_REG_ATTR_CORE_HEAP, i, &total_size);
42+
/* check region overlapping */
43+
if (region_address < region_end && new_region_end > region_start) {
44+
return -EINVAL;
45+
}
46+
pos++;
3947
}
4048

41-
append_region((void *)((uintptr_t)virtual_memory_regions[i - 1].addr +
42-
virtual_memory_regions[i - 1].size),
43-
CORE_HEAP_SIZE, MEM_REG_ATTR_SHARED_HEAP, i, &total_size);
44-
i++;
45-
append_region((void *)((uintptr_t)virtual_memory_regions[i - 1].addr +
46-
virtual_memory_regions[i - 1].size),
47-
OPPORTUNISTIC_REGION_SIZE, MEM_REG_ATTR_OPPORTUNISTIC_MEMORY, i, &total_size);
48-
i++;
49-
/* Apending last region as 0 so iterators know where table is over
50-
* check is for size = 0;
51-
*/
52-
append_region(NULL, 0, 0, i, &total_size);
53-
54-
if (total_size > L2_VIRTUAL_SRAM_SIZE) {
55-
return -EINVAL;
49+
/* SYS_MM_DRV_MEMORY_REGION_FOREACH exits when an empty slot is found */
50+
if (pos == CONFIG_MM_DRV_INTEL_VIRTUAL_REGION_COUNT) {
51+
/* no more free slots */
52+
return -ENOMEM;
5653
}
5754

55+
/* add new region */
56+
virtual_memory_regions[pos].addr = (void *)region_address;
57+
virtual_memory_regions[pos].size = region_size;
58+
virtual_memory_regions[pos].attr = attr;
59+
5860
return 0;
5961
}

soc/intel/intel_adsp/ace/include/adsp_memory_regions.h

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,21 @@
66
#ifndef ZEPHYR_SOC_INTEL_ADSP_MEMORY_REGIONS_H_
77
#define ZEPHYR_SOC_INTEL_ADSP_MEMORY_REGIONS_H_
88

9-
/* Define amount of regions other than core heaps that virtual memory will be split to
10-
* currently includes shared heap and oma region and one regions set to 0 as for table
11-
* iterator end value.
9+
/**
10+
* Add a virtual memory region to the table
11+
*
12+
* @param virtual_address an address of a new region
13+
* @param region_size a size of a new region
14+
* @param attr an attribute of a new region, may not be unique
15+
*
16+
* @return 0 success
17+
* @return -ENOMEM if there are no empty slots for virtual memory regions
18+
* @return -EINVAL if virtual memory region overlaps with existing ones or exceeds memory size
1219
*/
13-
#define VIRTUAL_REGION_COUNT 3
14-
15-
#define CORE_HEAP_SIZE 0x100000
16-
#define SHARED_HEAP_SIZE 0x100000
17-
#define OPPORTUNISTIC_REGION_SIZE 0x100000
20+
int adsp_add_virtual_memory_region(uintptr_t region_address, uint32_t region_size, uint32_t attr);
1821

1922
/* size of TLB table */
2023
#define TLB_SIZE DT_REG_SIZE_BY_IDX(DT_INST(0, intel_adsp_mtl_tlb), 0)
2124

22-
/* Attribiutes for memory regions */
23-
#define MEM_REG_ATTR_CORE_HEAP 1U
24-
#define MEM_REG_ATTR_SHARED_HEAP 2U
25-
#define MEM_REG_ATTR_OPPORTUNISTIC_MEMORY 4U
2625

2726
#endif /* ZEPHYR_SOC_INTEL_ADSP_MEMORY_REGIONS_H_ */

0 commit comments

Comments
 (0)