소스 검색

bsp: k230: use configuration instead of immediate value

The original code used immediate values directly when it
came to memory layout, which was not a good programming
habit.

Now we use macros and try to be compatible with SDK
configuration values in the SDK compilation environment.

The main changes are to clean up board.h, and extract
some memory layout constants to define a new header
file mem_layout.h.

Also update KERNEL_VADDR_START to the default value for
Smart, so we can use it as base to calculate other mapping
address.

Signed-off-by: Chen Wang <unicorn_wang@outlook.com>
Chen Wang 3 주 전
부모
커밋
0c0b9e59d0
6개의 변경된 파일109개의 추가작업 그리고 33개의 파일을 삭제
  1. 1 1
      bsp/k230/.config
  2. 17 3
      bsp/k230/board/board.c
  3. 11 22
      bsp/k230/board/board.h
  4. 76 0
      bsp/k230/board/mem_layout.h
  5. 3 6
      bsp/k230/link.lds
  6. 1 1
      bsp/k230/rtconfig.h

+ 1 - 1
bsp/k230/.config

@@ -201,7 +201,7 @@ CONFIG_RT_BACKTRACE_LEVEL_MAX_NR=32
 CONFIG_ARCH_CPU_64BIT=y
 CONFIG_RT_USING_CACHE=y
 CONFIG_ARCH_MM_MMU=y
-CONFIG_KERNEL_VADDR_START=0xffffffc000020000
+CONFIG_KERNEL_VADDR_START=0xffffffc000000000
 CONFIG_ARCH_RISCV=y
 CONFIG_ARCH_RISCV_FPU=y
 CONFIG_ARCH_RISCV_FPU_D=y

+ 17 - 3
bsp/k230/board/board.c

@@ -22,16 +22,30 @@
 #ifdef RT_USING_SMART
 #include <mmu.h>
 #include "page.h"
+#endif
+
+extern unsigned int __sram_size;
+extern unsigned int __sram_base;
+extern unsigned int __sram_end;
+#define RAM_END (rt_size_t)((void *)&__sram_end)
+
+extern unsigned int __bss_start;
+extern unsigned int __bss_end;
 
-/* respect to boot loader, must be 0xFFFFFFC000200000 */
-RT_STATIC_ASSERT(kmem_region, KERNEL_VADDR_START == 0xffffffc000020000);
+#define RT_HW_HEAP_BEGIN    ((void *)&__bss_end)
+#define RT_HW_HEAP_END ((void *)(((rt_size_t)RT_HW_HEAP_BEGIN) + CONFIG_MEM_RTSMART_HEAP_SIZE))
+
+#define RT_HW_PAGE_START    ((void *)((rt_size_t)RT_HW_HEAP_END + sizeof(rt_size_t)))
+#define RT_HW_PAGE_END      ((void *)(RAM_END))
+
+#ifdef RT_USING_SMART
 
 rt_region_t init_page_region = {(rt_size_t)RT_HW_PAGE_START, (rt_size_t)RT_HW_PAGE_END};
 
 extern size_t MMUTable[];
 
 struct mem_desc platform_mem_desc[] = {
-    {KERNEL_VADDR_START, (rt_size_t)0xFFFFFFC020000000 - 1, (rt_size_t)ARCH_MAP_FAILED, NORMAL_MEM},
+    {KERNEL_VADDR_START, (rt_size_t)(KERNEL_VADDR_START + CONFIG_MEM_MMZ_BASE + CONFIG_MEM_MMZ_SIZE - 1), (rt_size_t)ARCH_MAP_FAILED, NORMAL_MEM},
 };
 
 #define NUM_MEM_DESC (sizeof(platform_mem_desc) / sizeof(platform_mem_desc[0]))

+ 11 - 22
bsp/k230/board/board.h

@@ -11,28 +11,14 @@
 #ifndef BOARD_H__
 #define BOARD_H__
 
-#include <rtconfig.h>
+#include "rtconfig.h"
+#include "mem_layout.h"
 
-extern unsigned int __sram_size;
-extern unsigned int __sram_base;
-extern unsigned int __sram_end;
-#define RAM_END (rt_size_t)((void *)&__sram_end)
-
-extern unsigned int __bss_start;
-extern unsigned int __bss_end;
-
-#define RT_HW_HEAP_BEGIN    ((void *)&__bss_end)
-#define RT_HW_HEAP_END ((void *)(((rt_size_t)RT_HW_HEAP_BEGIN) + 0x2000000 ))
-
-#define RT_HW_PAGE_START    ((void *)((rt_size_t)RT_HW_HEAP_END + sizeof(rt_size_t)))
-#define RT_HW_PAGE_END      ((void *)(RAM_END))
-
-void rt_hw_board_init(void);
-void rt_init_user_mem(struct rt_thread *thread, const char *name, unsigned long *entry);
-
-#define TIMER_CLK_FREQ  (27000000)
-
-/* From K230 Technical Reference Manual, chapter 1.5 Address Space mapping */
+/*
+ * K230 Memory Map
+ *
+ * See K230 Technical Reference Manual, chapter 1.5 Address Space mapping
+ */
 #define SRAM_BASE_ADDR              (0x80200000UL)
 #define SRAM_IO_SIZE                (0x00200000UL)
 
@@ -200,4 +186,7 @@ void rt_init_user_mem(struct rt_thread *thread, const char *name, unsigned long
 #define SPI_XIP_FLASH_IO_SIZE       (0x08000000UL)
 
 #define IO_SPACE_BASE_ADDR          (KPU_BASE_ADDR)
-#endif
+
+#define TIMER_CLK_FREQ  (27000000)
+
+#endif // BOARD_H__

+ 76 - 0
bsp/k230/board/mem_layout.h

@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2006-2021, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+#ifndef MEMORY_LAYOUT_H__
+#define MEMORY_LAYOUT_H__
+
+#include "../rtconfig.h"
+
+/*
+ * Physical Memory layout:
+ *
+ * +---------+ <- CONFIG_MEM_TOTAL_SIZE
+ * | ......  | maybe zero
+ * +---------+ <- CONFIG_MEM_MMZ_BASE + CONFIG_MEM_MMZ_SIZE
+ * |         |
+ * |         |
+ * |         |
+ * |         |
+ * |         |
+ * |         |
+ * |         |
+ * |         |
+ * |---------| <- CONFIG_MEM_MMZ_BASE
+ * | ......  | maybe zero
+ * +---------+ <- CONFIG_MEM_RTSMART_SIZE
+ * | guard   | MEM_GUARD_SIZE
+ * +---------+ <- End of Kerenl
+ * |         |
+ * |         |
+ * +---------+
+ * | heap    | CONFIG_MEM_RTSMART_HEAP_SIZE
+ * +---------+
+ * |         |
+ * +---------+ <- Beginning of Kernel <- mapping to KERNEL_VADDR_START + MEM_OPENSBI_SIZE
+ * | opensbi | MEM_OPENSBI_SIZE
+ * +---------+ <- Beginning of Physical Memory (0)  <- mapping to KERNEL_VADDR_START
+ */
+
+#define MEM_OPENSBI_SIZE	0x20000	// 128K
+#define MEM_GUARD_SIZE		0x1000	// 4K
+
+/*
+ * The value of CONFIG_XXX may come from rtconfig.h. This is mainly for
+ * compatibility with compiling RT-Thread in the SDK environment and using
+ * the SDK configuration.
+ *
+ * If CONFIG_XXX is defined, it means that the value comes from the SDK
+ * configuration, otherwise the default configuration of bsp/k230 in RT-Thead
+ * is used. The default configuration of bsp/k230 is for the 01Studio CanMV
+ * development board that supports 512MB of memory.
+ */
+#ifndef CONFIG_MEM_TOTAL_SIZE
+#define CONFIG_MEM_TOTAL_SIZE	0x20000000 // 512M
+#endif
+
+#ifndef CONFIG_MEM_RTSMART_SIZE
+#define CONFIG_MEM_RTSMART_SIZE	0x10000000 // 256M
+#endif
+
+#ifndef CONFIG_MEM_RTSMART_HEAP_SIZE
+#define CONFIG_MEM_RTSMART_HEAP_SIZE	0x2000000 // 32M
+#endif
+
+#ifndef CONFIG_MEM_MMZ_BASE
+#define CONFIG_MEM_MMZ_BASE	0x10000000 // 512M
+#endif
+
+#ifndef CONFIG_MEM_MMZ_SIZE
+#define CONFIG_MEM_MMZ_SIZE	0x10000000 // 256M
+#endif
+
+#define MEM_KERNEL_SIZE (CONFIG_MEM_RTSMART_SIZE - MEM_OPENSBI_SIZE - MEM_GUARD_SIZE)
+
+#endif // MEMORY_LAYOUT_H__

+ 3 - 6
bsp/k230/link.lds

@@ -8,21 +8,18 @@
  * 2020/12/12     bernard      The first version
  */
 
-#include "rtconfig.h"
+#include "board/mem_layout.h"
 
 OUTPUT_ARCH( "riscv" )
 
 /*
  * Memory layout:
- * 2M ==> +128K: Bootloader(sbi)
- * 2M+128K ==> +sizeof(rtthread.bin): Kernel
- * ~ ==> 32M: Heap
- * 32M - 66M: Page
+ * See mem_layout.h
  */
 
 MEMORY
 {
-   SRAM : ORIGIN = KERNEL_VADDR_START, LENGTH = 262012K
+   SRAM : ORIGIN = KERNEL_VADDR_START + MEM_OPENSBI_SIZE, LENGTH = MEM_KERNEL_SIZE
 }
 
 ENTRY(_start)

+ 1 - 1
bsp/k230/rtconfig.h

@@ -127,7 +127,7 @@
 #define ARCH_CPU_64BIT
 #define RT_USING_CACHE
 #define ARCH_MM_MMU
-#define KERNEL_VADDR_START 0xffffffc000020000
+#define KERNEL_VADDR_START 0xffffffc000000000
 #define ARCH_RISCV
 #define ARCH_RISCV_FPU
 #define ARCH_RISCV_FPU_D