RDK S 系列设备树内存分配调整指南

:waving_hand: 使用说明前言

大家好,为了帮助大家更高效地使用 RDK S 系列开发板,我们针对近期的 出厂系统内存资源不足问题,提供了一套可灵活配置的内存分配调整方案。
该方案支持多种内存策略选择,可根据应用场景进行快速切换(如 BPU 优先、CPU 优先或平衡模式)。

:open_book: 具体的使用方法、支持的模式及注意事项请参考本文下方的说明内容执行。

使用方式

sudo ./update_ion_dtb.sh <mode>

支持的模式:

模式名 描述
bpu_first 以 BPU 为优先分配内存,适用于 AI 算法推理场景
cpu_first 以 CPU 为优先分配内存,适用于传统计算负载场景
balanced 平衡 BPU 与 CPU 的内存分配,适合通用场景
default 恢复出厂默认内存分配(恢复为 .bak 备份)

示例:

sudo ./update_ion_dtb.sh bpu_first

:file_folder: 文件说明

  • /boot/hobot/rdk-*.dtb:系统当前使用的设备树文件。
  • /boot/hobot/rdk-*.dtb.bak:执行脚本首次时自动生成的备份文件。
  • /sys/class/boardinfo/adc_boardid:用于识别当前硬件平台型号。

:gear: 脚本功能点

  • 自动识别平台型号(如 s100、s100p 等)和硬件版本(如 v1-20)
  • 根据输入模式设定不同的内存分配策略(大小与地址)
  • 自动备份原始 .dtb 文件,支持恢复操作
  • 自动编译与替换设备树,实时生效(需重启设备生效)
  • 自动安装依赖(如 device-tree-compiler

运行要求

  • 系统平台为基于 RDK-S100 / S100P 系列的 Linux 发行版。
  • 需要拥有 /boot/hobot/ 目录下的设备树读写权限。
  • 建议使用 root 权限运行。

:warning: 注意事项

  • 请确保系统已正常启动,且 /boot/hobot/ 目录下的设备树文件存在。
  • 该脚本将修改设备启动过程中关键的设备树配置,执行前请谨慎评估风险。
  • 建议在操作前备份整个启动分区,以便出现问题时可快速恢复。
  • 修改后的设备树在系统重启后才能生效,请确保修改符合预期。
  • 如需恢复默认内存配置,可使用 default 参数自动还原 .bak 备份文件。
  • 本脚本主要面向开发与测试阶段使用,若操作不当导致启动失败,可能需要重新烧录系统,请务必谨慎执行。

:counterclockwise_arrows_button: 恢复默认配置

如需恢复出厂内存配置:

sudo ./update_ion_dtb.sh default

:telephone_receiver: 技术交流

如果需要进一步的沟通,请直接在帖子上留言即可,地瓜团队会持续为大家解决问题。

update_ion_dtb.sh 脚本内容

#!/bin/bash

help_msg()
{
    echo "Usage: $0 <bpu_first | cpu_first | balanced | default>"
    echo "Example: $0 bpu_first"
    exit 1
}

if [ $# -ne 1 ]; then
    help_msg
fi

target=$1

# Check if dtc command exists
if ! command -v dtc &> /dev/null; then
    apt update
    apt install device-tree-compiler -y
fi

function update_tmp_dts()
{
    COMPATIBLE="$1"
    NEW_REG="$2"
    tmp_dts_f=$3
    echo "Setting $COMPATIBLE's reg to <$NEW_REG>"
    # TODO: The following implementation assumes all ion nodes is properly
    #       layed out as reg directly following compatible format, use awk
    #       in the future to properly handle dts node.
    sed -i "/compatible = \"$COMPATIBLE\";/{n;d}" "${tmp_dts_f}" || {
        echo "Delete Original reg failed!"
        exit 1
    }

    sed -i "/compatible = \"$COMPATIBLE\";/a reg = <$NEW_REG>;" "${tmp_dts_f}" || {
        echo "Add"
        exit 1
    }
}

function get_dtb()
{
    boardid_sys_path="/sys/class/boardinfo/adc_boardid"
    if [ -f "$boardid_sys_path" ]; then
        boardid="$(cat $boardid_sys_path)"

        # get chip
        case $boardid in
            *"64"*) ;&
            *"65"*)
                chip_str="s100p"
            ;;
            *"6A"*) ;&
            *"6B"*)
                chip_str="s100"
            ;;
        esac
        case $boardid in
            *"60")
                hw_str="v1-21"
            ;;
            *"70")
                hw_str="v1-20"
            ;;
            *"84")
                hw_str="v0p5"
            ;;
            *"85")
                hw_str="v0p6"
            ;;
            *"86")
                hw_str="v1p0"
            ;;
        esac
        echo "rdk-${chip_str}-${hw_str}.dtb"
    else
        echo "Invalid"
    fi
}

# Create tmp dir
TMP_DIR=$(mktemp -d)
trap "rm -rf $TMP_DIR" EXIT

dtb_name=$(get_dtb)

if [ "$dtb_name" = "Invalid" ];then
    echo "ERROR: Cannot find valid dtb for current board!"
    exit 1
fi
INPUT_DTB="/boot/hobot/${dtb_name}"
ORIG_INPUT_DTB="${INPUT_DTB}.bak"
OUTPUT_DTB="${INPUT_DTB}"

case $target in
    "balanced")
        if [[ "${INPUT_DTB}" = *"-s100p-"* ]];then
            # ion-pool 7680MiB
            ion_pool_reg_val="0x4 0x00000000 0x1 0xe0000000"
            # ion-carveout 128MiB
            ion_carveout_reg_val="0x8 0x00000000 0x0 0x08000000"
            # ion-cam 128MiB
            ion_cma_reg_val="0xc 0x80000000 0x0 0x08000000"
        else
            # ion-pool 3840MiB
            ion_pool_reg_val="0x4 0x00000000 0x0 0xF0000000"
            # ion-carveout 1280MiB
            ion_carveout_reg_val="0x8 0x00000000 0x0 0x50000000"
            # ion-cam 1GiB
            ion_cma_reg_val="0xc 0x80000000 0x0 0x40000000"
        fi
    ;;
    "bpu_first")
        if [[ "${INPUT_DTB}" = *"-s100p-"* ]];then
            # ion-pool 7680MiB
            ion_pool_reg_val="0x4 0x00000000 0x1 0xe0000000"
            # ion-carveout 7680MiB
            ion_carveout_reg_val="0x8 0x00000000 0x1 0xe0000000"
            # ion-cma 5120MiB
            ion_cma_reg_val="0xc 0x80000000 0x1 0x40000000"
        else
            # ion-pool 3840MiB
            ion_pool_reg_val="0x4 0x00000000 0x0 0xF0000000"
            # ion-carveout 2304MiB
            ion_carveout_reg_val="0x8 0x00000000 0x0 0x90000000"
            # ion-cam 2GiB
            ion_cma_reg_val="0xc 0x80000000 0x0 0x80000000"
        fi
    ;;
    "cpu_first")
            # ion-pool 1GiB
            ion_pool_reg_val="0x4 0x00000000 0x0 0x40000000"
            # ion-carveout 512MiB
            ion_carveout_reg_val="0x8 0x00000000 0x0 0x20000000"
            # ion-cma 512MiB
            ion_cma_reg_val="0xc 0x80000000 0x0 0x20000000"
    ;;
    "default")
        echo "INFO: Restoring to default..."
        if [ -f ${ORIG_INPUT_DTB} ];then
            cp ${ORIG_INPUT_DTB} ${OUTPUT_DTB}
            rm ${ORIG_INPUT_DTB}
        fi
        exit 0
    ;;
    *)
        echo "ERROR: Invalid option $target"
        help_msg
    ;;
esac


# Set ion region sizes according to input

# DTB->DTS
if [ ! -f ${ORIG_INPUT_DTB} ];then
    cp ${INPUT_DTB} ${ORIG_INPUT_DTB}
    echo "INFO: Backup created:${ORIG_INPUT_DTB}"
else
    echo "INFO: Backup(${ORIG_INPUT_DTB}) already exists, skip backup..."
fi
tmp_dts_f="$TMP_DIR/temp.dts"
dtc -I dtb -O dts -o "${tmp_dts_f}" "$INPUT_DTB" > /dev/null 2>&1 || { echo "ERROR: Decompile $INPUT_DTB failed!"; exit 1; }

update_tmp_dts "ion-pool" "${ion_pool_reg_val}" "$tmp_dts_f"
update_tmp_dts "ion-carveout" "${ion_carveout_reg_val}" "$tmp_dts_f"
update_tmp_dts "ion-cma" "${ion_cma_reg_val}" "$tmp_dts_f"

# DTS->DTB
dtc -I dts -O dtb -o "$OUTPUT_DTB" "$tmp_dts_f" > /dev/null 2>&1 || { echo "ERROR: Compile $INPUT_DTB from $TMP_DIR/modified.dts failed!"; exit 1; }


echo "INFO: Update ${OUTPUT_DTB} for $target Done!"
1 个赞