Skip to contentSkip to conversation

of: Fix of_empty_ranges_quirk()

PATCH In review [email protected] 2 participants 5 messages Sep 7, 2019 – Sep 14, 2019 Device tree

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 of: Fix of_empty_ranges_quirk()

From: Marek Vasut <[email protected]>

The of_empty_ranges_quirk() returns a mix of boolean and signed integer types, which cannot work well. Replace that with boolean only and fix usage logic in of_translate_one() -- the check should trigger when the ranges are NULL and the quirk is applicable on the hardware.

To: [email protected]
---
V2: - Rename of_empty_ranges_quirk() to of_missing_ranges_is_ok()
- Move comment into the of_missing_ranges_is_ok() function
- Make of_missing_ranges_is_ok() a bit more readable by adding a variable marking the quirk_state as initialized.
- Reinstate check for !of_missing_ranges_is_ok() in of_translate_one()

---
 drivers/of/address.c | 47 ++++++++++++++++++++++++--------------------
 1 file changed, 26 insertions(+), 21 deletions(-)
drivers/of/address.c +26 -21
index 978427a9d5e6..df82ef88823f 100644
@@ -473,21 +473,42 @@ static struct of_bus *of_match_bus(struct device_node *np)
 	return NULL;
 }
 
-static int of_empty_ranges_quirk(struct device_node *np)
+static bool of_missing_ranges_is_ok(struct device_node *np)
 {
+	/*
+	 * Normally, an absence of a "ranges" property means we are
+	 * crossing a non-translatable boundary, and thus the addresses
+	 * below the current cannot be converted to CPU physical ones.
+	 * Unfortunately, while this is very clear in the spec, it's not
+	 * what Apple understood, and they do have things like /uni-n or
+	 * /ht nodes with no "ranges" property and a lot of perfectly
+	 * useable mapped devices below them. Thus we treat the absence of
+	 * "ranges" as equivalent to an empty "ranges" property which means
+	 * a 1:1 translation at that level. It's up to the caller not to try
+	 * to translate addresses that aren't supposed to be translated in
+	 * the first place. --BenH.
+	 *
+	 * As far as we know, this damage only exists on Apple machines, so
+	 * This code is only enabled on powerpc.
+	 */
+
 	if (IS_ENABLED(CONFIG_PPC)) {
 		/* To save cycles, we cache the result for global "Mac" setting */
-		static int quirk_state = -1;
+		static int quirk_state_initialized;
+		static bool quirk_state;
 
 		/* PA-SEMI sdc DT bug */
 		if (of_device_is_compatible(np, "1682m-sdc"))
 			return true;
 
 		/* Make quirk cached */
-		if (quirk_state < 0)
+		if (!quirk_state_initialized) {
+			quirk_state_initialized = 1;
 			quirk_state =
 				of_machine_is_compatible("Power Macintosh") ||
 				of_machine_is_compatible("MacRISC");
+		}
+
 		return quirk_state;
 	}
 	return false;
@@ -502,25 +523,9 @@ static int of_translate_one(struct device_node *parent, struct of_bus *bus,
 	int rone;
 	u64 offset = OF_BAD_ADDR;
 
-	/*
-	 * Normally, an absence of a "ranges" property means we are
-	 * crossing a non-translatable boundary, and thus the addresses
-	 * below the current cannot be converted to CPU physical ones.
-	 * Unfortunately, while this is very clear in the spec, it's not
-	 * what Apple understood, and they do have things like /uni-n or
-	 * /ht nodes with no "ranges" property and a lot of perfectly
-	 * useable mapped devices below them. Thus we treat the absence of
-	 * "ranges" as equivalent to an empty "ranges" property which means
-	 * a 1:1 translation at that level. It's up to the caller not to try
-	 * to translate addresses that aren't supposed to be translated in
-	 * the first place. --BenH.
-	 *
-	 * As far as we know, this damage only exists on Apple machines, so
-	 * This code is only enabled on powerpc. --gcl
-	 */
 	ranges = of_get_property(parent, rprop, &rlen);
-	if (ranges == NULL && !of_empty_ranges_quirk(parent)) {
-		pr_debug("no ranges; cannot translate\n");
+	if (ranges == NULL && !of_missing_ranges_is_ok(parent)) {
+		pr_err("no ranges; cannot translate\n");
 		return 1;
 	}
 	if (ranges == NULL || rlen == 0) {
Signature · 1 line
2.23.0.rc1
#1 lore raw
Quoted 39 lines · On Sat, Sep 7, 2019 at 5:15 PM <[email protected]> wrote:

From: Marek Vasut <[email protected]>

The of_empty_ranges_quirk() returns a mix of boolean and signed integer types, which cannot work well. Replace that with boolean only and fix usage logic in of_translate_one() -- the check should trigger when the ranges are NULL and the quirk is applicable on the hardware.

To: [email protected]
---
V2: - Rename of_empty_ranges_quirk() to of_missing_ranges_is_ok()
- Move comment into the of_missing_ranges_is_ok() function
- Make of_missing_ranges_is_ok() a bit more readable by adding a variable marking the quirk_state as initialized.
- Reinstate check for !of_missing_ranges_is_ok() in of_translate_one()

---
 drivers/of/address.c | 47 ++++++++++++++++++++++++--------------------
 1 file changed, 26 insertions(+), 21 deletions(-)
drivers/of/address.c +6 -1
index 978427a9d5e6..df82ef88823f 100644
@@ -473,21 +473,42 @@ static struct of_bus *of_match_bus(struct device_node *np)
        return NULL;
 }
 
-static int of_empty_ranges_quirk(struct device_node *np)
+static bool of_missing_ranges_is_ok(struct device_node *np)
 {
+       /*
+        * Normally, an absence of a "ranges" property means we are
+        * crossing a non-translatable boundary, and thus the addresses
+        * below the current cannot be converted to CPU physical ones.
+        * Unfortunately, while this is very clear in the spec, it's not

drivers/of/address.c static struct of_bus *of_match_bus(struct device_node *np) · line 492

Quoted 10 lines
drivers/of/address.c +10 -0
+        * what Apple understood, and they do have things like /uni-n or
+        * /ht nodes with no "ranges" property and a lot of perfectly
+        * useable mapped devices below them. Thus we treat the absence of
+        * "ranges" as equivalent to an empty "ranges" property which means
+        * a 1:1 translation at that level. It's up to the caller not to try
+        * to translate addresses that aren't supposed to be translated in
+        * the first place. --BenH.
+        *
+        * As far as we know, this damage only exists on Apple machines, so
+        * This code is only enabled on powerpc.

You dropped Grant's name on this. Wouldn't matter too much as we can run 'git blame', but now we have another level to trace back thru.

drivers/of/address.c static struct of_bus *of_match_bus(struct device_node *np) · line 497

Quoted 6 lines
drivers/of/address.c +3 -1
+        */
+
        if (IS_ENABLED(CONFIG_PPC)) {
                /* To save cycles, we cache the result for global "Mac" setting */
-               static int quirk_state = -1;
+               static int quirk_state_initialized;

This can be bool too.

Quoted 49 lines
drivers/of/address.c +7 -20
+               static bool quirk_state;
 
                /* PA-SEMI sdc DT bug */
                if (of_device_is_compatible(np, "1682m-sdc"))
                        return true;
 
                /* Make quirk cached */
-               if (quirk_state < 0)
+               if (!quirk_state_initialized) {
+                       quirk_state_initialized = 1;
                        quirk_state =
                                of_machine_is_compatible("Power Macintosh") ||
                                of_machine_is_compatible("MacRISC");
+               }
+
                return quirk_state;
        }
        return false;
@@ -502,25 +523,9 @@ static int of_translate_one(struct device_node *parent, struct of_bus *bus,
        int rone;
        u64 offset = OF_BAD_ADDR;
 
-       /*
-        * Normally, an absence of a "ranges" property means we are
-        * crossing a non-translatable boundary, and thus the addresses
-        * below the current cannot be converted to CPU physical ones.
-        * Unfortunately, while this is very clear in the spec, it's not
-        * what Apple understood, and they do have things like /uni-n or
-        * /ht nodes with no "ranges" property and a lot of perfectly
-        * useable mapped devices below them. Thus we treat the absence of
-        * "ranges" as equivalent to an empty "ranges" property which means
-        * a 1:1 translation at that level. It's up to the caller not to try
-        * to translate addresses that aren't supposed to be translated in
-        * the first place. --BenH.
-        *
-        * As far as we know, this damage only exists on Apple machines, so
-        * This code is only enabled on powerpc. --gcl
-        */
        ranges = of_get_property(parent, rprop, &rlen);
-       if (ranges == NULL && !of_empty_ranges_quirk(parent)) {
-               pr_debug("no ranges; cannot translate\n");
+       if (ranges == NULL && !of_missing_ranges_is_ok(parent)) {
+               pr_err("no ranges; cannot translate\n");
                return 1;
        }
        if (ranges == NULL || rlen == 0) {
--

2.23.0.rc1

#2 lore raw

On 9/9/19 7:37 PM, Rob Herring wrote:
[...]

Quoted 24 lines
Quoted 24 lines
 drivers/of/address.c | 47 ++++++++++++++++++++++++--------------------
 1 file changed, 26 insertions(+), 21 deletions(-)
drivers/of/address.c +11 -1
index 978427a9d5e6..df82ef88823f 100644
@@ -473,21 +473,42 @@ static struct of_bus *of_match_bus(struct device_node *np)
        return NULL;
 }
 
-static int of_empty_ranges_quirk(struct device_node *np)
+static bool of_missing_ranges_is_ok(struct device_node *np)
 {
+       /*
+        * Normally, an absence of a "ranges" property means we are
+        * crossing a non-translatable boundary, and thus the addresses
+        * below the current cannot be converted to CPU physical ones.
+        * Unfortunately, while this is very clear in the spec, it's not
+        * what Apple understood, and they do have things like /uni-n or
+        * /ht nodes with no "ranges" property and a lot of perfectly
+        * useable mapped devices below them. Thus we treat the absence of
+        * "ranges" as equivalent to an empty "ranges" property which means
+        * a 1:1 translation at that level. It's up to the caller not to try
Quoted 8 lines

drivers/of/address.c static struct of_bus *of_match_bus(struct device_node *np) · line 492

Quoted 5 lines
drivers/of/address.c +5 -0
+        * to translate addresses that aren't supposed to be translated in
+        * the first place. --BenH.
+        *
+        * As far as we know, this damage only exists on Apple machines, so
+        * This code is only enabled on powerpc.

You dropped Grant's name on this. Wouldn't matter too much as we can run 'git blame', but now we have another level to trace back thru.

I think that's what Frank suggested, since he reworded this message before.

Quoted 8 lines

drivers/of/address.c static struct of_bus *of_match_bus(struct device_node *np) · line 497

Quoted 6 lines
drivers/of/address.c +3 -1
+        */
+
        if (IS_ENABLED(CONFIG_PPC)) {
                /* To save cycles, we cache the result for global "Mac" setting */
-               static int quirk_state = -1;
+               static int quirk_state_initialized;

This can be bool too.

Fine

[...]

Signature · 2 lines
Best regards,
Marek Vasut
#3 lore raw
In reply to [email protected]
Quoted 7 lines · On Sat, Sep 7, 2019 at 5:15 PM <[email protected]> wrote:

From: Marek Vasut <[email protected]>

The of_empty_ranges_quirk() returns a mix of boolean and signed integer types, which cannot work well. Replace that with boolean only and fix usage logic in of_translate_one() -- the check should trigger when the ranges are NULL and the quirk is applicable on the hardware.

Just moving to boolean has seemed like weak justification for this churn, but now that I've seen your work on PCI dma-ranges it makes a bit more sense.

We do have a problem that unlike 'ranges', 'dma-ranges' being missing probably needs to be treated as 1:1 translation. I can't really picture a case where dma-ranges would be used with a non-translatable address. Perhaps a module with optional DMA and the DMA is not hooked up. That could be expressed with dma-ranges with a 0 size or a different compatible. So your v1 patch was perhaps correct change in behavior, but only for dma-ranges. (I've written one that works in both cases).

Rob

#4 lore raw
Quoted 9 lines · Rob Herring
Quoted 4 lines

The of_empty_ranges_quirk() returns a mix of boolean and signed integer types, which cannot work well. Replace that with boolean only and fix usage logic in of_translate_one() -- the check should trigger when the ranges are NULL and the quirk is applicable on the hardware.

Just moving to boolean has seemed like weak justification for this churn, but now that I've seen your work on PCI dma-ranges it makes a bit more sense.

Quoted 8 lines

We do have a problem that unlike 'ranges', 'dma-ranges' being missing probably needs to be treated as 1:1 translation. I can't really picture a case where dma-ranges would be used with a non-translatable address. Perhaps a module with optional DMA and the DMA is not hooked up. That could be expressed with dma-ranges with a 0 size or a different compatible. So your v1 patch was perhaps correct change in behavior, but only for dma-ranges. (I've written one that works in both cases).

Is that posted somewhere ?

The controller node /soc/pcie@fe000000/ already has dma-ranges [1] though, so I wonder if it's the PCIe translation patches which need to be updated then ?

[1]
https://elixir.bootlin.com/linux/v5.3-rc8/source/arch/arm64/boot/dts/renesas/r8a7795.dtsi#L2653

Signature · 2 lines
Best regards,
Marek Vasut