Linux Kernel优化--优化dts

将dts中没必要的设备删掉以及defconfig中没必要的配置删掉,可以加快kernel的启动时间。
本文主要讲述如何通过dt_to_config工具,将dts中的设备与.config中的配置匹配起来,通过它们之间的匹配关系,来决定优化哪些设备和模块。


dt_to_config介绍

在linux 4.8之后,引入了一个工具scripts/dtc/dt_to_config,这个工具可以将dts或dtb的信息与当前kernel所使用的.config一起解析,然后将dts中的节点信息与.config的配置信息和所使用的源码信息一一匹配。
dt_to_config用法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
sub usage()
{
print
"
Usage: $script_name [options] device-tree...
device_tree is: dts_file | dtb_file | proc_device-tree
Valid options:
-c FILE Read kernel config options from FILE
--config FILE synonym for 'c'
--config-format config file friendly output format
--exclude-flag FLAG exclude entries with a matching flag
-h Display this message and exit
--help synonym for 'h'
--black-list-driver use driver black list
--white-list-config use config white list
--white-list-driver use driver white list
--include-flag FLAG include only entries with a matching flag
--include-suspect include only entries with an uppercase flag
--short-name do not show the path portion of the node name
--show-lists report of white and black lists
--version Display program version and exit
Report driver source files that match the compatibles in the device
tree file and the kernel config options that enable the driver source
files.
This program must be run in the root directory of a Linux kernel
source tree.
The default format is a report that is intended to be easily human
scannable.
An alternate format can be selected by --config-format. This will
create output that can easily be edited to create a fragment that can
be appended to the existing kernel config file. Each entry consists of
multiple lines. The first line reports flags, the node path, compatible
value, driver file matching the compatible, configuration options, and
current values of the configuration options. For each configuration
option, the following lines report the current value and the value that
is required for the driver file to be included in the kernel.
If a large number of drivers or config options is listed for a node,
and the '$pr_flag_value[$pr_flag_pos_hard_coded]' flag is set consider using --white-list-config and/or
--white-list-driver. If the white list option suppresses the correct
entry please report that as a bug.
CAUTION:
This program uses heuristics to guess which driver(s) support each
compatible string and which config option(s) enables the driver(s).
Do not believe that the reported information is fully correct.
This program is intended to aid the process of determining the
proper kernel configuration for a device tree, but this is not
a fully automated process -- human involvement may still be
required!
The driver match heuristic used is to search for source files
containing the compatible string enclosed in quotes.
This program might not be able to find all drivers matching a
compatible string.
Some makefiles are overly clever. This program was not made
complex enough to handle them. If no config option is listed
for a driver, look at the makefile for the driver source file.
Even if a config option is listed for a driver, some other
available config options may not be listed.
FLAG values:
";

for ($k = 0; $k < $num_pr_flags; $k++) {
printf " %s %s\n", $pr_flag_value[$k], $pr_flag_help[$k];
}

print
"
Upper case letters indicate potential issues or problems.
The flag:
";

$k = $pr_flag_pos_hard_coded;
printf " %s %s\n", $pr_flag_value[$k], $pr_flag_help[$k];

print
"
will be set if the config or driver is in the white lists, even if
--white-list-config and --white-list-driver are not specified.
This is a hint that 1) many of these reported lines are likely to
be incorrect, and 2) using those options will reduce the number of
drivers and/or config options reported.
--white-list-config and --white-list-driver may not be accurate if this
program is not well maintained. Use them with appropriate skepticism.
Use the --show-lists option to report the values in the list.
Return value:
0 if no error
1 error processing command line
2 unable to open or read kernel config file
3 unable to open or process input device tree file(s)
EXAMPLES:
dt_to_config arch/arm/boot/dts/my_dts_file.dts
Basic report.
dt_to_config \\
--config \${KBUILD_OUTPUT}/.config \\
arch/\${ARCH}/boot/dts/my_dts_file.dts
Full report, with config file issues noted.
dt_to_config --include-suspect \\
--config \${KBUILD_OUTPUT}/.config \\
arch/\${ARCH}/boot/dts/my_dts_file.dts
Report of node / compatible string / driver tuples that should
be further investigated. A node may have multiple compatible
strings. A compatible string may be matched by multiple drivers.
A driver may have config file issues noted. The compatible string
and/or driver may be in the white lists.
dt_to_config --include-suspect --config-format \\
--config ${KBUILD_OUTPUT}/.config \\
arch/\${ARCH}/boot/dts/my_dts_file.dts
Report of node / compatible string / driver tuples that should
be further investigated. The report can be edited to uncomment
the config options to select the desired tuple for a given node.
A node may have multiple compatible strings. A compatible string
may be matched by multiple drivers. A driver may have config file
issues noted. The compatible string and/or driver may be in the
white lists.
";
}


实践

刚好当前开发使用的是imx8 4.9.51的内核版本,需要优化Linux Kernel,刚好可以借助这个工具来看当前系统配置和使用了哪些资源。使用如下:

注意,必须在kernel的目录下执行。
1
# ./scripts/dtc/dt_to_config arch/arm64/boot/dts/freescale/fsl-imx8-xxx.dtb --short-name --config ./.config --config-format > dts_output.txt


对于各个参数的解释如下:
1. fsl-imx8-xxx.dtb:为xxx项目所生成的dtb文件;
2. --short-name:do not show the path portion of the node name
3. --config ./.config:指定当前目录下的.config文件进行匹配
4. --config-format:config file friendly output format

最终,在dts_output.txt文件中会以如下的方式呈现出来:
1
2
3
4
5
6
7
8
9
# Md-c-------y- : camera : fsl,mxc-md : drivers/media/platform/imx8/mxc-media-dev.c : CONFIG_IMX8_MEDIA_DEVICE : y
# CONFIG_IMX8_MEDIA_DEVICE is set
# CONFIG_IMX8_MEDIA_DEVICE=y
# M-----W------ : camera : simple-bus : no_driver : no_config : x
# no_config

# -d-c-E-----y- : csi@58227000 : fsl,mxc-mipi-csi2 : drivers/media/platform/imx8/mxc-mipi-csi2.c : CONFIG_IMX8_MIPI_CSI2 : y
# CONFIG_IMX8_MIPI_CSI2 is set
# CONFIG_IMX8_MIPI_CSI2=y


接下来以下面的信息来分段解释:
1
-d-c-E-----y- : csi@58227000 : fsl,mxc-mipi-csi2 : drivers/media/platform/imx8/mxc-mipi-csi2.c : CONFIG_IMX8_MIPI_CSI2 : y


1. -d-c-E-----y-:flags,具体干啥用的目前未知;
2. : csi@58227000:dts里的node;
3. : fsl,mxc-mipi-csi2:dts里的compatible属性;
4. : drivers/media/platform/imx8/mxc-mipi-csi2.c:与之对应的驱动文件;
5. : CONFIG_IMX8_MIPI_CSI2:与之对应的配置选项
6. : y:配置的值

关于这部分的内容可以参照:Dt_debugging_part_3

———————

## 注意事项
(1)、
dt_to_config目前还不能完全解释dts文件,刚开始的时候我尝试去解析dts文件,但会报如下类似的错误:
1
2
3
4
5
arch/arm64/boot/dts/freescale/fsl-imx8-ca35.dtsi:16:45: error: no include path in which to search for dt-bindings/clock/imx8qxp-clock.h
#include <dt-bindings/clock/imx8qxp-clock.h>
^
arch/arm64/boot/dts/freescale/fsl-imx8-ca35.dtsi:17:54: error: no include path in which to search for dt-bindings/interrupt-controller/arm-gic.h
#include <dt-bindings/interrupt-controller/arm-gic.h>


也就是说,目前还不能解析#include带<>的文件,需要将其转换为#include带” ”的文件,可以将#include <dt-bindings/clock/imx8qxp-clock.h>转换为#include “../include/dt-bindings/clock/imx8qxp-clock.h”就不会报错了。
但是,这种方法很费劲,最终干脆直接解析.dtb文件。

(2)、
正如dt_to_config中的usage所说的:
> 该程序使用启发式技术来猜测每个驱动程序支持哪一个兼容的字符串和哪个配置选项启用驱动程序。不要相信所报告的信息是完全正确的。
这个程序的目的是帮助确定的过程设备树的正确内核配置,但事实并非如此一个完全自动化的过程 - 人类的参与可能仍然是需要!
所使用的驱动程序匹配启发式是搜索源文件包含用引号括起来的兼容字符串。此程序可能无法找到所有匹配a的驱动程序兼容字符串。

因此,最终的结果只是参考,并且需要自己去判断。

(3)、
关于device tree的讨论和改进,linux社区到现在还在努力着。关于device tree最新的消息,可以参照以下资料:
https://elinux.org/Device_Tree_frowand
https://elinux.org/Device_tree_future#presentation_material_2
https://elinux.org/images/e/e5/Dt_debugging_part_3.pdf
https://github.com/torvalds/linux/blob/master/scripts/dtc/dt_to_config

Title:Linux Kernel优化--优化dts

Author:Victor Huang

Time:2019-07-03 / 22:07

Link:http://wowothink.com/ddc19a34/

License: Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)