2018-08-23 09:52:41 +08:00
|
|
|
/*
|
2023-04-22 23:59:11 +08:00
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
2018-08-23 09:52:41 +08:00
|
|
|
*
|
2020-01-10 10:38:21 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2018-08-23 09:52:41 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
2022-01-07 13:49:06 +08:00
|
|
|
* Date Author Notes
|
2022-12-20 17:49:37 +08:00
|
|
|
* 2012-01-10 bernard porting to AM1808
|
2023-04-22 23:59:11 +08:00
|
|
|
* 2021-11-28 GuEe-GUI first version
|
|
|
|
* 2022-12-10 WangXiaoyao porting to MM
|
2018-08-23 09:52:41 +08:00
|
|
|
*/
|
2023-12-22 15:44:45 +08:00
|
|
|
|
|
|
|
#define DBG_TAG "hw.mmu"
|
|
|
|
#define DBG_LVL DBG_LOG
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
#include <rthw.h>
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2020-02-20 15:42:10 +08:00
|
|
|
|
2023-12-16 18:08:11 +08:00
|
|
|
#define __MMU_INTERNAL
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
#include "mm_aspace.h"
|
|
|
|
#include "mm_page.h"
|
2022-12-20 17:49:37 +08:00
|
|
|
#include "mmu.h"
|
2023-01-09 10:08:55 +08:00
|
|
|
#include "tlb.h"
|
2022-12-20 17:49:37 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
#include "ioremap.h"
|
2023-12-16 18:08:11 +08:00
|
|
|
#ifdef RT_USING_SMART
|
2022-12-20 17:49:37 +08:00
|
|
|
#include <lwp_mm.h>
|
|
|
|
#endif
|
|
|
|
|
2023-12-22 15:44:45 +08:00
|
|
|
#define TCR_CONFIG_TBI0 rt_hw_mmu_config_tbi(0)
|
|
|
|
#define TCR_CONFIG_TBI1 rt_hw_mmu_config_tbi(1)
|
2022-12-20 17:49:37 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
#define MMU_LEVEL_MASK 0x1ffUL
|
|
|
|
#define MMU_LEVEL_SHIFT 9
|
|
|
|
#define MMU_ADDRESS_BITS 39
|
|
|
|
#define MMU_ADDRESS_MASK 0x0000fffffffff000UL
|
|
|
|
#define MMU_ATTRIB_MASK 0xfff0000000000ffcUL
|
|
|
|
|
|
|
|
#define MMU_TYPE_MASK 3UL
|
|
|
|
#define MMU_TYPE_USED 1UL
|
|
|
|
#define MMU_TYPE_BLOCK 1UL
|
|
|
|
#define MMU_TYPE_TABLE 3UL
|
|
|
|
#define MMU_TYPE_PAGE 3UL
|
2022-12-20 17:49:37 +08:00
|
|
|
|
|
|
|
#define MMU_TBL_BLOCK_2M_LEVEL 2
|
|
|
|
#define MMU_TBL_PAGE_4k_LEVEL 3
|
|
|
|
#define MMU_TBL_LEVEL_NR 4
|
|
|
|
|
2023-12-16 18:08:11 +08:00
|
|
|
#ifndef KERNEL_VADDR_START
|
|
|
|
#define KERNEL_VADDR_START ARCH_TEXT_OFFSET
|
|
|
|
#endif
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
volatile unsigned long MMUTable[512] __attribute__((aligned(4 * 1024)));
|
2018-08-23 09:52:41 +08:00
|
|
|
|
2022-12-20 17:49:37 +08:00
|
|
|
struct mmu_level_info
|
2018-08-23 09:52:41 +08:00
|
|
|
{
|
2022-12-20 17:49:37 +08:00
|
|
|
unsigned long *pos;
|
|
|
|
void *page;
|
|
|
|
};
|
2018-08-23 09:52:41 +08:00
|
|
|
|
2022-12-20 17:49:37 +08:00
|
|
|
static void _kenrel_unmap_4K(unsigned long *lv0_tbl, void *v_addr)
|
|
|
|
{
|
|
|
|
int level;
|
|
|
|
unsigned long va = (unsigned long)v_addr;
|
|
|
|
unsigned long *cur_lv_tbl = lv0_tbl;
|
|
|
|
unsigned long page;
|
|
|
|
unsigned long off;
|
|
|
|
struct mmu_level_info level_info[4];
|
|
|
|
int ref;
|
|
|
|
int level_shift = MMU_ADDRESS_BITS;
|
|
|
|
unsigned long *pos;
|
|
|
|
|
|
|
|
rt_memset(level_info, 0, sizeof level_info);
|
|
|
|
for (level = 0; level < MMU_TBL_LEVEL_NR; level++)
|
|
|
|
{
|
|
|
|
off = (va >> level_shift);
|
|
|
|
off &= MMU_LEVEL_MASK;
|
|
|
|
page = cur_lv_tbl[off];
|
|
|
|
if (!(page & MMU_TYPE_USED))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((page & MMU_TYPE_MASK) == MMU_TYPE_BLOCK)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2023-04-22 23:59:11 +08:00
|
|
|
/* next table entry in current level */
|
2022-12-20 17:49:37 +08:00
|
|
|
level_info[level].pos = cur_lv_tbl + off;
|
|
|
|
cur_lv_tbl = (unsigned long *)(page & MMU_ADDRESS_MASK);
|
|
|
|
cur_lv_tbl = (unsigned long *)((unsigned long)cur_lv_tbl - PV_OFFSET);
|
|
|
|
level_info[level].page = cur_lv_tbl;
|
|
|
|
level_shift -= MMU_LEVEL_SHIFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
level = MMU_TBL_PAGE_4k_LEVEL;
|
|
|
|
pos = level_info[level].pos;
|
|
|
|
if (pos)
|
2022-01-07 13:49:06 +08:00
|
|
|
{
|
2022-12-20 17:49:37 +08:00
|
|
|
*pos = (unsigned long)RT_NULL;
|
|
|
|
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, pos, sizeof(void *));
|
2022-01-07 13:49:06 +08:00
|
|
|
}
|
2022-12-20 17:49:37 +08:00
|
|
|
level--;
|
|
|
|
|
|
|
|
while (level >= 0)
|
2022-01-07 13:49:06 +08:00
|
|
|
{
|
2022-12-20 17:49:37 +08:00
|
|
|
pos = level_info[level].pos;
|
|
|
|
if (pos)
|
|
|
|
{
|
|
|
|
void *cur_page = level_info[level].page;
|
|
|
|
ref = rt_page_ref_get(cur_page, 0);
|
|
|
|
if (ref == 1)
|
|
|
|
{
|
|
|
|
*pos = (unsigned long)RT_NULL;
|
|
|
|
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, pos, sizeof(void *));
|
|
|
|
}
|
|
|
|
rt_pages_free(cur_page, 0);
|
|
|
|
}
|
2023-02-25 19:14:02 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2022-12-20 17:49:37 +08:00
|
|
|
level--;
|
2022-01-07 13:49:06 +08:00
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
|
2022-12-20 17:49:37 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-22 23:59:11 +08:00
|
|
|
static int _kernel_map_4K(unsigned long *lv0_tbl, void *vaddr, void *paddr, unsigned long attr)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
int level;
|
|
|
|
unsigned long *cur_lv_tbl = lv0_tbl;
|
|
|
|
unsigned long page;
|
|
|
|
unsigned long off;
|
2023-01-09 10:08:55 +08:00
|
|
|
intptr_t va = (intptr_t)vaddr;
|
|
|
|
intptr_t pa = (intptr_t)paddr;
|
2022-12-20 17:49:37 +08:00
|
|
|
int level_shift = MMU_ADDRESS_BITS;
|
|
|
|
|
|
|
|
if (va & ARCH_PAGE_MASK)
|
2018-08-23 09:52:41 +08:00
|
|
|
{
|
2022-12-20 17:49:37 +08:00
|
|
|
return MMU_MAP_ERROR_VANOTALIGN;
|
2020-02-20 15:42:10 +08:00
|
|
|
}
|
2022-12-20 17:49:37 +08:00
|
|
|
if (pa & ARCH_PAGE_MASK)
|
2020-02-20 15:42:10 +08:00
|
|
|
{
|
2022-12-20 17:49:37 +08:00
|
|
|
return MMU_MAP_ERROR_PANOTALIGN;
|
2020-02-20 15:42:10 +08:00
|
|
|
}
|
2022-12-20 17:49:37 +08:00
|
|
|
for (level = 0; level < MMU_TBL_PAGE_4k_LEVEL; level++)
|
|
|
|
{
|
|
|
|
off = (va >> level_shift);
|
|
|
|
off &= MMU_LEVEL_MASK;
|
|
|
|
if (!(cur_lv_tbl[off] & MMU_TYPE_USED))
|
|
|
|
{
|
2023-04-22 23:59:11 +08:00
|
|
|
page = (unsigned long)rt_pages_alloc_ext(0, PAGE_ANY_AVAILABLE);
|
2022-12-20 17:49:37 +08:00
|
|
|
if (!page)
|
|
|
|
{
|
|
|
|
ret = MMU_MAP_ERROR_NOPAGE;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
rt_memset((void *)page, 0, ARCH_PAGE_SIZE);
|
2023-02-20 13:48:00 +08:00
|
|
|
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, (void *)page, ARCH_PAGE_SIZE);
|
2022-12-20 17:49:37 +08:00
|
|
|
cur_lv_tbl[off] = (page + PV_OFFSET) | MMU_TYPE_TABLE;
|
2023-02-20 13:48:00 +08:00
|
|
|
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, cur_lv_tbl + off, sizeof(void *));
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
page = cur_lv_tbl[off];
|
|
|
|
page &= MMU_ADDRESS_MASK;
|
|
|
|
/* page to va */
|
|
|
|
page -= PV_OFFSET;
|
|
|
|
rt_page_ref_inc((void *)page, 0);
|
|
|
|
}
|
|
|
|
page = cur_lv_tbl[off];
|
|
|
|
if ((page & MMU_TYPE_MASK) == MMU_TYPE_BLOCK)
|
|
|
|
{
|
|
|
|
/* is block! error! */
|
|
|
|
ret = MMU_MAP_ERROR_CONFLICT;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
cur_lv_tbl = (unsigned long *)(page & MMU_ADDRESS_MASK);
|
|
|
|
cur_lv_tbl = (unsigned long *)((unsigned long)cur_lv_tbl - PV_OFFSET);
|
|
|
|
level_shift -= MMU_LEVEL_SHIFT;
|
|
|
|
}
|
|
|
|
/* now is level page */
|
|
|
|
attr &= MMU_ATTRIB_MASK;
|
|
|
|
pa |= (attr | MMU_TYPE_PAGE); /* page */
|
|
|
|
off = (va >> ARCH_PAGE_SHIFT);
|
|
|
|
off &= MMU_LEVEL_MASK;
|
|
|
|
cur_lv_tbl[off] = pa; /* page */
|
|
|
|
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, cur_lv_tbl + off, sizeof(void *));
|
|
|
|
return ret;
|
|
|
|
err:
|
|
|
|
_kenrel_unmap_4K(lv0_tbl, (void *)va);
|
|
|
|
return ret;
|
|
|
|
}
|
2022-01-07 13:49:06 +08:00
|
|
|
|
2023-04-22 23:59:11 +08:00
|
|
|
static int _kernel_map_2M(unsigned long *lv0_tbl, void *vaddr, void *paddr, unsigned long attr)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
int level;
|
|
|
|
unsigned long *cur_lv_tbl = lv0_tbl;
|
|
|
|
unsigned long page;
|
|
|
|
unsigned long off;
|
|
|
|
unsigned long va = (unsigned long)vaddr;
|
|
|
|
unsigned long pa = (unsigned long)paddr;
|
|
|
|
|
|
|
|
int level_shift = MMU_ADDRESS_BITS;
|
|
|
|
|
|
|
|
if (va & ARCH_SECTION_MASK)
|
|
|
|
{
|
|
|
|
return MMU_MAP_ERROR_VANOTALIGN;
|
|
|
|
}
|
2023-10-17 13:07:59 +08:00
|
|
|
if (pa & ARCH_PAGE_MASK)
|
2023-04-22 23:59:11 +08:00
|
|
|
{
|
|
|
|
return MMU_MAP_ERROR_PANOTALIGN;
|
|
|
|
}
|
|
|
|
for (level = 0; level < MMU_TBL_BLOCK_2M_LEVEL; level++)
|
|
|
|
{
|
|
|
|
off = (va >> level_shift);
|
|
|
|
off &= MMU_LEVEL_MASK;
|
|
|
|
if (!(cur_lv_tbl[off] & MMU_TYPE_USED))
|
|
|
|
{
|
|
|
|
page = (unsigned long)rt_pages_alloc_ext(0, PAGE_ANY_AVAILABLE);
|
|
|
|
if (!page)
|
|
|
|
{
|
|
|
|
ret = MMU_MAP_ERROR_NOPAGE;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
rt_memset((char *)page, 0, ARCH_PAGE_SIZE);
|
|
|
|
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, (void *)page, ARCH_PAGE_SIZE);
|
|
|
|
cur_lv_tbl[off] = (page + PV_OFFSET) | MMU_TYPE_TABLE;
|
|
|
|
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, cur_lv_tbl + off, sizeof(void *));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
page = cur_lv_tbl[off];
|
|
|
|
page &= MMU_ADDRESS_MASK;
|
|
|
|
/* page to va */
|
|
|
|
page -= PV_OFFSET;
|
|
|
|
rt_page_ref_inc((void *)page, 0);
|
|
|
|
}
|
|
|
|
page = cur_lv_tbl[off];
|
|
|
|
if ((page & MMU_TYPE_MASK) == MMU_TYPE_BLOCK)
|
|
|
|
{
|
|
|
|
/* is block! error! */
|
|
|
|
ret = MMU_MAP_ERROR_CONFLICT;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
cur_lv_tbl = (unsigned long *)(page & MMU_ADDRESS_MASK);
|
|
|
|
cur_lv_tbl = (unsigned long *)((unsigned long)cur_lv_tbl - PV_OFFSET);
|
|
|
|
level_shift -= MMU_LEVEL_SHIFT;
|
|
|
|
}
|
|
|
|
/* now is level page */
|
|
|
|
attr &= MMU_ATTRIB_MASK;
|
|
|
|
pa |= (attr | MMU_TYPE_BLOCK); /* block */
|
|
|
|
off = (va >> ARCH_SECTION_SHIFT);
|
|
|
|
off &= MMU_LEVEL_MASK;
|
|
|
|
cur_lv_tbl[off] = pa;
|
|
|
|
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, cur_lv_tbl + off, sizeof(void *));
|
|
|
|
return ret;
|
|
|
|
err:
|
|
|
|
_kenrel_unmap_4K(lv0_tbl, (void *)va);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
void *rt_hw_mmu_map(rt_aspace_t aspace, void *v_addr, void *p_addr, size_t size,
|
|
|
|
size_t attr)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
int ret = -1;
|
2022-01-07 13:49:06 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
void *unmap_va = v_addr;
|
2023-04-22 23:59:11 +08:00
|
|
|
size_t npages;
|
|
|
|
size_t stride;
|
|
|
|
int (*mapper)(unsigned long *lv0_tbl, void *vaddr, void *paddr, unsigned long attr);
|
|
|
|
|
|
|
|
if (((rt_ubase_t)v_addr & ARCH_SECTION_MASK) || (size & ARCH_SECTION_MASK))
|
|
|
|
{
|
|
|
|
/* legacy 4k mapping */
|
|
|
|
npages = size >> ARCH_PAGE_SHIFT;
|
|
|
|
stride = ARCH_PAGE_SIZE;
|
|
|
|
mapper = _kernel_map_4K;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* 2m huge page */
|
|
|
|
npages = size >> ARCH_SECTION_SHIFT;
|
|
|
|
stride = ARCH_SECTION_SIZE;
|
|
|
|
mapper = _kernel_map_2M;
|
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
|
|
|
|
while (npages--)
|
2020-02-20 15:42:10 +08:00
|
|
|
{
|
2023-02-20 13:48:00 +08:00
|
|
|
MM_PGTBL_LOCK(aspace);
|
2023-04-22 23:59:11 +08:00
|
|
|
ret = mapper(aspace->page_table, v_addr, p_addr, attr);
|
2023-02-20 13:48:00 +08:00
|
|
|
MM_PGTBL_UNLOCK(aspace);
|
|
|
|
|
2020-02-20 15:42:10 +08:00
|
|
|
if (ret != 0)
|
2018-08-23 09:52:41 +08:00
|
|
|
{
|
2023-03-03 11:51:21 +08:00
|
|
|
/* other types of return value are taken as programming error */
|
|
|
|
RT_ASSERT(ret == MMU_MAP_ERROR_NOPAGE);
|
2023-01-09 10:08:55 +08:00
|
|
|
/* error, undo map */
|
|
|
|
while (unmap_va != v_addr)
|
|
|
|
{
|
2023-02-20 13:48:00 +08:00
|
|
|
MM_PGTBL_LOCK(aspace);
|
2023-01-09 10:08:55 +08:00
|
|
|
_kenrel_unmap_4K(aspace->page_table, (void *)unmap_va);
|
2023-02-20 13:48:00 +08:00
|
|
|
MM_PGTBL_UNLOCK(aspace);
|
2023-04-22 23:59:11 +08:00
|
|
|
unmap_va = (char *)unmap_va + stride;
|
2023-01-09 10:08:55 +08:00
|
|
|
}
|
|
|
|
break;
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
2023-04-22 23:59:11 +08:00
|
|
|
v_addr = (char *)v_addr + stride;
|
|
|
|
p_addr = (char *)p_addr + stride;
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
return unmap_va;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
void rt_hw_mmu_unmap(rt_aspace_t aspace, void *v_addr, size_t size)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
// caller guarantee that v_addr & size are page aligned
|
|
|
|
size_t npages = size >> ARCH_PAGE_SHIFT;
|
2022-12-20 17:49:37 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
if (!aspace->page_table)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-12-20 17:49:37 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
while (npages--)
|
|
|
|
{
|
2023-02-20 13:48:00 +08:00
|
|
|
MM_PGTBL_LOCK(aspace);
|
2023-08-16 15:38:59 +08:00
|
|
|
if (rt_hw_mmu_v2p(aspace, v_addr) != ARCH_MAP_FAILED)
|
|
|
|
_kenrel_unmap_4K(aspace->page_table, v_addr);
|
2023-02-20 13:48:00 +08:00
|
|
|
MM_PGTBL_UNLOCK(aspace);
|
2023-04-22 23:59:11 +08:00
|
|
|
v_addr = (char *)v_addr + ARCH_PAGE_SIZE;
|
2023-01-09 10:08:55 +08:00
|
|
|
}
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
void rt_hw_aspace_switch(rt_aspace_t aspace)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
if (aspace != &rt_kernel_space)
|
|
|
|
{
|
|
|
|
void *pgtbl = aspace->page_table;
|
2023-02-20 13:48:00 +08:00
|
|
|
pgtbl = rt_kmem_v2p(pgtbl);
|
2023-04-22 23:59:11 +08:00
|
|
|
rt_ubase_t tcr;
|
2023-01-09 10:08:55 +08:00
|
|
|
|
|
|
|
__asm__ volatile("msr ttbr0_el1, %0" ::"r"(pgtbl) : "memory");
|
|
|
|
|
|
|
|
__asm__ volatile("mrs %0, tcr_el1" : "=r"(tcr));
|
|
|
|
tcr &= ~(1ul << 7);
|
|
|
|
__asm__ volatile("msr tcr_el1, %0\n"
|
|
|
|
"isb" ::"r"(tcr)
|
|
|
|
: "memory");
|
|
|
|
|
|
|
|
rt_hw_tlb_invalidate_all_local();
|
|
|
|
}
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
void rt_hw_mmu_ktbl_set(unsigned long tbl)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
#ifdef RT_USING_SMART
|
|
|
|
tbl += PV_OFFSET;
|
|
|
|
__asm__ volatile("msr TTBR1_EL1, %0\n dsb sy\nisb" ::"r"(tbl) : "memory");
|
|
|
|
#else
|
|
|
|
__asm__ volatile("msr TTBR0_EL1, %0\n dsb sy\nisb" ::"r"(tbl) : "memory");
|
|
|
|
#endif
|
|
|
|
__asm__ volatile("tlbi vmalle1\n dsb sy\nisb" ::: "memory");
|
|
|
|
__asm__ volatile("ic ialluis\n dsb sy\nisb" ::: "memory");
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
/**
|
|
|
|
* @brief setup Page Table for kernel space. It's a fixed map
|
|
|
|
* and all mappings cannot be changed after initialization.
|
|
|
|
*
|
|
|
|
* Memory region in struct mem_desc must be page aligned,
|
|
|
|
* otherwise is a failure and no report will be
|
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
* @param mmu_info
|
|
|
|
* @param mdesc
|
|
|
|
* @param desc_nr
|
|
|
|
*/
|
|
|
|
void rt_hw_mmu_setup(rt_aspace_t aspace, struct mem_desc *mdesc, int desc_nr)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
void *err;
|
|
|
|
for (size_t i = 0; i < desc_nr; i++)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
size_t attr;
|
|
|
|
switch (mdesc->attr)
|
|
|
|
{
|
|
|
|
case NORMAL_MEM:
|
|
|
|
attr = MMU_MAP_K_RWCB;
|
|
|
|
break;
|
|
|
|
case NORMAL_NOCACHE_MEM:
|
|
|
|
attr = MMU_MAP_K_RWCB;
|
|
|
|
break;
|
|
|
|
case DEVICE_MEM:
|
|
|
|
attr = MMU_MAP_K_DEVICE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
attr = MMU_MAP_K_DEVICE;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct rt_mm_va_hint hint = {.flags = MMF_MAP_FIXED,
|
|
|
|
.limit_start = aspace->start,
|
|
|
|
.limit_range_size = aspace->size,
|
|
|
|
.map_size = mdesc->vaddr_end -
|
|
|
|
mdesc->vaddr_start + 1,
|
|
|
|
.prefer = (void *)mdesc->vaddr_start};
|
|
|
|
|
2023-02-14 23:08:32 +08:00
|
|
|
if (mdesc->paddr_start == (rt_size_t)ARCH_MAP_FAILED)
|
|
|
|
mdesc->paddr_start = mdesc->vaddr_start + PV_OFFSET;
|
2023-02-20 13:48:00 +08:00
|
|
|
int retval;
|
|
|
|
retval = rt_aspace_map_phy_static(aspace, &mdesc->varea, &hint, attr,
|
2023-01-09 10:08:55 +08:00
|
|
|
mdesc->paddr_start >> MM_PAGE_SHIFT, &err);
|
2023-02-20 13:48:00 +08:00
|
|
|
if (retval)
|
|
|
|
{
|
|
|
|
LOG_E("%s: map failed with code %d", retval);
|
|
|
|
RT_ASSERT(0);
|
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
mdesc++;
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
|
|
|
|
rt_hw_mmu_ktbl_set((unsigned long)rt_kernel_space.page_table);
|
|
|
|
rt_page_cleanup();
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
|
|
|
|
2023-04-22 23:59:11 +08:00
|
|
|
static void _init_region(void *vaddr, size_t size)
|
2023-01-09 10:08:55 +08:00
|
|
|
{
|
|
|
|
rt_ioremap_start = vaddr;
|
|
|
|
rt_ioremap_size = size;
|
2023-04-22 23:59:11 +08:00
|
|
|
rt_mpr_start = (char *)rt_ioremap_start - rt_mpr_size;
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will initialize rt_mmu_info structure.
|
|
|
|
*
|
|
|
|
* @param mmu_info rt_mmu_info structure
|
|
|
|
* @param v_address virtual address
|
|
|
|
* @param size map size
|
|
|
|
* @param vtable mmu table
|
|
|
|
* @param pv_off pv offset in kernel space
|
|
|
|
*
|
|
|
|
* @return 0 on successful and -1 for fail
|
|
|
|
*/
|
2023-01-09 10:08:55 +08:00
|
|
|
int rt_hw_mmu_map_init(rt_aspace_t aspace, void *v_address, size_t size,
|
|
|
|
size_t *vtable, size_t pv_off)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
|
|
|
size_t va_s, va_e;
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
if (!aspace || !vtable)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_s = (size_t)v_address;
|
|
|
|
va_e = (size_t)v_address + size - 1;
|
|
|
|
|
|
|
|
if (va_e < va_s)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_s >>= ARCH_SECTION_SHIFT;
|
|
|
|
va_e >>= ARCH_SECTION_SHIFT;
|
|
|
|
|
|
|
|
if (va_s == 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
rt_aspace_init(aspace, (void *)KERNEL_VADDR_START, 0 - KERNEL_VADDR_START,
|
|
|
|
vtable);
|
|
|
|
|
|
|
|
_init_region(v_address, size);
|
2022-12-20 17:49:37 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-12-22 15:44:45 +08:00
|
|
|
rt_weak long rt_hw_mmu_config_tbi(int tbi_index)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
/************ setting el1 mmu register**************
|
|
|
|
MAIR_EL1
|
|
|
|
index 0 : memory outer writeback, write/read alloc
|
|
|
|
index 1 : memory nocache
|
|
|
|
index 2 : device nGnRnE
|
|
|
|
*****************************************************/
|
|
|
|
void mmu_tcr_init(void)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
unsigned long val64;
|
2023-12-16 18:08:11 +08:00
|
|
|
unsigned long pa_range;
|
2023-01-09 10:08:55 +08:00
|
|
|
|
|
|
|
val64 = 0x00447fUL;
|
|
|
|
__asm__ volatile("msr MAIR_EL1, %0\n dsb sy\n" ::"r"(val64));
|
|
|
|
|
2023-12-16 18:08:11 +08:00
|
|
|
__asm__ volatile ("mrs %0, ID_AA64MMFR0_EL1":"=r"(val64));
|
|
|
|
pa_range = val64 & 0xf; /* PARange */
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
/* TCR_EL1 */
|
2023-12-22 15:44:45 +08:00
|
|
|
val64 = (16UL << 0) /* t0sz 48bit */
|
|
|
|
| (0x0UL << 6) /* reserved */
|
|
|
|
| (0x0UL << 7) /* epd0 */
|
|
|
|
| (0x3UL << 8) /* t0 wb cacheable */
|
|
|
|
| (0x3UL << 10) /* inner shareable */
|
|
|
|
| (0x2UL << 12) /* t0 outer shareable */
|
|
|
|
| (0x0UL << 14) /* t0 4K */
|
|
|
|
| (16UL << 16) /* t1sz 48bit */
|
|
|
|
| (0x0UL << 22) /* define asid use ttbr0.asid */
|
|
|
|
| (0x0UL << 23) /* epd1 */
|
|
|
|
| (0x3UL << 24) /* t1 inner wb cacheable */
|
|
|
|
| (0x3UL << 26) /* t1 outer wb cacheable */
|
|
|
|
| (0x2UL << 28) /* t1 outer shareable */
|
|
|
|
| (0x2UL << 30) /* t1 4k */
|
|
|
|
| (pa_range << 32) /* PA range */
|
|
|
|
| (0x0UL << 35) /* reserved */
|
|
|
|
| (0x1UL << 36) /* as: 0:8bit 1:16bit */
|
|
|
|
| (TCR_CONFIG_TBI0 << 37) /* tbi0 */
|
|
|
|
| (TCR_CONFIG_TBI1 << 38); /* tbi1 */
|
2023-01-09 10:08:55 +08:00
|
|
|
__asm__ volatile("msr TCR_EL1, %0\n" ::"r"(val64));
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
struct page_table
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
unsigned long page[512];
|
|
|
|
};
|
2022-12-20 17:49:37 +08:00
|
|
|
|
2023-07-01 22:46:43 +08:00
|
|
|
/* */
|
2023-12-16 18:08:11 +08:00
|
|
|
static struct page_table* __init_page_array;
|
|
|
|
static unsigned long __page_off = 0UL;
|
2023-07-01 22:46:43 +08:00
|
|
|
unsigned long get_ttbrn_base(void)
|
|
|
|
{
|
|
|
|
return (unsigned long) __init_page_array;
|
|
|
|
}
|
|
|
|
|
2023-12-16 18:08:11 +08:00
|
|
|
void set_free_page(void *page_array)
|
|
|
|
{
|
|
|
|
__init_page_array = page_array;
|
|
|
|
}
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
unsigned long get_free_page(void)
|
|
|
|
{
|
2023-12-16 18:08:11 +08:00
|
|
|
return (unsigned long) (__init_page_array[__page_off++].page);
|
2023-01-09 10:08:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _map_single_page_2M(unsigned long *lv0_tbl, unsigned long va,
|
|
|
|
unsigned long pa, unsigned long attr)
|
|
|
|
{
|
|
|
|
int level;
|
|
|
|
unsigned long *cur_lv_tbl = lv0_tbl;
|
|
|
|
unsigned long page;
|
|
|
|
unsigned long off;
|
|
|
|
int level_shift = MMU_ADDRESS_BITS;
|
2022-12-20 17:49:37 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
if (va & ARCH_SECTION_MASK)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
return MMU_MAP_ERROR_VANOTALIGN;
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
2023-10-17 13:07:59 +08:00
|
|
|
if (pa & ARCH_PAGE_MASK)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
return MMU_MAP_ERROR_PANOTALIGN;
|
|
|
|
}
|
|
|
|
for (level = 0; level < MMU_TBL_BLOCK_2M_LEVEL; level++)
|
|
|
|
{
|
|
|
|
off = (va >> level_shift);
|
|
|
|
off &= MMU_LEVEL_MASK;
|
|
|
|
if (!(cur_lv_tbl[off] & MMU_TYPE_USED))
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
page = get_free_page();
|
|
|
|
if (!page)
|
|
|
|
{
|
|
|
|
return MMU_MAP_ERROR_NOPAGE;
|
|
|
|
}
|
|
|
|
rt_memset((char *)page, 0, ARCH_PAGE_SIZE);
|
|
|
|
cur_lv_tbl[off] = page | MMU_TYPE_TABLE;
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
page = cur_lv_tbl[off];
|
|
|
|
if ((page & MMU_TYPE_MASK) == MMU_TYPE_BLOCK)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
/* is block! error! */
|
|
|
|
return MMU_MAP_ERROR_CONFLICT;
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
cur_lv_tbl = (unsigned long *)(page & MMU_ADDRESS_MASK);
|
|
|
|
level_shift -= MMU_LEVEL_SHIFT;
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
attr &= MMU_ATTRIB_MASK;
|
|
|
|
pa |= (attr | MMU_TYPE_BLOCK); /* block */
|
|
|
|
off = (va >> ARCH_SECTION_SHIFT);
|
|
|
|
off &= MMU_LEVEL_MASK;
|
|
|
|
cur_lv_tbl[off] = pa;
|
2022-12-20 17:49:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-02-21 11:45:04 +08:00
|
|
|
void *rt_hw_mmu_tbl_get()
|
|
|
|
{
|
|
|
|
uintptr_t tbl;
|
|
|
|
__asm__ volatile("MRS %0, TTBR0_EL1" : "=r"(tbl));
|
|
|
|
return rt_kmem_p2v((void *)(tbl & ((1ul << 48) - 2)));
|
|
|
|
}
|
|
|
|
|
2023-08-02 12:48:24 +08:00
|
|
|
void *rt_ioremap_early(void *paddr, size_t size)
|
|
|
|
{
|
2024-02-06 10:20:27 +08:00
|
|
|
volatile size_t count;
|
2023-12-16 18:08:11 +08:00
|
|
|
rt_ubase_t base;
|
2023-08-02 12:48:24 +08:00
|
|
|
static void *tbl = RT_NULL;
|
|
|
|
|
|
|
|
if (!size)
|
|
|
|
{
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tbl)
|
|
|
|
{
|
|
|
|
tbl = rt_hw_mmu_tbl_get();
|
|
|
|
}
|
|
|
|
|
|
|
|
count = (size + ARCH_SECTION_MASK) >> ARCH_SECTION_SHIFT;
|
2023-12-16 18:08:11 +08:00
|
|
|
base = (rt_ubase_t)paddr & (~ARCH_SECTION_MASK);
|
2023-08-02 12:48:24 +08:00
|
|
|
|
|
|
|
while (count --> 0)
|
|
|
|
{
|
2023-12-16 18:08:11 +08:00
|
|
|
if (_map_single_page_2M(tbl, base, base, MMU_MAP_K_DEVICE))
|
|
|
|
{
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
base += ARCH_SECTION_SIZE;
|
2023-08-02 12:48:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return paddr;
|
|
|
|
}
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
static int _init_map_2M(unsigned long *lv0_tbl, unsigned long va,
|
|
|
|
unsigned long pa, unsigned long count,
|
|
|
|
unsigned long attr)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
unsigned long i;
|
|
|
|
int ret;
|
2022-12-20 17:49:37 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
if (va & ARCH_SECTION_MASK)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
if (pa & ARCH_SECTION_MASK)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
for (i = 0; i < count; i++)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
ret = _map_single_page_2M(lv0_tbl, va, pa, attr);
|
|
|
|
va += ARCH_SECTION_SIZE;
|
|
|
|
pa += ARCH_SECTION_SIZE;
|
|
|
|
if (ret != 0)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
return ret;
|
2018-08-23 09:52:41 +08:00
|
|
|
}
|
|
|
|
}
|
2022-12-20 17:49:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
static unsigned long *_query(rt_aspace_t aspace, void *vaddr, int *plvl_shf)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
int level;
|
|
|
|
unsigned long va = (unsigned long)vaddr;
|
|
|
|
unsigned long *cur_lv_tbl;
|
|
|
|
unsigned long page;
|
|
|
|
unsigned long off;
|
|
|
|
int level_shift = MMU_ADDRESS_BITS;
|
2022-12-20 17:49:37 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
cur_lv_tbl = aspace->page_table;
|
|
|
|
RT_ASSERT(cur_lv_tbl);
|
2022-12-20 17:49:37 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
for (level = 0; level < MMU_TBL_PAGE_4k_LEVEL; level++)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
off = (va >> level_shift);
|
|
|
|
off &= MMU_LEVEL_MASK;
|
2022-12-20 17:49:37 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
if (!(cur_lv_tbl[off] & MMU_TYPE_USED))
|
|
|
|
{
|
|
|
|
return (void *)0;
|
|
|
|
}
|
2020-02-20 15:42:10 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
page = cur_lv_tbl[off];
|
|
|
|
if ((page & MMU_TYPE_MASK) == MMU_TYPE_BLOCK)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
*plvl_shf = level_shift;
|
|
|
|
return &cur_lv_tbl[off];
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
|
|
|
|
cur_lv_tbl = (unsigned long *)(page & MMU_ADDRESS_MASK);
|
|
|
|
cur_lv_tbl = (unsigned long *)((unsigned long)cur_lv_tbl - PV_OFFSET);
|
|
|
|
level_shift -= MMU_LEVEL_SHIFT;
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
/* now is level MMU_TBL_PAGE_4k_LEVEL */
|
|
|
|
off = (va >> ARCH_PAGE_SHIFT);
|
|
|
|
off &= MMU_LEVEL_MASK;
|
|
|
|
page = cur_lv_tbl[off];
|
2022-12-20 17:49:37 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
if (!(page & MMU_TYPE_USED))
|
|
|
|
{
|
|
|
|
return (void *)0;
|
|
|
|
}
|
|
|
|
*plvl_shf = level_shift;
|
|
|
|
return &cur_lv_tbl[off];
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
void *rt_hw_mmu_v2p(rt_aspace_t aspace, void *v_addr)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
int level_shift;
|
|
|
|
unsigned long paddr;
|
2022-12-20 17:49:37 +08:00
|
|
|
|
2023-04-22 23:59:11 +08:00
|
|
|
if (aspace == &rt_kernel_space)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-04-22 23:59:11 +08:00
|
|
|
paddr = (unsigned long)rt_hw_mmu_kernel_v2p(v_addr);
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-22 23:59:11 +08:00
|
|
|
unsigned long *pte = _query(aspace, v_addr, &level_shift);
|
|
|
|
|
|
|
|
if (pte)
|
|
|
|
{
|
|
|
|
paddr = *pte & MMU_ADDRESS_MASK;
|
|
|
|
paddr |= (rt_ubase_t)v_addr & ((1ul << level_shift) - 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
paddr = (unsigned long)ARCH_MAP_FAILED;
|
|
|
|
}
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
2023-04-22 23:59:11 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
return (void *)paddr;
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
|
|
|
|
2023-04-22 23:59:11 +08:00
|
|
|
static int _noncache(rt_ubase_t *pte)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
int err = 0;
|
2023-04-22 23:59:11 +08:00
|
|
|
const rt_ubase_t idx_shift = 2;
|
|
|
|
const rt_ubase_t idx_mask = 0x7 << idx_shift;
|
|
|
|
rt_ubase_t entry = *pte;
|
2023-01-09 10:08:55 +08:00
|
|
|
if ((entry & idx_mask) == (NORMAL_MEM << idx_shift))
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
*pte = (entry & ~idx_mask) | (NORMAL_NOCACHE_MEM << idx_shift);
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
else
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
// do not support other type to be noncache
|
2023-03-23 13:54:42 +08:00
|
|
|
err = -RT_ENOSYS;
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
return err;
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
|
|
|
|
2023-04-22 23:59:11 +08:00
|
|
|
static int _cache(rt_ubase_t *pte)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
int err = 0;
|
2023-04-22 23:59:11 +08:00
|
|
|
const rt_ubase_t idx_shift = 2;
|
|
|
|
const rt_ubase_t idx_mask = 0x7 << idx_shift;
|
|
|
|
rt_ubase_t entry = *pte;
|
2023-01-09 10:08:55 +08:00
|
|
|
if ((entry & idx_mask) == (NORMAL_NOCACHE_MEM << idx_shift))
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
*pte = (entry & ~idx_mask) | (NORMAL_MEM << idx_shift);
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
// do not support other type to be cache
|
|
|
|
err = -RT_ENOSYS;
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
return err;
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
|
|
|
|
2023-04-22 23:59:11 +08:00
|
|
|
static int (*control_handler[MMU_CNTL_DUMMY_END])(rt_ubase_t *pte) = {
|
2023-01-09 10:08:55 +08:00
|
|
|
[MMU_CNTL_CACHE] = _cache,
|
|
|
|
[MMU_CNTL_NONCACHE] = _noncache,
|
|
|
|
};
|
2018-08-23 09:52:41 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
int rt_hw_mmu_control(struct rt_aspace *aspace, void *vaddr, size_t size,
|
|
|
|
enum rt_mmu_cntl cmd)
|
2018-08-23 09:52:41 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
int level_shift;
|
|
|
|
int err = -RT_EINVAL;
|
2023-04-22 23:59:11 +08:00
|
|
|
rt_ubase_t vstart = (rt_ubase_t)vaddr;
|
|
|
|
rt_ubase_t vend = vstart + size;
|
2022-01-07 13:49:06 +08:00
|
|
|
|
2023-04-22 23:59:11 +08:00
|
|
|
int (*handler)(rt_ubase_t * pte);
|
2023-01-09 10:08:55 +08:00
|
|
|
if (cmd >= 0 && cmd < MMU_CNTL_DUMMY_END)
|
2018-08-23 09:52:41 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
handler = control_handler[cmd];
|
2020-03-17 13:45:13 +08:00
|
|
|
|
2023-04-22 23:59:11 +08:00
|
|
|
while (vstart < vend)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-04-22 23:59:11 +08:00
|
|
|
rt_ubase_t *pte = _query(aspace, (void *)vstart, &level_shift);
|
|
|
|
rt_ubase_t range_end = vstart + (1ul << level_shift);
|
2023-01-09 10:08:55 +08:00
|
|
|
RT_ASSERT(range_end <= vend);
|
|
|
|
|
|
|
|
if (pte)
|
|
|
|
{
|
|
|
|
err = handler(pte);
|
|
|
|
RT_ASSERT(err == RT_EOK);
|
|
|
|
}
|
2023-04-22 23:59:11 +08:00
|
|
|
vstart = range_end;
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
else
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
2023-01-09 10:08:55 +08:00
|
|
|
err = -RT_ENOSYS;
|
2022-12-20 17:49:37 +08:00
|
|
|
}
|
2020-03-17 13:45:13 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
return err;
|
2021-03-27 17:51:56 +08:00
|
|
|
}
|
2022-12-20 17:49:37 +08:00
|
|
|
|
2023-02-14 23:08:32 +08:00
|
|
|
void rt_hw_mem_setup_early(unsigned long *tbl0, unsigned long *tbl1,
|
2023-01-09 10:08:55 +08:00
|
|
|
unsigned long size, unsigned long pv_off)
|
2022-12-20 17:49:37 +08:00
|
|
|
{
|
|
|
|
int ret;
|
2023-06-12 19:49:18 +08:00
|
|
|
unsigned long count = (size + ARCH_SECTION_MASK) >> ARCH_SECTION_SHIFT;
|
|
|
|
unsigned long normal_attr = MMU_MAP_CUSTOM(MMU_AP_KAUN, NORMAL_MEM);
|
2023-02-14 23:08:32 +08:00
|
|
|
|
2023-06-05 14:18:00 +08:00
|
|
|
#ifdef RT_USING_SMART
|
2022-12-20 17:49:37 +08:00
|
|
|
unsigned long va = KERNEL_VADDR_START;
|
2023-06-05 14:18:00 +08:00
|
|
|
#else
|
2023-10-20 13:27:38 +08:00
|
|
|
extern unsigned char _start;
|
|
|
|
unsigned long va = (unsigned long) &_start;
|
2023-06-12 19:49:18 +08:00
|
|
|
va = RT_ALIGN_DOWN(va, 0x200000);
|
2023-06-05 14:18:00 +08:00
|
|
|
#endif
|
|
|
|
|
2023-06-12 19:49:18 +08:00
|
|
|
/* setup pv off */
|
|
|
|
rt_kmem_pvoff_set(pv_off);
|
2022-12-20 17:49:37 +08:00
|
|
|
|
|
|
|
/* clean the first two pages */
|
2023-01-09 10:08:55 +08:00
|
|
|
rt_memset((char *)tbl0, 0, ARCH_PAGE_SIZE);
|
|
|
|
rt_memset((char *)tbl1, 0, ARCH_PAGE_SIZE);
|
2022-12-20 17:49:37 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
ret = _init_map_2M(tbl1, va, va + pv_off, count, normal_attr);
|
2022-12-20 17:49:37 +08:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
while (1);
|
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
ret = _init_map_2M(tbl0, va + pv_off, va + pv_off, count, normal_attr);
|
2022-12-20 17:49:37 +08:00
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
while (1);
|
|
|
|
}
|
|
|
|
}
|
2023-10-17 13:07:59 +08:00
|
|
|
|
|
|
|
void *rt_hw_mmu_pgtbl_create(void)
|
|
|
|
{
|
|
|
|
size_t *mmu_table;
|
|
|
|
mmu_table = (size_t *)rt_pages_alloc_ext(0, PAGE_ANY_AVAILABLE);
|
|
|
|
if (!mmu_table)
|
|
|
|
{
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(mmu_table, 0, ARCH_PAGE_SIZE);
|
|
|
|
rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, mmu_table, ARCH_PAGE_SIZE);
|
|
|
|
return mmu_table;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rt_hw_mmu_pgtbl_delete(void *pgtbl)
|
|
|
|
{
|
|
|
|
rt_pages_free(pgtbl, 0);
|
|
|
|
}
|