* [PATCH 1/2] of/unittest: Add test that of_address_to_resource() fails on non-translatable address
@ 2025-01-10 21:50 Rob Herring (Arm)
2025-01-10 21:50 ` [PATCH 2/2] of/address: Fix WARN when attempting translating non-translatable addresses Rob Herring (Arm)
0 siblings, 1 reply; 3+ messages in thread
From: Rob Herring (Arm) @ 2025-01-10 21:50 UTC (permalink / raw)
To: Michael Ellerman, Saravana Kannan; +Cc: linuxppc-dev, devicetree, linux-kernel
of_address_to_resource() on a non-translatable address should return an
error. Additionally, this case also triggers a spurious WARN for
missing #address-cells/#size-cells.
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
drivers/of/unittest-data/tests-platform.dtsi | 13 +++++++++++++
drivers/of/unittest.c | 14 ++++++++++++++
2 files changed, 27 insertions(+)
diff --git a/drivers/of/unittest-data/tests-platform.dtsi b/drivers/of/unittest-data/tests-platform.dtsi
index fa39611071b3..cd310b26b50c 100644
--- a/drivers/of/unittest-data/tests-platform.dtsi
+++ b/drivers/of/unittest-data/tests-platform.dtsi
@@ -34,5 +34,18 @@ dev@100 {
};
};
};
+
+ platform-tests-2 {
+ // No #address-cells or #size-cells
+ node {
+ #address-cells = <1>;
+ #size-cells = <1>;
+
+ test-device@100 {
+ compatible = "test-sub-device";
+ reg = <0x100 1>;
+ };
+ };
+ };
};
};
diff --git a/drivers/of/unittest.c b/drivers/of/unittest.c
index 80483e38d7b4..34c957add8b9 100644
--- a/drivers/of/unittest.c
+++ b/drivers/of/unittest.c
@@ -1380,6 +1380,7 @@ static void __init of_unittest_bus_3cell_ranges(void)
static void __init of_unittest_reg(void)
{
struct device_node *np;
+ struct resource res;
int ret;
u64 addr, size;
@@ -1396,6 +1397,19 @@ static void __init of_unittest_reg(void)
np, addr);
of_node_put(np);
+
+ np = of_find_node_by_path("/testcase-data/platform-tests-2/node/test-device@100");
+ if (!np) {
+ pr_err("missing testcase data\n");
+ return;
+ }
+
+ ret = of_address_to_resource(np, 0, &res);
+ unittest(ret == -EINVAL, "of_address_to_resource(%pOF) expected error on untranslatable address\n",
+ np);
+
+ of_node_put(np);
+
}
struct of_unittest_expected_res {
--
2.45.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH 2/2] of/address: Fix WARN when attempting translating non-translatable addresses
2025-01-10 21:50 [PATCH 1/2] of/unittest: Add test that of_address_to_resource() fails on non-translatable address Rob Herring (Arm)
@ 2025-01-10 21:50 ` Rob Herring (Arm)
2025-01-10 22:04 ` Sean Anderson
0 siblings, 1 reply; 3+ messages in thread
From: Rob Herring (Arm) @ 2025-01-10 21:50 UTC (permalink / raw)
To: Michael Ellerman, Saravana Kannan; +Cc: linuxppc-dev, devicetree, linux-kernel
The recently added WARN() for deprecated #address-cells and #size-cells
triggered a WARN when of_platform_populate() (which calls
of_address_to_resource()) is used on nodes with non-translatable
addresses. This case is expected to return an error.
Rework the bus matching to allow no match and make the default require
an #address-cells property. That should be safe to do as any platform
missing #address-cells would have a warning already.
Fixes: 045b14ca5c36 ("of: WARN on deprecated #address-cells/#size-cells handling")
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
---
drivers/of/address.c | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/drivers/of/address.c b/drivers/of/address.c
index c1f1c810e810..8770004d9b08 100644
--- a/drivers/of/address.c
+++ b/drivers/of/address.c
@@ -340,6 +340,15 @@ static int of_bus_default_flags_match(struct device_node *np)
return of_property_present(np, "#address-cells") && (of_bus_n_addr_cells(np) == 3);
}
+static int of_bus_default_match(struct device_node *np)
+{
+ /*
+ * Check for presence first since of_bus_n_addr_cells() will warn when
+ * walking parent nodes.
+ */
+ return of_property_present(np, "#address-cells");
+}
+
/*
* Array of bus specific translators
*/
@@ -384,7 +393,7 @@ static const struct of_bus of_busses[] = {
{
.name = "default",
.addresses = "reg",
- .match = NULL,
+ .match = of_bus_default_match,
.count_cells = of_bus_default_count_cells,
.map = of_bus_default_map,
.translate = of_bus_default_translate,
@@ -399,7 +408,6 @@ static const struct of_bus *of_match_bus(struct device_node *np)
for (i = 0; i < ARRAY_SIZE(of_busses); i++)
if (!of_busses[i].match || of_busses[i].match(np))
return &of_busses[i];
- BUG();
return NULL;
}
@@ -521,6 +529,8 @@ static u64 __of_translate_address(struct device_node *node,
if (parent == NULL)
return OF_BAD_ADDR;
bus = of_match_bus(parent);
+ if (!bus)
+ return OF_BAD_ADDR;
/* Count address cells & copy address locally */
bus->count_cells(dev, &na, &ns);
@@ -564,6 +574,8 @@ static u64 __of_translate_address(struct device_node *node,
/* Get new parent bus and counts */
pbus = of_match_bus(parent);
+ if (!pbus)
+ return OF_BAD_ADDR;
pbus->count_cells(dev, &pna, &pns);
if (!OF_CHECK_COUNTS(pna, pns)) {
pr_err("Bad cell count for %pOF\n", dev);
@@ -703,7 +715,7 @@ const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no,
/* match the parent's bus type */
bus = of_match_bus(parent);
- if (strcmp(bus->name, "pci") && (bar_no >= 0))
+ if (!bus || (strcmp(bus->name, "pci") && (bar_no >= 0)))
return NULL;
/* Get "reg" or "assigned-addresses" property */
--
2.45.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH 2/2] of/address: Fix WARN when attempting translating non-translatable addresses
2025-01-10 21:50 ` [PATCH 2/2] of/address: Fix WARN when attempting translating non-translatable addresses Rob Herring (Arm)
@ 2025-01-10 22:04 ` Sean Anderson
0 siblings, 0 replies; 3+ messages in thread
From: Sean Anderson @ 2025-01-10 22:04 UTC (permalink / raw)
To: Rob Herring (Arm), Michael Ellerman, Saravana Kannan
Cc: linuxppc-dev, devicetree, linux-kernel
On 1/10/25 16:50, Rob Herring (Arm) wrote:
> The recently added WARN() for deprecated #address-cells and #size-cells
> triggered a WARN when of_platform_populate() (which calls
> of_address_to_resource()) is used on nodes with non-translatable
> addresses. This case is expected to return an error.
>
> Rework the bus matching to allow no match and make the default require
> an #address-cells property. That should be safe to do as any platform
> missing #address-cells would have a warning already.
>
> Fixes: 045b14ca5c36 ("of: WARN on deprecated #address-cells/#size-cells handling")
> Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
> ---
> drivers/of/address.c | 18 +++++++++++++++---
> 1 file changed, 15 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/of/address.c b/drivers/of/address.c
> index c1f1c810e810..8770004d9b08 100644
> --- a/drivers/of/address.c
> +++ b/drivers/of/address.c
> @@ -340,6 +340,15 @@ static int of_bus_default_flags_match(struct device_node *np)
> return of_property_present(np, "#address-cells") && (of_bus_n_addr_cells(np) == 3);
> }
>
> +static int of_bus_default_match(struct device_node *np)
> +{
> + /*
> + * Check for presence first since of_bus_n_addr_cells() will warn when
> + * walking parent nodes.
> + */
> + return of_property_present(np, "#address-cells");
> +}
> +
> /*
> * Array of bus specific translators
> */
> @@ -384,7 +393,7 @@ static const struct of_bus of_busses[] = {
> {
> .name = "default",
> .addresses = "reg",
> - .match = NULL,
> + .match = of_bus_default_match,
> .count_cells = of_bus_default_count_cells,
> .map = of_bus_default_map,
> .translate = of_bus_default_translate,
> @@ -399,7 +408,6 @@ static const struct of_bus *of_match_bus(struct device_node *np)
> for (i = 0; i < ARRAY_SIZE(of_busses); i++)
> if (!of_busses[i].match || of_busses[i].match(np))
> return &of_busses[i];
> - BUG();
> return NULL;
> }
>
> @@ -521,6 +529,8 @@ static u64 __of_translate_address(struct device_node *node,
> if (parent == NULL)
> return OF_BAD_ADDR;
> bus = of_match_bus(parent);
> + if (!bus)
> + return OF_BAD_ADDR;
>
> /* Count address cells & copy address locally */
> bus->count_cells(dev, &na, &ns);
> @@ -564,6 +574,8 @@ static u64 __of_translate_address(struct device_node *node,
>
> /* Get new parent bus and counts */
> pbus = of_match_bus(parent);
> + if (!pbus)
> + return OF_BAD_ADDR;
> pbus->count_cells(dev, &pna, &pns);
> if (!OF_CHECK_COUNTS(pna, pns)) {
> pr_err("Bad cell count for %pOF\n", dev);
> @@ -703,7 +715,7 @@ const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no,
>
> /* match the parent's bus type */
> bus = of_match_bus(parent);
> - if (strcmp(bus->name, "pci") && (bar_no >= 0))
> + if (!bus || (strcmp(bus->name, "pci") && (bar_no >= 0)))
> return NULL;
>
> /* Get "reg" or "assigned-addresses" property */
Tested-by: Sean Anderson <sean.anderson@linux.dev>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-01-10 22:06 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-10 21:50 [PATCH 1/2] of/unittest: Add test that of_address_to_resource() fails on non-translatable address Rob Herring (Arm)
2025-01-10 21:50 ` [PATCH 2/2] of/address: Fix WARN when attempting translating non-translatable addresses Rob Herring (Arm)
2025-01-10 22:04 ` Sean Anderson
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).