Skip to contentSkip to conversation

drm/bridge: cdns-mhdp8546: Add suspend resume support to the bridge driver

PATCH New Abhash Kumar Jha 2 participants 2 messages Dec 2

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

PATCH v5 1/1 drm/bridge: cdns-mhdp8546: Add suspend resume support to the bridge driver

Add system suspend and resume hooks to the cdns-mhdp8546 bridge driver.

While resuming we either load the firmware or activate it. Firmware is loaded only when resuming from a successful suspend-resume cycle.

If resuming due to an aborted suspend, loading the firmware is not possible because the uCPU's IMEM is only accessible after a reset and the bridge has not gone through a reset in this case. Hence, activate the firmware that is already loaded.

Use the KEEP_ALIVE register to determine if there is a firmware already running or not and load the FW accordingly.

---
Hi,

This patch depends on the following patch which adds phy_power_on/off in probe and remove. -https://lore.kernel.org/all/[email protected]/

Changes in v5:
- Removed the no_hpd check to fix the build error.
- Mask HPD interrupt in the bridge during suspend.
- Enable HPD and schedule hpd_work if stopping the firmware fails in suspend.
- Evaluate the sink state at the end of resume by scheduling hpd_work.
- Link to v4: https://lore.kernel.org/all/[email protected]/

Changes in v4:
- Use LATE_SYSTEM_SLEEP_PM_OPS for the pm_ops.
- Replace genpd_notifier callback with reading the KEEP_ALIVE register to determine if FW is already running in the bridge
- Link to v3: https://lore.kernel.org/all/[email protected]/

Changes in v3:
- Register genpd_notifier callback only when genpd is available.
- Mark mhdp->powered_off = false after resume is successful.
- Use SIMPLE_DEV_PM_OPS instead of SET_SYSTEM_SLEEP_PM_OPS.
- Set mhdp->hw_state appropriately in error paths.
- Link to v2: https://lore.kernel.org/all/[email protected]/

Changes in v2:
- Fixed defined but not used [-Wunused-function] warning for suspend and resume calls.
- Link to v1: https://lore.kernel.org/all/[email protected]/

Thanks and Regards,
Abhash

 .../drm/bridge/cadence/cdns-mhdp8546-core.c   | 139 +++++++++++++++++-
 1 file changed, 138 insertions(+), 1 deletion(-)
drivers/gpu/drm/bridge/cadence/cdns-mhdp8546-core.c +138 -1
index 86e850a4752dc..080bc2ea6f68f 100644
@@ -12,7 +12,6 @@
  *
  * TODO:
  *     - Implement optimized mailbox communication using mailbox interrupts
- *     - Add support for power management
  *     - Add support for features like audio, MST and fast link training
  *     - Implement request_fw_cancel to handle HW_STATE
  *     - Fix asynchronous loading of firmware implementation
@@ -2258,6 +2257,143 @@ static void cdns_mhdp_hpd_work(struct work_struct *work)
 	drm_bridge_hpd_notify(&mhdp->bridge, cdns_mhdp_detect(mhdp));
 }
 
+static int cdns_mhdp_resume(struct device *dev)
+{
+	struct cdns_mhdp_device *mhdp = dev_get_drvdata(dev);
+	unsigned int keep_alive, keep_alive_inc;
+	unsigned long rate;
+	int ret;
+
+	ret = clk_prepare_enable(mhdp->clk);
+	if (ret)
+		return ret;
+
+	rate = clk_get_rate(mhdp->clk);
+	writel(rate % 1000000, mhdp->regs + CDNS_SW_CLK_L);
+	writel(rate / 1000000, mhdp->regs + CDNS_SW_CLK_H);
+	writel(~0, mhdp->regs + CDNS_APB_INT_MASK);
+
+	ret = phy_init(mhdp->phy);
+	if (ret) {
+		dev_err(mhdp->dev, "Failed to initialize PHY: %d\n", ret);
+		goto disable_clk;
+	}
+	ret = phy_power_on(mhdp->phy);
+	if (ret < 0) {
+		dev_err(mhdp->dev, "Failed to power on PHY: %d\n", ret);
+		goto phy_exit;
+	}
+
+	/*
+	 * The KEEP_ALIVE register is incremented at every scheduler loop of the
+	 * firmware. If it changes between reads, the firmware is still running
+	 * (e.g. the suspend was aborted) and only needs to be activated. If it
+	 * does not change, the bridge lost power and the firmware must be loaded
+	 * again.
+	 */
+	keep_alive = readl(mhdp->regs + CDNS_KEEP_ALIVE) & CDNS_KEEP_ALIVE_MASK;
+	ret = readl_poll_timeout(mhdp->regs + CDNS_KEEP_ALIVE, keep_alive_inc,
+				 (keep_alive_inc & CDNS_KEEP_ALIVE_MASK) != keep_alive,
+				 2000, 2 * CDNS_KEEP_ALIVE_TIMEOUT);
+	if (ret) {
+		ret = cdns_mhdp_load_firmware(mhdp);
+		if (ret)
+			goto phy_off;
+
+		ret = wait_event_timeout(mhdp->fw_load_wq,
+					 mhdp->hw_state == MHDP_HW_READY,
+					 msecs_to_jiffies(1000));
+		if (ret == 0) {
+			dev_err(mhdp->dev, "%s: Timeout waiting for fw loading\n",
+				__func__);
+			ret = -ETIMEDOUT;
+			goto phy_off;
+		}
+	} else {
+		ret = cdns_mhdp_set_firmware_active(mhdp, true);
+		if (ret) {
+			dev_err(mhdp->dev, "Failed to activate firmware (%pe)\n",
+				ERR_PTR(ret));
+			goto phy_off;
+		}
+
+		spin_lock(&mhdp->start_lock);
+		mhdp->hw_state = MHDP_HW_READY;
+		cdns_mhdp_bridge_hpd_enable(&mhdp->bridge);
+		spin_unlock(&mhdp->start_lock);
+	}
+
+	/*
+	 * A hpd interrupt while suspended is lost. Schedule the HPD work
+	 * to re-read the sink state and notify DRM of any change.
+	 */
+	spin_lock(&mhdp->start_lock);
+	if (mhdp->bridge_attached)
+		schedule_work(&mhdp->hpd_work);
+	spin_unlock(&mhdp->start_lock);
+
+	return 0;
+
+phy_off:
+	phy_power_off(mhdp->phy);
+phy_exit:
+	phy_exit(mhdp->phy);
+disable_clk:
+	clk_disable_unprepare(mhdp->clk);
+
+	return ret;
+}
+
+static int cdns_mhdp_suspend(struct device *dev)
+{
+	struct cdns_mhdp_device *mhdp = dev_get_drvdata(dev);
+	unsigned long timeout = msecs_to_jiffies(100);
+	int ret;
+
+	ret = wait_event_timeout(mhdp->fw_load_wq,
+				 mhdp->hw_state == MHDP_HW_READY,
+				 timeout);
+	if (ret == 0) {
+		dev_err(mhdp->dev, "%s: Timeout waiting for fw loading\n", __func__);
+		return -ETIMEDOUT;
+	}
+
+	/*
+	 * Mask HPD interrupt so that hpd_work cannot be queued again
+	 * once it is cancelled and the clock is gone.
+	 */
+	cdns_mhdp_bridge_hpd_disable(&mhdp->bridge);
+	synchronize_irq(platform_get_irq(to_platform_device(dev), 0));
+	cancel_work_sync(&mhdp->hpd_work);
+
+	spin_lock(&mhdp->start_lock);
+	mhdp->hw_state = MHDP_HW_STOPPED;
+	spin_unlock(&mhdp->start_lock);
+
+	ret = cdns_mhdp_set_firmware_active(mhdp, false);
+	if (ret) {
+		dev_err(mhdp->dev, "Failed to stop firmware (%pe)\n", ERR_PTR(ret));
+		spin_lock(&mhdp->start_lock);
+		mhdp->hw_state = MHDP_HW_READY;
+		cdns_mhdp_bridge_hpd_enable(&mhdp->bridge);
+		/* HPD may have changed while masked, so re-read the sink state. */
+		if (mhdp->bridge_attached)
+			schedule_work(&mhdp->hpd_work);
+		spin_unlock(&mhdp->start_lock);
+		return ret;
+	}
+
+	phy_power_off(mhdp->phy);
+	phy_exit(mhdp->phy);
+	clk_disable_unprepare(mhdp->clk);
+
+	return 0;
+}
+
+static const struct dev_pm_ops cdns_mhdp_pm_ops = {
+	LATE_SYSTEM_SLEEP_PM_OPS(cdns_mhdp_suspend, cdns_mhdp_resume)
+};
+
 static int cdns_mhdp_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
@@ -2464,6 +2600,7 @@ static struct platform_driver mhdp_driver = {
 	.driver	= {
 		.name		= "cdns-mhdp8546",
 		.of_match_table	= mhdp_ids,
+		.pm = pm_sleep_ptr(&cdns_mhdp_pm_ops),
 	},
 	.probe	= cdns_mhdp_probe,
 	.remove = cdns_mhdp_remove,
Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 6 potential issues to consider: - [High] drm/bridge: cdns-mhdp8546: unbalanced phy_power_off() and phy_power_on() - [High] drm/bridge…

Thank you for your contribution! Sashiko AI review found 6 potential issues to consider:
- [High] drm/bridge: cdns-mhdp8546: unbalanced phy_power_off() and phy_power_on()
- [High] drm/bridge: cdns-mhdp8546: stale link_up state across suspend and resume
- [High] drm/bridge: cdns-mhdp8546: uncanceled async fw load on resume timeout
- [Medium] drm/bridge: cdns-mhdp8546: HPD work in cdns_mhdp_resume() drops HPD events
- [Medium] drm/bridge: cdns-mhdp8546: double clk and phy teardown after failed resume
- [Low] drm/bridge: cdns-mhdp8546: unlocked HPD disable in cdns_mhdp_suspend()
--

commit dc5fd446a2b779e0075a3701545188c489993730
Author: Abhash Kumar Jha <[email protected]>

drm/bridge: cdns-mhdp8546: Add suspend resume support to the bridge driver

This commit adds system suspend and resume callbacks to the cdns-mhdp8546 bridge driver, managing the PHY, clock, HPD interrupts, and firmware state across power transitions.

Quoted 4 lines
drivers/gpu/drm/bridge/cadence/cdns-mhdp8546-core.c +0 -0
index f47717bd10798..7ee0434627f01 100644

[ ... ]

Quoted 20 lines
drivers/gpu/drm/bridge/cadence/cdns-mhdp8546-core.c +16 -0
@@ -2258,6 +2257,143 @@ static void cdns_mhdp_hpd_work(struct work_struct *work)
 	drm_bridge_hpd_notify(&mhdp->bridge, cdns_mhdp_detect(mhdp));
 }
 
+static int cdns_mhdp_resume(struct device *dev)
+{
+	struct cdns_mhdp_device *mhdp = dev_get_drvdata(dev);
+	unsigned int keep_alive, keep_alive_inc;
+	unsigned long rate;
+	int ret;
+
+	ret = clk_prepare_enable(mhdp->clk);
+	if (ret)
+		return ret;
+
+	rate = clk_get_rate(mhdp->clk);
+	writel(rate % 1000000, mhdp->regs + CDNS_SW_CLK_L);
+	writel(rate / 1000000, mhdp->regs + CDNS_SW_CLK_H);
+	writel(~0, mhdp->regs + CDNS_APB_INT_MASK);
+

drivers/gpu/drm/bridge/cadence/cdns-mhdp8546-core.c static void cdns_mhdp_hpd_work(struct work_struct *work) · line 2285

Quoted 10 lines
drivers/gpu/drm/bridge/cadence/cdns-mhdp8546-core.c +10 -0
+	ret = phy_init(mhdp->phy);
+	if (ret) {
+		dev_err(mhdp->dev, "Failed to initialize PHY: %d\n", ret);
+		goto disable_clk;
+	}
+	ret = phy_power_on(mhdp->phy);
+	if (ret < 0) {
+		dev_err(mhdp->dev, "Failed to power on PHY: %d\n", ret);
+		goto phy_exit;
+	}

[Severity: High]
Are phy_power_off() in cdns_mhdp_suspend() and phy_power_on() here balanced with probe and remove?

cdns_mhdp_probe() only calls phy_init(mhdp->phy) and never calls phy_power_on(mhdp->phy), leaving mhdp->phy->power_count at 0. On the first suspend, phy_power_off(mhdp->phy) sees power_count != 1, skips phy->ops->power_off(), underflows power_count to -1, and performs an unbalanced runtime PM put on the PHY device. On resume, phy_power_on(mhdp->phy) sees power_count == -1 instead of 0, so it skips phy->ops->power_on() and only increments power_count back to 0.

Quoted 16 lines
drivers/gpu/drm/bridge/cadence/cdns-mhdp8546-core.c +16 -0
+
+	/*
+	 * The KEEP_ALIVE register is incremented at every scheduler loop of the
+	 * firmware. If it changes between reads, the firmware is still running
+	 * (e.g. the suspend was aborted) and only needs to be activated. If it
+	 * does not change, the bridge lost power and the firmware must be loaded
+	 * again.
+	 */
+	keep_alive = readl(mhdp->regs + CDNS_KEEP_ALIVE) & CDNS_KEEP_ALIVE_MASK;
+	ret = readl_poll_timeout(mhdp->regs + CDNS_KEEP_ALIVE, keep_alive_inc,
+				 (keep_alive_inc & CDNS_KEEP_ALIVE_MASK) != keep_alive,
+				 2000, 2 * CDNS_KEEP_ALIVE_TIMEOUT);
+	if (ret) {
+		ret = cdns_mhdp_load_firmware(mhdp);
+		if (ret)
+			goto phy_off;

drivers/gpu/drm/bridge/cadence/cdns-mhdp8546-core.c static void cdns_mhdp_hpd_work(struct work_struct *work) · line 2311

Quoted 10 lines
drivers/gpu/drm/bridge/cadence/cdns-mhdp8546-core.c +10 -0
+
+		ret = wait_event_timeout(mhdp->fw_load_wq,
+					 mhdp->hw_state == MHDP_HW_READY,
+					 msecs_to_jiffies(1000));
+		if (ret == 0) {
+			dev_err(mhdp->dev, "%s: Timeout waiting for fw loading\n",
+				__func__);
+			ret = -ETIMEDOUT;
+			goto phy_off;
+		}

[Severity: High]
If wait_event_timeout() times out here, can cdns_mhdp_fw_cb() still run after the error path disables mhdp->clk?

cdns_mhdp_load_firmware() queues cdns_mhdp_fw_cb() asynchronously via request_firmware_nowait(), and jumping to phy_off does not cancel or wait for that work before calling clk_disable_unprepare(mhdp->clk):

CPU0 CPU1 (kworker)
cdns_mhdp_resume()
cdns_mhdp_load_firmware()
request_firmware_nowait(...)
wait_event_timeout(..., 1000ms)

    /* times out */
  goto phy_off;
  ...
  clk_disable_unprepare(mhdp->clk)
                                  cdns_mhdp_fw_cb()
                                    cdns_mhdp_fw_activate()
                                      writel(..., CDNS_APB_CTRL)
                                      memcpy_toio(..., CDNS_MHDP_IMEM, ...)
                                        <-- MMIO access with clk disabled

In addition, if cdns_mhdp_fw_cb() fails (for example when !fw or cdns_mhdp_fw_activate() returns an error), it returns without waking mhdp->fw_load_wq, forcing cdns_mhdp_resume() to stall for the full 1000 ms timeout.

[ ... ]

drivers/gpu/drm/bridge/cadence/cdns-mhdp8546-core.c static void cdns_mhdp_hpd_work(struct work_struct *work) · line 2333

Quoted 8 lines
drivers/gpu/drm/bridge/cadence/cdns-mhdp8546-core.c +8 -0
+	/*
+	 * A hpd interrupt while suspended is lost. Schedule the HPD work
+	 * to re-read the sink state and notify DRM of any change.
+	 */
+	spin_lock(&mhdp->start_lock);
+	if (mhdp->bridge_attached)
+		schedule_work(&mhdp->hpd_work);
+	spin_unlock(&mhdp->start_lock);

[Severity: Medium]
Can scheduling mhdp->hpd_work in .resume_early drop hotplug events that occurred while the system was suspended?

During system suspend, the display driver's .suspend hook runs drm_mode_config_helper_suspend() -> drm_kms_helper_poll_disable(), which calls drm_bridge_hpd_disable() and clears mhdp->bridge.hpd_cb to NULL. Because cdns_mhdp_pm_ops uses LATE_SYSTEM_SLEEP_PM_OPS(), cdns_mhdp_resume() runs in .resume_early, before the display driver's .resume hook calls drm_kms_helper_poll_enable() to restore mhdp->bridge.hpd_cb.

If mhdp->hpd_work runs before drm_kms_helper_poll_enable(), cdns_mhdp_update_link_status() consumes the firmware HPD events and updates mhdp->plugged, and drm_bridge_hpd_notify() sees bridge->hpd_cb == NULL and drops the notification. When drm_bridge_hpd_enable() later runs at the end of drm_mode_config_helper_resume(), cdns_mhdp_bridge_hpd_enable() only unmasks CDNS_APB_INT_MASK_SW_EVENT_INT without re-notifying DRM.

drivers/gpu/drm/bridge/cadence/cdns-mhdp8546-core.c static void cdns_mhdp_hpd_work(struct work_struct *work) · line 2345

Quoted 12 lines
drivers/gpu/drm/bridge/cadence/cdns-mhdp8546-core.c +12 -0
+
+	return 0;
+
+phy_off:
+	phy_power_off(mhdp->phy);
+phy_exit:
+	phy_exit(mhdp->phy);
+disable_clk:
+	clk_disable_unprepare(mhdp->clk);
+
+	return ret;
+}

[Severity: Medium]
If cdns_mhdp_resume() fails, does unwinding mhdp->phy and mhdp->clk cause a double disable when the driver is later unbound?

mhdp->clk is acquired with devm_clk_get_enabled() in cdns_mhdp_probe(), and cdns_mhdp_remove() unconditionally calls phy_exit(mhdp->phy). If cdns_mhdp_resume() fails and leaves mhdp->phy exited and mhdp->clk disabled, later bridge detach or driver removal accesses CDNS_APB_INT_MASK in cdns_mhdp_detach() with the clock disabled, calls phy_exit(mhdp->phy) a second time in cdns_mhdp_remove(), and calls clk_disable_unprepare(mhdp->clk) a second time during devres release.

Quoted 10 lines
drivers/gpu/drm/bridge/cadence/cdns-mhdp8546-core.c +10 -0
+
+static int cdns_mhdp_suspend(struct device *dev)
+{
+	struct cdns_mhdp_device *mhdp = dev_get_drvdata(dev);
+	unsigned long timeout = msecs_to_jiffies(100);
+	int ret;
+
+	ret = wait_event_timeout(mhdp->fw_load_wq,
+				 mhdp->hw_state == MHDP_HW_READY,
+				 timeout);

drivers/gpu/drm/bridge/cadence/cdns-mhdp8546-core.c static void cdns_mhdp_hpd_work(struct work_struct *work) · line 2365

Quoted 10 lines
drivers/gpu/drm/bridge/cadence/cdns-mhdp8546-core.c +10 -0
+	if (ret == 0) {
+		dev_err(mhdp->dev, "%s: Timeout waiting for fw loading\n", __func__);
+		return -ETIMEDOUT;
+	}
+
+	/*
+	 * Mask HPD interrupt so that hpd_work cannot be queued again
+	 * once it is cancelled and the clock is gone.
+	 */
+	cdns_mhdp_bridge_hpd_disable(&mhdp->bridge);

[Severity: Low]
Should cdns_mhdp_bridge_hpd_disable() be called under mhdp->start_lock?

In cdns_mhdp_fw_activate(), mhdp->hw_state = MHDP_HW_READY is set before cdns_mhdp_bridge_hpd_enable() unmasks CDNS_APB_INT_MASK_SW_EVENT_INT under mhdp->start_lock:

CPU0 CPU1
cdns_mhdp_fw_activate() cdns_mhdp_suspend()
spin_lock(&mhdp->start_lock)
mhdp->hw_state = MHDP_HW_READY

                                    wait_event_timeout(...,
                                      mhdp->hw_state == MHDP_HW_READY, ...)
                                    cdns_mhdp_bridge_hpd_disable()
                                      /* masks SW_EVENT_INT */
  cdns_mhdp_bridge_hpd_enable()
    /* unmasks SW_EVENT_INT */      <-- HPD IRQ left enabled in suspend

spin_unlock(&mhdp->start_lock)

[ ... ]

drivers/gpu/drm/bridge/cadence/cdns-mhdp8546-core.c static void cdns_mhdp_hpd_work(struct work_struct *work) · line 2391

Quoted 6 lines
drivers/gpu/drm/bridge/cadence/cdns-mhdp8546-core.c +6 -0
+	phy_power_off(mhdp->phy);
+	phy_exit(mhdp->phy);
+	clk_disable_unprepare(mhdp->clk);
+
+	return 0;
+}

[Severity: High]
Does suspending while a sink is connected but the bridge is not actively streaming leave mhdp->link_up stale across suspend and resume?

When a DisplayPort sink is plugged in, cdns_mhdp_update_link_status() calls cdns_mhdp_link_up(), which configures the PHY, trains the link, and sets mhdp->link_up = true even when no CRTC is active (mhdp->bridge_enabled == false). If the system suspends while mhdp->bridge_enabled is false (for example when the screen is blanked), cdns_mhdp_atomic_disable() is not called, so cdns_mhdp_suspend() powers down the PHY and clock while mhdp->link_up remains true.

After resume, both cdns_mhdp_update_link_status() and cdns_mhdp_atomic_enable() check !mhdp->link_up and skip cdns_mhdp_link_up(), leaving the PHY unconfigured and the DisplayPort link untrained when video output is enabled.

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