Skip to content

Commit b14dc8f

Browse files
lupyuenpkarashchenko
authored andcommitted
risc-v/mmu: Configure T-Head MMU to cache User Text, Data and Heap
This PR configures the T-Head MMU to cache the the User Text, Data and Heap. We enable the MMU Flags for Shareable, Bufferable and Cacheable, as explained in this article: https://lupyuen.github.io/articles/plic3#appendix-mmu-caching-for-t-head-c906 This PR fixes the Slow Memory Access for NuttX Apps on BL808 and SG2000 SoCs: apache#12696. With this fix, SG2000 NuttX CoreMark jumps from 21 to 2,423. (Close to SG2000 Debian CoreMark) We introduce a Kconfig Option: `ARCH_MMU_EXT_THEAD` ("System Type > Enable T-Head MMU extension support"). Enabling this Kconfig Option will configure the T-Head MMU to cache the User Text, Data and Heap. This PR enables the MMU cache for only SG2000 SoC (Milk-V Duo S SBC). The next PR will apply the same settings to BL808 SoC (Pine64 Ox64 SBC). Modified Files: `arch/risc-v/Kconfig`: Added Kconfig Option `ARCH_MMU_EXT_THEAD` that will configure the T-Head MMU. Enabled `ARCH_MMU_EXT_THEAD` for SG2000 SoC. `arch/risc-v/src/common/riscv_mmu.h`: Set the T-Head MMU Flags (Shareable, Bufferable and Cacheable) for User Text, Data and Heap, if `ARCH_MMU_EXT_THEAD` is enabled `arch/risc-v/src/common/riscv_addrenv.c`: Extended the MMU Flags from 32 bits to 64 bits, to accommodate the T-Head MMU Flags `arch/risc-v/src/common/riscv_exception.c`: Extended the MMU Flags from 32 bits to 64 bit, to accommodate the T-Head MMU Flags. This code is enabled only for MMU Paging (`CONFIG_PAGING`).
1 parent 2d7c47c commit b14dc8f

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

arch/risc-v/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ config ARCH_CHIP_SG2000
336336
select ARCH_HAVE_MULTICPU
337337
select ARCH_HAVE_MPU
338338
select ARCH_MMU_TYPE_SV39
339+
select ARCH_MMU_EXT_THEAD
339340
select ARCH_HAVE_ADDRENV
340341
select ARCH_NEED_ADDRENV_MAPPING
341342
select ARCH_HAVE_S_MODE
@@ -516,6 +517,13 @@ config ARCH_MMU_TYPE_SV32
516517
default n
517518
select ARCH_HAVE_MMU
518519

520+
config ARCH_MMU_EXT_THEAD
521+
bool "Enable T-Head MMU extension support"
522+
default n
523+
depends on ARCH_HAVE_MMU
524+
---help---
525+
Enable support for T-Head MMU extension.
526+
519527
config ARCH_HAVE_S_MODE
520528
bool
521529
default n

arch/risc-v/src/common/riscv_addrenv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ static int copy_kernel_mappings(arch_addrenv_t *addrenv)
231231
****************************************************************************/
232232

233233
static int create_region(arch_addrenv_t *addrenv, uintptr_t vaddr,
234-
size_t size, uint32_t mmuflags)
234+
size_t size, uint64_t mmuflags)
235235
{
236236
uintptr_t ptlast;
237237
uintptr_t ptprev;

arch/risc-v/src/common/riscv_exception.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ int riscv_fillpage(int mcause, void *regs, void *args)
177177
uintptr_t vaddr;
178178
uint32_t ptlevel;
179179
uintptr_t satp;
180-
uint32_t mmuflags;
180+
uint64_t mmuflags;
181181

182182
_info("EXCEPTION: %s. MCAUSE: %" PRIxREG ", EPC: %" PRIxREG
183183
", MTVAL: %" PRIxREG "\n",

arch/risc-v/src/common/riscv_mmu.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@
4646
#define PTE_A (1 << 6) /* Page has been accessed */
4747
#define PTE_D (1 << 7) /* Page is dirty */
4848

49+
/* T-Head MMU needs Text and Data to be Shareable, Bufferable, Cacheable */
50+
51+
#ifdef CONFIG_ARCH_MMU_EXT_THEAD
52+
# define PTE_SEC (1UL << 59) /* Security */
53+
# define PTE_SHARE (1UL << 60) /* Shareable */
54+
# define PTE_BUF (1UL << 61) /* Bufferable */
55+
# define PTE_CACHE (1UL << 62) /* Cacheable */
56+
# define PTE_SO (1UL << 63) /* Strong Order */
57+
58+
# define EXT_UTEXT_FLAGS (PTE_SHARE | PTE_BUF | PTE_CACHE)
59+
# define EXT_UDATA_FLAGS (PTE_SHARE | PTE_BUF | PTE_CACHE)
60+
#else
61+
# define EXT_UTEXT_FLAGS (0)
62+
# define EXT_UDATA_FLAGS (0)
63+
#endif
64+
4965
/* Check if leaf PTE entry or not (if X/W/R are set it is) */
5066

5167
#define PTE_LEAF_MASK (7 << 1)
@@ -56,8 +72,8 @@
5672

5773
/* Flags for user FLASH (RX) and user RAM (RW) */
5874

59-
#define MMU_UTEXT_FLAGS (PTE_R | PTE_X | PTE_U)
60-
#define MMU_UDATA_FLAGS (PTE_R | PTE_W | PTE_U)
75+
#define MMU_UTEXT_FLAGS (PTE_R | PTE_X | PTE_U | EXT_UTEXT_FLAGS)
76+
#define MMU_UDATA_FLAGS (PTE_R | PTE_W | PTE_U | EXT_UDATA_FLAGS)
6177

6278
/* I/O region flags */
6379

0 commit comments

Comments
 (0)