of: Add of_machine_compatible_match()
Subsystems (5)
Michael Ellerman
reviewerPATCH v2 1/5 of: Add of_machine_compatible_match()
We have of_machine_is_compatible() to check if a machine is compatible with a single compatible string. However some code is able to support multiple compatible boards, and so wants to check for one of many compatible strings.
So add of_machine_compatible_match() which takes a NULL terminated array of compatible strings to check against the root node's compatible property.
Compared to an open coded match this is slightly more self documenting, and also avoids the caller needing to juggle the root node either directly or via of_find_node_by_path().
--- drivers/of/base.c | 21 +++++++++++++++++++++ include/linux/of.h | 6 ++++++ 2 files changed, 27 insertions(+)
v2: Unchanged.
drivers/of/base.c +21 -0
@@ -394,6 +394,27 @@ int of_device_compatible_match(const struct device_node *device, }EXPORT_SYMBOL_GPL (of_device_compatible_match); +/** +* of_machine_compatible_match - Test root of device tree against a compatible array +* @compats: NULL terminated array of compatible strings to look for in root node's compatible property. +* +* Returns true if the root node has any of the given compatible values in its +* compatible property. +*/ +bool of_machine_compatible_match (const char *const *compats) +{ +struct device_node *root; +int rc =0 ; + + root =of_find_node_by_path ("/" ); +if (root) { + rc =of_device_compatible_match (root, compats); +of_node_put (root); + } + +return rc !=0 ; +} +/** * of_machine_is_compatible - Test root of device tree for a given compatible value * @compat: compatible string to look for in root node's compatible property.
include/linux/of.h +6 -0
@@ -403,6 +403,7 @@ extern int of_alias_get_id(struct device_node *np, const char *stem);extern int of_alias_get_highest_id (const char *stem);extern int of_machine_is_compatible (const char *compat); +bool of_machine_compatible_match (const char *const *compats);extern int of_add_property (struct device_node *np,struct property *prop);extern int of_remove_property (struct device_node *np,struct property *prop);
@@ -808,6 +809,11 @@ static inline int of_remove_property(struct device_node *np, struct property *prreturn 0 ; } +static inline bool of_machine_compatible_match (const char *const *compats) +{ +return false; +} +static inline bool of_console_check (const struct device_node *dn,const char *name,int index) {return false;
Signature · 1 line
2.43.0
Michael Ellerman
reviewerPATCH v2 2/5 of: Change of_machine_is_compatible() to return bool
of_machine_is_compatible() currently returns a positive integer if it finds a match. However none of the callers ever check the value, they all treat it as a true/false.
So change of_machine_is_compatible() to return bool, which will allow the implementation to be changed in a subsequent patch.
--- drivers/of/base.c | 5 ++--- include/linux/of.h | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-)
v2: New.
drivers/of/base.c +2 -3
@@ -419,10 +419,9 @@ bool of_machine_compatible_match(const char *const *compats)* of_machine_is_compatible - Test root of device tree for a given compatible value * @compat: compatible string to look for in root node's compatible property. * -* Return: A positive integer if the root node has the given value in its -* compatible property. +* Return: true if the root node has the given value in its compatible property. */ -int of_machine_is_compatible (const char *compat) +bool of_machine_is_compatible (const char *compat) {struct device_node *root;int rc =0 ;
include/linux/of.h +1 -1
@@ -402,7 +402,7 @@ extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));extern int of_alias_get_id (struct device_node *np,const char *stem);extern int of_alias_get_highest_id (const char *stem); -extern int of_machine_is_compatible (const char *compat); +extern bool of_machine_is_compatible (const char *compat);bool of_machine_compatible_match (const char *const *compats);extern int of_add_property (struct device_node *np,struct property *prop);
Signature · 1 line
2.43.0
Michael Ellerman
reviewerPATCH v2 3/5 of: Reimplement of_machine_is_compatible() using of_machine_compatible_match()
In reply to Michael EllermanFrom: Christophe Leroy <[email protected]>
of_machine_compatible_match() works with a table of strings. of_machine_is_compatible() is a simplier version with only one string.
Re-implement of_machine_is_compatible() by setting a table of strings with a single string then using of_machine_compatible_match().
--- drivers/of/base.c | 21 +-------------------- include/linux/of.h | 14 +++++++++++++- 2 files changed, 14 insertions(+), 21 deletions(-)
v2: Unchanged.
drivers/of/base.c +1 -20
@@ -414,26 +414,7 @@ bool of_machine_compatible_match(const char *const *compats)return rc !=0 ; } - -/** -* of_machine_is_compatible - Test root of device tree for a given compatible value -* @compat: compatible string to look for in root node's compatible property. -* -* Return: true if the root node has the given value in its compatible property. -*/ -bool of_machine_is_compatible (const char *compat) -{ -struct device_node *root; -int rc =0 ; - - root =of_find_node_by_path ("/" ); -if (root) { - rc =of_device_is_compatible (root, compat); -of_node_put (root); - } -return rc; -} -EXPORT_SYMBOL (of_machine_is_compatible); +EXPORT_SYMBOL (of_machine_compatible_match);/** * __of_device_is_available - check if a device is available for use
include/linux/of.h +13 -1
@@ -402,9 +402,21 @@ extern void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align));extern int of_alias_get_id (struct device_node *np,const char *stem);extern int of_alias_get_highest_id (const char *stem); -extern bool of_machine_is_compatible (const char *compat);bool of_machine_compatible_match (const char *const *compats); +/** +* of_machine_is_compatible - Test root of device tree for a given compatible value +* @compat: compatible string to look for in root node's compatible property. +* +* Return: true if the root node has the given value in its compatible property. +*/ +static inline bool of_machine_is_compatible (const char *compat) +{ +const char *compats[] = { compat,NULL }; + +return of_machine_compatible_match (compats); +} +extern int of_add_property (struct device_node *np,struct property *prop);extern int of_remove_property (struct device_node *np,struct property *prop);extern int of_update_property (struct device_node *np,struct property *newprop);
Signature · 1 line
2.43.0
Michael Ellerman
reviewerPATCH v2 4/5 powerpc/machdep: Define 'compatibles' property in ppc_md and use it
In reply to Michael EllermanFrom: Christophe Leroy <[email protected]>
Most probe functions that do not use the 'compatible' string do nothing else than checking whether the machine is compatible with one of the strings in a NULL terminated table of strings.
Define that table of strings in ppc_md structure and check it directly from probe_machine() instead of using ppc_md.probe() for that.
Keep checking in ppc_md.probe() only for more complex probing.
All .compatible could be replaced with a single element NULL terminated list but that's not worth the churn. Can be do incrementaly in follow-up patches.
--- arch/powerpc/include/asm/machdep.h | 1 + arch/powerpc/kernel/setup-common.c | 2 ++ arch/powerpc/platforms/40x/ppc40x_simple.c | 9 +++------ arch/powerpc/platforms/512x/mpc512x_generic.c | 4 +--- arch/powerpc/platforms/52xx/lite5200.c | 10 +--------- arch/powerpc/platforms/52xx/mpc5200_simple.c | 10 +--------- arch/powerpc/platforms/83xx/mpc830x_rdb.c | 10 +--------- arch/powerpc/platforms/83xx/mpc831x_rdb.c | 10 +--------- arch/powerpc/platforms/83xx/mpc837x_rdb.c | 10 +--------- arch/powerpc/platforms/85xx/corenet_generic.c | 2 +- arch/powerpc/platforms/85xx/tqm85xx.c | 10 +--------- 11 files changed, 14 insertions(+), 64 deletions(-)
v2: Unchanged.
arch/powerpc/include/asm/machdep.h +1 -0
@@ -22,6 +22,7 @@ struct pci_host_bridge;struct machdep_calls {const char *name;const char *compatible; +const char *const *compatibles;#ifdef CONFIG_PPC64 #ifdef CONFIG_PM void (*iommu_restore)(void );
arch/powerpc/kernel/setup-common.c +2 -0
@@ -616,6 +616,8 @@ static __init void probe_machine(void)DBG (" %s ...\n" , machine_id->name);if (machine_id->compatible && !of_machine_is_compatible (machine_id->compatible))continue ; +if (machine_id->compatibles && !of_machine_compatible_match (machine_id->compatibles)) +continue ;memcpy (&ppc_md, machine_id,sizeof (struct machdep_calls ));if (ppc_md.probe && !ppc_md.probe ())continue ;
arch/powerpc/platforms/40x/ppc40x_simple.c +3 -6
@@ -59,16 +59,13 @@ static const char * const board[] __initconst = {static int __init ppc40x_probe (void ) { -if (of_device_compatible_match (of_root, board)) { -pci_set_flags (PCI_REASSIGN_ALL_RSRC ); -return 1 ; - } - -return 0 ; +pci_set_flags (PCI_REASSIGN_ALL_RSRC ); +return 1 ; }define_machine (ppc40x_simple) { .name = "PowerPC 40x Platform", + .compatibles = board, .probe = ppc40x_probe, .progress = udbg_progress, .init_IRQ = uic_init_tree,
arch/powerpc/platforms/512x/mpc512x_generic.c +1 -3
@@ -32,9 +32,6 @@ static const char * const board[] __initconst = {*/ static int __init mpc512x_generic_probe (void ) { -if (!of_device_compatible_match (of_root, board)) -return 0 ; -mpc512x_init_early ();return 1 ;
@@ -42,6 +39,7 @@ static int __init mpc512x_generic_probe(void)define_machine (mpc512x_generic) { .name = "MPC512x generic", + .compatibles = board, .probe = mpc512x_generic_probe, .init = mpc512x_init, .setup_arch = mpc512x_setup_arch,
arch/powerpc/platforms/52xx/lite5200.c +1 -9
@@ -172,17 +172,9 @@ static const char * const board[] __initconst = { NULL, }; -/* -* Called very early, MMU is off, device-tree isn't unflattened -*/ -static int __init lite5200_probe (void ) -{ -return of_device_compatible_match (of_root, board); -} -define_machine (lite5200) { .name = "lite5200", - .probe = lite5200_probe, + .compatibles = board, .setup_arch = lite5200_setup_arch, .discover_phbs = mpc52xx_setup_pci, .init = mpc52xx_declare_of_platform_devices,
arch/powerpc/platforms/52xx/mpc5200_simple.c +1 -9
@@ -59,17 +59,9 @@ static const char *board[] __initdata = {NULL }; -/* -* Called very early, MMU is off, device-tree isn't unflattened -*/ -static int __init mpc5200_simple_probe (void ) -{ -return of_device_compatible_match (of_root, board); -} -define_machine (mpc5200_simple_platform) { .name = "mpc5200-simple-platform", - .probe = mpc5200_simple_probe, + .compatibles = board, .setup_arch = mpc5200_simple_setup_arch, .discover_phbs = mpc52xx_setup_pci, .init = mpc52xx_declare_of_platform_devices,
arch/powerpc/platforms/83xx/mpc830x_rdb.c +1 -9
@@ -34,19 +34,11 @@ static const char *board[] __initdata = {NULL }; -/* -* Called very early, MMU is off, device-tree isn't unflattened -*/ -static int __init mpc830x_rdb_probe (void ) -{ -return of_device_compatible_match (of_root, board); -} -machine_device_initcall (mpc830x_rdb, mpc83xx_declare_of_platform_devices);define_machine (mpc830x_rdb) { .name = "MPC830x RDB", - .probe = mpc830x_rdb_probe, + .compatibles = board, .setup_arch = mpc830x_rdb_setup_arch, .discover_phbs = mpc83xx_setup_pci, .init_IRQ = mpc83xx_ipic_init_IRQ,
arch/powerpc/platforms/83xx/mpc831x_rdb.c +1 -9
@@ -34,19 +34,11 @@ static const char *board[] __initdata = {NULL }; -/* -* Called very early, MMU is off, device-tree isn't unflattened -*/ -static int __init mpc831x_rdb_probe (void ) -{ -return of_device_compatible_match (of_root, board); -} -machine_device_initcall (mpc831x_rdb, mpc83xx_declare_of_platform_devices);define_machine (mpc831x_rdb) { .name = "MPC831x RDB", - .probe = mpc831x_rdb_probe, + .compatibles = board, .setup_arch = mpc831x_rdb_setup_arch, .discover_phbs = mpc83xx_setup_pci, .init_IRQ = mpc83xx_ipic_init_IRQ,
arch/powerpc/platforms/83xx/mpc837x_rdb.c +1 -9
@@ -61,17 +61,9 @@ static const char * const board[] __initconst = {NULL }; -/* -* Called very early, MMU is off, device-tree isn't unflattened -*/ -static int __init mpc837x_rdb_probe (void ) -{ -return of_device_compatible_match (of_root, board); -} -define_machine (mpc837x_rdb) { .name = "MPC837x RDB/WLAN", - .probe = mpc837x_rdb_probe, + .compatibles = board, .setup_arch = mpc837x_rdb_setup_arch, .discover_phbs = mpc83xx_setup_pci, .init_IRQ = mpc83xx_ipic_init_IRQ,
arch/powerpc/platforms/85xx/corenet_generic.c +1 -1
@@ -149,7 +149,7 @@ static int __init corenet_generic_probe(void)extern struct smp_ops_t smp_85xx_ops;#endif -if (of_device_compatible_match (of_root, boards)) +if (of_machine_compatible_match (boards))return 1 ;/* Check if we're running under the Freescale hypervisor */
arch/powerpc/platforms/85xx/tqm85xx.c +1 -9
@@ -112,17 +112,9 @@ static const char * const board[] __initconst = {NULL }; -/* -* Called very early, device-tree isn't unflattened -*/ -static int __init tqm85xx_probe (void ) -{ -return of_device_compatible_match (of_root, board); -} -define_machine (tqm85xx) { .name = "TQM85xx", - .probe = tqm85xx_probe, + .compatibles = board, .setup_arch = tqm85xx_setup_arch, .init_IRQ = tqm85xx_pic_init, .show_cpuinfo = tqm85xx_show_cpuinfo,
Signature · 1 line
2.43.0
Michael Ellerman
reviewerPATCH v2 5/5 powerpc: Stop using of_root
In reply to Michael EllermanFrom: Christophe Leroy <[email protected]>
Replace all usages of of_root by of_find_node_by_path("/")
--- arch/powerpc/kernel/secure_boot.c | 8 ++++++-- arch/powerpc/kexec/ranges.c | 8 +++++--- arch/powerpc/mm/drmem.c | 10 +++++----- arch/powerpc/mm/numa.c | 6 ++++-- arch/powerpc/platforms/52xx/efika.c | 4 +++- arch/powerpc/platforms/pasemi/pci.c | 4 +++- arch/powerpc/platforms/pseries/lparcfg.c | 6 +++++- arch/powerpc/platforms/pseries/setup.c | 12 +++++++++--- 8 files changed, 40 insertions(+), 18 deletions(-)
v2: Unchanged.
arch/powerpc/kernel/secure_boot.c +6 -2
@@ -32,8 +32,10 @@ bool is_ppc_secureboot_enabled(void)if (enabled) goto out; -if (!of_property_read_u32 (of_root,"ibm,secure-boot" , &secureboot)) + node =of_find_node_by_path ("/" ); +if (!of_property_read_u32 (node,"ibm,secure-boot" , &secureboot)) enabled = (secureboot >1 ); +of_node_put (node); out:pr_info ("Secure boot mode %s\n" , enabled ?"enabled" :"disabled" );
@@ -54,8 +56,10 @@ bool is_ppc_trustedboot_enabled(void)if (enabled) goto out; -if (!of_property_read_u32 (of_root,"ibm,trusted-boot" , &trustedboot)) + node =of_find_node_by_path ("/" ); +if (!of_property_read_u32 (node,"ibm,trusted-boot" , &trustedboot)) enabled = (trustedboot >0 ); +of_node_put (node); out:pr_info ("Trusted boot mode %s\n" , enabled ?"enabled" :"disabled" );
arch/powerpc/kexec/ranges.c +5 -3
@@ -385,14 +385,16 @@ int add_opal_mem_range(struct crash_mem **mem_ranges)int add_reserved_mem_ranges (struct crash_mem **mem_ranges) {int n_mem_addr_cells, n_mem_size_cells, i, len, cells, ret =0 ; +struct device_node *root =of_find_node_by_path ("/" );const __be32 *prop; - prop =of_get_property (of_root,"reserved-ranges" , &len); + prop =of_get_property (root,"reserved-ranges" , &len); + n_mem_addr_cells =of_n_addr_cells (root); + n_mem_size_cells =of_n_size_cells (root); +of_node_put (root);if (!prop)return 0 ; - n_mem_addr_cells =of_n_addr_cells (of_root); - n_mem_size_cells =of_n_size_cells (of_root); cells = n_mem_addr_cells + n_mem_size_cells;/* Each reserved range is an (address,size) pair */
arch/powerpc/mm/drmem.c +5 -5
@@ -393,17 +393,17 @@ static const __be32 *of_get_usable_memory(struct device_node *dn)int walk_drmem_lmbs (struct device_node *dn,void *data,int (*func)(struct drmem_lmb *,const __be32 **,void *)) { +struct device_node *root =of_find_node_by_path ("/" );const __be32 *prop, *usm;int ret = -ENODEV ; -if (!of_root) +if (!root)return ret;/* Get the address & size cells */ -of_node_get (of_root); - n_root_addr_cells =of_n_addr_cells (of_root); - n_root_size_cells =of_n_size_cells (of_root); -of_node_put (of_root); + n_root_addr_cells =of_n_addr_cells (root); + n_root_size_cells =of_n_size_cells (root); +of_node_put (root);if (init_drmem_lmb_size (dn))return ret;
arch/powerpc/mm/numa.c +4 -2
@@ -1111,7 +1111,7 @@ static void __init setup_node_data(int nid, u64 start_pfn, u64 end_pfn)static void __init find_possible_nodes (void ) { -struct device_node *rtas; +struct device_node *rtas, *root;const __be32 *domains =NULL ;int prop_length, max_nodes;u32 i;
@@ -1132,10 +1132,12 @@ static void __init find_possible_nodes(void)* If the LPAR is migratable, new nodes might be activated after a LPM, * so we should consider the max number in that case. */ -if (!of_get_property (of_root,"ibm,migratable-partition" ,NULL )) + root =of_find_node_by_path ("/" ); +if (!of_get_property (root,"ibm,migratable-partition" ,NULL )) domains =of_get_property (rtas,"ibm,current-associativity-domains" , &prop_length); +of_node_put (root);if (!domains) { domains =of_get_property (rtas,"ibm,max-associativity-domains" , &prop_length);
arch/powerpc/platforms/52xx/efika.c +3 -1
@@ -195,8 +195,10 @@ static void __init efika_setup_arch(void)static int __init efika_probe (void ) { -const char *model =of_get_property (of_root,"model" ,NULL ); +struct device_node *root =of_find_node_by_path ("/" ); +const char *model =of_get_property (root,"model" ,NULL ); +of_node_put (root);if (model ==NULL )return 0 ;if (strcmp (model,"EFIKA5K2" ))
arch/powerpc/platforms/pasemi/pci.c +3 -1
@@ -270,16 +270,18 @@ static int __init pas_add_bridge(struct device_node *dev)void __init pas_pci_init (void ) { +struct device_node *root =of_find_node_by_path ("/" );struct device_node *np;int res;pci_set_flags (PCI_SCAN_ALL_PCIE_DEVS ); - np =of_find_compatible_node (of_root,NULL ,"pasemi,rootbus" ); + np =of_find_compatible_node (root,NULL ,"pasemi,rootbus" );if (np) { res =pas_add_bridge (np);of_node_put (np); } +of_node_put (root); }void __iomem *__init pasemi_pci_getcfgaddr (struct pci_dev *dev,int offset)
arch/powerpc/platforms/pseries/lparcfg.c +5 -1
@@ -346,9 +346,13 @@ static int read_rtas_lpar_name(struct seq_file *m)*/ static int read_dt_lpar_name (struct seq_file *m) { +struct device_node *root =of_find_node_by_path ("/" );const char *name; +int ret; -if (of_property_read_string (of_root,"ibm,partition-name" , &name)) + ret =of_property_read_string (root,"ibm,partition-name" , &name); +of_node_put (root); +if (ret)return -ENOENT ;seq_printf (m,"partition_name=%s\n" , name);
arch/powerpc/platforms/pseries/setup.c +9 -3
@@ -1029,9 +1029,11 @@ static void __init pseries_add_hw_description(void)return ; } -if (of_property_read_bool (of_root,"ibm,powervm-partition" ) || -of_property_read_bool (of_root,"ibm,fw-net-version" )) + dn =of_find_node_by_path ("/" ); +if (of_property_read_bool (dn,"ibm,powervm-partition" ) || +of_property_read_bool (dn,"ibm,fw-net-version" ))seq_buf_printf (&ppc_hw_desc,"hv:phyp " ); +of_node_put (dn); }/*
@@ -1091,7 +1093,11 @@ static void pseries_power_off(void)static int __init pSeries_probe (void ) { -if (!of_node_is_type (of_root,"chrp" )) +struct device_node *root =of_find_node_by_path ("/" ); +bool ret =of_node_is_type (root,"chrp" ); + +of_node_put (root); +if (!ret)return 0 ;/* Cell blades firmware claims to be chrp while it's not. Until this
Signature · 1 line
2.43.0
Michael Ellerman
AppliedQuoted 10 lines · Michael Ellerman
We have
of_machine_is_compatible()to check if a machine is compatible with a single compatible string. However some code is able to support multiple compatible boards, and so wants to check for one of many compatible strings.So add
of_machine_compatible_match()which takes a NULL terminated array of compatible strings to check against the root node's compatible property.[...]
Applied to powerpc/next.
[1/5] of: Add of_machine_compatible_match()
https://git.kernel.org/powerpc/c/c029b22f8a98e14988f800d5c0176a9eaec3c8db [2/5] of: Change of_machine_is_compatible() to return bool https://git.kernel.org/powerpc/c/cefdb366dcbe97908b6055595a15bf7689556bf8 [3/5] of: Reimplement of_machine_is_compatible() using of_machine_compatible_match() https://git.kernel.org/powerpc/c/1ac8205f907517a306b661212496fedce79d7cc5 [4/5] powerpc/machdep: Define 'compatibles' property in ppc_md and use it https://git.kernel.org/powerpc/c/28da734d58c8d0113d0ac4f59880d94c9f249564 [5/5] powerpc: Stop using of_root https://git.kernel.org/powerpc/c/2a066ae11861257223500d7515e1541199cb7832
cheers