From patchwork Fri Jun 19 21:11:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Walter Lozano X-Patchwork-Id: 242716 List-Id: U-Boot discussion From: walter.lozano at collabora.com (Walter Lozano) Date: Fri, 19 Jun 2020 18:11:37 -0300 Subject: [RFC 1/4] dtoc: add POC for dtb shrink In-Reply-To: <20200619211140.5081-1-walter.lozano@collabora.com> References: <20200619211140.5081-1-walter.lozano@collabora.com> Message-ID: <20200619211140.5081-2-walter.lozano@collabora.com> Based on several reports and discussions [1], [2] it is clear that U-Boot's footprint is always a concern, and any kind of reduction is an improvement. In particular dtb is one of the sources of footprint increment, as U-Boot uses the same dtb as Linux. However is interesting to note that U-Boot does not require all the nodes and properties declared in it. Some improvements in this sense are already present, such as removing properties based on configuration and using specific "u-boot" properties to keep only specific node in SPL. However, this require manual configuration. Additionally reducing dtb, will allow ATF for better handing FDT buffer, which is an issue in some contexts [3]. In order to improve this situation, this patch adds a proof of concept for dtb shrink. The idea behind this is simple, remove all the nodes from dtb which compatible string is not supported by any driver present. This approach makes sense for those configuration where Linux is expected to have its own dtb. This patch is based on the work of Simon Glass present in [4] which adds support to dtoc for parsing compatible strings. Some early untested results shows that the reduction in size is 50 % in case of mx6_cuboxi_defconfig, which is promising. Some additional reduction could be possible by only keeping the nodes for whose compatible string is supported by any enabled driver. However, this requires to add extra logic to parse config files and map configuration to compatible strings. This proof of concept uses fdtgrep to implement the node removal, but the idea is to implement this logic inside the dtoc for better handling. [1] http://patchwork.ozlabs.org/project/uboot/patch/20200525093539.1.Ibf2d19439cde35e39192a9d4a8dad23539fae2e6 at changeid/ [2] http://u-boot.10912.n7.nabble.com/PATCH-v1-00-15-add-basic-driver-support-for-broadcom-NS3-soc-tt412411.html#none [3] https://review.trustedfirmware.org/c/TF-A/trusted-firmware-a/+/4512 [4] https://gitlab.denx.de/u-boot/custodians/u-boot-dm/-/tree/dtoc-working Signed-off-by: Walter Lozano --- tools/dtoc/dtb_platdata.py | 42 +++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/tools/dtoc/dtb_platdata.py b/tools/dtoc/dtb_platdata.py index 21cce5afb5..1df13b2cd2 100644 --- a/tools/dtoc/dtb_platdata.py +++ b/tools/dtoc/dtb_platdata.py @@ -399,7 +399,10 @@ class DtbPlatdata(object): """Scan the driver folders to build a list of driver names and possible aliases """ - for (dirpath, dirnames, filenames) in os.walk('/home/sglass/u'): + basedir = sys.argv[0].replace('tools/dtoc/dtoc', '') + if basedir == '': + basedir = './' + for (dirpath, dirnames, filenames) in os.walk(basedir): for fn in filenames: if not fn.endswith('.c'): continue @@ -802,6 +805,32 @@ class DtbPlatdata(object): self.out(''.join(self.get_buf())) self.close_output() + def shrink(self): + """Generate a shrunk version of DTB bases on valid drivers + + This function removes nodes from dtb which compatible string is not + found in drivers. The output is saved in a file with suffix name -shrink.dtb + """ + compat = [] + cmd = './tools/fdtgrep ' + #print(self._drivers) + for dr in self._drivers.values(): + compat = compat + dr.compat + + for cp in compat: + #print(cp) + cmd += ' -c ' + cp + + cmd += ' -O dtb -o ' + self._dtb_fname.replace('.dtb', '') + '-shrink.dtb ' + self._dtb_fname + + if False: + with open('dt_shrink.sh', 'w+') as script: + script.write(cmd) + + os.system(cmd) + + return + def run_steps(args, dtb_file, config_file, include_disabled, output): """Run all the steps of the dtoc tool @@ -816,6 +845,10 @@ def run_steps(args, dtb_file, config_file, include_disabled, output): if not args: raise ValueError('Please specify a command: struct, platdata') + skip_scan = False + if args == ['shrink']: + skip_scan = True + plat = DtbPlatdata(dtb_file, config_file, include_disabled) plat.scan_drivers() plat.scan_dtb() @@ -823,14 +856,17 @@ def run_steps(args, dtb_file, config_file, include_disabled, output): plat.scan_config() plat.scan_reg_sizes() plat.setup_output(output) - structs = plat.scan_structs() - plat.scan_phandles() + if not skip_scan: + structs = plat.scan_structs() + plat.scan_phandles() for cmd in args[0].split(','): if cmd == 'struct': plat.generate_structs(structs) elif cmd == 'platdata': plat.generate_tables() + elif cmd == 'shrink': + plat.shrink() else: raise ValueError("Unknown command '%s': (use: struct, platdata)" % cmd) From patchwork Fri Jun 19 21:11:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Walter Lozano X-Patchwork-Id: 242717 List-Id: U-Boot discussion From: walter.lozano at collabora.com (Walter Lozano) Date: Fri, 19 Jun 2020 18:11:38 -0300 Subject: [RFC 2/4] dtoc: add initial support for deleting DTB nodes In-Reply-To: <20200619211140.5081-1-walter.lozano@collabora.com> References: <20200619211140.5081-1-walter.lozano@collabora.com> Message-ID: <20200619211140.5081-3-walter.lozano@collabora.com> This patch introduce a test for deleting DTB nodes using Python library. Signed-off-by: Walter Lozano --- tools/dtoc/dtb_platdata.py | 28 ++++++++++++++++++++++++++++ tools/dtoc/fdt.py | 3 +++ 2 files changed, 31 insertions(+) diff --git a/tools/dtoc/dtb_platdata.py b/tools/dtoc/dtb_platdata.py index 1df13b2cd2..d3fb4dcbf2 100644 --- a/tools/dtoc/dtb_platdata.py +++ b/tools/dtoc/dtb_platdata.py @@ -831,6 +831,30 @@ class DtbPlatdata(object): return + def test_del_node(self): + """Test the support of node deletion' + + This function tests the support of node removal by deleting a specific + node name + """ + for n in self._fdt.GetRoot().subnodes: + print('Name', n.name) + #print('Props', n.props) + if n.name == 'board-detect': + n.DelNode() + #self._fdt.GetRoot().subnodes.remove(n) + self._fdt.Scan() + print('') + for n in self._fdt.GetRoot().subnodes: + print('Name', n.name) + #print('Props', n.props) + if n.name == 'serial': + self._fdt.GetRoot().subnodes.remove(n) + + self._fdt.Pack() + self._fdt.Flush() + self._fdt.Sync() + def run_steps(args, dtb_file, config_file, include_disabled, output): """Run all the steps of the dtoc tool @@ -848,6 +872,8 @@ def run_steps(args, dtb_file, config_file, include_disabled, output): skip_scan = False if args == ['shrink']: skip_scan = True + if args == ['test_del_node']: + skip_scan = True plat = DtbPlatdata(dtb_file, config_file, include_disabled) plat.scan_drivers() @@ -867,6 +893,8 @@ def run_steps(args, dtb_file, config_file, include_disabled, output): plat.generate_tables() elif cmd == 'shrink': plat.shrink() + elif cmd == 'test_del_node': + plat.test_del_node() else: raise ValueError("Unknown command '%s': (use: struct, platdata)" % cmd) diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py index 3d4bc3b2ef..b3b626cd4d 100644 --- a/tools/dtoc/fdt.py +++ b/tools/dtoc/fdt.py @@ -502,6 +502,9 @@ class Node: for prop in prop_list: prop.Sync(auto_resize) + def DelNode(self): + fdt_obj = self._fdt._fdt_obj + fdt_obj.del_node(self._offset) class Fdt: """Provides simple access to a flat device tree blob using libfdts. From patchwork Fri Jun 19 21:11:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Walter Lozano X-Patchwork-Id: 242718 List-Id: U-Boot discussion From: walter.lozano at collabora.com (Walter Lozano) Date: Fri, 19 Jun 2020 18:11:39 -0300 Subject: [RFC 3/4] dtoc: add support for generate stuct udevice_id In-Reply-To: <20200619211140.5081-1-walter.lozano@collabora.com> References: <20200619211140.5081-1-walter.lozano@collabora.com> Message-ID: <20200619211140.5081-4-walter.lozano@collabora.com> Based on several reports there is an increasing concern in the impact of adding additional features to drivers based on compatible strings. A good example of this situation is found in [1]. In order to reduce this impact and as an initial step for further reduction, propose a new way to declare compatible strings, which allows to only include the useful ones. The idea is to define compatible strings in a way to be easily parsed by dtoc, which will be responsible to build struct udevice_id [] based on the compatible strings present in the dtb. Additional features can be easily added, such as define constants depending on the presence of compatible strings, which allows to enable code blocks only in such cases without the need of adding additional configuration options. [1] http://patchwork.ozlabs.org/project/uboot/patch/20200525202429.2146-1-agust at denx.de/ Signed-off-by: Walter Lozano --- tools/dtoc/dtb_platdata.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tools/dtoc/dtb_platdata.py b/tools/dtoc/dtb_platdata.py index d3fb4dcbf2..e199caf8c9 100644 --- a/tools/dtoc/dtb_platdata.py +++ b/tools/dtoc/dtb_platdata.py @@ -170,6 +170,9 @@ class DtbPlatdata(object): key: alias name value: node path _tiny_uclasses: List of uclass names that are marked as 'tiny' + _compatible_strings: Dict of compatible strings read from drivres + key: driver name + value: list of struct udevice_id """ def __init__(self, dtb_fname, config_fname, include_disabled): self._fdt = None @@ -188,6 +191,7 @@ class DtbPlatdata(object): self._tiny_uclasses = [] self._of_match = {} self._compat_to_driver = {} + self._compatible_strings = {} def get_normalized_compat_name(self, node): compat_c, aliases_c = get_compat_name(node) @@ -395,6 +399,15 @@ class DtbPlatdata(object): except: pass + # find entries declared with the form + # COMPATIBLE(driver_name, compatible_string) + compatible_strings = re.findall('COMPATIBLE\(\s*(\w+)\s*,\s*(\S+)\s*\)', b) + + for co in compatible_strings: + if not self._compatible_strings.get(co[0]): + self._compatible_strings[co[0]] = [] + self._compatible_strings[co[0]].append(co) + def scan_drivers(self): """Scan the driver folders to build a list of driver names and possible aliases @@ -805,6 +818,23 @@ class DtbPlatdata(object): self.out(''.join(self.get_buf())) self.close_output() + def generate_compatible(self): + """Generates the struct udevice_id[] to be used in drivers + + This writes C code to implement struct udevice_id[] based on + COMPATIBLE(driver_name, compatible) entries found in drivers. + + Additionally this function can filter entries in order to avoid + adding those that are not present in DT. + """ + self.out('#define COMPATIBLE(__driver_name, __compatible, ...)\n\n') + for vals in self._compatible_strings.values(): + st = '' + for comp in vals: + st += '{.compatible = %s},\\\n' % (comp[1]) + st += '{ /* sentinel */ },\\\n' + self.out('#define COMPATIBLE_LIST_%s { \\\n%s}\n' % (comp[0], st)) + def shrink(self): """Generate a shrunk version of DTB bases on valid drivers @@ -895,6 +925,8 @@ def run_steps(args, dtb_file, config_file, include_disabled, output): plat.shrink() elif cmd == 'test_del_node': plat.test_del_node() + elif cmd == 'compatible': + plat.generate_compatible() else: raise ValueError("Unknown command '%s': (use: struct, platdata)" % cmd) From patchwork Fri Jun 19 21:11:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Walter Lozano X-Patchwork-Id: 242719 List-Id: U-Boot discussion From: walter.lozano at collabora.com (Walter Lozano) Date: Fri, 19 Jun 2020 18:11:40 -0300 Subject: [RFC 4/4] mmc: fsl_esdhc_imx: make use of dtoc to generate struct udevice_id In-Reply-To: <20200619211140.5081-1-walter.lozano@collabora.com> References: <20200619211140.5081-1-walter.lozano@collabora.com> Message-ID: <20200619211140.5081-5-walter.lozano@collabora.com> As an example of use, convert fsl_esdhc_imx to use COMPATIBLE entries for declaring compatible strings. As a result of these entries dtoc will generate code similar to Signed-off-by: Walter Lozano --- drivers/mmc/fsl_esdhc_imx.c | 58 ++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/drivers/mmc/fsl_esdhc_imx.c b/drivers/mmc/fsl_esdhc_imx.c index 588d6a9d76..16448a9ebe 100644 --- a/drivers/mmc/fsl_esdhc_imx.c +++ b/drivers/mmc/fsl_esdhc_imx.c @@ -33,6 +33,7 @@ #include #include #include +#include #if !CONFIG_IS_ENABLED(BLK) #include "mmc_private.h" @@ -1613,22 +1614,45 @@ static struct esdhc_soc_data usdhc_imx8qm_data = { ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES, }; -static const struct udevice_id fsl_esdhc_ids[] = { - { .compatible = "fsl,imx53-esdhc", }, - { .compatible = "fsl,imx6ul-usdhc", }, - { .compatible = "fsl,imx6sx-usdhc", }, - { .compatible = "fsl,imx6sl-usdhc", }, - { .compatible = "fsl,imx6q-usdhc", }, - { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,}, - { .compatible = "fsl,imx7ulp-usdhc", }, - { .compatible = "fsl,imx8qm-usdhc", .data = (ulong)&usdhc_imx8qm_data,}, - { .compatible = "fsl,imx8mm-usdhc", .data = (ulong)&usdhc_imx8qm_data,}, - { .compatible = "fsl,imx8mn-usdhc", .data = (ulong)&usdhc_imx8qm_data,}, - { .compatible = "fsl,imx8mq-usdhc", .data = (ulong)&usdhc_imx8qm_data,}, - { .compatible = "fsl,imxrt-usdhc", }, - { .compatible = "fsl,esdhc", }, - { /* sentinel */ } -}; +#if 0 +// As result of the COMPATIBLE entries, dtoc will produce +// code similar to this + +#define COMPATIBLE(__driver_name, compatible, ...) + +#define COMPATIBLE_LIST_FSL_ESDHC { \ +{.compatible = "fsl,imx53-esdhc"},\ +{.compatible = "fsl,imx6ul-usdhc"},\ +{.compatible = "fsl,imx6sx-usdhc"},\ +{.compatible = "fsl,imx6sl-usdhc"},\ +{.compatible = "fsl,imx6q-usdhc"},\ +{.compatible = "fsl,imx7ulp-usdhc"},\ +{.compatible = "fsl,imxrt-usdhc"},\ +{.compatible = "fsl,esdhc"},\ +{ /* sentinel */ },\ +} +#define COMPATIBLE_LIST_FSL_ESDHC2 { \ +{.compatible = "fsl,imxrt-usdhc2"},\ +{.compatible = "fsl,esdhc2"},\ +{ /* sentinel */ },\ +} +#endif + +COMPATIBLE(FSL_ESDHC, "fsl,imx53-esdhc") +COMPATIBLE(FSL_ESDHC, "fsl,imx6ul-usdhc") +COMPATIBLE(FSL_ESDHC, "fsl,imx6sx-usdhc") +COMPATIBLE(FSL_ESDHC, "fsl,imx6sl-usdhc") +COMPATIBLE(FSL_ESDHC, "fsl,imx6q-usdhc") +COMPATIBLE(FSL_ESDHC, "fsl,imx7d-usdhc", (ulong)&usdhc_imx7d_data) +COMPATIBLE(FSL_ESDHC, "fsl,imx7ulp-usdhc") +COMPATIBLE(FSL_ESDHC, "fsl,imx8qm-usdhc", &usdhc_imx8qm_data) +COMPATIBLE(FSL_ESDHC, "fsl,imx8mm-usdhc", &usdhc_imx8qm_data) +COMPATIBLE(FSL_ESDHC, "fsl,imx8mn-usdhc", &usdhc_imx8qm_data) +COMPATIBLE(FSL_ESDHC, "fsl,imx8mq-usdhc", &usdhc_imx8qm_data) +COMPATIBLE(FSL_ESDHC, "fsl,imxrt-usdhc") +COMPATIBLE(FSL_ESDHC, "fsl,esdhc") + +static const struct udevice_id fsl_esdhc_ids[] = COMPATIBLE_LIST_FSL_ESDHC; #if CONFIG_IS_ENABLED(BLK) static int fsl_esdhc_bind(struct udevice *dev) @@ -1640,7 +1664,7 @@ static int fsl_esdhc_bind(struct udevice *dev) #endif U_BOOT_DRIVER(fsl_esdhc) = { - .name = "fsl-esdhc-mmc", + .name = "fsl_esdhc", .id = UCLASS_MMC, .of_match = fsl_esdhc_ids, .ops = &fsl_esdhc_ops,