kinsamanka / meta-milkv

Milk-V Duo overlay for OpenEmbedded/Yocto
Other
17 stars 3 forks source link

How can I increase my RAM memory? #1

Closed Markus250 closed 3 months ago

Markus250 commented 6 months ago

Hello! Thanks for your work on adding a layer! I have Milk-V Duo 64MB, in Linux I see only ~22MB (free and even less). I find posts like this and this, , but that's all for buildroot (i am using yocto). How can I use your layer to increase the memory for the Linux kernel to the maximum (well, or somehow divide it differently, in one project I use the RTOS kernel, in another only the camera, and in others I do not use either the RTOS or the camera, but only the network interface, I need all the memory). I hope I expressed my thoughts correctly, thanks in advance!

kinsamanka commented 6 months ago

You can control the RAM size by modifying the DTB used by the kernel.

You have two options:

  1. Edit the DTS files and create a bbappend recipe containing the patch of the changes made.
  2. Use the existing DTB from your working setup:

    • extract the DTB from a running system
      $ cp /sys/firmware/fdt new.dtb
    • copy new.dtb to the boot partition of the new image
      root@milkv-duo:~# ls -l /boot
      -rwxr-xr-x    1 root     root         19329 Apr  5  2011 default.dtb
      -rwxr-xr-x    1 root     root        635904 Apr  5  2011 fip.bin
      -rwxr-xr-x    1 root     root         20480 Jan  1  1980 new.dtb
      -rwxr-xr-x    1 root     root           460 Jan  1  1980 uEnv.txt
      -rwxr-xr-x    1 root     root       4000128 Apr  5  2011 uImage.fit
    • edit uEnv.txt to use new.dtb
      
      @@ -4,7 +4,7 @@
      root=root=/dev/mmcblk0p2
      setbootargs=setenv bootargs ${root} console=${consoledev},${baudrate} ${optargs}

    -fdt=default.dtb +fdt=new.dtb kernel=uImage.fit

    loadaddr=0x81400000

BTW, this is the default image produced by this layer:

root@milkv-duo:~# uname -a
Linux milkv-duo 5.10.4-yocto-standard-milkv-duo #1 PREEMPT Fri Oct 6 23:47:33 UTC 2023 riscv64 GNU/Linux
root@milkv-duo:~# free -h
              total        used        free      shared  buff/cache   available
Mem:          46576        8668       32884           4        5024       34476
Swap:             0           0           0
kinsamanka commented 6 months ago

You can edit these lines to adjust the RAM allocation.

The other way of increasing free RAM is to remove the modules that are not used.

For example, replace the PREFERRED_PROVIDER_virtual/kernel with milkv-duo-v5.10.y here:

root@milkv-duo:~# uname -a
Linux milkv-duo 5.10.215 #1 PREEMPT Sat Apr 13 10:59:59 UTC 2024 riscv64 GNU/Linux
root@milkv-duo:~# free
              total        used        free      shared  buff/cache   available
Mem:          55820        5400       46944           0        3476       47268
Swap:             0           0           0
root@milkv-duo:~# dmesg | grep Memory
[    0.000000] Memory: 55716K/64768K available (3662K kernel code, 442K rwdata, 1277K rodata, 104K init, 195K bss, 9052K reserved, 0K cma-reserved)
Markus250 commented 3 months ago

Thanks! It's work! I implemented it like this, I wrote my script in python3 which creates file cvi_board_memmap.h, and i just create patch for linux kernel! In my-meta/recipes-kernel/linux/linux-milkv-duo-dev.bbappend i put my patch "file://0001-memmap.patch", and it works! I’m sharing the script, you never know who will need it:


SIZE_1M = 0x100000
SIZE_1K = 1024

DRAM_BASE = 0x80000000
DRAM_SIZE = 64 * SIZE_1M

# ==============
# C906L FreeRTOS
# ==============
FREERTOS_SIZE = 0 * SIZE_1K
# FreeRTOS is at the end of DRAM
FREERTOS_ADDR = DRAM_BASE + DRAM_SIZE - FREERTOS_SIZE
FSBL_C906L_START_ADDR = FREERTOS_ADDR

# ==============================
# OpenSBI | arm-trusted-firmware
# ==============================
# Monitor is at the begining of DRAM
MONITOR_ADDR = DRAM_BASE

ATF_SIZE = 512 * SIZE_1K
OPENSBI_SIZE = 512 * SIZE_1K
OPENSBI_FDT_ADDR = MONITOR_ADDR + OPENSBI_SIZE

# =========================
# memory@DRAM_BASE in .dts.
# =========================
# Ignore the area of FreeRTOS in u-boot and kernel
KERNEL_MEMORY_ADDR = DRAM_BASE
KERNEL_MEMORY_SIZE = DRAM_SIZE - FREERTOS_SIZE

# =================
# Multimedia buffer. Used by u-boot/kernel/FreeRTOS
# =================
ION_SIZE = 0 * SIZE_1M
H26X_BITSTREAM_SIZE = 0 * SIZE_1M
H26X_ENC_BUFF_SIZE = 0
ISP_MEM_BASE_SIZE = 0 * SIZE_1M
BOOTLOGO_SIZE = 0 * SIZE_1M
FREERTOS_RESERVED_ION_SIZE = H26X_BITSTREAM_SIZE + H26X_ENC_BUFF_SIZE + ISP_MEM_BASE_SIZE + BOOTLOGO_SIZE

# ION after FreeRTOS
ION_ADDR = FREERTOS_ADDR - ION_SIZE

# Buffers of the fast image are inside the ION buffer
H26X_BITSTREAM_ADDR = ION_ADDR
H26X_ENC_BUFF_ADDR = H26X_BITSTREAM_ADDR + H26X_BITSTREAM_SIZE
ISP_MEM_BASE_ADDR = H26X_ENC_BUFF_ADDR + H26X_ENC_BUFF_SIZE

# Boot logo is after ISP buffer and inside the ION buffer
BOOTLOGO_ADDR = ISP_MEM_BASE_ADDR + ISP_MEM_BASE_SIZE

assert BOOTLOGO_ADDR + BOOTLOGO_SIZE <= ION_ADDR + ION_SIZE

# ===================
# FSBL and u-boot-2021
# ===================
CVI_UPDATE_HEADER_SIZE = SIZE_1K
UIMAG_SIZE = 15 * SIZE_1M

# kernel image loading buffer
UIMAG_ADDR = DRAM_BASE + 20 * SIZE_1M
CVI_UPDATE_HEADER_ADDR = UIMAG_ADDR - CVI_UPDATE_HEADER_SIZE

# FSBL decompress buffer
FSBL_UNZIP_ADDR = UIMAG_ADDR
FSBL_UNZIP_SIZE = UIMAG_SIZE

assert UIMAG_ADDR + UIMAG_SIZE <= ION_ADDR

# u-boot's run address and entry point
CONFIG_SYS_TEXT_BASE = DRAM_BASE + 2 * SIZE_1M
# u-boot's init stack point is only used before board_init_f()
CONFIG_SYS_INIT_SP_ADDR = UIMAG_ADDR + UIMAG_SIZE

def to_hex(val):
    value = hex(int(val))
    return value

with open("cvi_board_memmap.h", "w") as f:
    f.write("#ifndef __BOARD_MMAP__b126ca63__\n")
    f.write("#define __BOARD_MMAP__b126ca63__\n\n")

    f.write(f"#define CONFIG_SYS_TEXT_BASE {to_hex(CONFIG_SYS_TEXT_BASE)}\n")
    f.write(f"#define CVIMMAP_ATF_SIZE {to_hex(ATF_SIZE)}\n")
    f.write(f"#define CVIMMAP_BOOTLOGO_ADDR {to_hex(BOOTLOGO_ADDR)}\n")
    f.write(f"#define CVIMMAP_BOOTLOGO_SIZE {to_hex(BOOTLOGO_SIZE)}\n")
    f.write(f"#define CVIMMAP_CONFIG_SYS_INIT_SP_ADDR {to_hex(CONFIG_SYS_INIT_SP_ADDR)}\n")
    f.write(f"#define CVIMMAP_CVI_UPDATE_HEADER_ADDR {to_hex(CVI_UPDATE_HEADER_ADDR)}\n")
    f.write(f"#define CVIMMAP_CVI_UPDATE_HEADER_SIZE {to_hex(CVI_UPDATE_HEADER_SIZE)}\n")
    f.write(f"#define CVIMMAP_DRAM_BASE {to_hex(DRAM_BASE)}\n")
    f.write(f"#define CVIMMAP_DRAM_SIZE {to_hex(DRAM_SIZE)}\n")
    f.write(f"#define CVIMMAP_FREERTOS_ADDR {to_hex(FREERTOS_ADDR)}\n")
    f.write(f"#define CVIMMAP_FREERTOS_RESERVED_ION_SIZE {to_hex(FREERTOS_RESERVED_ION_SIZE)}\n")
    f.write(f"#define CVIMMAP_FREERTOS_SIZE {to_hex(FREERTOS_SIZE)}\n")
    f.write(f"#define CVIMMAP_FSBL_C906L_START_ADDR {to_hex(FSBL_C906L_START_ADDR)}\n")
    f.write(f"#define CVIMMAP_FSBL_UNZIP_ADDR {to_hex(FSBL_UNZIP_ADDR)}\n")
    f.write(f"#define CVIMMAP_FSBL_UNZIP_SIZE {to_hex(FSBL_UNZIP_SIZE)}\n")
    f.write(f"#define CVIMMAP_H26X_BITSTREAM_ADDR {to_hex(H26X_BITSTREAM_ADDR)}\n")
    f.write(f"#define CVIMMAP_H26X_BITSTREAM_SIZE {to_hex(H26X_BITSTREAM_SIZE)}\n")
    f.write(f"#define CVIMMAP_H26X_ENC_BUFF_ADDR {to_hex(H26X_ENC_BUFF_ADDR)}\n")
    f.write(f"#define CVIMMAP_H26X_ENC_BUFF_SIZE {to_hex(H26X_ENC_BUFF_SIZE)}\n")
    f.write(f"#define CVIMMAP_ION_ADDR {to_hex(ION_ADDR)}\n")
    f.write(f"#define CVIMMAP_ION_SIZE {to_hex(ION_SIZE)}\n")
    f.write(f"#define CVIMMAP_ISP_MEM_BASE_ADDR {to_hex(ISP_MEM_BASE_ADDR)}\n")
    f.write(f"#define CVIMMAP_ISP_MEM_BASE_SIZE {to_hex(ISP_MEM_BASE_SIZE)}\n")
    f.write(f"#define CVIMMAP_KERNEL_MEMORY_ADDR {to_hex(KERNEL_MEMORY_ADDR)}\n")
    f.write(f"#define CVIMMAP_KERNEL_MEMORY_SIZE {to_hex(KERNEL_MEMORY_SIZE)}\n")
    f.write(f"#define CVIMMAP_MONITOR_ADDR {to_hex(MONITOR_ADDR)}\n")
    f.write(f"#define CVIMMAP_OPENSBI_FDT_ADDR {to_hex(OPENSBI_FDT_ADDR)}\n")
    f.write(f"#define CVIMMAP_OPENSBI_SIZE {to_hex(OPENSBI_SIZE)}\n")
    f.write(f"#define CVIMMAP_UIMAG_ADDR {to_hex(UIMAG_ADDR)}\n")
    f.write(f"#define CVIMMAP_UIMAG_SIZE {to_hex(UIMAG_SIZE)}\n\n")

    f.write("#endif /* __BOARD_MMAP__b126ca63__ */\n")
`