Skip to contentSkip to conversation

soc/tegra: Various PMC fixes

PATCH Applied Jon Hunter 2 participants 12 messages Jun 28, 2016 – Jun 30, 2016 Tegra architecture support

lore
Share & tools
lore

Jon Hunter

author maintainer
#0 lore raw

cover soc/tegra: Various PMC fixes

A few fixes for the Tegra PMC driver.

Jon Hunter (6):
soc/tegra: pmc: Ensure powergate is available when powering on soc/tegra: pmc: Fix early initialisation of PMC
soc/tegra: pmc: Don't populate soc data until register space is mapped soc/tegra: pmc: Ensure mutex is always initialised soc/tegra: pmc: Add missing of_node_put
soc/tegra: pmc: Don't probe pmc if early initialisation fails

 drivers/soc/tegra/pmc.c | 51 ++++++++++++++++++++++++++++++++-----------------
 1 file changed, 33 insertions(+), 18 deletions(-)
Signature · 1 line
2.1.4
author maintainer
#1 lore raw

PATCH 1/6 soc/tegra: pmc: Ensure powergate is available when powering on

The function tegra_power_sequence_power_up() is a public function used to power on a partition. When this function is called, we do not check to see if the partition being powered up is valid/available. Fix this by checking to see that the partition is valid/available.

---
 drivers/soc/tegra/pmc.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/soc/tegra/pmc.c +3 -0
index 8a421a0b1ece..52a9e9703668 100644
@@ -549,6 +549,9 @@ int tegra_powergate_sequence_power_up(unsigned int id, struct clk *clk,
 	struct tegra_powergate pg;
 	int err;
 
+	if (!tegra_powergate_is_available(id))
+		return -EINVAL;
+
 	pg.id = id;
 	pg.clks = &clk;
 	pg.num_clks = 1;
Signature · 1 line
2.1.4
author maintainer
#2 lore raw

PATCH 2/6 soc/tegra: pmc: Fix early initialisation of PMC

In reply to Jon Hunter

During early initialisation, the available power partitions for a given device is configured as well as the polarity of the PMC interrupt. Both of which should only be configured if there is a valid device node for the PMC device. This is because the soc data used for configuring the power partitions is only available if a device node for the PMC is found and the code to configure the interrupt polarity uses the device node pointer directly.

Some early device-tree images may not have this device node and so fix this by ensuring the device node pointer is valid when configuring these items.

---
 drivers/soc/tegra/pmc.c | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)
drivers/soc/tegra/pmc.c +18 -16
index 52a9e9703668..2e031c4ad547 100644
@@ -1550,27 +1550,29 @@ static int __init tegra_pmc_early_init(void)
 		return -ENXIO;
 	}
 
-	/* Create a bit-map of the available and valid partitions */
-	for (i = 0; i < pmc->soc->num_powergates; i++)
-		if (pmc->soc->powergates[i])
-			set_bit(i, pmc->powergates_available);
-
 	mutex_init(&pmc->powergates_lock);
 
-	/*
-	 * Invert the interrupt polarity if a PMC device tree node exists and
-	 * contains the nvidia,invert-interrupt property.
-	 */
-	invert = of_property_read_bool(np, "nvidia,invert-interrupt");
+	if (np) {
+		/* Create a bit-map of the available and valid partitions */
+		for (i = 0; i < pmc->soc->num_powergates; i++)
+			if (pmc->soc->powergates[i])
+				set_bit(i, pmc->powergates_available);
 
-	value = tegra_pmc_readl(PMC_CNTRL);
+		/*
+		 * Invert the interrupt polarity if a PMC device tree node
+		 * exists and contains the nvidia,invert-interrupt property.
+		 */
+		invert = of_property_read_bool(np, "nvidia,invert-interrupt");
 
-	if (invert)
-		value |= PMC_CNTRL_INTR_POLARITY;
-	else
-		value &= ~PMC_CNTRL_INTR_POLARITY;
+		value = tegra_pmc_readl(PMC_CNTRL);
 
-	tegra_pmc_writel(value, PMC_CNTRL);
+		if (invert)
+			value |= PMC_CNTRL_INTR_POLARITY;
+		else
+			value &= ~PMC_CNTRL_INTR_POLARITY;
+
+		tegra_pmc_writel(value, PMC_CNTRL);
+	}
 
 	return 0;
 }
Signature · 1 line
2.1.4
author maintainer
#3 lore raw

Re: [PATCH 2/6] soc/tegra: pmc: Fix early initialisation of PMC

Quoted 57 lines · Jon Hunter

During early initialisation, the available power partitions for a given device is configured as well as the polarity of the PMC interrupt. Both of which should only be configured if there is a valid device node for the PMC device. This is because the soc data used for configuring the power partitions is only available if a device node for the PMC is found and the code to configure the interrupt polarity uses the device node pointer directly.

Some early device-tree images may not have this device node and so fix this by ensuring the device node pointer is valid when configuring these items.

---
 drivers/soc/tegra/pmc.c | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)
drivers/soc/tegra/pmc.c +11 -16
index 52a9e9703668..2e031c4ad547 100644
@@ -1550,27 +1550,29 @@ static int __init tegra_pmc_early_init(void)
 		return -ENXIO;
 	}
 
-	/* Create a bit-map of the available and valid partitions */
-	for (i = 0; i < pmc->soc->num_powergates; i++)
-		if (pmc->soc->powergates[i])
-			set_bit(i, pmc->powergates_available);
-
 	mutex_init(&pmc->powergates_lock);
 
-	/*
-	 * Invert the interrupt polarity if a PMC device tree node exists and
-	 * contains the nvidia,invert-interrupt property.
-	 */
-	invert = of_property_read_bool(np, "nvidia,invert-interrupt");
+	if (np) {
+		/* Create a bit-map of the available and valid partitions */
+		for (i = 0; i < pmc->soc->num_powergates; i++)
+			if (pmc->soc->powergates[i])
+				set_bit(i, pmc->powergates_available);
 
-	value = tegra_pmc_readl(PMC_CNTRL);
+		/*
+		 * Invert the interrupt polarity if a PMC device tree node
+		 * exists and contains the nvidia,invert-interrupt property.
+		 */
+		invert = of_property_read_bool(np, "nvidia,invert-interrupt");
 
-	if (invert)
-		value |= PMC_CNTRL_INTR_POLARITY;
-	else
-		value &= ~PMC_CNTRL_INTR_POLARITY;
+		value = tegra_pmc_readl(PMC_CNTRL);
 
-	tegra_pmc_writel(value, PMC_CNTRL);

drivers/soc/tegra/pmc.c static int __init tegra_pmc_early_init(void) · line 1578

Quoted 10 lines
drivers/soc/tegra/pmc.c +7 -0
+		if (invert)
+			value |= PMC_CNTRL_INTR_POLARITY;
+		else
+			value &= ~PMC_CNTRL_INTR_POLARITY;
+
+		tegra_pmc_writel(value, PMC_CNTRL);
+	}
 
 	return 0;
 }

By the way, the more I think about this, if there is no valid 'pmc' node in the device-tree blob, then I wonder if we should even bother mapping the pmc address space at all? The reason being, if there is no 'pmc' node then we cannot look-up the SoC data and so all the public PMC APIs are pretty useless AFAICT. I wonder if we should do something like ...

drivers/soc/tegra/pmc.c +7 -32
index e09c7ed385f6..34d1385d7ef0 100644
@@ -1510,7 +1510,6 @@ static int __init tegra_pmc_early_init(void)
 {
        const struct of_device_id *match;
        struct device_node *np;
-       struct resource regs;
        unsigned int i;
        bool invert;
        u32 value;
@@ -1520,73 +1519,47 @@ static int __init tegra_pmc_early_init(void)
        np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
        if (!np) {
                /*
-                * Fall back to legacy initialization for 32-bit ARM only. All
-                * 64-bit ARM device tree files for Tegra are required to have
-                * a PMC node.
-                *
-                * This is for backwards-compatibility with old device trees
-                * that didn't contain a PMC node. Note that in this case the
-                * SoC data can't be matched and therefore powergating is
-                * disabled.
+                * All 64-bit ARM device tree files for Tegra are required to
+                * have a PMC node. For old 32-bit Tegra device trees that
+                * don't contain a PMC node, the SoC data can't be matched
+                * and therefore powergating is disabled.
                 */
-               if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
+               if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra())
                        pr_warn("DT node not found, powergating disabled\n");
 
-                       regs.start = 0x7000e400;
-                       regs.end = 0x7000e7ff;
-                       regs.flags = IORESOURCE_MEM;
-
-                       pr_warn("Using memory region %pR\n", &regs);
-               } else {
-                       /*
-                        * At this point we're not running on Tegra, so play
-                        * nice with multi-platform kernels.
-                        */
-                       return 0;
-               }
-       } else {
-               /*
-                * Extract information from the device tree if we've found a
-                * matching node.
-                */
-               if (of_address_to_resource(np, 0, &regs) < 0) {
-                       pr_err("failed to get PMC registers\n");
-                       return -ENXIO;
-               }
+               return 0;
        }
 
-       pmc->base = ioremap_nocache(regs.start, resource_size(&regs));
+       pmc->base = of_iomap(np, 0);
        if (!pmc->base) {
                pr_err("failed to map PMC registers\n");
                of_node_put(np);
                return -ENXIO;
        }
 

Cheers
Jon

Signature · 1 line
nvpublic
#4 lore raw

Re: [PATCH 2/6] soc/tegra: pmc: Fix early initialisation of PMC

Quoted 67 lines · Jon Hunter
Quoted 65 lines · Jon Hunter

During early initialisation, the available power partitions for a given device is configured as well as the polarity of the PMC interrupt. Both of which should only be configured if there is a valid device node for the PMC device. This is because the soc data used for configuring the power partitions is only available if a device node for the PMC is found and the code to configure the interrupt polarity uses the device node pointer directly.

Some early device-tree images may not have this device node and so fix this by ensuring the device node pointer is valid when configuring these items.

---
 drivers/soc/tegra/pmc.c | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)
drivers/soc/tegra/pmc.c +18 -16
index 52a9e9703668..2e031c4ad547 100644
@@ -1550,27 +1550,29 @@ static int __init tegra_pmc_early_init(void)
 		return -ENXIO;
 	}
 
-	/* Create a bit-map of the available and valid partitions */
-	for (i = 0; i < pmc->soc->num_powergates; i++)
-		if (pmc->soc->powergates[i])
-			set_bit(i, pmc->powergates_available);
-
 	mutex_init(&pmc->powergates_lock);
 
-	/*
-	 * Invert the interrupt polarity if a PMC device tree node exists and
-	 * contains the nvidia,invert-interrupt property.
-	 */
-	invert = of_property_read_bool(np, "nvidia,invert-interrupt");
+	if (np) {
+		/* Create a bit-map of the available and valid partitions */
+		for (i = 0; i < pmc->soc->num_powergates; i++)
+			if (pmc->soc->powergates[i])
+				set_bit(i, pmc->powergates_available);
 
-	value = tegra_pmc_readl(PMC_CNTRL);
+		/*
+		 * Invert the interrupt polarity if a PMC device tree node
+		 * exists and contains the nvidia,invert-interrupt property.
+		 */
+		invert = of_property_read_bool(np, "nvidia,invert-interrupt");
 
-	if (invert)
-		value |= PMC_CNTRL_INTR_POLARITY;
-	else
-		value &= ~PMC_CNTRL_INTR_POLARITY;
+		value = tegra_pmc_readl(PMC_CNTRL);
 
-	tegra_pmc_writel(value, PMC_CNTRL);
+		if (invert)
+			value |= PMC_CNTRL_INTR_POLARITY;
+		else
+			value &= ~PMC_CNTRL_INTR_POLARITY;
+
+		tegra_pmc_writel(value, PMC_CNTRL);
+	}
 
Quoted 9 lines

drivers/soc/tegra/pmc.c static int __init tegra_pmc_early_init(void) · line 1578

Quoted 2 lines
drivers/soc/tegra/pmc.c +0 -0
 	return 0;
 }

By the way, the more I think about this, if there is no valid 'pmc' node in the device-tree blob, then I wonder if we should even bother mapping the pmc address space at all? The reason being, if there is no 'pmc' node then we cannot look-up the SoC data and so all the public PMC APIs are pretty useless AFAICT. I wonder if we should do something like ...

I think it's fine as-is. The PMC driver does more than just powergates and it'd be useful to have the rest continue to work even on old DTBs. Everything should already be properly guarded against this exception.

Thierry

author maintainer
#5 lore raw

PATCH 3/6 soc/tegra: pmc: Don't populate soc data until register space is mapped

In reply to Jon Hunter

The public functions exported by the PMC driver use the presence of the soc data pointer to determine if the PMC device is configured and the registers can be accessed. However, the soc data is populated before the PMC register space is mapped and this opens a window where the soc data pointer is valid but the register space has not yet been mapped which could lead to a crash. Furthermore, if the mapping of the PMC register space fails, then the soc data pointer is not cleared and so would expose a larger window where a crash could occur.

Fix this by initialising the soc data pointer after the PMC register space has been mapped.

---
 drivers/soc/tegra/pmc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/soc/tegra/pmc.c +2 -2
index 2e031c4ad547..ed2b2c83e4eb 100644
@@ -1540,8 +1540,6 @@ static int __init tegra_pmc_early_init(void)
 			pr_err("failed to get PMC registers\n");
 			return -ENXIO;
 		}
-
-		pmc->soc = match->data;
 	}
 
 	pmc->base = ioremap_nocache(regs.start, resource_size(&regs));
@@ -1553,6 +1551,8 @@ static int __init tegra_pmc_early_init(void)
 	mutex_init(&pmc->powergates_lock);
 
 	if (np) {
+		pmc->soc = match->data;
+
 		/* Create a bit-map of the available and valid partitions */
 		for (i = 0; i < pmc->soc->num_powergates; i++)
 			if (pmc->soc->powergates[i])
Signature · 1 line
2.1.4
author maintainer
#6 lore raw

PATCH 4/6 soc/tegra: pmc: Ensure mutex is always initialised

In reply to Jon Hunter

The mutex used by the PMC driver may not be initialised if early initialisation of the driver fails. If this does happen, then it could be possible for callers of the public PMC functions to still attempt to acquire the mutex. Fix this by initialising the mutex as soon as possible to ensure it will always be initialised.

---
 drivers/soc/tegra/pmc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/soc/tegra/pmc.c +2 -2
index ed2b2c83e4eb..483d54623ec5 100644
@@ -1504,6 +1504,8 @@ static int __init tegra_pmc_early_init(void)
 	bool invert;
 	u32 value;
 
+	mutex_init(&pmc->powergates_lock);
+
 	np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
 	if (!np) {
 		/*
@@ -1548,8 +1550,6 @@ static int __init tegra_pmc_early_init(void)
 		return -ENXIO;
 	}
 
-	mutex_init(&pmc->powergates_lock);
-
 	if (np) {
 		pmc->soc = match->data;
 
Signature · 1 line
2.1.4
author maintainer
#7 lore raw

PATCH 5/6 soc/tegra: pmc: Add missing of_node_put

In reply to Jon Hunter

Add missing of_node_put() in PMC early initialisation function.

---
 drivers/soc/tegra/pmc.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/soc/tegra/pmc.c +2 -0
index 483d54623ec5..e62acaef140a 100644
@@ -1572,6 +1572,8 @@ static int __init tegra_pmc_early_init(void)
 			value &= ~PMC_CNTRL_INTR_POLARITY;
 
 		tegra_pmc_writel(value, PMC_CNTRL);
+
+		of_node_put(np);
 	}
 
 	return 0;
Signature · 1 line
2.1.4
author maintainer
#8 lore raw

Re: [PATCH 5/6] soc/tegra: pmc: Add missing of_node_put

Quoted 12 lines · Jon Hunter

Add missing of_node_put() in PMC early initialisation function.

---
 drivers/soc/tegra/pmc.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/soc/tegra/pmc.c +0 -0
index 483d54623ec5..e62acaef140a 100644

drivers/soc/tegra/pmc.c static int __init tegra_pmc_early_init(void) · line 1579

Quoted 8 lines
drivers/soc/tegra/pmc.c +2 -0
@@ -1572,6 +1572,8 @@ static int __init tegra_pmc_early_init(void)
 			value &= ~PMC_CNTRL_INTR_POLARITY;
 
 		tegra_pmc_writel(value, PMC_CNTRL);
+
+		of_node_put(np);
 	}
 
 	return 0;

Actually there are a couple other places we should put the node ...

drivers/soc/tegra/pmc.c +4 -0
index 483d54623ec5..48e1de2f7aeb 100644
@@ -1540,6 +1540,7 @@ static int __init tegra_pmc_early_init(void)
                 */
                if (of_address_to_resource(np, 0, &regs) < 0) {
                        pr_err("failed to get PMC registers\n");
+                       of_node_put(np);
                        return -ENXIO;
                }
        }
@@ -1547,6 +1548,7 @@ static int __init tegra_pmc_early_init(void)
        pmc->base = ioremap_nocache(regs.start, resource_size(&regs));
        if (!pmc->base) {
                pr_err("failed to map PMC registers\n");
+               of_node_put(np);
                return -ENXIO;
        }
 
@@ -1572,6 +1574,8 @@ static int __init tegra_pmc_early_init(void)
                        value &= ~PMC_CNTRL_INTR_POLARITY;
 
                tegra_pmc_writel(value, PMC_CNTRL);
+
+               of_node_put(np);
        }
 
        return 0;
Signature · 1 line
nvpublic
author maintainer
#9 lore raw

PATCH 6/6 soc/tegra: pmc: Don't probe pmc if early initialisation fails

In reply to Jon Hunter

Commit 0259f522e04f ('soc/tegra: pmc: Restore base address on probe failure') fixes an issue where the PMC base address pointer is not restored on probe failure. However, this fix creates another problem where if early initialisation of the PMC driver fails and an initial mapping for the PMC address space is not created, then when the PMC device is probed, the PMC base address pointer will not be valid and this will cause a crash when tegra_pmc_init() is called and attempts to access a register.

Although the PMC address space is mapped a 2nd time during the probe and so this could be fixed by populating the base address pointer earlier during the probe, this adds more complexity to the code. Moreover, the PMC probe also assumes the the soc data pointer is also initialised when the device is probed and if not will also lead to a crash when calling tegra_pmc_init_tsense_reset(). Given that if the early initialisation does fail then something bad has happen, it seems acceptable to allow the PMC device probe to fail as well. Therefore, if the PMC base address pointer or soc data pointer are not valid when probing the PMC device, WARN and return an error.

Fixes 0259f522e04f ('soc/tegra: pmc: Restore base address on probe failure')

---

I am not too happy with this. The more I was looking at the transition from the early init code to the driver probe the more holes I saw. I tried to see if I could handle this better in the probe itself, but it was adding a lot more code to the probe. So this was a simple way to at least make it more robust.

If this approach is not acceptable, then how about if we simplify matters in the probe, and if the probe fails then set the 'pmc->base' and 'pmc->soc' pointers to NULL and not worry about restoring the initial base address? Yes it may prevent users from controlling the powergates, but if the probe fails then there are probably other problems and so may be we don't care.

 drivers/soc/tegra/pmc.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/soc/tegra/pmc.c +8 -0
index e62acaef140a..d5635db78b61 100644
@@ -1228,6 +1228,14 @@ static int tegra_pmc_probe(struct platform_device *pdev)
 	struct resource *res;
 	int err;
 
+	/*
+	 * Early initialisation should have configured an initial
+	 * register mapping and setup the soc data pointer. If these
+	 * are not valid then something went badly wrong!
+	 */
+	if (WARN_ON(!pmc->base || !pmc->soc))
+		return -ENODEV;
+
 	err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
 	if (err < 0)
 		return err;
Signature · 1 line
2.1.4
author maintainer
#10 lore raw
In reply to Jon Hunter
Quoted 10 lines · Jon Hunter

A few fixes for the Tegra PMC driver.

Jon Hunter (6):
soc/tegra: pmc: Ensure powergate is available when powering on soc/tegra: pmc: Fix early initialisation of PMC
soc/tegra: pmc: Don't populate soc data until register space is mapped soc/tegra: pmc: Ensure mutex is always initialised soc/tegra: pmc: Add missing of_node_put
soc/tegra: pmc: Don't probe pmc if early initialisation fails

Quoted 2 lines
 drivers/soc/tegra/pmc.c | 51 ++++++++++++++++++++++++++++++++-----------------
 1 file changed, 33 insertions(+), 18 deletions(-)

Forgot to mentioned this is based upon the DPAUX series [0] as that also has a change for the pmc driver.

Cheers
Jon

[0] http://marc.info/?l=linux-tegra&m=146669756307955&w=2

Signature · 1 line
nvpublic
#11 lore raw
In reply to Jon Hunter
Quoted 10 lines · Jon Hunter

A few fixes for the Tegra PMC driver.

Jon Hunter (6):
soc/tegra: pmc: Ensure powergate is available when powering on soc/tegra: pmc: Fix early initialisation of PMC
soc/tegra: pmc: Don't populate soc data until register space is mapped soc/tegra: pmc: Ensure mutex is always initialised soc/tegra: pmc: Add missing of_node_put
soc/tegra: pmc: Don't probe pmc if early initialisation fails

Quoted 2 lines
 drivers/soc/tegra/pmc.c | 51 ++++++++++++++++++++++++++++++++-----------------
 1 file changed, 33 insertions(+), 18 deletions(-)

Applied all patches to for-4.8/soc and squashed the follow-up from 5/6 into 5/6. Thanks.

Thierry