The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v2 0/2] Fix S0i3 wakeup with alarmtimer
@ 2026-05-21  4:37 Mario Limonciello
  2026-05-21  4:37 ` [PATCH v2 1/2] rtc: Add rtc_read_next_alarm() to read next expiring timer Mario Limonciello
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Mario Limonciello @ 2026-05-21  4:37 UTC (permalink / raw)
  To: Shyam Sundar S K, Alexandre Belloni
  Cc: Hans de Goede, Ilpo Järvinen, platform-driver-x86,
	linux-kernel, linux-rtc, Thomas Gleixner, Mario Limonciello

It was reported that suspend-then-hibernate stopped working with modern
systemd versions on AMD Cezanne systems. The reason for this breakage
was because systemd switched to using alarmtimer instead of the wakealarm
sysfs file.

But really it uncovered deeper problems with how these timers work.  Adjust
the code accordingly.

Mario Limonciello (2):
  rtc: Add rtc_read_next_alarm() to read next expiring timer
  platform/x86: amd-pmc: Fix S0i3 wakeup with alarmtimer

 drivers/platform/x86/amd/pmc/pmc.c |  9 ++++---
 drivers/rtc/interface.c            | 40 ++++++++++++++++++++++++++++++
 include/linux/rtc.h                |  2 ++
 3 files changed, 48 insertions(+), 3 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 1/2] rtc: Add rtc_read_next_alarm() to read next expiring timer
  2026-05-21  4:37 [PATCH v2 0/2] Fix S0i3 wakeup with alarmtimer Mario Limonciello
@ 2026-05-21  4:37 ` Mario Limonciello
  2026-06-24 21:15   ` (subset) " Alexandre Belloni
  2026-05-21  4:37 ` [PATCH v2 2/2] platform/x86: amd-pmc: Fix S0i3 wakeup with alarmtimer Mario Limonciello
  2026-06-25 12:43 ` (subset) [PATCH v2 0/2] " Alexandre Belloni
  2 siblings, 1 reply; 8+ messages in thread
From: Mario Limonciello @ 2026-05-21  4:37 UTC (permalink / raw)
  To: Shyam Sundar S K, Alexandre Belloni
  Cc: Hans de Goede, Ilpo Järvinen, platform-driver-x86,
	linux-kernel, linux-rtc, Thomas Gleixner, Mario Limonciello

Add a new function rtc_read_next_alarm() that reads the next expiring
alarm from the RTC timerqueue. This is different from rtc_read_alarm(),
which only reads the aie_timer.

The wakealarm sysfs file programs the rtc->aie_timer, whereas the
alarmtimer suspend routine programs its own timer into the RTC timerqueue.
Both timers end up in the RTC's timerqueue, and the first expiring timer
is what gets armed in the hardware.

This new function allows code to query which alarm will actually fire
next, regardless of which subsystem programmed it. This is needed by
platform code that needs to program secondary timers based on the
actual next wakeup time.

Link: https://lore.kernel.org/all/87ed50z0le.ffs@tglx
Suggested-by: Thomas Gleixner <tglx@linutronix.de>
Assisted-by: Claude:claude-opus-4-6
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
v2:
 * Drop pointless variable assignments
 * Add missing ":" in kdoc for returns
---
 drivers/rtc/interface.c | 40 ++++++++++++++++++++++++++++++++++++++++
 include/linux/rtc.h     |  2 ++
 2 files changed, 42 insertions(+)

diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c
index 1906f4884a834..7859be8f2a923 100644
--- a/drivers/rtc/interface.c
+++ b/drivers/rtc/interface.c
@@ -384,6 +384,46 @@ int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
 	return err;
 }
 
+/**
+ * rtc_read_next_alarm - read the next expiring alarm
+ * @rtc: RTC device
+ * @alarm: storage for the alarm information
+ *
+ * Read the next expiring alarm from the RTC timerqueue. This returns
+ * the alarm that will actually fire next, which may be different from
+ * rtc_read_alarm() if multiple timers are queued (e.g., alarmtimer
+ * and wakealarm sysfs both active).
+ *
+ * Returns: 0 on success, -ENOENT if no alarm is pending, or other error.
+ */
+int rtc_read_next_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
+{
+	struct timerqueue_node *next;
+	int err;
+
+	if (!rtc || !alarm)
+		return -EINVAL;
+
+	err = mutex_lock_interruptible(&rtc->ops_lock);
+	if (err)
+		return err;
+
+	next = timerqueue_getnext(&rtc->timerqueue);
+	if (!next) {
+		err = -ENOENT;
+		goto unlock;
+	}
+
+	memset(alarm, 0, sizeof(struct rtc_wkalrm));
+	alarm->time = rtc_ktime_to_tm(next->expires);
+	alarm->enabled = 1;
+
+unlock:
+	mutex_unlock(&rtc->ops_lock);
+	return err;
+}
+EXPORT_SYMBOL_GPL(rtc_read_next_alarm);
+
 int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
 {
 	int err;
diff --git a/include/linux/rtc.h b/include/linux/rtc.h
index 95da051fb155d..c09fc22819d0c 100644
--- a/include/linux/rtc.h
+++ b/include/linux/rtc.h
@@ -190,6 +190,8 @@ extern int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm);
 int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm);
 extern int rtc_read_alarm(struct rtc_device *rtc,
 			struct rtc_wkalrm *alrm);
+extern int rtc_read_next_alarm(struct rtc_device *rtc,
+			       struct rtc_wkalrm *alrm);
 extern int rtc_set_alarm(struct rtc_device *rtc,
 				struct rtc_wkalrm *alrm);
 extern int rtc_initialize_alarm(struct rtc_device *rtc,
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v2 2/2] platform/x86: amd-pmc: Fix S0i3 wakeup with alarmtimer
  2026-05-21  4:37 [PATCH v2 0/2] Fix S0i3 wakeup with alarmtimer Mario Limonciello
  2026-05-21  4:37 ` [PATCH v2 1/2] rtc: Add rtc_read_next_alarm() to read next expiring timer Mario Limonciello
@ 2026-05-21  4:37 ` Mario Limonciello
  2026-06-25 10:06   ` Ilpo Järvinen
  2026-06-25 12:43 ` (subset) [PATCH v2 0/2] " Alexandre Belloni
  2 siblings, 1 reply; 8+ messages in thread
From: Mario Limonciello @ 2026-05-21  4:37 UTC (permalink / raw)
  To: Shyam Sundar S K, Alexandre Belloni
  Cc: Hans de Goede, Ilpo Järvinen, platform-driver-x86,
	linux-kernel, linux-rtc, Thomas Gleixner, Mario Limonciello

It was reported that suspend-then-hibernate stopped working with modern
systemd versions on AMD Cezanne systems. The reason for this breakage
was because systemd switched to using alarmtimer instead of the wakealarm
sysfs file.

On AMD Cezanne systems, amd_pmc_verify_czn_rtc() programs a secondary
timer with the alarm time. This was introduced by
commit 59348401ebed ("platform/x86: amd-pmc: Add special handling for
timer based S0i3 wakeup"). However, this function uses rtc_read_alarm(),
which only reads the aie_timer, not the next expiring timer from the
timerqueue.

When both alarmtimer and wakealarm are active, the first expiring timer
might be the alarmtimer, but amd_pmc_verify_czn_rtc() would only see
the aie_timer, potentially missing the earlier alarm.

Switch to rtc_read_next_alarm() to read whichever timer will fire next.
Also handle -ENOENT (no alarm pending) explicitly as a non-error case.

Closes: https://gitlab.freedesktop.org/drm/amd/-/issues/3591
Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
---
 drivers/platform/x86/amd/pmc/pmc.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/platform/x86/amd/pmc/pmc.c b/drivers/platform/x86/amd/pmc/pmc.c
index 50f5784f2aa2e..8cd2db0ccaacd 100644
--- a/drivers/platform/x86/amd/pmc/pmc.c
+++ b/drivers/platform/x86/amd/pmc/pmc.c
@@ -595,9 +595,12 @@ static int amd_pmc_verify_czn_rtc(struct amd_pmc_dev *pdev, u32 *arg)
 	rtc_device = rtc_class_open("rtc0");
 	if (!rtc_device)
 		return 0;
-	rc = rtc_read_alarm(rtc_device, &alarm);
-	if (rc)
-		return rc;
+	rc = rtc_read_next_alarm(rtc_device, &alarm);
+	if (rc) {
+		if (rc == -ENOENT)
+			dev_dbg(pdev->dev, "no alarm pending\n");
+		return rc == -ENOENT ? 0 : rc;
+	}
 	if (!alarm.enabled) {
 		dev_dbg(pdev->dev, "alarm not enabled\n");
 		return 0;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: (subset) [PATCH v2 1/2] rtc: Add rtc_read_next_alarm() to read next expiring timer
  2026-05-21  4:37 ` [PATCH v2 1/2] rtc: Add rtc_read_next_alarm() to read next expiring timer Mario Limonciello
@ 2026-06-24 21:15   ` Alexandre Belloni
  2026-06-24 23:30     ` Mario Limonciello
  0 siblings, 1 reply; 8+ messages in thread
From: Alexandre Belloni @ 2026-06-24 21:15 UTC (permalink / raw)
  To: Shyam Sundar S K, Mario Limonciello
  Cc: Hans de Goede, Ilpo Järvinen, platform-driver-x86,
	linux-kernel, linux-rtc, Thomas Gleixner

On Wed, 20 May 2026 23:37:13 -0500, Mario Limonciello wrote:
> Add a new function rtc_read_next_alarm() that reads the next expiring
> alarm from the RTC timerqueue. This is different from rtc_read_alarm(),
> which only reads the aie_timer.
> 
> The wakealarm sysfs file programs the rtc->aie_timer, whereas the
> alarmtimer suspend routine programs its own timer into the RTC timerqueue.
> Both timers end up in the RTC's timerqueue, and the first expiring timer
> is what gets armed in the hardware.
> 
> [...]

Applied, thanks!

[1/2] rtc: Add rtc_read_next_alarm() to read next expiring timer
      https://git.kernel.org/abelloni/c/947d7ea6f60b

Best regards,

-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: (subset) [PATCH v2 1/2] rtc: Add rtc_read_next_alarm() to read next expiring timer
  2026-06-24 21:15   ` (subset) " Alexandre Belloni
@ 2026-06-24 23:30     ` Mario Limonciello
  2026-06-25 10:08       ` Ilpo Järvinen
  0 siblings, 1 reply; 8+ messages in thread
From: Mario Limonciello @ 2026-06-24 23:30 UTC (permalink / raw)
  To: Shyam Sundar S K, Ilpo Järvinen
  Cc: Hans de Goede, platform-driver-x86, linux-kernel, linux-rtc,
	Thomas Gleixner, Alexandre Belloni



On 6/24/26 14:15, Alexandre Belloni wrote:
> On Wed, 20 May 2026 23:37:13 -0500, Mario Limonciello wrote:
>> Add a new function rtc_read_next_alarm() that reads the next expiring
>> alarm from the RTC timerqueue. This is different from rtc_read_alarm(),
>> which only reads the aie_timer.
>>
>> The wakealarm sysfs file programs the rtc->aie_timer, whereas the
>> alarmtimer suspend routine programs its own timer into the RTC timerqueue.
>> Both timers end up in the RTC's timerqueue, and the first expiring timer
>> is what gets armed in the hardware.
>>
>> [...]
> 
> Applied, thanks!
> 
> [1/2] rtc: Add rtc_read_next_alarm() to read next expiring timer
>        https://git.kernel.org/abelloni/c/947d7ea6f60b
> 
> Best regards,
> 

Thanks!

Ilpo, Shyam,

How do you want to handle patch 2/2?  Immutable branch and come through 
pdx86?  Or would you rather it go through Alexandre's tree?

Thanks,

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 2/2] platform/x86: amd-pmc: Fix S0i3 wakeup with alarmtimer
  2026-05-21  4:37 ` [PATCH v2 2/2] platform/x86: amd-pmc: Fix S0i3 wakeup with alarmtimer Mario Limonciello
@ 2026-06-25 10:06   ` Ilpo Järvinen
  0 siblings, 0 replies; 8+ messages in thread
From: Ilpo Järvinen @ 2026-06-25 10:06 UTC (permalink / raw)
  To: Mario Limonciello
  Cc: Shyam Sundar S K, Alexandre Belloni, Hans de Goede,
	platform-driver-x86, LKML, linux-rtc, Thomas Gleixner

[-- Attachment #1: Type: text/plain, Size: 2080 bytes --]

On Wed, 20 May 2026, Mario Limonciello wrote:

> It was reported that suspend-then-hibernate stopped working with modern
> systemd versions on AMD Cezanne systems. The reason for this breakage
> was because systemd switched to using alarmtimer instead of the wakealarm
> sysfs file.
> 
> On AMD Cezanne systems, amd_pmc_verify_czn_rtc() programs a secondary
> timer with the alarm time. This was introduced by
> commit 59348401ebed ("platform/x86: amd-pmc: Add special handling for
> timer based S0i3 wakeup"). However, this function uses rtc_read_alarm(),
> which only reads the aie_timer, not the next expiring timer from the
> timerqueue.
> 
> When both alarmtimer and wakealarm are active, the first expiring timer
> might be the alarmtimer, but amd_pmc_verify_czn_rtc() would only see
> the aie_timer, potentially missing the earlier alarm.
> 
> Switch to rtc_read_next_alarm() to read whichever timer will fire next.
> Also handle -ENOENT (no alarm pending) explicitly as a non-error case.
> 
> Closes: https://gitlab.freedesktop.org/drm/amd/-/issues/3591
> Signed-off-by: Mario Limonciello <mario.limonciello@amd.com>
> ---
>  drivers/platform/x86/amd/pmc/pmc.c | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/platform/x86/amd/pmc/pmc.c b/drivers/platform/x86/amd/pmc/pmc.c
> index 50f5784f2aa2e..8cd2db0ccaacd 100644
> --- a/drivers/platform/x86/amd/pmc/pmc.c
> +++ b/drivers/platform/x86/amd/pmc/pmc.c
> @@ -595,9 +595,12 @@ static int amd_pmc_verify_czn_rtc(struct amd_pmc_dev *pdev, u32 *arg)
>  	rtc_device = rtc_class_open("rtc0");
>  	if (!rtc_device)
>  		return 0;
> -	rc = rtc_read_alarm(rtc_device, &alarm);
> -	if (rc)
> -		return rc;
> +	rc = rtc_read_next_alarm(rtc_device, &alarm);
> +	if (rc) {
> +		if (rc == -ENOENT)
> +			dev_dbg(pdev->dev, "no alarm pending\n");
> +		return rc == -ENOENT ? 0 : rc;
> +	}
>  	if (!alarm.enabled) {
>  		dev_dbg(pdev->dev, "alarm not enabled\n");
>  		return 0;

Acked-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>

-- 
 i.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: (subset) [PATCH v2 1/2] rtc: Add rtc_read_next_alarm() to read next expiring timer
  2026-06-24 23:30     ` Mario Limonciello
@ 2026-06-25 10:08       ` Ilpo Järvinen
  0 siblings, 0 replies; 8+ messages in thread
From: Ilpo Järvinen @ 2026-06-25 10:08 UTC (permalink / raw)
  To: Mario Limonciello
  Cc: Shyam Sundar S K, Hans de Goede, platform-driver-x86, LKML,
	linux-rtc, Thomas Gleixner, Alexandre Belloni

On Wed, 24 Jun 2026, Mario Limonciello wrote:
> On 6/24/26 14:15, Alexandre Belloni wrote:
> > On Wed, 20 May 2026 23:37:13 -0500, Mario Limonciello wrote:
> > > Add a new function rtc_read_next_alarm() that reads the next expiring
> > > alarm from the RTC timerqueue. This is different from rtc_read_alarm(),
> > > which only reads the aie_timer.
> > > 
> > > The wakealarm sysfs file programs the rtc->aie_timer, whereas the
> > > alarmtimer suspend routine programs its own timer into the RTC timerqueue.
> > > Both timers end up in the RTC's timerqueue, and the first expiring timer
> > > is what gets armed in the hardware.
> > > 
> > > [...]
> > 
> > Applied, thanks!
> > 
> > [1/2] rtc: Add rtc_read_next_alarm() to read next expiring timer
> >        https://git.kernel.org/abelloni/c/947d7ea6f60b
> > 
> > Best regards,
> > 
> 
> Thanks!
> 
> Ilpo, Shyam,
> 
> How do you want to handle patch 2/2?  Immutable branch and come through pdx86?
> Or would you rather it go through Alexandre's tree?

It would be easier if it too goes through Alexandre's tree and I don't 
expect conflicts on that area. I've replied with my Ack to 2/2.

-- 
 i.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: (subset) [PATCH v2 0/2] Fix S0i3 wakeup with alarmtimer
  2026-05-21  4:37 [PATCH v2 0/2] Fix S0i3 wakeup with alarmtimer Mario Limonciello
  2026-05-21  4:37 ` [PATCH v2 1/2] rtc: Add rtc_read_next_alarm() to read next expiring timer Mario Limonciello
  2026-05-21  4:37 ` [PATCH v2 2/2] platform/x86: amd-pmc: Fix S0i3 wakeup with alarmtimer Mario Limonciello
@ 2026-06-25 12:43 ` Alexandre Belloni
  2 siblings, 0 replies; 8+ messages in thread
From: Alexandre Belloni @ 2026-06-25 12:43 UTC (permalink / raw)
  To: Shyam Sundar S K, Mario Limonciello
  Cc: Hans de Goede, Ilpo Järvinen, platform-driver-x86,
	linux-kernel, linux-rtc, Thomas Gleixner

On Wed, 20 May 2026 23:37:12 -0500, Mario Limonciello wrote:
> It was reported that suspend-then-hibernate stopped working with modern
> systemd versions on AMD Cezanne systems. The reason for this breakage
> was because systemd switched to using alarmtimer instead of the wakealarm
> sysfs file.
> 
> But really it uncovered deeper problems with how these timers work.  Adjust
> the code accordingly.
> 
> [...]

Applied, thanks!

[2/2] platform/x86: amd-pmc: Fix S0i3 wakeup with alarmtimer
      https://git.kernel.org/abelloni/c/f7ecfc3fe575

Best regards,

-- 
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-06-25 12:43 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-21  4:37 [PATCH v2 0/2] Fix S0i3 wakeup with alarmtimer Mario Limonciello
2026-05-21  4:37 ` [PATCH v2 1/2] rtc: Add rtc_read_next_alarm() to read next expiring timer Mario Limonciello
2026-06-24 21:15   ` (subset) " Alexandre Belloni
2026-06-24 23:30     ` Mario Limonciello
2026-06-25 10:08       ` Ilpo Järvinen
2026-05-21  4:37 ` [PATCH v2 2/2] platform/x86: amd-pmc: Fix S0i3 wakeup with alarmtimer Mario Limonciello
2026-06-25 10:06   ` Ilpo Järvinen
2026-06-25 12:43 ` (subset) [PATCH v2 0/2] " Alexandre Belloni

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox