mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 0/3] FIT: match best configuration when multiple are compatible
@ 2023-12-05 11:36 Ahmad Fatoum
  2023-12-05 11:36 ` [PATCH 1/3] of: define macro for maximum of_device_is_compatible return value Ahmad Fatoum
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2023-12-05 11:36 UTC (permalink / raw)
  To: barebox

Currently, we match the first compatible configuration. There may be
multiple matching configurations however and we should continue looking
for a better match if a match didn't achieve maximal score.

Do that by checking score against OF_DEVICE_COMPATIBLE_MAX_SCORE and
continuing the search if unequal.

Ahmad Fatoum (3):
  of: define macro for maximum of_device_is_compatible return value
  of: early exit of_match_node if no better matches are possible
  FIT: match best configuration when multiple are compatible

 common/image-fit.c | 15 ++++++++++++---
 drivers/of/base.c  |  9 +++++++--
 include/of.h       |  4 ++++
 3 files changed, 23 insertions(+), 5 deletions(-)

-- 
2.39.2




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/3] of: define macro for maximum of_device_is_compatible return value
  2023-12-05 11:36 [PATCH 0/3] FIT: match best configuration when multiple are compatible Ahmad Fatoum
@ 2023-12-05 11:36 ` Ahmad Fatoum
  2023-12-05 11:36 ` [PATCH 2/3] of: early exit of_match_node if no better matches are possible Ahmad Fatoum
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2023-12-05 11:36 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

of_device_is_compatible() will return zero on mismatch and a positive
value up to and including (INT_MAX / 2) on match. Knowledge of the upper
bound can be useful to callers, because if inside a loop a maximum score
was achieved, there's usually no point in continuing the search.

Therefore add a macro for INT_MAX / 2 and document it for potential
users.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/of/base.c | 6 ++++--
 include/of.h      | 4 ++++
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 463db1058ae9..8a42fcee1d40 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -546,7 +546,9 @@ struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
 EXPORT_SYMBOL(of_get_cpu_node);
 
 /** Checks if the given "compat" string matches one of the strings in
- * the device's "compatible" property
+ * the device's "compatible" property. Returns 0 on mismatch and a
+ * positive score on match with the maximum being OF_DEVICE_COMPATIBLE_MAX_SCORE,
+ * which is only returned if the first compatible matched.
  */
 int of_device_is_compatible(const struct device_node *device,
 		const char *compat)
@@ -559,7 +561,7 @@ int of_device_is_compatible(const struct device_node *device,
 	for (cp = of_prop_next_string(prop, NULL); cp;
 	     cp = of_prop_next_string(prop, cp), index++) {
 		if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
-			score = INT_MAX/2 - (index << 2);
+			score = OF_DEVICE_COMPATIBLE_MAX_SCORE - (index << 2);
 			break;
 		}
 	}
diff --git a/include/of.h b/include/of.h
index 7d1df462d8cf..19b8e7c038a4 100644
--- a/include/of.h
+++ b/include/of.h
@@ -5,6 +5,7 @@
 #include <fdt.h>
 #include <errno.h>
 #include <linux/types.h>
+#include <linux/limits.h>
 #include <linux/list.h>
 #include <linux/err.h>
 #include <asm/byteorder.h>
@@ -124,6 +125,9 @@ int of_fixup_reserved_memory(struct device_node *node, void *data);
 
 struct cdev;
 
+/* Maximum score returned by of_device_is_compatible() */
+#define OF_DEVICE_COMPATIBLE_MAX_SCORE	(INT_MAX / 2)
+
 #ifdef CONFIG_OFTREE
 extern struct device_node *of_read_file(const char *filename);
 extern struct of_reserve_map *of_get_reserve_map(void);
-- 
2.39.2




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/3] of: early exit of_match_node if no better matches are possible
  2023-12-05 11:36 [PATCH 0/3] FIT: match best configuration when multiple are compatible Ahmad Fatoum
  2023-12-05 11:36 ` [PATCH 1/3] of: define macro for maximum of_device_is_compatible return value Ahmad Fatoum
@ 2023-12-05 11:36 ` Ahmad Fatoum
  2023-12-05 11:36 ` [PATCH 3/3] FIT: match best configuration when multiple are compatible Ahmad Fatoum
  2023-12-13  6:44 ` [PATCH 0/3] " Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2023-12-05 11:36 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

The maximum score that can be returned by of_device_is_compatible is
OF_DEVICE_COMPATIBLE_MAX_SCORE. Once that score is returned, there is no
point in continuing with the string comparisons, so just early exit.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/of/base.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 8a42fcee1d40..b22959dabebf 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -717,6 +717,9 @@ const struct of_device_id *of_match_node(const struct of_device_id *matches,
 		if (score > best_score) {
 			best_match = matches;
 			best_score = score;
+
+			if (score == OF_DEVICE_COMPATIBLE_MAX_SCORE)
+				break;
 		}
 	}
 
-- 
2.39.2




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/3] FIT: match best configuration when multiple are compatible
  2023-12-05 11:36 [PATCH 0/3] FIT: match best configuration when multiple are compatible Ahmad Fatoum
  2023-12-05 11:36 ` [PATCH 1/3] of: define macro for maximum of_device_is_compatible return value Ahmad Fatoum
  2023-12-05 11:36 ` [PATCH 2/3] of: early exit of_match_node if no better matches are possible Ahmad Fatoum
@ 2023-12-05 11:36 ` Ahmad Fatoum
  2023-12-13  6:44 ` [PATCH 0/3] " Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2023-12-05 11:36 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Currently, we match the first compatible configuration. There may be
multiple matching configurations however and we should continue looking
for a better match if a match didn't achieve maximal score.

Do that by checking score against OF_DEVICE_COMPATIBLE_MAX_SCORE and
continuing the search if unequal.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/image-fit.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/common/image-fit.c b/common/image-fit.c
index 5ef5013bd41d..b16752de05bc 100644
--- a/common/image-fit.c
+++ b/common/image-fit.c
@@ -720,6 +720,7 @@ static int fit_find_compatible_unit(struct device_node *conf_node,
 {
 	struct device_node *child = NULL;
 	struct device_node *barebox_root;
+	int best_score = 0;
 	const char *machine;
 	int ret;
 
@@ -732,13 +733,21 @@ static int fit_find_compatible_unit(struct device_node *conf_node,
 		return -ENOENT;
 
 	for_each_child_of_node(conf_node, child) {
-		if (of_device_is_compatible(child, machine)) {
+		int score = of_device_is_compatible(child, machine);
+		if (score > best_score) {
+			best_score = score;
 			*unit = child->name;
-			pr_info("matching unit '%s' found\n", *unit);
-			return 0;
+
+			if (score == OF_DEVICE_COMPATIBLE_MAX_SCORE)
+				break;
 		}
 	}
 
+	if (best_score) {
+		pr_info("matching unit '%s' found\n", *unit);
+		return 0;
+	}
+
 default_unit:
 	pr_info("No match found. Trying default.\n");
 	if (of_property_read_string(conf_node, "default", unit) == 0)
-- 
2.39.2




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 0/3] FIT: match best configuration when multiple are compatible
  2023-12-05 11:36 [PATCH 0/3] FIT: match best configuration when multiple are compatible Ahmad Fatoum
                   ` (2 preceding siblings ...)
  2023-12-05 11:36 ` [PATCH 3/3] FIT: match best configuration when multiple are compatible Ahmad Fatoum
@ 2023-12-13  6:44 ` Sascha Hauer
  3 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2023-12-13  6:44 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Tue, Dec 05, 2023 at 12:36:15PM +0100, Ahmad Fatoum wrote:
> Currently, we match the first compatible configuration. There may be
> multiple matching configurations however and we should continue looking
> for a better match if a match didn't achieve maximal score.
> 
> Do that by checking score against OF_DEVICE_COMPATIBLE_MAX_SCORE and
> continuing the search if unequal.
> 
> Ahmad Fatoum (3):
>   of: define macro for maximum of_device_is_compatible return value
>   of: early exit of_match_node if no better matches are possible
>   FIT: match best configuration when multiple are compatible

Applied, thanks

Sascha

> 
>  common/image-fit.c | 15 ++++++++++++---
>  drivers/of/base.c  |  9 +++++++--
>  include/of.h       |  4 ++++
>  3 files changed, 23 insertions(+), 5 deletions(-)
> 
> -- 
> 2.39.2
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-12-13  6:46 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-05 11:36 [PATCH 0/3] FIT: match best configuration when multiple are compatible Ahmad Fatoum
2023-12-05 11:36 ` [PATCH 1/3] of: define macro for maximum of_device_is_compatible return value Ahmad Fatoum
2023-12-05 11:36 ` [PATCH 2/3] of: early exit of_match_node if no better matches are possible Ahmad Fatoum
2023-12-05 11:36 ` [PATCH 3/3] FIT: match best configuration when multiple are compatible Ahmad Fatoum
2023-12-13  6:44 ` [PATCH 0/3] " Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox