* [PATCH 1/2] time: Introduce one suspend clocksource to compensate the suspend time
@ 2018-07-17 7:55 Baolin Wang
2018-07-17 7:55 ` [PATCH 2/2] clocksource: sprd: Register one always-on timer to compensate " Baolin Wang
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: Baolin Wang @ 2018-07-17 7:55 UTC (permalink / raw)
To: tglx, john.stultz, sboyd, arnd, broonie, daniel.lezcano
Cc: linux-kernel, baolin.wang
On some hardware with multiple clocksources, we have coarse grained
clocksources that support the CLOCK_SOURCE_SUSPEND_NONSTOP flag, but
which are less than ideal for timekeeping whereas other clocksources
can be better candidates but halt on suspend.
Currently, the timekeeping core only supports timing suspend using
CLOCK_SOURCE_SUSPEND_NONSTOP clocksources if that clocksource is the
current clocksource for timekeeping.
As a result, some architectures try to implement read_persistent_clock64()
using those non-stop clocksources, but isn't really ideal, which will
introduce more duplicate code. To fix this, provide logic to allow a
registered SUSPEND_NONSTOP clocksource, which isn't the current
clocksource, to be used to calculate the suspend time.
Suggested-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
---
Changes from RFC v1:
- Improve commit message.
- Remove the WARN_ON_ONCE().
- Fix some coding style issues.
- Do not force to select a replacement suspend clocksource.
---
include/linux/clocksource.h | 3 +
kernel/time/clocksource.c | 149 +++++++++++++++++++++++++++++++++++++++++++
kernel/time/timekeeping.c | 22 ++++---
3 files changed, 166 insertions(+), 8 deletions(-)
diff --git a/include/linux/clocksource.h b/include/linux/clocksource.h
index 7dff196..3089189 100644
--- a/include/linux/clocksource.h
+++ b/include/linux/clocksource.h
@@ -194,6 +194,9 @@ static inline s64 clocksource_cyc2ns(u64 cycles, u32 mult, u32 shift)
extern void clocksource_resume(void);
extern struct clocksource * __init clocksource_default_clock(void);
extern void clocksource_mark_unstable(struct clocksource *cs);
+extern void
+clocksource_start_suspend_timing(struct clocksource *cs, u64 start_cycles);
+extern u64 clocksource_stop_suspend_timing(struct clocksource *cs, u64 now);
extern u64
clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cycles);
diff --git a/kernel/time/clocksource.c b/kernel/time/clocksource.c
index f89a78e..f74fb00 100644
--- a/kernel/time/clocksource.c
+++ b/kernel/time/clocksource.c
@@ -94,6 +94,8 @@
/*[Clocksource internal variables]---------
* curr_clocksource:
* currently selected clocksource.
+ * suspend_clocksource:
+ * used to calculate the suspend time.
* clocksource_list:
* linked list with the registered clocksources
* clocksource_mutex:
@@ -102,10 +104,12 @@
* Name of the user-specified clocksource.
*/
static struct clocksource *curr_clocksource;
+static struct clocksource *suspend_clocksource;
static LIST_HEAD(clocksource_list);
static DEFINE_MUTEX(clocksource_mutex);
static char override_name[CS_NAME_LEN];
static int finished_booting;
+static u64 suspend_start;
#ifdef CONFIG_CLOCKSOURCE_WATCHDOG
static void clocksource_watchdog_work(struct work_struct *work);
@@ -447,6 +451,140 @@ static inline void clocksource_watchdog_unlock(unsigned long *flags) { }
#endif /* CONFIG_CLOCKSOURCE_WATCHDOG */
+static bool clocksource_is_suspend(struct clocksource *cs)
+{
+ return cs == suspend_clocksource;
+}
+
+static void __clocksource_suspend_select(struct clocksource *cs)
+{
+ /*
+ * Skip the clocksource which will be stopped in suspend state.
+ */
+ if (!(cs->flags & CLOCK_SOURCE_SUSPEND_NONSTOP))
+ return;
+
+ /*
+ * The nonstop clocksource can be selected as the suspend clocksource to
+ * calculate the suspend time, so it should not supply suspend/resume
+ * interfaces to suspend the nonstop clocksource when system suspends.
+ */
+ if (cs->suspend || cs->resume) {
+ pr_warn("Nonstop clocksource %s should not supply suspend/resume interfaces\n",
+ cs->name);
+ }
+
+ /* Pick the best rating. */
+ if (!suspend_clocksource || cs->rating > suspend_clocksource->rating)
+ suspend_clocksource = cs;
+}
+
+/**
+ * clocksource_suspend_select - Select the best clocksource for suspend timing
+ * @fallback: if select a fallback clocksource
+ */
+static void clocksource_suspend_select(bool fallback)
+{
+ struct clocksource *cs, *old_suspend;
+
+ old_suspend = suspend_clocksource;
+ if (fallback)
+ suspend_clocksource = NULL;
+
+ list_for_each_entry(cs, &clocksource_list, list) {
+ /* Skip current if we were requested for a fallback. */
+ if (fallback && cs == old_suspend)
+ continue;
+
+ __clocksource_suspend_select(cs);
+ }
+}
+
+/**
+ * clocksource_start_suspend_timing - Start measuring the suspend timing
+ * @cs: current clocksource from timekeeping
+ * @start_cycles: current cycles from timekeeping
+ *
+ * This function will save the start cycle values of suspend timer to calculate
+ * the suspend time when resuming system.
+ *
+ * This function is called late in the suspend process from timekeeping_suspend(),
+ * that means processes are freezed, non-boot cpus and interrupts are disabled
+ * now. It is therefore possible to start the suspend timer without taking the
+ * clocksource mutex.
+ */
+void clocksource_start_suspend_timing(struct clocksource *cs, u64 start_cycles)
+{
+ if (!suspend_clocksource)
+ return;
+
+ /*
+ * If current clocksource is the suspend timer, we should use the
+ * tkr_mono.cycle_last value as suspend_start to avoid same reading
+ * from suspend timer.
+ */
+ if (clocksource_is_suspend(cs)) {
+ suspend_start = start_cycles;
+ return;
+ }
+
+ if (suspend_clocksource->enable &&
+ suspend_clocksource->enable(suspend_clocksource)) {
+ pr_warn_once("Failed to enable the non-suspend-able clocksource.\n");
+ return;
+ }
+
+ suspend_start = suspend_clocksource->read(suspend_clocksource);
+}
+
+/**
+ * clocksource_stop_suspend_timing - Stop measuring the suspend timing
+ * @cs: current clocksource from timekeeping
+ * @cycle_now: current cycles from timekeeping
+ *
+ * This function will calculate the suspend time from suspend timer.
+ *
+ * Returns nanoseconds since suspend started, 0 if no usable suspend clocksource.
+ *
+ * This function is called early in the resume process from timekeeping_resume(),
+ * that means there is only one cpu, no processes are running and the interrupts
+ * are disabled. It is therefore possible to stop the suspend timer without
+ * taking the clocksource mutex.
+ */
+u64 clocksource_stop_suspend_timing(struct clocksource *cs, u64 cycle_now)
+{
+ u64 now, delta, nsec = 0;
+
+ if (!suspend_clocksource)
+ return 0;
+
+ /*
+ * If current clocksource is the suspend timer, we should use the
+ * tkr_mono.cycle_last value from timekeeping as current cycle to
+ * avoid same reading from suspend timer.
+ */
+ if (clocksource_is_suspend(cs))
+ now = cycle_now;
+ else
+ now = suspend_clocksource->read(suspend_clocksource);
+
+ if (now > suspend_start) {
+ delta = clocksource_delta(now, suspend_start,
+ suspend_clocksource->mask);
+ nsec = mul_u64_u32_shr(delta, suspend_clocksource->mult,
+ suspend_clocksource->shift);
+ }
+
+ /*
+ * Disable the suspend timer to save power if current clocksource is
+ * not the suspend timer.
+ */
+ if (!clocksource_is_suspend(cs) && suspend_clocksource->disable)
+ suspend_clocksource->disable(suspend_clocksource);
+
+ return nsec;
+}
+
/**
* clocksource_suspend - suspend the clocksource(s)
*/
@@ -792,6 +930,7 @@ int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
clocksource_select();
clocksource_select_watchdog(false);
+ __clocksource_suspend_select(cs);
mutex_unlock(&clocksource_mutex);
return 0;
}
@@ -820,6 +959,7 @@ void clocksource_change_rating(struct clocksource *cs, int rating)
clocksource_select();
clocksource_select_watchdog(false);
+ clocksource_suspend_select(false);
mutex_unlock(&clocksource_mutex);
}
EXPORT_SYMBOL(clocksource_change_rating);
@@ -845,6 +985,15 @@ static int clocksource_unbind(struct clocksource *cs)
return -EBUSY;
}
+ if (clocksource_is_suspend(cs)) {
+ /*
+ * Select and try to install a replacement suspend clocksource.
+ * If no replacement suspend clocksource, we will just let the
+ * clocksource go and have no suspend clocksource.
+ */
+ clocksource_suspend_select(true);
+ }
+
clocksource_watchdog_lock(&flags);
clocksource_dequeue_watchdog(cs);
list_del_init(&cs->list);
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 4786df9..d80dba3 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -1669,7 +1669,7 @@ void timekeeping_resume(void)
struct clocksource *clock = tk->tkr_mono.clock;
unsigned long flags;
struct timespec64 ts_new, ts_delta;
- u64 cycle_now;
+ u64 cycle_now, nsec;
sleeptime_injected = false;
read_persistent_clock64(&ts_new);
@@ -1693,13 +1693,8 @@ void timekeeping_resume(void)
* usable source. The rtc part is handled separately in rtc core code.
*/
cycle_now = tk_clock_read(&tk->tkr_mono);
- if ((clock->flags & CLOCK_SOURCE_SUSPEND_NONSTOP) &&
- cycle_now > tk->tkr_mono.cycle_last) {
- u64 nsec, cyc_delta;
-
- cyc_delta = clocksource_delta(cycle_now, tk->tkr_mono.cycle_last,
- tk->tkr_mono.mask);
- nsec = mul_u64_u32_shr(cyc_delta, clock->mult, clock->shift);
+ nsec = clocksource_stop_suspend_timing(clock, cycle_now);
+ if (nsec > 0) {
ts_delta = ns_to_timespec64(nsec);
sleeptime_injected = true;
} else if (timespec64_compare(&ts_new, &timekeeping_suspend_time) > 0) {
@@ -1732,6 +1727,8 @@ int timekeeping_suspend(void)
unsigned long flags;
struct timespec64 delta, delta_delta;
static struct timespec64 old_delta;
+ struct clocksource *curr_clock;
+ u64 cycle_now;
read_persistent_clock64(&timekeeping_suspend_time);
@@ -1748,6 +1745,15 @@ int timekeeping_suspend(void)
timekeeping_forward_now(tk);
timekeeping_suspended = 1;
+ /*
+ * Since we've called forward_now, cycle_last stores the value
+ * just read from the current clocksource. Save this to potentially
+ * use in suspend timing.
+ */
+ curr_clock = tk->tkr_mono.clock;
+ cycle_now = tk->tkr_mono.cycle_last;
+ clocksource_start_suspend_timing(curr_clock, cycle_now);
+
if (persistent_clock_exists) {
/*
* To avoid drift caused by repeated suspend/resumes,
--
1.7.9.5
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 2/2] clocksource: sprd: Register one always-on timer to compensate suspend time
2018-07-17 7:55 [PATCH 1/2] time: Introduce one suspend clocksource to compensate the suspend time Baolin Wang
@ 2018-07-17 7:55 ` Baolin Wang
2018-07-17 8:57 ` Daniel Lezcano
2018-07-17 8:40 ` [PATCH 1/2] time: Introduce one suspend clocksource to compensate the " Thomas Gleixner
` (2 subsequent siblings)
3 siblings, 1 reply; 6+ messages in thread
From: Baolin Wang @ 2018-07-17 7:55 UTC (permalink / raw)
To: tglx, john.stultz, sboyd, arnd, broonie, daniel.lezcano
Cc: linux-kernel, baolin.wang
Since the clocksource framework has introduced one suspend clocksource to
compensate the suspend time, this patch registers one always-on timer as
the suspend clocksource.
Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
---
Changes from RFC v1:
- None.
---
drivers/clocksource/timer-sprd.c | 50 ++++++++++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+)
diff --git a/drivers/clocksource/timer-sprd.c b/drivers/clocksource/timer-sprd.c
index ef9ebea..430cb99 100644
--- a/drivers/clocksource/timer-sprd.c
+++ b/drivers/clocksource/timer-sprd.c
@@ -156,4 +156,54 @@ static int __init sprd_timer_init(struct device_node *np)
return 0;
}
+static struct timer_of suspend_to = {
+ .flags = TIMER_OF_BASE | TIMER_OF_CLOCK,
+};
+
+static u64 sprd_suspend_timer_read(struct clocksource *cs)
+{
+ return ~(u64)readl_relaxed(timer_of_base(&suspend_to) +
+ TIMER_VALUE_SHDW_LO) & cs->mask;
+}
+
+static int sprd_suspend_timer_enable(struct clocksource *cs)
+{
+ sprd_timer_update_counter(timer_of_base(&suspend_to),
+ TIMER_VALUE_LO_MASK);
+ sprd_timer_enable(timer_of_base(&suspend_to), TIMER_CTL_PERIOD_MODE);
+
+ return 0;
+}
+
+static void sprd_suspend_timer_disable(struct clocksource *cs)
+{
+ sprd_timer_disable(timer_of_base(&suspend_to));
+}
+
+static struct clocksource suspend_clocksource = {
+ .name = "sprd_suspend_timer",
+ .rating = 200,
+ .read = sprd_suspend_timer_read,
+ .enable = sprd_suspend_timer_enable,
+ .disable = sprd_suspend_timer_disable,
+ .mask = CLOCKSOURCE_MASK(32),
+ .flags = CLOCK_SOURCE_IS_CONTINUOUS | CLOCK_SOURCE_SUSPEND_NONSTOP,
+};
+
+static int __init sprd_suspend_timer_init(struct device_node *np)
+{
+ int ret;
+
+ ret = timer_of_init(np, &suspend_to);
+ if (ret)
+ return ret;
+
+ clocksource_register_hz(&suspend_clocksource,
+ timer_of_rate(&suspend_to));
+
+ return 0;
+}
+
TIMER_OF_DECLARE(sc9860_timer, "sprd,sc9860-timer", sprd_timer_init);
+TIMER_OF_DECLARE(sc9860_persistent_timer, "sprd,sc9860-suspend-timer",
+ sprd_suspend_timer_init);
--
1.7.9.5
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH 1/2] time: Introduce one suspend clocksource to compensate the suspend time
2018-07-17 7:55 [PATCH 1/2] time: Introduce one suspend clocksource to compensate the suspend time Baolin Wang
2018-07-17 7:55 ` [PATCH 2/2] clocksource: sprd: Register one always-on timer to compensate " Baolin Wang
@ 2018-07-17 8:40 ` Thomas Gleixner
2018-07-17 8:48 ` Daniel Lezcano
2018-07-17 20:11 ` John Stultz
3 siblings, 0 replies; 6+ messages in thread
From: Thomas Gleixner @ 2018-07-17 8:40 UTC (permalink / raw)
To: Baolin Wang
Cc: john.stultz, sboyd, arnd, broonie, daniel.lezcano, linux-kernel
On Tue, 17 Jul 2018, Baolin Wang wrote:
> On some hardware with multiple clocksources, we have coarse grained
> clocksources that support the CLOCK_SOURCE_SUSPEND_NONSTOP flag, but
> which are less than ideal for timekeeping whereas other clocksources
> can be better candidates but halt on suspend.
>
> Currently, the timekeeping core only supports timing suspend using
> CLOCK_SOURCE_SUSPEND_NONSTOP clocksources if that clocksource is the
> current clocksource for timekeeping.
>
> As a result, some architectures try to implement read_persistent_clock64()
> using those non-stop clocksources, but isn't really ideal, which will
> introduce more duplicate code. To fix this, provide logic to allow a
> registered SUSPEND_NONSTOP clocksource, which isn't the current
> clocksource, to be used to calculate the suspend time.
>
> Suggested-by: Thomas Gleixner <tglx@linutronix.de>
> Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
Well done!
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 1/2] time: Introduce one suspend clocksource to compensate the suspend time
2018-07-17 7:55 [PATCH 1/2] time: Introduce one suspend clocksource to compensate the suspend time Baolin Wang
2018-07-17 7:55 ` [PATCH 2/2] clocksource: sprd: Register one always-on timer to compensate " Baolin Wang
2018-07-17 8:40 ` [PATCH 1/2] time: Introduce one suspend clocksource to compensate the " Thomas Gleixner
@ 2018-07-17 8:48 ` Daniel Lezcano
2018-07-17 20:11 ` John Stultz
3 siblings, 0 replies; 6+ messages in thread
From: Daniel Lezcano @ 2018-07-17 8:48 UTC (permalink / raw)
To: Baolin Wang, tglx, john.stultz, sboyd, arnd, broonie; +Cc: linux-kernel
On 17/07/2018 09:55, Baolin Wang wrote:
> On some hardware with multiple clocksources, we have coarse grained
> clocksources that support the CLOCK_SOURCE_SUSPEND_NONSTOP flag, but
> which are less than ideal for timekeeping whereas other clocksources
> can be better candidates but halt on suspend.
>
> Currently, the timekeeping core only supports timing suspend using
> CLOCK_SOURCE_SUSPEND_NONSTOP clocksources if that clocksource is the
> current clocksource for timekeeping.
>
> As a result, some architectures try to implement read_persistent_clock64()
> using those non-stop clocksources, but isn't really ideal, which will
> introduce more duplicate code. To fix this, provide logic to allow a
> registered SUSPEND_NONSTOP clocksource, which isn't the current
> clocksource, to be used to calculate the suspend time.
>
> Suggested-by: Thomas Gleixner <tglx@linutronix.de>
> Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
> ---
Reviewed-by: Daniel Lezcano <daniel.lezcano@linaro.org>
--
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs
Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 1/2] time: Introduce one suspend clocksource to compensate the suspend time
2018-07-17 7:55 [PATCH 1/2] time: Introduce one suspend clocksource to compensate the suspend time Baolin Wang
` (2 preceding siblings ...)
2018-07-17 8:48 ` Daniel Lezcano
@ 2018-07-17 20:11 ` John Stultz
3 siblings, 0 replies; 6+ messages in thread
From: John Stultz @ 2018-07-17 20:11 UTC (permalink / raw)
To: Baolin Wang
Cc: Thomas Gleixner, Stephen Boyd, Arnd Bergmann, Mark Brown,
Daniel Lezcano, lkml
On Tue, Jul 17, 2018 at 12:55 AM, Baolin Wang <baolin.wang@linaro.org> wrote:
> On some hardware with multiple clocksources, we have coarse grained
> clocksources that support the CLOCK_SOURCE_SUSPEND_NONSTOP flag, but
> which are less than ideal for timekeeping whereas other clocksources
> can be better candidates but halt on suspend.
>
> Currently, the timekeeping core only supports timing suspend using
> CLOCK_SOURCE_SUSPEND_NONSTOP clocksources if that clocksource is the
> current clocksource for timekeeping.
>
> As a result, some architectures try to implement read_persistent_clock64()
> using those non-stop clocksources, but isn't really ideal, which will
> introduce more duplicate code. To fix this, provide logic to allow a
> registered SUSPEND_NONSTOP clocksource, which isn't the current
> clocksource, to be used to calculate the suspend time.
>
> Suggested-by: Thomas Gleixner <tglx@linutronix.de>
> Signed-off-by: Baolin Wang <baolin.wang@linaro.org>
> ---
> Changes from RFC v1:
> - Improve commit message.
> - Remove the WARN_ON_ONCE().
> - Fix some coding style issues.
> - Do not force to select a replacement suspend clocksource.
Thanks again, I'll get this queued up for testing.
thanks
-john
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2018-07-17 20:11 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-17 7:55 [PATCH 1/2] time: Introduce one suspend clocksource to compensate the suspend time Baolin Wang
2018-07-17 7:55 ` [PATCH 2/2] clocksource: sprd: Register one always-on timer to compensate " Baolin Wang
2018-07-17 8:57 ` Daniel Lezcano
2018-07-17 8:40 ` [PATCH 1/2] time: Introduce one suspend clocksource to compensate the " Thomas Gleixner
2018-07-17 8:48 ` Daniel Lezcano
2018-07-17 20:11 ` John Stultz
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®