mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Igor Velkov via B4 Relay <devnull+iav.iav.lv@kernel.org>
To: John Stultz <jstultz@google.com>,
	Thomas Gleixner <tglx@kernel.org>,
	 Alexandre Belloni <alexandre.belloni@bootlin.com>,
	 Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	 Conor Dooley <conor+dt@kernel.org>,
	 Neil Armstrong <neil.armstrong@linaro.org>,
	 Kevin Hilman <khilman@baylibre.com>
Cc: Stephen Boyd <sboyd@kernel.org>,
	Miroslav Lichvar <mlichvar@redhat.com>,
	 Jerome Brunet <jbrunet@baylibre.com>,
	 Martin Blumenstingl <martin.blumenstingl@googlemail.com>,
	 linux-rtc@vger.kernel.org, devicetree@vger.kernel.org,
	 linux-arm-kernel@lists.infradead.org,
	linux-amlogic@lists.infradead.org,  linux-kernel@vger.kernel.org,
	Igor Velkov <iav@iav.lv>
Subject: [PATCH v2 2/8] alarmtimer: Move the wakeup state into struct rtc_wakeup
Date: Mon, 16 Nov 2026 00:25:01 +0000	[thread overview]
Message-ID: <20261116-b4-alarmtimer-rtc-priority-v2-2-b2c69721e489@iav.lv> (raw)
In-Reply-To: <20261116-b4-alarmtimer-rtc-priority-v2-0-b2c69721e489@iav.lv>

From: Igor Velkov <iav@iav.lv>

The RTC and the RTC timer which alarmtimer programs on suspend and the
record of the clock_nanosleep() call the freezer interrupted are separate
static variables.

Collect them in one struct to prepare for an RTC per suspend state. No
functional change.

Signed-off-by: Igor Velkov <iav@iav.lv>
---
 kernel/time/alarmtimer.c | 60 +++++++++++++++++++++++++++++-------------------
 1 file changed, 36 insertions(+), 24 deletions(-)

diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
index 6c663f7d93c2..35630f0ff566 100644
--- a/kernel/time/alarmtimer.c
+++ b/kernel/time/alarmtimer.c
@@ -50,17 +50,29 @@ static struct alarm_base {
 } alarm_bases[ALARM_NUMTYPE];
 
 #if defined(CONFIG_POSIX_TIMERS) || defined(CONFIG_RTC_CLASS)
-/* freezer information to handle clock_nanosleep triggered wakeups */
-static enum alarmtimer_type freezer_alarmtype;
-static ktime_t freezer_expires;
-static ktime_t freezer_delta;
+/**
+ * struct rtc_wakeup - State for waking the system from suspend on alarms
+ * @rtc:		RTC programmed on suspend, NULL if none
+ * @timer:		RTC timer started on suspend
+ * @freezer_delta:	Least remaining time of the clock_nanosleep() calls
+ *			which the freezer interrupted, 0 if none
+ * @freezer_expires:	Expiry of that call
+ * @freezer_type:	Alarm type of that call
+ */
+struct rtc_wakeup {
+	struct rtc_device	*rtc;
+	struct rtc_timer	timer;
+	ktime_t			freezer_delta;
+	ktime_t			freezer_expires;
+	enum alarmtimer_type	freezer_type;
+};
+
+static struct rtc_wakeup wakeup_ctl;
+
 static DEFINE_SPINLOCK(freezer_delta_lock);
 #endif
 
 #ifdef CONFIG_RTC_CLASS
-/* rtc timer and device for setting alarm wakeups at suspend */
-static struct rtc_timer		rtctimer;
-static struct rtc_device	*rtcdev;
 
 /**
  * alarmtimer_has_rtcdev - Check whether an RTC can wake the system for alarms
@@ -69,7 +81,7 @@ static struct rtc_device	*rtcdev;
  */
 bool alarmtimer_has_rtcdev(void)
 {
-	return !!READ_ONCE(rtcdev);
+	return !!READ_ONCE(wakeup_ctl.rtc);
 }
 EXPORT_SYMBOL_GPL(alarmtimer_has_rtcdev);
 
@@ -78,7 +90,7 @@ static int alarmtimer_rtc_add_device(struct device *dev)
 	struct rtc_device *rtc = to_rtc_device(dev);
 	struct platform_device *pdev;
 
-	if (rtcdev)
+	if (wakeup_ctl.rtc)
 		return -EBUSY;
 
 	if (!test_bit(RTC_FEATURE_ALARM, rtc->features))
@@ -98,7 +110,7 @@ static int alarmtimer_rtc_add_device(struct device *dev)
 		return -1;
 	}
 
-	WRITE_ONCE(rtcdev, rtc);
+	WRITE_ONCE(wakeup_ctl.rtc, rtc);
 	/* Hold a reference so it doesn't go away */
 	get_device(dev);
 
@@ -107,7 +119,7 @@ static int alarmtimer_rtc_add_device(struct device *dev)
 
 static inline void alarmtimer_rtc_timer_init(void)
 {
-	rtc_timer_init(&rtctimer, NULL, NULL);
+	rtc_timer_init(&wakeup_ctl.timer, NULL, NULL);
 }
 
 static struct class_interface alarmtimer_rtc_interface = {
@@ -215,13 +227,13 @@ static int alarmtimer_suspend(struct device *dev)
 	int i, ret, type;
 
 	scoped_guard(spinlock_irqsave, &freezer_delta_lock) {
-		min = freezer_delta;
-		expires = freezer_expires;
-		type = freezer_alarmtype;
-		freezer_delta = 0;
+		min = wakeup_ctl.freezer_delta;
+		expires = wakeup_ctl.freezer_expires;
+		type = wakeup_ctl.freezer_type;
+		wakeup_ctl.freezer_delta = 0;
 	}
 
-	rtc = READ_ONCE(rtcdev);
+	rtc = READ_ONCE(wakeup_ctl.rtc);
 	/* If we have no rtcdev, just return */
 	if (!rtc)
 		return 0;
@@ -258,7 +270,7 @@ static int alarmtimer_suspend(struct device *dev)
 	trace_alarmtimer_suspend(expires, type);
 
 	/* Setup an rtc timer to fire that far in the future */
-	rtc_timer_cancel(rtc, &rtctimer);
+	rtc_timer_cancel(rtc, &wakeup_ctl.timer);
 	rtc_read_time(rtc, &tm);
 	now = rtc_tm_to_ktime(tm);
 
@@ -275,7 +287,7 @@ static int alarmtimer_suspend(struct device *dev)
 	now = ktime_add(now, min);
 
 	/* Set alarm, if in the past reject suspend briefly to handle */
-	ret = rtc_timer_start(rtc, &rtctimer, now, 0);
+	ret = rtc_timer_start(rtc, &wakeup_ctl.timer, now, 0);
 	if (ret < 0)
 		pm_wakeup_event(dev, MSEC_PER_SEC);
 	return ret;
@@ -285,9 +297,9 @@ static int alarmtimer_resume(struct device *dev)
 {
 	struct rtc_device *rtc;
 
-	rtc = READ_ONCE(rtcdev);
+	rtc = READ_ONCE(wakeup_ctl.rtc);
 	if (rtc)
-		rtc_timer_cancel(rtc, &rtctimer);
+		rtc_timer_cancel(rtc, &wakeup_ctl.timer);
 	return 0;
 }
 
@@ -462,10 +474,10 @@ static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
 	delta = ktime_sub(absexp, base->get_ktime());
 
 	guard(spinlock_irqsave)(&freezer_delta_lock);
-	if (!freezer_delta || (delta < freezer_delta)) {
-		freezer_delta = delta;
-		freezer_expires = absexp;
-		freezer_alarmtype = type;
+	if (!wakeup_ctl.freezer_delta || (delta < wakeup_ctl.freezer_delta)) {
+		wakeup_ctl.freezer_delta = delta;
+		wakeup_ctl.freezer_expires = absexp;
+		wakeup_ctl.freezer_type = type;
 	}
 }
 

-- 
2.43.0



  parent reply	other threads:[~2026-11-16  0:25 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16  0:24 [PATCH v2 0/8] alarmtimer: Pick the wakeup RTC per suspend state Igor Velkov via B4 Relay
2026-11-16  0:25 ` [PATCH v2 1/8] alarmtimer: Replace alarmtimer_get_rtcdev() with alarmtimer_has_rtcdev() Igor Velkov via B4 Relay
2026-11-16  0:25 ` Igor Velkov via B4 Relay [this message]
2026-11-16  0:25 ` [PATCH v2 3/8] alarmtimer: Replace the freezer delta with per type expiry times Igor Velkov via B4 Relay
2026-11-16  0:25 ` [PATCH v2 4/8] alarmtimer: Register the platform device without a parent Igor Velkov via B4 Relay
2026-11-16  0:25 ` [PATCH v2 5/8] alarmtimer: Keep an RTC per suspend state Igor Velkov via B4 Relay
2026-11-16  0:25 ` [PATCH v2 6/8] dt-bindings: rtc: Add linux,wakeup-states Igor Velkov via B4 Relay
2026-11-16  0:25 ` [PATCH v2 7/8] alarmtimer: Read the wakeup states of an RTC from linux,wakeup-states Igor Velkov via B4 Relay
2026-11-16  0:25 ` [PATCH v2 8/8] arm64: dts: amlogic: meson-sm1-odroid-hc4: Describe which RTC wakes the board Igor Velkov via B4 Relay

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261116-b4-alarmtimer-rtc-priority-v2-2-b2c69721e489@iav.lv \
    --to=devnull+iav.iav.lv@kernel.org \
    --cc=alexandre.belloni@bootlin.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=iav@iav.lv \
    --cc=jbrunet@baylibre.com \
    --cc=jstultz@google.com \
    --cc=khilman@baylibre.com \
    --cc=krzk+dt@kernel.org \
    --cc=linux-amlogic@lists.infradead.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rtc@vger.kernel.org \
    --cc=martin.blumenstingl@googlemail.com \
    --cc=mlichvar@redhat.com \
    --cc=neil.armstrong@linaro.org \
    --cc=robh@kernel.org \
    --cc=sboyd@kernel.org \
    --cc=tglx@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®