系统启动时,dts怎么被加载的?

转:http://blog.csdn.net/lichengtongxiazai/article/details/38941913

此文章针对高通msm8953平台,启动过程中,bootloader(默认是bootable/bootloader/lk)会根据机器硬件信息选择合适的devicetree装入内存,把地址等相关信息传给kernel。kernel中,会根据传入的信息创建设备。

1,先从little kernel开始:

1.1 总体来说Lk/arch/arm/crt0.S文件中语句:

bl kmain
调用的是lk/kernel/main.c文件中的函数:kmain()
kmain()
  |bootstrap2()
     |arch_init()
     |platform_init()
     |target_init()
     |apps_init()//call init() of APPs defined using APP_START macro
        |aboot_init()
           |boot_linux_from_mmc()
              |//1,Device tree的第一种方法
                 |dev_tree_get_entry_info()
                    |__dev_tree_get_entry_info()
                 |memmove();
              |//2,Device tree的第二种方法
                 |dev_tree_appended()
              |boot_linux()
                 |update_device_tree()
                 |entry(0, machtype, tags_phys);//pass control to kernel
 

  Aboot.c (bootableootloaderlkappaboot)
APP_START(aboot)
.init = aboot_init,
APP_END

 在下面aboot_init() ---> boot_linux_from_mmc()中,调用dev_tree_get_entry_info(),里面会根据硬件(chipset和platform的id,系统实际跑时的信息在系统boot的更早阶段由N侧设置并传来,而DT中的信息由根节点的"qcom,msm-id"属性定义)来选择合适的DT,后面会把该DT装入内存,把地址等信息传给kernel(通过CPU寄存器)。

 1 void boot_linux(void *kernel, unsigned *tags,
 2 const char *cmdline, unsigned machtype,
 3 void *ramdisk, unsigned ramdisk_size)
 4 {
 5 #if DEVICE_TREE
 6 
 7 
 8 //更新Device Tree
 9 ret = update_device_tree((void *)tags, final_cmdline, ramdisk, ramdisk_size);
10 }
11 
12 
13 /* Top level function that updates the device tree. */
14 int update_device_tree(void *fdt, const char *cmdline,
15   void *ramdisk, uint32_t ramdisk_size)
16 {
17 int ret = 0;
18 uint32_t offset;
19 
20 
21 /* Check the device tree header */
22 //核查其magic数是否正确:version和size
23 ret = fdt_check_header(fdt);
24 
25 
26 
27 
28 /* Add padding to make space for new nodes and properties. */
29 //Move or resize dtb buffer
30 ret = fdt_open_into(fdt, fdt, fdt_totalsize(fdt) + DTB_PAD_SIZE);
31 
32 
33 /* Get offset of the memory node */
34 ret = fdt_path_offset(fdt, "/memory");
35 
36 
37 offset = ret;
38 
39 
40 ret = target_dev_tree_mem(fdt, offset);
41 
42 
43 /* Get offset of the chosen node */
44 ret = fdt_path_offset(fdt, "/chosen");
45 
46 
47 offset = ret;
48 /* Adding the cmdline to the chosen node */
49 ret = fdt_setprop_string(fdt, offset, (const char*)"bootargs", (const void*)cmdline);
50 
51 
52 /* Adding the initrd-start to the chosen node */
53 ret = fdt_setprop_u32(fdt, offset, "linux,initrd-start", (uint32_t)ramdisk);
54 if (ret)
55 
56 
57 /* Adding the initrd-end to the chosen node */
58 ret = fdt_setprop_u32(fdt, offset, "linux,initrd-end", ((uint32_t)ramdisk + ramdisk_size));
59 
60 
61 fdt_pack(fdt);
62 
63 
64 return ret;
65 }

2,Kernel中的处理


主要的数据流包括: 
(1)初始化流程,即扫描dtb并将其转换成Device Tree Structure。 
(2)传递运行时参数传递以及platform的识别 
(3)将Device Tree Structure并入linux kernel的设备驱动模型。 

2.1,汇编部分的代码分析 

linux/arch/arm/kernel/head.S文件定义了bootloader和kernel的参数传递要求:
MMU = off, D-cache = off, I-cache = dont care, r0 = 0, r1 = machine nr, r2 = atags or dtb pointer. 


目前的kernel支持旧的tag list的方式,同时也支持device tree的方式。r2可能是device tree binary file的指针(bootloader要传递给内核之前要copy到memory中),也可以是tag list的指针。在ARM的汇编部分的启动代码中(主要是head.S和head-common.S),machine type ID和指向DTB或者atags的指针被保存在变量__machine_arch_type和__atags_pointer中,这么做是为了后续C代码进行处理。
start_kernel()
  |setup_arch()
     |setup_machine_fdt()//select machine description according to DT info

2.2,获得machine描述符

1 //根据Device Tree的信息,找到最适合的machine描述符。
2 struct machine_desc * __init setup_machine_fdt(unsigned int dt_phys)
3 {
4 /* 扫描 /chosen node,保存运行时参数(bootargs)到boot_command_line,此外,还处理initrd相关的property,并保存在initrd_start和initrd_end这两个全局变量中 */
5
of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);/* 扫描根节点,获取 {size,address}-cells信息,并保存在dt_root_size_cells和dt_root_addr_cells全局变量中 */
6
of_scan_flat_dt(early_init_dt_scan_root, NULL);/* 扫描DTB中的memory node,并把相关信息保存在meminfo中,全局变量meminfo保存了系统内存相关的信息。*/
7
of_scan_flat_dt(early_init_dt_scan_memory, NULL);/* Change machine number to match the mdesc we're using */
8
__machine_arch_type = mdesc_best->nr;
9 return mdesc_best;
10 }

运行时参数是在扫描DTB的chosen node时候完成的,具体的动作就是获取chosen node的bootargs、initrd等属性的value,并将其保存在全局变量(boot_command_line,initrd_start、initrd_end)中。


2.3,将DTB转换成device node的结构的节点

在系统初始化的过程中,我们需要将DTB转换成节点是device_node的树状结构,以便后续方便操作。具体的代码位于setup_arch->unflatten_device_tree中。

1 void __init unflatten_device_tree(void)
2 {
3   __unflatten_device_tree(initial_boot_params, &allnodes,early_init_dt_alloc_memory_arch);
4 /* Get pointer to "/chosen" and "/aliasas" nodes for use everywhere */
5   of_alias_scan(early_init_dt_alloc_memory_arch);
6 
7 }

unflatten_device_tree函数的主要功能就是扫描DTB,将device node被组织成:
(1)global list。全局变量struct device_node *of_allnodes就是指向设备树的global list
(2)tree。

 1 static void __unflatten_device_tree(struct boot_param_header *blob,
 2     struct device_node **mynodes,
 3     void * (*dt_alloc)(u64 size, u64 align))
 4 {
 5   //此处删除了health check代码,例如检查DTB header的magic,确认blob的确指向一个DTB。
 6   /* scan过程分成两轮,第一轮主要是确定device-tree structure的长度,保存在size变量中 */
 7 start = ((unsigned long)blob) +
 8 be32_to_cpu(blob->off_dt_struct);
 9 size = unflatten_dt_node(blob, 0, &start, NULL, NULL, 0);
10 size = (size | 3) + 1;
11 
12 
13 /* 初始化的时候,并不是扫描到一个node或者property就分配相应的内存,实际上内核是一次性的分配了一大片内存,这些内存包括了所有的struct device_node、node name、struct property所需要的内存。*/
14 mem = (unsigned long)
15 dt_alloc(size + 4, __alignof__(struct device_node));
16 ((__be32 *)mem)[size / 4] = cpu_to_be32(0xdeadbeef);
17 
18 
19 /* 这是第二轮的scan,第一次scan是为了得到保存所有node和property所需要的内存size,第二次就是实打实的要构建device node tree了 */
20 start = ((unsigned long)blob) +
21 be32_to_cpu(blob->off_dt_struct);
22 unflatten_dt_node(blob, mem, &start, NULL, &allnextp, 0);
23 //此处略去校验溢出和校验OF_DT_END。
24 }


2.4,并入linux kernel的设备驱动模型


在linux kernel引入统一设备模型之后,bus、driver和device形成了设备模型中的铁三角。在驱动初始化的时候会将代表该driver的一个数据结构(一般是xxx_driver)挂入bus上的driver链表。device挂入链表分成两种情况,一种是即插即用类型的bus,在插入一个设备后,总线可以检测到这个行为并动态分配一个device数据结构(一般是xxx_device,例如usb_device),之后,将该数据结构挂入bus上的device链表。bus上挂满了driver和device,那么如何让device遇到“对”的那个driver呢?就是bus的match函数。
系统应该会根据Device tree来动态的增加系统中的platform_device(这个过程并非只发生在platform bus上,也可能发生在其他的非即插即用的bus上,例如AMBA总线、PCI总线)。 如果要并入linux kernel的设备驱动模型,那么就需要根据device_node的树状结构(root是of_allnodes)将一个个的device node挂入到相应的总线device链表中。只要做到这一点,总线机制就会安排device和driver的约会。当然,也不是所有的device node都会挂入bus上的设备链表,比如cpus node,memory node,choose node等。
4.1 没有挂入bus的device node
(1) cpus node的处理
暂无,只有choose node的相关处理。
(2) memory的处理

 1 int __init early_init_dt_scan_memory(unsigned long node, const char *uname,
 2     int depth, void *data)
 3 {
 4 char *type = of_get_flat_dt_prop(node, "device_type", NULL);
 5 /*在初始化的时候,我们会对每一个device node都要调用该call back函数,因此,我们要过滤掉那些和memory block定义无关的node。和memory block定义有的节点有两种,一种是node name是memory@形态的,另外一种是node中定义了device_type属性并且其值是memory。*/
 6 if (type == NULL) {
 7 if (depth != 1 || strcmp(uname, "memory@0") != 0)
 8 return 0;
 9 } else if (strcmp(type, "memory") != 0)
10 return 0;
11 /*获取memory的起始地址和length的信息。有两种属性和该信息有关,一个是linux,usable-memory,不过最新的方式还是使用reg属性。*/
12 reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
13 if (reg == NULL)
14 reg = of_get_flat_dt_prop(node, "reg", &l);
15 if (reg == NULL)
16 return 0;
17 endp = reg + (l / sizeof(__be32));
18 /*reg属性的值是address,size数组,那么如何来取出一个个的address/size呢?由于memory node一定是root node的child,因此dt_root_addr_cells(root node的#address-cells属性值)和dt_root_size_cells(root node的#size-cells属性值)之和就是address,size数组的entry size。*/
19 while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
20 u64 base, size;
21 base = dt_mem_next_cell(dt_root_addr_cells, &reg);
22 size = dt_mem_next_cell(dt_root_size_cells, &reg);
23 if (size == 0)
24 continue;
25 //将具体的memory block信息加入到内核中。
26 early_init_dt_add_memory_arch(base, size);
27 }
28 return 0;
29 }
30 (3) interrupt controller的处理
31 初始化是通过start_kernel->init_IRQ->machine_desc->init_irq()实现的。我们用Qualcomm MSM 8974为例来描述interrupt controller的处理过程。下面是machine描述符的定义:/arch/arm/mach-msm/board-8974.c
32 DT_MACHINE_START(MSM8974_DT, "Qualcomm MSM 8974 (Flattened Device Tree)")
33 .init_irq = msm_dt_init_irq,
34 .dt_compat = msm8974_dt_match,
35 ...
36 MACHINE_END
37 源码文件:/arch/arm/mach-msm/board-dt.c
38 void __init msm_dt_init_irq(void)
39 {
40 struct device_node *node;
41 
42 
43 of_irq_init(irq_match);
44 node = of_find_matching_node(NULL, mpm_match);
45 }
46 of_irq_init函数:遍历Device Tree,找到匹配的irqchip。具体的代码如下:
47 void __init of_irq_init(const struct of_device_id *matches)
48 {
49 /*遍历所有的node,寻找定义了interrupt-controller属性的node,如果定义了interrupt-controller属性则说明该node就是一个中断控制器。*/
50 for_each_matching_node(np, matches) {
51 if (!of_find_property(np, "interrupt-controller", NULL))
52 continue;
53 /*分配内存并挂入链表,当然还有根据interrupt-parent建立controller之间的父子关系。对于interrupt controller,它也可能是一个树状的结构。*/
54 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
55 
56 
57 desc->dev = np;
58 desc->interrupt_parent = of_irq_find_parent(np);
59 if (desc->interrupt_parent == np)
60 desc->interrupt_parent = NULL;
61 list_add_tail(&desc->list, &intc_desc_list);
62 }
63 
64 
65 /*正因为interrupt controller被组织成树状的结构,因此初始化的顺序就需要控制,应该从根节点开始,依次递进到下一个level的interrupt controller。 */
66 while (!list_empty(&intc_desc_list)) {
67 /*intc_desc_list链表中的节点会被一个个的处理,每处理完一个节点就会将该节点删除,当所有的节点被删除,整个处理过程也就是结束了。*/
68 list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
69 const struct of_device_id *match;
70 int ret;
71 of_irq_init_cb_t irq_init_cb;
72 /*最开始的时候parent变量是NULL,确保第一个被处理的是root interrupt controller。在处理完root node之后,parent变量被设定为root interrupt controller,因此,第二个循环中处理的是所有parent是root interrupt controller的child interrupt controller。也就是level 1(如果root是level 0的话)的节点。*/
73 if (desc->interrupt_parent != parent)
74 continue;
75 
76 
77 list_del(&desc->list);//从链表中删除
78 match = of_match_node(matches, desc->dev);//匹配并初始化
79 //match->data是初始化函数
80 if (WARN(!match->data,
81    "of_irq_init: no init function for %s
",
82    match->compatible)) {
83 kfree(desc);
84 continue;
85 }
86 irq_init_cb = match->data;//执行初始化函数
87 ret = irq_init_cb(desc->dev, desc->interrupt_parent);
88 /*处理完的节点放入intc_parent_list链表,后面会用到*/
89 list_add_tail(&desc->list, &intc_parent_list);
90 }
91 
92 
93 /* 对于level 0,只有一个root interrupt controller,对于level 1,可能有若干个interrupt controller,因此要遍历这些parent interrupt controller,以便处理下一个level的child node。 */
94 desc = list_first_entry(&intc_parent_list, typeof(*desc), list);
95 list_del(&desc->list);
96 parent = desc->dev;
97 kfree(desc);
98 }
99 }

有该node中有interrupt-controller这个属性定义,那么linux kernel就会分配一个interrupt controller的描述符(struct intc_desc)并挂入队列。通过interrupt-parent属性,可以确定各个interrupt controller的层次关系。在scan了所有的Device Tree中的interrupt controller的定义之后,系统开始匹配过程。一旦匹配到了interrupt chip列表中的项次后,就会调用相应的初始化函数。

原文地址:https://www.cnblogs.com/linhaostudy/p/8109947.html