Skip to contentSkip to conversation

platform/x86: amd-pmc: Set QOS during suspend on CZN w/ timer wakeup

PATCH Superseded Mario Limonciello 2 participants 3 messages Feb 22, 2022 – Feb 23, 2022 x86 platform drivers

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 platform/x86: amd-pmc: Set QOS during suspend on CZN w/ timer wakeup

commit 59348401ebed ("platform/x86: amd-pmc: Add special handling for timer based S0i3 wakeup") adds support for using another platform timer in lieu of the RTC which doesn't work properly on some systems. This path was validated and worked well before submission. During the 5.16-rc1 merge window other patches were merged that caused this to stop working properly.

When this feature was used with 5.16-rc1 or later some OEM laptops with the matching firmware requirements from that commit would shutdown instead of program a timer based wakeup.

This was bisected to commit 8d89835b0467 ("PM: suspend: Do not pause cpuidle in the suspend-to-idle path"). This wasn't supposed to cause any negative impacts and also tested well on both Intel and ARM platforms. However this changed the semantics of when CPUs are allowed to be in the deepest state. For the AMD systems in question it appears this causes a firmware crash for timer based wakeup.

It's hypothesized to be caused by the amd-pmc driver sending OS_HINT and all the CPUs going into a deep state while the timer is still being programmed. It's likely a firmware bug, but to avoid it don't allow setting CPUs into the deepest state while using CZN timer wakeup path.

If later it's discovered that this also occurs from "regular" suspends without a timer as well or on other silicon, this may be later expanded to run in the suspend path for more scenarios.

Fixes 8d89835b0467 ("PM: suspend: Do not pause cpuidle in the suspend-to-idle path")
Fixes 23f62d7ab25b ("PM: sleep: Pause cpuidle later and resume it earlier during system transitions")
Fixes 59348401ebed ("platform/x86: amd-pmc: Add special handling for timer based S0i3 wakeup"

---
Changes from v1->v2:
* Update for Rafael's suggested changes:
- fix resume comment
- undo QoS at end of resume instead
- Use smaller number for QoS to allow C1 entry

 drivers/platform/x86/amd-pmc.c | 39 ++++++++++++++++++++++++++--------
 1 file changed, 30 insertions(+), 9 deletions(-)
drivers/platform/x86/amd-pmc.c +30 -9
index 4c72ba68b315..248dc6735f5a 100644
@@ -21,6 +21,7 @@
 #include <linux/module.h>
 #include <linux/pci.h>
 #include <linux/platform_device.h>
+#include <linux/pm_qos.h>
 #include <linux/rtc.h>
 #include <linux/suspend.h>
 #include <linux/seq_file.h>
@@ -131,6 +132,7 @@ struct amd_pmc_dev {
 	struct device *dev;
 	struct pci_dev *rdev;
 	struct mutex lock; /* generic mutex lock */
+	struct pm_qos_request amd_pmc_pm_qos_req;
 #if IS_ENABLED(CONFIG_DEBUG_FS)
 	struct dentry *dbgfs_dir;
 #endif /* CONFIG_DEBUG_FS */
@@ -521,6 +523,14 @@ static int amd_pmc_verify_czn_rtc(struct amd_pmc_dev *pdev, u32 *arg)
 	rc = rtc_alarm_irq_enable(rtc_device, 0);
 	dev_dbg(pdev->dev, "wakeup timer programmed for %lld seconds\n", duration);
 
+	/*
+	 * Prevent CPUs from getting into idle states while running the code
+	 * below which is generally safe to run when none of the CPUs are in
+	 * idle states.
+	 */
+	cpu_latency_qos_update_request(&pdev->amd_pmc_pm_qos_req, 3);
+	wake_up_all_idle_cpus();
+
 	return rc;
 }
 
@@ -538,24 +548,31 @@ static int __maybe_unused amd_pmc_suspend(struct device *dev)
 	/* Activate CZN specific RTC functionality */
 	if (pdev->cpu_id == AMD_CPU_ID_CZN) {
 		rc = amd_pmc_verify_czn_rtc(pdev, &arg);
-		if (rc < 0)
-			return rc;
+		if (rc)
+			goto fail;
 	}
 
 	/* Dump the IdleMask before we send hint to SMU */
 	amd_pmc_idlemask_read(pdev, dev, NULL);
 	msg = amd_pmc_get_os_hint(pdev);
 	rc = amd_pmc_send_cmd(pdev, arg, NULL, msg, 0);
-	if (rc)
+	if (rc) {
 		dev_err(pdev->dev, "suspend failed\n");
+		goto fail;
+	}
 
 	if (enable_stb)
 		rc = amd_pmc_write_stb(pdev, AMD_PMC_STB_PREDEF);
-	if (rc)	{
+	if (rc) {
 		dev_err(pdev->dev, "error writing to STB\n");
-		return rc;
+		goto fail;
 	}
 
+	return 0;
+fail:
+	if (pdev->cpu_id == AMD_CPU_ID_CZN)
+		cpu_latency_qos_update_request(&pdev->amd_pmc_pm_qos_req,
+						PM_QOS_DEFAULT_VALUE);
 	return rc;
 }
 
@@ -579,12 +596,15 @@ static int __maybe_unused amd_pmc_resume(struct device *dev)
 	/* Write data incremented by 1 to distinguish in stb_read */
 	if (enable_stb)
 		rc = amd_pmc_write_stb(pdev, AMD_PMC_STB_PREDEF + 1);
-	if (rc)	{
+	if (rc)
 		dev_err(pdev->dev, "error writing to STB\n");
-		return rc;
-	}
 
-	return 0;
+	/* Restore the QoS request back to defaults if it was set */
+	if (pdev->cpu_id == AMD_CPU_ID_CZN)
+		cpu_latency_qos_update_request(&pdev->amd_pmc_pm_qos_req,
+						PM_QOS_DEFAULT_VALUE);
+
+	return rc;
 }
 
 static const struct dev_pm_ops amd_pmc_pm_ops = {
@@ -722,6 +742,7 @@ static int amd_pmc_probe(struct platform_device *pdev)
 	amd_pmc_get_smu_version(dev);
 	platform_set_drvdata(pdev, dev);
 	amd_pmc_dbgfs_register(dev);
+	cpu_latency_qos_add_request(&dev->amd_pmc_pm_qos_req, PM_QOS_DEFAULT_VALUE);
 	return 0;
 
 err_pci_dev_put:
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 63 lines · Mario Limonciello

commit 59348401ebed ("platform/x86: amd-pmc: Add special handling for timer based S0i3 wakeup") adds support for using another platform timer in lieu of the RTC which doesn't work properly on some systems. This path was validated and worked well before submission. During the 5.16-rc1 merge window other patches were merged that caused this to stop working properly.

When this feature was used with 5.16-rc1 or later some OEM laptops with the matching firmware requirements from that commit would shutdown instead of program a timer based wakeup.

This was bisected to commit 8d89835b0467 ("PM: suspend: Do not pause cpuidle in the suspend-to-idle path"). This wasn't supposed to cause any negative impacts and also tested well on both Intel and ARM platforms. However this changed the semantics of when CPUs are allowed to be in the deepest state. For the AMD systems in question it appears this causes a firmware crash for timer based wakeup.

It's hypothesized to be caused by the amd-pmc driver sending OS_HINT and all the CPUs going into a deep state while the timer is still being programmed. It's likely a firmware bug, but to avoid it don't allow setting CPUs into the deepest state while using CZN timer wakeup path.

If later it's discovered that this also occurs from "regular" suspends without a timer as well or on other silicon, this may be later expanded to run in the suspend path for more scenarios.

Fixes 8d89835b0467 ("PM: suspend: Do not pause cpuidle in the suspend-to-idle path")
Fixes 23f62d7ab25b ("PM: sleep: Pause cpuidle later and resume it earlier during system transitions")
Fixes 59348401ebed ("platform/x86: amd-pmc: Add special handling for timer based S0i3 wakeup"

---
Changes from v1->v2:
* Update for Rafael's suggested changes:
- fix resume comment
- undo QoS at end of resume instead
- Use smaller number for QoS to allow C1 entry

  drivers/platform/x86/amd-pmc.c | 39 ++++++++++++++++++++++++++--------
  1 file changed, 30 insertions(+), 9 deletions(-)
drivers/platform/x86/amd-pmc.c +2 -0
index 4c72ba68b315..248dc6735f5a 100644
@@ -21,6 +21,7 @@
  #include <linux/module.h>
  #include <linux/pci.h>
  #include <linux/platform_device.h>
+#include <linux/pm_qos.h>
  #include <linux/rtc.h>
  #include <linux/suspend.h>
  #include <linux/seq_file.h>
@@ -131,6 +132,7 @@ struct amd_pmc_dev {
  	struct device *dev;
  	struct pci_dev *rdev;
  	struct mutex lock; /* generic mutex lock */
+	struct pm_qos_request amd_pmc_pm_qos_req;
  #if IS_ENABLED(CONFIG_DEBUG_FS)
  	struct dentry *dbgfs_dir;
  #endif /* CONFIG_DEBUG_FS */

drivers/platform/x86/amd-pmc.c static int amd_pmc_verify_czn_rtc(struct amd_pmc_dev *pdev, u32 *arg) · line 527

Quoted 5 lines
drivers/platform/x86/amd-pmc.c +2 -0
@@ -521,6 +523,14 @@ static int amd_pmc_verify_czn_rtc(struct amd_pmc_dev *pdev, u32 *arg)
  	rc = rtc_alarm_irq_enable(rtc_device, 0);
  	dev_dbg(pdev->dev, "wakeup timer programmed for %lld seconds\n", duration);
 
+	/*
+	 * Prevent CPUs from getting into idle states while running the code

This should be "into deep idle states" now.

drivers/platform/x86/amd-pmc.c static int amd_pmc_verify_czn_rtc(struct amd_pmc_dev *pdev, u32 *arg) · line 531

Quoted 4 lines
drivers/platform/x86/amd-pmc.c +4 -0
+	 * below which is generally safe to run when none of the CPUs are in
+	 * idle states.
+	 */
+	cpu_latency_qos_update_request(&pdev->amd_pmc_pm_qos_req, 3);

And I would define a symbol for the 3, possibly with a comment documenting it.  Something like AMD_PMC_SUSPEND_MAX_IDLE_STATE_LATENCY or similar.

With the above addressed, please feel free to add

to this patch.

And thanks for taking care of this!

Quoted 70 lines
drivers/platform/x86/amd-pmc.c +22 -9
+	wake_up_all_idle_cpus();
+
  	return rc;
  }
 
@@ -538,24 +548,31 @@ static int __maybe_unused amd_pmc_suspend(struct device *dev)
  	/* Activate CZN specific RTC functionality */
  	if (pdev->cpu_id == AMD_CPU_ID_CZN) {
  		rc = amd_pmc_verify_czn_rtc(pdev, &arg);
-		if (rc < 0)
-			return rc;
+		if (rc)
+			goto fail;
  	}
 
  	/* Dump the IdleMask before we send hint to SMU */
  	amd_pmc_idlemask_read(pdev, dev, NULL);
  	msg = amd_pmc_get_os_hint(pdev);
  	rc = amd_pmc_send_cmd(pdev, arg, NULL, msg, 0);
-	if (rc)
+	if (rc) {
  		dev_err(pdev->dev, "suspend failed\n");
+		goto fail;
+	}
 
  	if (enable_stb)
  		rc = amd_pmc_write_stb(pdev, AMD_PMC_STB_PREDEF);
-	if (rc)	{
+	if (rc) {
  		dev_err(pdev->dev, "error writing to STB\n");
-		return rc;
+		goto fail;
  	}
 
+	return 0;
+fail:
+	if (pdev->cpu_id == AMD_CPU_ID_CZN)
+		cpu_latency_qos_update_request(&pdev->amd_pmc_pm_qos_req,
+						PM_QOS_DEFAULT_VALUE);
  	return rc;
  }
 
@@ -579,12 +596,15 @@ static int __maybe_unused amd_pmc_resume(struct device *dev)
  	/* Write data incremented by 1 to distinguish in stb_read */
  	if (enable_stb)
  		rc = amd_pmc_write_stb(pdev, AMD_PMC_STB_PREDEF + 1);
-	if (rc)	{
+	if (rc)
  		dev_err(pdev->dev, "error writing to STB\n");
-		return rc;
-	}
 
-	return 0;
+	/* Restore the QoS request back to defaults if it was set */
+	if (pdev->cpu_id == AMD_CPU_ID_CZN)
+		cpu_latency_qos_update_request(&pdev->amd_pmc_pm_qos_req,
+						PM_QOS_DEFAULT_VALUE);
+
+	return rc;
  }
 
  static const struct dev_pm_ops amd_pmc_pm_ops = {
@@ -722,6 +742,7 @@ static int amd_pmc_probe(struct platform_device *pdev)
  	amd_pmc_get_smu_version(dev);
  	platform_set_drvdata(pdev, dev);
  	amd_pmc_dbgfs_register(dev);
+	cpu_latency_qos_add_request(&dev->amd_pmc_pm_qos_req, PM_QOS_DEFAULT_VALUE);
  	return 0;
 
  err_pci_dev_put:
#2 lore raw

[Public]

Quoted 54 lines
Quoted 7 lines

Changes from v1->v2:
* Update for Rafael's suggested changes:
- fix resume comment
- undo QoS at end of resume instead
- Use smaller number for QoS to allow C1 entry

  drivers/platform/x86/amd-pmc.c | 39 ++++++++++++++++++++++++++--

------

drivers/platform/x86/amd-

Quoted 3 lines
  1 file changed, 30 insertions(+), 9 deletions(-)
drivers/platform/x86/amd- renamed from drivers/platform/x86/amd-pmc.c +0 -0

pmc.c

drivers/platform/x86/amd-pmc.c static int amd_pmc_verify_czn_rtc(struct

Quoted 20 lines

index 4c72ba68b315..248dc6735f5a 100644

drivers/platform/x86/amd-pmc.c +2 -0
@@ -21,6 +21,7 @@
  #include <linux/module.h>
  #include <linux/pci.h>
  #include <linux/platform_device.h>
+#include <linux/pm_qos.h>
  #include <linux/rtc.h>
  #include <linux/suspend.h>
  #include <linux/seq_file.h>
@@ -131,6 +132,7 @@ struct amd_pmc_dev {
  	struct device *dev;
  	struct pci_dev *rdev;
  	struct mutex lock; /* generic mutex lock */
+	struct pm_qos_request amd_pmc_pm_qos_req;
  #if IS_ENABLED(CONFIG_DEBUG_FS)
  	struct dentry *dbgfs_dir;
  #endif /* CONFIG_DEBUG_FS */
@@ -521,6 +523,14 @@ static int amd_pmc_verify_czn_rtc(struct

amd_pmc_dev *pdev, u32 *arg)

drivers/platform/x86/amd-pmc.c static int amd_pmc_verify_czn_rtc(struct · line 524

Quoted 2 lines
drivers/platform/x86/amd-pmc.c +0 -0
  	rc = rtc_alarm_irq_enable(rtc_device, 0);
  	dev_dbg(pdev->dev, "wakeup timer programmed for %lld

seconds\n", duration);

drivers/platform/x86/amd-pmc.c static int amd_pmc_verify_czn_rtc(struct amd_pmc_dev *pdev, u32 *arg) · line 527

Quoted 3 lines
drivers/platform/x86/amd-pmc.c +2 -0
+	/*
+	 * Prevent CPUs from getting into idle states while running the code

This should be "into deep idle states" now.

drivers/platform/x86/amd-pmc.c static int amd_pmc_verify_czn_rtc(struct amd_pmc_dev *pdev, u32 *arg)

Quoted 5 lines
drivers/platform/x86/amd-pmc.c +4 -0
+	 * below which is generally safe to run when none of the CPUs are in
+	 * idle states.
+	 */
+	cpu_latency_qos_update_request(&pdev-
 amd_pmc_pm_qos_req, 3);

And I would define a symbol for the 3, possibly with a comment documenting it.  Something like AMD_PMC_SUSPEND_MAX_IDLE_STATE_LATENCY
or similar.

Quoted 7 lines

With the above addressed, please feel free to add

to this patch.

And thanks for taking care of this!

Sure thing, will make those minor modifications and send up a v3 with your tag. Really appreciate your help and suggestions here too.

Quoted 80 lines

drivers/platform/x86/amd-pmc.c static int __maybe_unused

Quoted 8 lines
drivers/platform/x86/amd-pmc.c +2 -0
+	wake_up_all_idle_cpus();
+
  	return rc;
  }
 
@@ -538,24 +548,31 @@ static int __maybe_unused

amd_pmc_suspend(struct device *dev)

drivers/platform/x86/amd-pmc.c static int __maybe_unused

Quoted 38 lines
drivers/platform/x86/amd-pmc.c +12 -5
  	/* Activate CZN specific RTC functionality */
  	if (pdev->cpu_id == AMD_CPU_ID_CZN) {
  		rc = amd_pmc_verify_czn_rtc(pdev, &arg);
-		if (rc < 0)
-			return rc;
+		if (rc)
+			goto fail;
  	}
 
  	/* Dump the IdleMask before we send hint to SMU */
  	amd_pmc_idlemask_read(pdev, dev, NULL);
  	msg = amd_pmc_get_os_hint(pdev);
  	rc = amd_pmc_send_cmd(pdev, arg, NULL, msg, 0);
-	if (rc)
+	if (rc) {
  		dev_err(pdev->dev, "suspend failed\n");
+		goto fail;
+	}
 
  	if (enable_stb)
  		rc = amd_pmc_write_stb(pdev, AMD_PMC_STB_PREDEF);
-	if (rc)	{
+	if (rc) {
  		dev_err(pdev->dev, "error writing to STB\n");
-		return rc;
+		goto fail;
  	}
 
+	return 0;
+fail:
+	if (pdev->cpu_id == AMD_CPU_ID_CZN)
+		cpu_latency_qos_update_request(&pdev-
 amd_pmc_pm_qos_req,
+						PM_QOS_DEFAULT_VALUE);
  	return rc;
  }
@@ -579,12 +596,15 @@ static int __maybe_unused

amd_pmc_resume(struct device *dev)

drivers/platform/x86/amd-pmc.c static int __maybe_unused amd_pmc_resume(struct device *dev)

Quoted 3 lines
drivers/platform/x86/amd-pmc.c +0 -0
  	/* Write data incremented by 1 to distinguish in stb_read */
  	if (enable_stb)
  		rc = amd_pmc_write_stb(pdev, AMD_PMC_STB_PREDEF +

1);

drivers/platform/x86/amd-pmc.c static int amd_pmc_probe(struct platform_device

Quoted 18 lines
drivers/platform/x86/amd-pmc.c +7 -4
-	if (rc)	{
+	if (rc)
  		dev_err(pdev->dev, "error writing to STB\n");
-		return rc;
-	}
 
-	return 0;
+	/* Restore the QoS request back to defaults if it was set */
+	if (pdev->cpu_id == AMD_CPU_ID_CZN)
+		cpu_latency_qos_update_request(&pdev-
 amd_pmc_pm_qos_req,
+						PM_QOS_DEFAULT_VALUE);
+
+	return rc;
  }
 
  static const struct dev_pm_ops amd_pmc_pm_ops = {
drivers/platform/x86/amd-pmc.c +0 -0
@@ -722,6 +742,7 @@ static int amd_pmc_probe(struct platform_device

*pdev)

drivers/platform/x86/amd-pmc.c static int amd_pmc_probe(struct platform_device *pdev)

Quoted 4 lines
drivers/platform/x86/amd-pmc.c +1 -0
  	amd_pmc_get_smu_version(dev);
  	platform_set_drvdata(pdev, dev);
  	amd_pmc_dbgfs_register(dev);
+	cpu_latency_qos_add_request(&dev->amd_pmc_pm_qos_req,

PM_QOS_DEFAULT_VALUE);

Quoted 3 lines
drivers/platform/x86/amd-pmc.c +0 -0
  	return 0;
 
  err_pci_dev_put: