Skip to content

Commit e710d36

Browse files
carlocaionenashif
authored andcommitted
aarch64: mmu: Enable CONFIG_MMU
Enable CONFIG_MMU for AArch64 and add the new arch_mem_map() required function. Signed-off-by: Carlo Caione <[email protected]>
1 parent 6a3401d commit e710d36

File tree

5 files changed

+62
-11
lines changed

5 files changed

+62
-11
lines changed

arch/arm/core/aarch64/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ config GEN_IRQ_VECTOR_TABLE
9292
config ARM_MMU
9393
bool "ARM MMU Support"
9494
default y
95+
select MMU
96+
select SRAM_REGION_PERMISSIONS
9597
help
9698
Memory Management Unit support.
9799

@@ -106,6 +108,9 @@ config EXCEPTION_DEBUG
106108

107109
if ARM_MMU
108110

111+
config MMU_PAGE_SIZE
112+
default 0x1000
113+
109114
config MAX_XLAT_TABLES
110115
int "Maximum numbers of translation tables"
111116
default 8

arch/arm/core/aarch64/mmu/arm_mmu.c

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ static void add_map(struct arm_mmu_ptables *ptables, const char *name,
237237
name, virt, phys, size);
238238

239239
/* check minimum alignment requirement for given mmap region */
240-
__ASSERT(((virt & (PAGE_SIZE - 1)) == 0) &&
241-
((size & (PAGE_SIZE - 1)) == 0),
240+
__ASSERT(((virt & (CONFIG_MMU_PAGE_SIZE - 1)) == 0) &&
241+
((size & (CONFIG_MMU_PAGE_SIZE - 1)) == 0),
242242
"address/size are not page aligned\n");
243243

244244
desc = get_region_desc(attrs);
@@ -407,6 +407,9 @@ static int arm_mmu_init(const struct device *arg)
407407
/* Current MMU code supports only EL1 */
408408
__asm__ volatile("mrs %0, CurrentEL" : "=r" (val));
409409

410+
__ASSERT(CONFIG_MMU_PAGE_SIZE == KB(4),
411+
"Only 4K page size is supported\n");
412+
410413
__ASSERT(GET_EL(val) == MODE_EL1,
411414
"Exception level not EL1, MMU not enabled!\n");
412415

@@ -429,3 +432,51 @@ SYS_INIT(arm_mmu_init, PRE_KERNEL_1,
429432
CONFIG_KERNEL_INIT_PRIORITY_DEVICE
430433
#endif
431434
);
435+
436+
int arch_mem_map(void *virt, uintptr_t phys, size_t size, uint32_t flags)
437+
{
438+
struct arm_mmu_ptables *ptables;
439+
uint32_t entry_flags = MT_SECURE | MT_P_RX_U_NA;
440+
441+
/* Always map in the kernel page tables */
442+
ptables = &kernel_ptables;
443+
444+
/* Translate flags argument into HW-recognized entry flags. */
445+
switch (flags & K_MEM_CACHE_MASK) {
446+
/*
447+
* K_MEM_CACHE_NONE => MT_DEVICE_nGnRnE
448+
* (Device memory nGnRnE)
449+
* K_MEM_CACHE_WB => MT_NORMAL
450+
* (Normal memory Outer WB + Inner WB)
451+
* K_MEM_CACHE_WT => MT_NORMAL_WT
452+
* (Normal memory Outer WT + Inner WT)
453+
*/
454+
case K_MEM_CACHE_NONE:
455+
entry_flags |= MT_DEVICE_nGnRnE;
456+
break;
457+
case K_MEM_CACHE_WT:
458+
entry_flags |= MT_NORMAL_WT;
459+
break;
460+
case K_MEM_CACHE_WB:
461+
entry_flags |= MT_NORMAL;
462+
break;
463+
default:
464+
return -ENOTSUP;
465+
}
466+
467+
if ((flags & K_MEM_PERM_RW) != 0U) {
468+
entry_flags |= MT_RW;
469+
}
470+
471+
if ((flags & K_MEM_PERM_EXEC) == 0U) {
472+
entry_flags |= MT_P_EXECUTE_NEVER;
473+
}
474+
475+
if ((flags & K_MEM_PERM_USER) != 0U) {
476+
return -ENOTSUP;
477+
}
478+
479+
add_map(ptables, "generic", phys, (uintptr_t)virt, size, entry_flags);
480+
481+
return 0;
482+
}

arch/arm/core/aarch64/mmu/arm_mmu.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242

4343
/* Only 4K granule is supported */
4444
#define PAGE_SIZE_SHIFT 12U
45-
#define PAGE_SIZE (1U << PAGE_SIZE_SHIFT)
4645

4746
/* 48-bit VA address */
4847
#define VA_SIZE_SHIFT_MAX 48U

include/arch/arm/aarch64/arm_mmu.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
#define MT_DEVICE_GRE 2U
2020
#define MT_NORMAL_NC 3U
2121
#define MT_NORMAL 4U
22+
#define MT_NORMAL_WT 5U
2223

2324
#define MEMORY_ATTRIBUTES ((0x00 << (MT_DEVICE_nGnRnE * 8)) | \
2425
(0x04 << (MT_DEVICE_nGnRE * 8)) | \
2526
(0x0c << (MT_DEVICE_GRE * 8)) | \
2627
(0x44 << (MT_NORMAL_NC * 8)) | \
27-
(0xffUL << (MT_NORMAL * 8)))
28+
(0xffUL << (MT_NORMAL * 8)) | \
29+
(0xbbUL << (MT_NORMAL_WT * 8)))
2830

2931
/* More flags from user's perpective are supported using remaining bits
3032
* of "attrs" field, i.e. attrs[31:3], underlying code will take care

include/arch/arm/aarch64/scripts/linker.ld

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@
3939
#define ROM_ADDR (CONFIG_FLASH_BASE_ADDRESS + CONFIG_FLASH_LOAD_OFFSET)
4040
#endif
4141

42-
/*
43-
* MMU currently supports 4 kB translation granule size,
44-
* so all regions are required to be 4 kB aligned
45-
*/
46-
#define PAGE_SIZE 0x1000
47-
4842
#if CONFIG_FLASH_LOAD_SIZE > 0
4943
#define ROM_SIZE CONFIG_FLASH_LOAD_SIZE
5044
#else
@@ -66,7 +60,7 @@
6660
#endif
6761

6862
#if defined(CONFIG_ARM_MMU)
69-
_region_min_align = PAGE_SIZE;
63+
_region_min_align = CONFIG_MMU_PAGE_SIZE;
7064
#else
7165
/* If building without MMU support, use default 4-byte alignment. */
7266
_region_min_align = 4;

0 commit comments

Comments
 (0)