2022-12-03 12:07:44 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2021-01-30 lizhirui first version
|
2023-10-17 13:07:59 +08:00
|
|
|
* 2023-10-12 Shell Add permission control API
|
2022-12-03 12:07:44 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __MMU_H__
|
|
|
|
#define __MMU_H__
|
|
|
|
|
|
|
|
#include "riscv.h"
|
|
|
|
#include "riscv_mmu.h"
|
2023-01-09 10:08:55 +08:00
|
|
|
#include <mm_aspace.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
/* RAM, Flash, or ROM */
|
|
|
|
#define NORMAL_MEM 0
|
|
|
|
/* normal nocache memory mapping type */
|
|
|
|
#define NORMAL_NOCACHE_MEM 1
|
|
|
|
/* MMIO region */
|
|
|
|
#define DEVICE_MEM 2
|
|
|
|
|
|
|
|
typedef size_t rt_pte_t;
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
struct mem_desc
|
|
|
|
{
|
|
|
|
rt_size_t vaddr_start;
|
|
|
|
rt_size_t vaddr_end;
|
2024-09-02 17:58:56 +08:00
|
|
|
rt_ubase_t paddr_start;
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_size_t attr;
|
2023-01-09 10:08:55 +08:00
|
|
|
struct rt_varea varea;
|
2022-12-03 12:07:44 +08:00
|
|
|
};
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
#define GET_PF_ID(addr) ((addr) >> PAGE_OFFSET_BIT)
|
|
|
|
#define GET_PF_OFFSET(addr) __MASKVALUE(addr, PAGE_OFFSET_MASK)
|
|
|
|
#define GET_L1(addr) __PARTBIT(addr, VPN2_SHIFT, VPN2_BIT)
|
|
|
|
#define GET_L2(addr) __PARTBIT(addr, VPN1_SHIFT, VPN1_BIT)
|
|
|
|
#define GET_L3(addr) __PARTBIT(addr, VPN0_SHIFT, VPN0_BIT)
|
|
|
|
#define GET_PPN(pte) \
|
|
|
|
(__PARTBIT(pte, PTE_PPN_SHIFT, PHYSICAL_ADDRESS_WIDTH_BITS - PTE_PPN_SHIFT))
|
|
|
|
#define GET_PADDR(pte) (GET_PPN(pte) << PAGE_OFFSET_BIT)
|
2024-09-02 17:58:56 +08:00
|
|
|
#define VPN_TO_PPN(vaddr, pv_off) (((rt_uintptr_t)(vaddr)) + (pv_off))
|
|
|
|
#define PPN_TO_VPN(paddr, pv_off) (((rt_uintptr_t)(paddr)) - (pv_off))
|
2023-01-09 10:08:55 +08:00
|
|
|
#define COMBINEVADDR(l1_off, l2_off, l3_off) \
|
|
|
|
(((l1_off) << VPN2_SHIFT) | ((l2_off) << VPN1_SHIFT) | \
|
|
|
|
((l3_off) << VPN0_SHIFT))
|
|
|
|
#define COMBINEPTE(paddr, attr) \
|
|
|
|
((((paddr) >> PAGE_OFFSET_BIT) << PTE_PPN_SHIFT) | (attr))
|
|
|
|
|
|
|
|
#define MMU_MAP_ERROR_VANOTALIGN -1
|
|
|
|
#define MMU_MAP_ERROR_PANOTALIGN -2
|
|
|
|
#define MMU_MAP_ERROR_NOPAGE -3
|
|
|
|
#define MMU_MAP_ERROR_CONFLICT -4
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2024-02-21 11:45:04 +08:00
|
|
|
void *rt_hw_mmu_tbl_get(void);
|
2024-09-02 17:58:56 +08:00
|
|
|
int rt_hw_mmu_map_init(rt_aspace_t aspace, void *v_address, rt_ubase_t size,
|
|
|
|
rt_ubase_t *vtable, rt_ubase_t pv_off);
|
2023-01-09 10:08:55 +08:00
|
|
|
void rt_hw_mmu_setup(rt_aspace_t aspace, struct mem_desc *mdesc, int desc_nr);
|
2024-09-02 17:58:56 +08:00
|
|
|
void rt_hw_mmu_kernel_map_init(rt_aspace_t aspace, rt_ubase_t vaddr_start,
|
|
|
|
rt_ubase_t size);
|
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);
|
|
|
|
void rt_hw_mmu_unmap(rt_aspace_t aspace, void *v_addr, size_t size);
|
|
|
|
void rt_hw_aspace_switch(rt_aspace_t aspace);
|
|
|
|
void *rt_hw_mmu_v2p(rt_aspace_t aspace, void *vaddr);
|
|
|
|
|
|
|
|
int rt_hw_mmu_control(struct rt_aspace *aspace, void *vaddr, size_t size,
|
|
|
|
enum rt_mmu_cntl cmd);
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-10-17 13:07:59 +08:00
|
|
|
void *rt_hw_mmu_pgtbl_create(void);
|
|
|
|
void rt_hw_mmu_pgtbl_delete(void *pgtbl);
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
#endif
|