Skip to contentSkip to conversation

ARM: tegra: Fix warm restarts on Tegra20

PATCH New Artur Kowalski 2 participants 7 messages Nov 28

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover ARM: tegra: Fix warm restarts on Tegra20

On Tegra20 boards with a TPS6586x PMIC, every restart is a cold restart:
the PMIC restart handler has a higher priority than the PMC one and power cycles the whole board, regardless of the configured reboot mode. That includes RAM, which makes pstore unreliable, and was the main motivation for this series. Testing has also shown that warm reboots are more reliable on my TF101: on cold reboots the board tends to just power off, which breaks automated kernel build/deploy/reboot loops, while with warm reboots (and the latest U-Boot) I haven't observed any boot issues.

This series does three things:
- it fixes warm reboots on boards with a TPS6586x,
- it registers the PMC restart handler much earlier, so that kernel panics that happen before the regulator driver is brought up are able to reset the board,
- it improves the old workaround for the boot ROM hanging on a warm reset, which now also covers emergency restarts. Without it, boards that don't have a PMIC restart handler, and therefore reset through the PMC, hang if the restart was triggered by a panic.

Patches were created with help of Claude. I've tested them on ASUS TF101.

---
Patch 4 is a cleanup of the old workaround, requires patch 3.

To: Lee Jones <[email protected]>
To: Thierry Reding <[email protected]>
To: Jonathan Hunter <[email protected]>
To: Hans Verkuil <[email protected]>
To: Dmitry Osipenko <[email protected]>
To: Mauro Carvalho Chehab <[email protected]>
To: Mauro Carvalho Chehab <[email protected]>

---
Artur Kowalski (4):
mfd: tps6586x: Leave warm and soft restarts to other handlers soc/tegra: pmc: Register restart handler from early init on ARM soc/tegra: pmc: Power up VDE partition before restarting Tegra20
media: tegra-vde: Drop warm restart workaround

 drivers/media/platform/nvidia/tegra-vde/vde.c | 24 +------
 drivers/mfd/tps6586x.c                        |  3 +
 drivers/soc/tegra/pmc.c                       | 99 ++++++++++++++++++++++-----
 3 files changed, 86 insertions(+), 40 deletions(-)

---
base-commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
change-id: 20261128-t20-soft-reset-fa083c0b45e4

Best regards,

Signature · 1 line
Artur Kowalski <[email protected]>
#1 lore raw

PATCH 1/4 mfd: tps6586x: Leave warm and soft restarts to other handlers

The restart handler resets the PMIC, which power cycles the whole system, including RAM, so a ramoops/pstore log of a crash doesn't survive the restart. Its priority is above that of the Tegra PMC restart handler, which resets the SoC and keeps the RAM contents, so the PMIC handler is always used.

Let the reboot mode select between them: return without resetting the PMIC for warm and soft restarts, so the next handler performs them. Cold restarts, the default, keep using the PMIC.

---
 drivers/mfd/tps6586x.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/mfd/tps6586x.c +3 -0
index f524bbdc8f3b..a92a88ac987d 100644
@@ -480,6 +480,9 @@ static int tps6586x_restart_handler(struct sys_off_data *data)
 {
 	int ret;
 
+	if (data->mode == REBOOT_WARM || data->mode == REBOOT_SOFT)
+		return NOTIFY_DONE;
+
 	/* Put the PMIC into hard reboot state. This takes at least 20ms. */
 	ret = tps6586x_set_bits(data->dev, TPS6586X_SUPPLYENE, SOFT_RST_BIT);
 	if (ret)
Signature · 1 line
2.56.1
#2 lore raw

PATCH 2/4 soc/tegra: pmc: Register restart handler from early init on ARM

In reply to Artur Kowalski

On 32-bit ARM the PMC restart handler is registered at probe time, which is deferred until the core-supply regulator appears. If the kernel crashes before the PMC probes, it won't be able to reset the board.

The handler only needs the PMC registers, which tegra_pmc_early_init() already maps. Register it there, using early_pmc, so restarting works from early boot, and don't register it again at probe time on ARM.

Also switch early_pmc to the new register mapping before unmapping the old one at probe time, as the handler doesn't take the lock.

---
 drivers/soc/tegra/pmc.c | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)
drivers/soc/tegra/pmc.c +22 -3
index 721e9596b07d..fcd9a644d97f 100644
@@ -3090,7 +3090,8 @@ static int tegra_pmc_probe(struct platform_device *pdev)
 		}
 	}
 
-	if (dev_to_node(&pdev->dev) == NUMA_NO_NODE) {
+	/* On 32-bit ARM, tegra_pmc_early_init() registers the restart handler */
+	if (!IS_ENABLED(CONFIG_ARM) && dev_to_node(&pdev->dev) == NUMA_NO_NODE) {
 		err = devm_register_sys_off_handler(&pdev->dev,
 						    SYS_OFF_MODE_RESTART,
 						    SYS_OFF_PRIO_LOW,
@@ -3168,8 +3169,11 @@ static int tegra_pmc_probe(struct platform_device *pdev)
 #if defined(CONFIG_ARM)
 	mutex_lock(&early_pmc->powergates_lock);
 	early_pmc->rate = pmc->rate;
-	iounmap(early_pmc->base);
-	early_pmc->base = pmc->base;
+	/*
+	 * The restart handler may use early_pmc->base without taking the
+	 * lock, so switch to the new mapping before unmapping the old one.
+	 */
+	iounmap(xchg(&early_pmc->base, pmc->base));
 	mutex_unlock(&early_pmc->powergates_lock);
 #endif
 
@@ -5182,6 +5186,7 @@ void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)
  */
 static int __init tegra_pmc_early_init(void)
 {
+	struct sys_off_handler *handler;
 	const struct of_device_id *match;
 	struct device_node *np;
 	struct resource regs;
@@ -5235,6 +5240,20 @@ static int __init tegra_pmc_early_init(void)
 		return -ENXIO;
 	}
 
+	/*
+	 * Register the restart handler here rather than at probe time, so
+	 * that restarting works before the PMC driver has probed, e.g. on a
+	 * panic early during boot or while the probe is deferred waiting for
+	 * the core regulator. The handler keeps working after the probe, as
+	 * the driver hands its register mapping over to early_pmc.
+	 */
+	handler = register_sys_off_handler(SYS_OFF_MODE_RESTART,
+					   SYS_OFF_PRIO_LOW,
+					   tegra_pmc_restart_handler,
+					   early_pmc);
+	if (IS_ERR(handler))
+		pr_warn("failed to register restart handler: %pe\n", handler);
+
 	if (of_device_is_available(np)) {
 		early_pmc->soc = match->data;
 
Signature · 1 line
2.56.1
#3 lore raw

PATCH 3/4 soc/tegra: pmc: Power up VDE partition before restarting Tegra20

In reply to Artur Kowalski

The Tegra20 boot ROM hangs on a warm reset if the VDE power partition is power-gated or clamped. The partition is powered on after a cold boot, and since the PMC isn't reset by a warm reset, it keeps the state Linux left it in.

This is currently worked around by the VDE driver, which powers the partition on from its shutdown callback. That doesn't cover emergency restarts, such as on a panic, which don't run shutdown callbacks, or the cases where the VDE driver isn't loaded or hasn't probed yet. The PMC driver also powers the partition down at probe time, so the cold-boot state doesn't survive even without the VDE driver.

Power the partition on and remove its clamps from the PMC restart path instead, right before the main reset. Factor the clamp mask computation out of __tegra_powergate_remove_clamping() so that it can be shared.

Fixes 8f0c714ad9be ("media: staging: tegra-vde: Turn ON power domain on shutdown")
---
 drivers/soc/tegra/pmc.c | 74 +++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 60 insertions(+), 14 deletions(-)
drivers/soc/tegra/pmc.c +60 -14
index fcd9a644d97f..3fe4db473905 100644
@@ -377,6 +377,7 @@ struct tegra_pmc_soc {
 	bool has_tsense_reset;
 	bool has_gpu_clamps;
 	bool needs_mbist_war;
+	bool needs_vde_restart_war;
 	bool has_io_pad_wren;
 	bool maybe_tz_only;
 
@@ -711,11 +712,24 @@ static int tegra_powergate_set(struct tegra_pmc *pmc, unsigned int id,
 	return err;
 }
 
+static u32 tegra_powergate_clamp_mask(unsigned int id)
+{
+	/*
+	 * Tegra 2 has a bug where PCIE and VDE clamping masks are
+	 * swapped relatively to the partition ids
+	 */
+	if (id == TEGRA_POWERGATE_VDEC)
+		return BIT(TEGRA_POWERGATE_PCIE);
+
+	if (id == TEGRA_POWERGATE_PCIE)
+		return BIT(TEGRA_POWERGATE_VDEC);
+
+	return BIT(id);
+}
+
 static int __tegra_powergate_remove_clamping(struct tegra_pmc *pmc,
 					     unsigned int id)
 {
-	u32 mask;
-
 	mutex_lock(&pmc->powergates_lock);
 
 	/*
@@ -729,18 +743,7 @@ static int __tegra_powergate_remove_clamping(struct tegra_pmc *pmc,
 		}
 	}
 
-	/*
-	 * Tegra 2 has a bug where PCIE and VDE clamping masks are
-	 * swapped relatively to the partition ids
-	 */
-	if (id == TEGRA_POWERGATE_VDEC)
-		mask = (1 << TEGRA_POWERGATE_PCIE);
-	else if (id == TEGRA_POWERGATE_PCIE)
-		mask = (1 << TEGRA_POWERGATE_VDEC);
-	else
-		mask = (1 << id);
-
-	tegra_pmc_writel(pmc, mask, REMOVE_CLAMPING);
+	tegra_pmc_writel(pmc, tegra_powergate_clamp_mask(id), REMOVE_CLAMPING);
 
 out:
 	mutex_unlock(&pmc->powergates_lock);
@@ -1189,10 +1192,52 @@ static int tegra_pmc_reboot_notify(struct notifier_block *this,
 	return NOTIFY_DONE;
 }
 
+/*
+ * The Tegra20 boot ROM hangs on a warm reset if the VDE partition is
+ * power-gated or clamped, as it is powered on after a cold boot, so power
+ * it on before resetting. The PMC isn't reset, so the partition keeps its
+ * state.
+ *
+ * This runs with interrupts disabled, so tegra_powergate_set() can't be
+ * used, as it takes a mutex and sleeps. The partition is powered on without
+ * enabling its clocks and deasserting its resets, which the reset takes care
+ * of.
+ */
+static void tegra_pmc_restart_power_up_vde(struct tegra_pmc *pmc)
+{
+	const unsigned int id = TEGRA_POWERGATE_VDEC;
+	unsigned int retries = 3;
+	bool on;
+	int err;
+
+	if (!pmc->soc || !pmc->soc->needs_vde_restart_war)
+		return;
+
+	if (tegra_powergate_state(pmc, id))
+		return;
+
+	do {
+		tegra_pmc_writel(pmc, PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
+		err = read_poll_timeout_atomic(tegra_powergate_state, on, on,
+					       10, 100000, false, pmc, id);
+	} while (err && --retries);
+
+	if (err) {
+		pr_warn("failed to power up VDE partition: %d\n", err);
+		return;
+	}
+
+	udelay(10);
+	tegra_pmc_writel(pmc, tegra_powergate_clamp_mask(id), REMOVE_CLAMPING);
+	udelay(10);
+}
+
 static void tegra_pmc_restart(struct tegra_pmc *pmc)
 {
 	u32 value;
 
+	tegra_pmc_restart_power_up_vde(pmc);
+
 	/* reset everything but PMC_SCRATCH0 and PMC_RST_STATUS */
 	value = tegra_pmc_readl(pmc, PMC_CNTRL);
 	value |= PMC_CNTRL_MAIN_RST;
@@ -3448,6 +3493,7 @@ static const struct tegra_pmc_soc tegra20_pmc_soc = {
 	.has_tsense_reset = false,
 	.has_gpu_clamps = false,
 	.needs_mbist_war = false,
+	.needs_vde_restart_war = true,
 	.has_io_pad_wren = true,
 	.maybe_tz_only = false,
 	.num_io_pads = 0,
Signature · 1 line
2.56.1
#4 lore raw

PATCH 4/4 media: tegra-vde: Drop warm restart workaround

In reply to Artur Kowalski

The VDE driver powers the VDE power partition on from its shutdown callback, and leaves it powered on in remove, because the Tegra20 boot ROM hangs on a warm reset if the partition is power-gated or clamped. The PMC driver now powers the partition on right before restarting on Tegra20, which also covers emergency restarts and doesn't depend on the VDE driver being loaded.

Drop the shutdown callback, and power the hardware down on remove, unless user space has forbidden runtime PM through power/control.

Tegra30, Tegra114 and Tegra124 aren't affected by the boot ROM issue, so they no longer get the partition powered on before a restart.

---
 drivers/media/platform/nvidia/tegra-vde/vde.c | 24 +-----------------------
 1 file changed, 1 insertion(+), 23 deletions(-)
drivers/media/platform/nvidia/tegra-vde/vde.c +1 -23
index 878a5b37846c..6546959e4069 100644
@@ -394,22 +394,10 @@ static void tegra_vde_remove(struct platform_device *pdev)
 	tegra_vde_v4l2_deinit(vde);
 	tegra_vde_free_bo(vde->secure_bo);
 
-	/*
-	 * As it increments RPM usage_count even on errors, we don't need to
-	 * check the returned code here.
-	 */
-	pm_runtime_get_sync(dev);
-
+	pm_runtime_suspend(dev);
 	pm_runtime_dont_use_autosuspend(dev);
 	pm_runtime_disable(dev);
 
-	/*
-	 * Balance RPM state, the VDE power domain is left ON and hardware
-	 * is clock-gated. It's safe to reboot machine now.
-	 */
-	pm_runtime_put_noidle(dev);
-	clk_disable_unprepare(vde->clk);
-
 	tegra_vde_dmabuf_cache_unmap_all(vde);
 	tegra_vde_iommu_deinit(vde);
 
@@ -417,15 +405,6 @@ static void tegra_vde_remove(struct platform_device *pdev)
 		      gen_pool_size(vde->iram_pool));
 }
 
-static void tegra_vde_shutdown(struct platform_device *pdev)
-{
-	/*
-	 * On some devices bootloader isn't ready to a power-gated VDE on
-	 * a warm-reboot, machine will hang in that case.
-	 */
-	pm_runtime_get_sync(&pdev->dev);
-}
-
 static __maybe_unused int tegra_vde_pm_suspend(struct device *dev)
 {
 	struct tegra_vde *vde = dev_get_drvdata(dev);
@@ -543,7 +522,6 @@ MODULE_DEVICE_TABLE(of, tegra_vde_of_match);
 static struct platform_driver tegra_vde_driver = {
 	.probe		= tegra_vde_probe,
 	.remove		= tegra_vde_remove,
-	.shutdown	= tegra_vde_shutdown,
 	.driver		= {
 		.name		= "tegra-vde",
 		.of_match_table = tegra_vde_of_match,
Signature · 1 line
2.56.1
#5 lore raw

Re: [PATCH 4/4] media: tegra-vde: Drop warm restart workaround

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] media: tegra-vde: vde->clk leak in tegra_vde_remove() when RPM is forbidden --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] media: tegra-vde: vde->clk leak in tegra_vde_remove() when RPM is forbidden
--

commit 2c315ebe0db76f7ab987294827d90193c7a90774
Author: Artur Kowalski <[email protected]>

media: tegra-vde: Drop warm restart workaround

This commit removes the tegra-vde shutdown callback and changes remove to runtime-suspend the device instead of leaving the VDE power partition powered on for warm reboots.

Quoted 15 lines
drivers/media/platform/nvidia/tegra-vde/vde.c +1 -6
index 878a5b37846ce..6546959e40694 100644
@@ -394,22 +394,10 @@ static void tegra_vde_remove(struct platform_device *pdev)
 	tegra_vde_v4l2_deinit(vde);
 	tegra_vde_free_bo(vde->secure_bo);
 
-	/*
-	 * As it increments RPM usage_count even on errors, we don't need to
-	 * check the returned code here.
-	 */
-	pm_runtime_get_sync(dev);
-
+	pm_runtime_suspend(dev);

drivers/media/platform/nvidia/tegra-vde/vde.c static void tegra_vde_remove(struct platform_device *pdev) · line 412

Quoted 10 lines
drivers/media/platform/nvidia/tegra-vde/vde.c +0 -7
 	pm_runtime_dont_use_autosuspend(dev);
 	pm_runtime_disable(dev);
 
-	/*
-	 * Balance RPM state, the VDE power domain is left ON and hardware
-	 * is clock-gated. It's safe to reboot machine now.
-	 */
-	pm_runtime_put_noidle(dev);
-	clk_disable_unprepare(vde->clk);
-

[Severity: Medium]
Does removing clk_disable_unprepare(vde->clk) here leak the prepare and enable counts on vde->clk when runtime PM is forbidden via sysfs power/control?

When runtime PM is forbidden, dev->power.usage_count is non-zero and the device is active with vde->clk enabled by tegra_vde_runtime_resume(). Both pm_runtime_suspend() and pm_runtime_dont_use_autosuspend() then return -EAGAIN without calling tegra_vde_runtime_suspend(). Because vde->clk is acquired with devm_clk_get(), devres release only calls clk_put(), leaving vde->clk prepared and enabled as the generic PM domain detaches and powers off, and leaking an additional reference on each rebind.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#6 lore raw
In reply to Artur Kowalski
Sashiko has finished reviewing this 4-patch series:

Sashiko has finished reviewing this 4-patch series:

- Patches 1-3: no issues found.
- Patch 4 ("media: tegra-vde: Drop warm restart workaround"): 1 potential issue (1 Medium)

A detailed review has been sent as a separate reply to patch 4.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]