* [PATCH 0/2] Fix S0i3 wakeup with alarmtimer @ 2026-05-18 13:38 Mario Limonciello 2026-05-18 13:38 ` [PATCH 1/2] rtc: Add rtc_read_next_alarm() to read next expiring timer Mario Limonciello 2026-05-18 13:38 ` [PATCH 2/2] platform/x86: amd-pmc: Fix S0i3 wakeup with alarmtimer Mario Limonciello 0 siblings, 2 replies; 6+ messages in thread From: Mario Limonciello @ 2026-05-18 13:38 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 | 42 ++++++++++++++++++++++++++++++ include/linux/rtc.h | 2 ++ 3 files changed, 50 insertions(+), 3 deletions(-) -- 2.43.0 ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 1/2] rtc: Add rtc_read_next_alarm() to read next expiring timer 2026-05-18 13:38 [PATCH 0/2] Fix S0i3 wakeup with alarmtimer Mario Limonciello @ 2026-05-18 13:38 ` Mario Limonciello 2026-05-18 16:48 ` Ilpo Järvinen 2026-05-18 13:38 ` [PATCH 2/2] platform/x86: amd-pmc: Fix S0i3 wakeup with alarmtimer Mario Limonciello 1 sibling, 1 reply; 6+ messages in thread From: Mario Limonciello @ 2026-05-18 13:38 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> --- drivers/rtc/interface.c | 42 +++++++++++++++++++++++++++++++++++++++++ include/linux/rtc.h | 2 ++ 2 files changed, 44 insertions(+) diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c index 1906f4884a834..dfcb32e272eb9 100644 --- a/drivers/rtc/interface.c +++ b/drivers/rtc/interface.c @@ -384,6 +384,48 @@ 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; + alarm->pending = 0; + err = 0; + +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] 6+ messages in thread
* Re: [PATCH 1/2] rtc: Add rtc_read_next_alarm() to read next expiring timer 2026-05-18 13:38 ` [PATCH 1/2] rtc: Add rtc_read_next_alarm() to read next expiring timer Mario Limonciello @ 2026-05-18 16:48 ` Ilpo Järvinen 2026-05-18 17:30 ` Mario Limonciello 0 siblings, 1 reply; 6+ messages in thread From: Ilpo Järvinen @ 2026-05-18 16:48 UTC (permalink / raw) To: Mario Limonciello Cc: Shyam Sundar S K, Alexandre Belloni, Hans de Goede, platform-driver-x86, LKML, linux-rtc, Thomas Gleixner On Mon, 18 May 2026, 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. > > 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> > --- > drivers/rtc/interface.c | 42 +++++++++++++++++++++++++++++++++++++++++ > include/linux/rtc.h | 2 ++ > 2 files changed, 44 insertions(+) > > diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c > index 1906f4884a834..dfcb32e272eb9 100644 > --- a/drivers/rtc/interface.c > +++ b/drivers/rtc/interface.c > @@ -384,6 +384,48 @@ 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. Missing : > + */ > +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; > + alarm->pending = 0; Doesn't the preceeding memset() already clear everything? > + err = 0; Why is this needed? > + > +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, > -- i. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 1/2] rtc: Add rtc_read_next_alarm() to read next expiring timer 2026-05-18 16:48 ` Ilpo Järvinen @ 2026-05-18 17:30 ` Mario Limonciello 2026-05-18 17:35 ` Ilpo Järvinen 0 siblings, 1 reply; 6+ messages in thread From: Mario Limonciello @ 2026-05-18 17:30 UTC (permalink / raw) To: Ilpo Järvinen Cc: Shyam Sundar S K, Alexandre Belloni, Hans de Goede, platform-driver-x86, LKML, linux-rtc, Thomas Gleixner On 5/18/26 11:48, Ilpo Järvinen wrote: > On Mon, 18 May 2026, 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. >> >> 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> >> --- >> drivers/rtc/interface.c | 42 +++++++++++++++++++++++++++++++++++++++++ >> include/linux/rtc.h | 2 ++ >> 2 files changed, 44 insertions(+) >> >> diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c >> index 1906f4884a834..dfcb32e272eb9 100644 >> --- a/drivers/rtc/interface.c >> +++ b/drivers/rtc/interface.c >> @@ -384,6 +384,48 @@ 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. > > Missing : Like this you mean, right? Returns: 0 on success,.. > >> + */ >> +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; >> + alarm->pending = 0; > > Doesn't the preceeding memset() already clear everything? Yeah; good point. > >> + err = 0; > > Why is this needed? Oh I guess your point is that err was set to zero by mutex_lock_interruptible() already, so this is unecessary. Good catch, will drop it, thx. > >> + >> +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, >> > ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 1/2] rtc: Add rtc_read_next_alarm() to read next expiring timer 2026-05-18 17:30 ` Mario Limonciello @ 2026-05-18 17:35 ` Ilpo Järvinen 0 siblings, 0 replies; 6+ messages in thread From: Ilpo Järvinen @ 2026-05-18 17:35 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: 4277 bytes --] On Mon, 18 May 2026, Mario Limonciello wrote: > On 5/18/26 11:48, Ilpo Järvinen wrote: > > On Mon, 18 May 2026, 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. > > > > > > 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> > > > --- > > > drivers/rtc/interface.c | 42 +++++++++++++++++++++++++++++++++++++++++ > > > include/linux/rtc.h | 2 ++ > > > 2 files changed, 44 insertions(+) > > > > > > diff --git a/drivers/rtc/interface.c b/drivers/rtc/interface.c > > > index 1906f4884a834..dfcb32e272eb9 100644 > > > --- a/drivers/rtc/interface.c > > > +++ b/drivers/rtc/interface.c > > > @@ -384,6 +384,48 @@ 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. > > > > Missing : > > Like this you mean, right? > > Returns: 0 on success,.. Yes. > > > + */ > > > +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; > > > + alarm->pending = 0; > > > > Doesn't the preceeding memset() already clear everything? > > Yeah; good point. > > > > > > + err = 0; > > > > Why is this needed? > > Oh I guess your point is that err was set to zero by > mutex_lock_interruptible() already, so this is unecessary. > > Good catch, will drop it, thx. > > > > > > + > > > +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, > > > > > > > -- i. ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 2/2] platform/x86: amd-pmc: Fix S0i3 wakeup with alarmtimer 2026-05-18 13:38 [PATCH 0/2] Fix S0i3 wakeup with alarmtimer Mario Limonciello 2026-05-18 13:38 ` [PATCH 1/2] rtc: Add rtc_read_next_alarm() to read next expiring timer Mario Limonciello @ 2026-05-18 13:38 ` Mario Limonciello 1 sibling, 0 replies; 6+ messages in thread From: Mario Limonciello @ 2026-05-18 13:38 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] 6+ messages in thread
end of thread, other threads:[~2026-05-18 17:35 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-05-18 13:38 [PATCH 0/2] Fix S0i3 wakeup with alarmtimer Mario Limonciello 2026-05-18 13:38 ` [PATCH 1/2] rtc: Add rtc_read_next_alarm() to read next expiring timer Mario Limonciello 2026-05-18 16:48 ` Ilpo Järvinen 2026-05-18 17:30 ` Mario Limonciello 2026-05-18 17:35 ` Ilpo Järvinen 2026-05-18 13:38 ` [PATCH 2/2] platform/x86: amd-pmc: Fix S0i3 wakeup with alarmtimer Mario Limonciello
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox