六 memory&chosen节点

根节点那一节我们说过,最简单的设备树也必须包含cpus节点和memory节点。memory节点用来描述硬件内存布局的。如果有多块内存,既可以通过多个memory节点表示,也可以通过一个memory节点的reg属性的多个元素支持。举一个例子,假如某个64位的系统有两块内存,分别是

• RAM: 起始地址 0x0, 长度 0x80000000 (2GB)
• RAM: 起始地址 0x100000000, 长度 0x100000000 (4GB)

对于64位的系统,根节点的#address-cells属性和#size-cells属性都设置成2。一个memory节点的形式如下(还记得前几节说过节点地址必须和reg属性第一个地址相同的事情吧):
    memory@0 {
        device_type = "memory";
        reg = <0x000000000 0x00000000 0x00000000 0x80000000
               0x000000001 0x00000000 0x00000001 0x00000000>;
    };

两个memory节点的形式如下:
    memory@0 {
        device_type = "memory";
        reg = <0x000000000 0x00000000 0x00000000 0x80000000>;
    };
    memory@100000000 {
        device_type = "memory";
        reg = <0x000000001 0x00000000 0x00000001 0x00000000>;
    };

chosen节点也位于根节点下,该节点用来给内核传递参数(不代表实际硬件)。对于Linux内核,该节点下最有用的属性是bootargs,该属性的类型是字符串,用来向Linux内核传递cmdline。规范中还定义了stdout-path和stdin-path两个可选的、字符串类型的属性,这两个属性的目的是用来指定标准输入输出设备的,在linux中,这两个属性基本不用。

memory和chosen节点在内核初始化的代码都位于start_kernel()->setup_arch()->setup_machine_fdt()->early_init_dt_scan_nodes()函数中(位于drivers/of/fdt.c),复制代码如下(本节所有代码都来自官方内核4.4-rc7版本):

1078 void __init early_init_dt_scan_nodes(void)
1079 {      
1080     /* Retrieve various information from the /chosen node */
1081     of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
1082 
1083     /* Initialize {size,address}-cells info */
1084     of_scan_flat_dt(early_init_dt_scan_root, NULL);
1085 
1086     /* Setup memory, calling early_init_dt_add_memory_arch */
1087     of_scan_flat_dt(early_init_dt_scan_memory, NULL);
1088 }

of_scan_flat_dt函数扫描整个设备树,实际的动作是在回调函数中完成的。第1081行是对chosen节点操作,该行代码的作用是将节点下的bootargs属性的字符串拷贝到boot_command_line指向的内存中。boot_command_line是内核的一个全局变量,在内核的多处都会用到。第1084行是根据根节点的#address-cells属性和#size-cells属性初始化全局变量dt_root_size_cells和dt_root_addr_cells,还记得前边说过如果没有设置属性的话就用默认值,这些都在early_init_dt_scan_root函数中实现。第1087行是对内存进行初始化,复制early_init_dt_scan_memory部分代码如下:

893 /**
 894  * early_init_dt_scan_memory - Look for an parse memory nodes
 895  */
 896 int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
 897                      int depth, void *data)
 898 {
 899     const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
 900     const __be32 *reg, *endp;
 901     int l;
 902 
 903     /* We are scanning "memory" nodes only */
 904     if (type == NULL) {
 905         /*
 906          * The longtrail doesn't have a device_type on the
 907          * /memory node, so look for the node called /memory@0.
 908          */
 909         if (!IS_ENABLED(CONFIG_PPC32) || depth != 1 || strcmp(uname, "memory@0") != 0)
 910             return 0;
 911     } else if (strcmp(type, "memory") != 0)
 912         return 0;
 913 
 914     reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
 915     if (reg == NULL)
 916         reg = of_get_flat_dt_prop(node, "reg", &l);
 917     if (reg == NULL)
 918         return 0;
 919 
 920     endp = reg + (l / sizeof(__be32));
 921 
 922     pr_debug("memory scan node %s, reg size %d,\n", uname, l);
 923 
 924     while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
 925         u64 base, size;
 926 
 927         base = dt_mem_next_cell(dt_root_addr_cells, &reg);
 928         size = dt_mem_next_cell(dt_root_size_cells, &reg);
 929
 930         if (size == 0)
 931             continue;
 932         pr_debug(" - %llx ,  %llx\n", (unsigned long long)base,
 933             (unsigned long long)size);
 934 
 935         early_init_dt_add_memory_arch(base, size);
 936     }
 937 
 938     return 0;
 939 }

第914行可以看出linux内核不仅支持reg属性,也支持linux,usable-memory属性。对于dt_root_addr_cells和dt_root_size_cells的使用也能看出根节点的#address-cells属性和#size-cells属性都是用来描述内存地址和大小的。得到每块内存的起始地址和大小后,在第935行调用early_init_dt_add_memory_arch函数,复制代码如下:
 
 983 void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size)
 984 {
 985     const u64 phys_offset = __pa(PAGE_OFFSET);
 986 
 987     if (!PAGE_ALIGNED(base)) {
 988         if (size < PAGE_SIZE - (base & ~PAGE_MASK)) {
 989             pr_warn("Ignoring memory block 0x%llx - 0x%llx\n",
 990                 base, base + size);
 991             return;
 992         }
 993         size -= PAGE_SIZE - (base & ~PAGE_MASK);
 994         base = PAGE_ALIGN(base);
 995     }
 996     size &= PAGE_MASK;
 997 
 998     if (base > MAX_MEMBLOCK_ADDR) {
 999         pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
1000                 base, base + size);
1001         return;
1002     }
1003 
1004     if (base + size - 1 > MAX_MEMBLOCK_ADDR) {
1005         pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
1006                 ((u64)MAX_MEMBLOCK_ADDR) + 1, base + size);
1007         size = MAX_MEMBLOCK_ADDR - base + 1;
1008     }
1009 
1010     if (base + size < phys_offset) {
1011         pr_warning("Ignoring memory block 0x%llx - 0x%llx\n",
1012                base, base + size);
1013         return;
1014     }
1015     if (base < phys_offset) {
1016         pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
1015     if (base < phys_offset) {
1016         pr_warning("Ignoring memory range 0x%llx - 0x%llx\n",
1017                base, phys_offset);
1018         size -= phys_offset - base;
1019         base = phys_offset;
1020     }
1021     memblock_add(base, size);
1022 }

从以上代码可以看出内核对地址和大小做了一系列判断后,最后调用memblock_add将内存块加入内核。

Linux设备树(六 memory&chosen节点)的更多相关文章

  1. 我眼中的Linux设备树(六 memory&chosen节点)

    六 memory&chosen节点根节点那一节我们说过,最简单的设备树也必须包含cpus节点和memory节点.memory节点用来描述硬件内存布局的.如果有多块内存,既可以通过多个memor ...

  2. 我眼中的Linux设备树(五 根节点)

    五 根节点一个最简单的设备树必须包含根节点,cpus节点,memory节点.根节点的名字及全路径都是"/",至少需要包含model和compatible两个属性.model属性我们 ...

  3. Linux设备树(五 根节点)

    五 根节点 一个最简单的设备树必须包含根节点,cpus节点,memory节点.根节点的名字及全路径都是“/”,至少需要包含model和compatible两个属性.model属性我们在属性那节已经说过 ...

  4. linux 设备树【转】

    转自:http://blog.csdn.net/chenqianleo/article/details/77779439 [-] linux 设备树 为什么要使用设备树Device Tree 设备树的 ...

  5. linux设备树语法

    设备树语法及绑定 概述 Device Tree是一种用来描述硬件的数据结构,类似板级描述语言,起源于OpenFirmware(OF). 就ARM平台来说,设备树文件存放在arch/arm/boot/d ...

  6. 【转载】Linux设备树(Device Tree)机制

    转:Linux设备树(Device Tree)机制   目录 1. 设备树(Device Tree)基本概念及作用2. 设备树的组成和使用 2.1. DTS和DTSI 2.2. DTC 2.3. DT ...

  7. Linux设备树语法详解

    概念 Linux内核从3.x开始引入设备树的概念,用于实现驱动代码与设备信息相分离.在设备树出现以前,所有关于设备的具体信息都要写在驱动里,一旦外围设备变化,驱动代码就要重写.引入了设备树之后,驱动代 ...

  8. Linux设备树语法详解【转】

    转自:http://www.cnblogs.com/xiaojiang1025/p/6131381.html 概念 Linux内核从3.x开始引入设备树的概念,用于实现驱动代码与设备信息相分离.在设备 ...

  9. 宋牧春: Linux设备树文件结构与解析深度分析(2) 【转】

    转自:https://mp.weixin.qq.com/s/WPZSElF3OQPMGqdoldm07A 作者简介 宋牧春,linux内核爱好者,喜欢阅读各种开源代码(uboot.linux.ucos ...

随机推荐

  1. [20190402]Library Cache mutex.txt

    [20190402]Library Cache mutex.txt 1.环境:SCOTT@book> @ ver1PORT_STRING                    VERSION   ...

  2. Python装饰器、内置函数之金兰契友

    装饰器:装饰器的实质就是一个闭包,而闭包又是嵌套函数的一种.所以也可以理解装饰器是一种特殊的函数.因为程序一般都遵守开放封闭原则,软件在设计初期不可能把所有情况都想到,所以一般软件都支持功能上的扩展, ...

  3. Linux学习历程——Centos 7 top命令

    一.命令介绍 top 命令用于动态的监控进程活动与系统负载信息. 格式为 top [参数] 二.实例 直接运行top命令 top命令执行结果的前五行为系统整体的统计信息,代表含义如下: 第1行:系统时 ...

  4. kunbernetes-快速入门

    1.入门概述 本文以在容器云上部署一个nexus3应用为例,通过通过一步步的操作,帮助用户快速的对Kubernetes有一个快速和整体的认识.通过快速入门,可以提供如下知识内容: 在集群中部署一个容器 ...

  5. 浅析foreach语句

    本篇是我对于foreach语句(增强for)的总结: 我的总结流程如下: 1.先整体说明增强for遍历集合与数组之间的区别. 2.通过一维数组来说明(给出反编译的源码,形成对照). 3.通过二维数组来 ...

  6. Spring Security(三十二):10. Core Services

    Now that we have a high-level overview of the Spring Security architecture and its core classes, let ...

  7. Spring Cloud:Security OAuth2 自定义异常响应

    对于客户端开发或者网站开发而言,调用接口返回有统一的响应体,可以针对性的设计界面,代码结构更加清晰,层次也更加分明. 默认异常响应 在使用 Spring Security Oauth2 登录和鉴权失败 ...

  8. TensorRT&Sample&Python[uff_custom_plugin]

    本文是基于TensorRT 5.0.2基础上,关于其内部的uff_custom_plugin例子的分析和介绍. 本例子展示如何使用cpp基于tensorrt python绑定和UFF解析器进行编写pl ...

  9. Linux下redis的安装及配置

    1.去官网下载redis(redis.io) 2.将其解压到根目录下 3.进入解压的目录,然后编译源程序, 如果不是root账户登录的,命令前面需要加sudo make make install PR ...

  10. Asible第三章:roles--小白博客

    本节内容: 什么场景下会用roles? roles示例 一.什么场景下会用roles? 假如我们现在有3个被管理主机,第一个要配置成httpd,第二个要配置成php服务器,第三个要配置成MySQL服务 ...