blob: b1b9b12899f5e43571e9cc39740f545cd6fad795 [file] [log] [blame]
Thomas Gleixner35728b82018-10-31 19:21:09 +01001// SPDX-License-Identifier: GPL-2.0
Russell King112f38a42010-12-15 19:23:07 +00002/*
Thomas Gleixner58c5fc22018-10-31 19:21:08 +01003 * Generic sched_clock() support, to extend low level hardware time
4 * counters to full 64-bit ns values.
Russell King112f38a42010-12-15 19:23:07 +00005 */
6#include <linux/clocksource.h>
7#include <linux/init.h>
8#include <linux/jiffies.h>
Stephen Boyda08ca5d2013-07-18 16:21:16 -07009#include <linux/ktime.h>
Russell King112f38a42010-12-15 19:23:07 +000010#include <linux/kernel.h>
Russell Kinga42c3622012-09-09 18:39:28 +010011#include <linux/moduleparam.h>
Russell King112f38a42010-12-15 19:23:07 +000012#include <linux/sched.h>
Ingo Molnare6017572017-02-01 16:36:40 +010013#include <linux/sched/clock.h>
Russell Kingf153d012012-02-04 12:31:27 +000014#include <linux/syscore_ops.h>
Stephen Boyda08ca5d2013-07-18 16:21:16 -070015#include <linux/hrtimer.h>
Stephen Boyd38ff87f2013-06-01 23:39:40 -070016#include <linux/sched_clock.h>
Stephen Boyd85c3d2d2013-07-18 16:21:15 -070017#include <linux/seqlock.h>
Stephen Boyde7e3ff12013-07-18 16:21:17 -070018#include <linux/bitops.h>
Russell King112f38a42010-12-15 19:23:07 +000019
Ben Dooks (Codethink)086ee462019-10-22 14:12:26 +010020#include "timekeeping.h"
21
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070022/**
Ingo Molnar32fea562015-03-27 07:08:06 +010023 * struct clock_data - all data needed for sched_clock() (including
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070024 * registration of a new clock source)
25 *
Daniel Thompson1809bfa2015-03-26 12:23:26 -070026 * @seq: Sequence counter for protecting updates. The lowest
27 * bit is the index for @read_data.
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070028 * @read_data: Data required to read from sched_clock.
Ingo Molnar32fea562015-03-27 07:08:06 +010029 * @wrap_kt: Duration for which clock can run before wrapping.
30 * @rate: Tick rate of the registered clock.
31 * @actual_read_sched_clock: Registered hardware level clock read function.
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070032 *
33 * The ordering of this structure has been chosen to optimize cache
Ingo Molnar32fea562015-03-27 07:08:06 +010034 * performance. In particular 'seq' and 'read_data[0]' (combined) should fit
35 * into a single 64-byte cache line.
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070036 */
37struct clock_data {
Ahmed S. Darwisha690ed02020-08-27 13:40:40 +020038 seqcount_latch_t seq;
Ingo Molnar32fea562015-03-27 07:08:06 +010039 struct clock_read_data read_data[2];
40 ktime_t wrap_kt;
41 unsigned long rate;
42
Daniel Thompson13dbeb32015-03-26 12:23:24 -070043 u64 (*actual_read_sched_clock)(void);
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070044};
45
Stephen Boyda08ca5d2013-07-18 16:21:16 -070046static struct hrtimer sched_clock_timer;
Russell Kinga42c3622012-09-09 18:39:28 +010047static int irqtime = -1;
48
49core_param(irqtime, irqtime, int, 0400);
Marc Zyngier2f0778af2011-12-15 12:19:23 +010050
Stephen Boyde7e3ff12013-07-18 16:21:17 -070051static u64 notrace jiffy_sched_clock_read(void)
Marc Zyngier2f0778af2011-12-15 12:19:23 +010052{
Stephen Boyde7e3ff12013-07-18 16:21:17 -070053 /*
54 * We don't need to use get_jiffies_64 on 32-bit arches here
55 * because we register with BITS_PER_LONG
56 */
57 return (u64)(jiffies - INITIAL_JIFFIES);
Marc Zyngier2f0778af2011-12-15 12:19:23 +010058}
59
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070060static struct clock_data cd ____cacheline_aligned = {
Daniel Thompson1809bfa2015-03-26 12:23:26 -070061 .read_data[0] = { .mult = NSEC_PER_SEC / HZ,
62 .read_sched_clock = jiffy_sched_clock_read, },
Daniel Thompson13dbeb32015-03-26 12:23:24 -070063 .actual_read_sched_clock = jiffy_sched_clock_read,
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070064};
Marc Zyngier2f0778af2011-12-15 12:19:23 +010065
Stephen Boydcea15092013-04-18 17:33:40 +010066static inline u64 notrace cyc_to_ns(u64 cyc, u32 mult, u32 shift)
Marc Zyngier2f0778af2011-12-15 12:19:23 +010067{
68 return (cyc * mult) >> shift;
69}
70
Quanyang Wang4cd2bb12020-09-29 16:20:27 +080071notrace struct clock_read_data *sched_clock_read_begin(unsigned int *seq)
Peter Zijlstra1b86abc12020-07-16 13:11:24 +080072{
Ahmed S. Darwishaadd6e52020-07-16 13:11:25 +080073 *seq = raw_read_seqcount_latch(&cd.seq);
Peter Zijlstra1b86abc12020-07-16 13:11:24 +080074 return cd.read_data + (*seq & 1);
75}
76
Quanyang Wang4cd2bb12020-09-29 16:20:27 +080077notrace int sched_clock_read_retry(unsigned int seq)
Peter Zijlstra1b86abc12020-07-16 13:11:24 +080078{
Ahmed S. Darwisha690ed02020-08-27 13:40:40 +020079 return read_seqcount_latch_retry(&cd.seq, seq);
Peter Zijlstra1b86abc12020-07-16 13:11:24 +080080}
81
Stephen Boydb4042ce2013-07-18 16:21:19 -070082unsigned long long notrace sched_clock(void)
Marc Zyngier2f0778af2011-12-15 12:19:23 +010083{
Daniel Thompson8710e912015-03-26 12:23:22 -070084 u64 cyc, res;
Rasmus Villemoese1e41b62019-03-18 20:55:56 +010085 unsigned int seq;
Daniel Thompson1809bfa2015-03-26 12:23:26 -070086 struct clock_read_data *rd;
Stephen Boyd336ae112013-06-17 15:40:58 -070087
Marc Zyngier2f0778af2011-12-15 12:19:23 +010088 do {
Peter Zijlstra1b86abc12020-07-16 13:11:24 +080089 rd = sched_clock_read_begin(&seq);
Daniel Thompson8710e912015-03-26 12:23:22 -070090
Daniel Thompson13dbeb32015-03-26 12:23:24 -070091 cyc = (rd->read_sched_clock() - rd->epoch_cyc) &
92 rd->sched_clock_mask;
93 res = rd->epoch_ns + cyc_to_ns(cyc, rd->mult, rd->shift);
Peter Zijlstra1b86abc12020-07-16 13:11:24 +080094 } while (sched_clock_read_retry(seq));
Marc Zyngier2f0778af2011-12-15 12:19:23 +010095
Daniel Thompson8710e912015-03-26 12:23:22 -070096 return res;
Marc Zyngier2f0778af2011-12-15 12:19:23 +010097}
98
99/*
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700100 * Updating the data required to read the clock.
101 *
Ingo Molnar32fea562015-03-27 07:08:06 +0100102 * sched_clock() will never observe mis-matched data even if called from
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700103 * an NMI. We do this by maintaining an odd/even copy of the data and
Ingo Molnar32fea562015-03-27 07:08:06 +0100104 * steering sched_clock() to one or the other using a sequence counter.
105 * In order to preserve the data cache profile of sched_clock() as much
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700106 * as possible the system reverts back to the even copy when the update
107 * completes; the odd copy is used *only* during an update.
108 */
109static void update_clock_read_data(struct clock_read_data *rd)
110{
111 /* update the backup (odd) copy with the new data */
112 cd.read_data[1] = *rd;
113
114 /* steer readers towards the odd copy */
115 raw_write_seqcount_latch(&cd.seq);
116
117 /* now its safe for us to update the normal (even) copy */
118 cd.read_data[0] = *rd;
119
120 /* switch readers back to the even copy */
121 raw_write_seqcount_latch(&cd.seq);
122}
123
124/*
Ingo Molnar32fea562015-03-27 07:08:06 +0100125 * Atomically update the sched_clock() epoch.
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100126 */
Daniel Thompson9fee69a2015-03-26 12:23:25 -0700127static void update_sched_clock(void)
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100128{
Stephen Boyde7e3ff12013-07-18 16:21:17 -0700129 u64 cyc;
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100130 u64 ns;
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700131 struct clock_read_data rd;
132
133 rd = cd.read_data[0];
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100134
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700135 cyc = cd.actual_read_sched_clock();
Ingo Molnar32fea562015-03-27 07:08:06 +0100136 ns = rd.epoch_ns + cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask, rd.mult, rd.shift);
Stephen Boyd85c3d2d2013-07-18 16:21:15 -0700137
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700138 rd.epoch_ns = ns;
139 rd.epoch_cyc = cyc;
140
141 update_clock_read_data(&rd);
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100142}
Russell King112f38a42010-12-15 19:23:07 +0000143
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700144static enum hrtimer_restart sched_clock_poll(struct hrtimer *hrt)
Russell King112f38a42010-12-15 19:23:07 +0000145{
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100146 update_sched_clock();
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700147 hrtimer_forward_now(hrt, cd.wrap_kt);
Ingo Molnar32fea562015-03-27 07:08:06 +0100148
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700149 return HRTIMER_RESTART;
Russell King112f38a42010-12-15 19:23:07 +0000150}
151
Ingo Molnar32fea562015-03-27 07:08:06 +0100152void __init
153sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
Russell King112f38a42010-12-15 19:23:07 +0000154{
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800155 u64 res, wrap, new_mask, new_epoch, cyc, ns;
156 u32 new_mult, new_shift;
Paul Cercueil27077452020-01-07 02:06:29 +0100157 unsigned long r, flags;
Russell King112f38a42010-12-15 19:23:07 +0000158 char r_unit;
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700159 struct clock_read_data rd;
Russell King112f38a42010-12-15 19:23:07 +0000160
Rob Herringc1157392013-02-08 16:14:59 -0600161 if (cd.rate > rate)
162 return;
163
Paul Cercueil27077452020-01-07 02:06:29 +0100164 /* Cannot register a sched_clock with interrupts on */
165 local_irq_save(flags);
Russell King112f38a42010-12-15 19:23:07 +0000166
Ingo Molnar32fea562015-03-27 07:08:06 +0100167 /* Calculate the mult/shift to convert counter ticks to ns. */
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800168 clocks_calc_mult_shift(&new_mult, &new_shift, rate, NSEC_PER_SEC, 3600);
169
170 new_mask = CLOCKSOURCE_MASK(bits);
Daniel Thompson8710e912015-03-26 12:23:22 -0700171 cd.rate = rate;
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800172
Ingo Molnar32fea562015-03-27 07:08:06 +0100173 /* Calculate how many nanosecs until we risk wrapping */
John Stultzfb82fe22015-03-11 21:16:31 -0700174 wrap = clocks_calc_max_nsecs(new_mult, new_shift, 0, new_mask, NULL);
Daniel Thompson8710e912015-03-26 12:23:22 -0700175 cd.wrap_kt = ns_to_ktime(wrap);
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800176
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700177 rd = cd.read_data[0];
178
Ingo Molnar32fea562015-03-27 07:08:06 +0100179 /* Update epoch for new counter and update 'epoch_ns' from old counter*/
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800180 new_epoch = read();
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700181 cyc = cd.actual_read_sched_clock();
Ingo Molnar32fea562015-03-27 07:08:06 +0100182 ns = rd.epoch_ns + cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask, rd.mult, rd.shift);
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700183 cd.actual_read_sched_clock = read;
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800184
Ingo Molnar32fea562015-03-27 07:08:06 +0100185 rd.read_sched_clock = read;
186 rd.sched_clock_mask = new_mask;
187 rd.mult = new_mult;
188 rd.shift = new_shift;
189 rd.epoch_cyc = new_epoch;
190 rd.epoch_ns = ns;
191
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700192 update_clock_read_data(&rd);
Russell King112f38a42010-12-15 19:23:07 +0000193
David Engraf1b8955b2017-02-17 08:51:03 +0100194 if (sched_clock_timer.function != NULL) {
195 /* update timeout for clock wrap */
Ahmed S. Darwish2c8bd582020-03-09 18:15:29 +0000196 hrtimer_start(&sched_clock_timer, cd.wrap_kt,
197 HRTIMER_MODE_REL_HARD);
David Engraf1b8955b2017-02-17 08:51:03 +0100198 }
199
Russell King112f38a42010-12-15 19:23:07 +0000200 r = rate;
201 if (r >= 4000000) {
202 r /= 1000000;
203 r_unit = 'M';
Ingo Molnar32fea562015-03-27 07:08:06 +0100204 } else {
205 if (r >= 1000) {
206 r /= 1000;
207 r_unit = 'k';
208 } else {
209 r_unit = ' ';
210 }
211 }
Russell King112f38a42010-12-15 19:23:07 +0000212
Ingo Molnar32fea562015-03-27 07:08:06 +0100213 /* Calculate the ns resolution of this counter */
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800214 res = cyc_to_ns(1ULL, new_mult, new_shift);
215
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700216 pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lluns\n",
217 bits, r, r_unit, res, wrap);
Russell King112f38a42010-12-15 19:23:07 +0000218
Ingo Molnar32fea562015-03-27 07:08:06 +0100219 /* Enable IRQ time accounting if we have a fast enough sched_clock() */
Russell Kinga42c3622012-09-09 18:39:28 +0100220 if (irqtime > 0 || (irqtime == -1 && rate >= 1000000))
221 enable_sched_clock_irqtime();
222
Paul Cercueil27077452020-01-07 02:06:29 +0100223 local_irq_restore(flags);
224
Sakari Ailusd75f7732019-03-25 21:32:28 +0200225 pr_debug("Registered %pS as sched_clock source\n", read);
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100226}
227
Pavel Tatashin5d2a4e92018-07-19 16:55:41 -0400228void __init generic_sched_clock_init(void)
Russell King211baa702011-01-11 16:23:04 +0000229{
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100230 /*
Ingo Molnar32fea562015-03-27 07:08:06 +0100231 * If no sched_clock() function has been provided at that point,
Randy Dunlapb0294f32020-08-06 20:32:48 -0700232 * make it the final one.
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100233 */
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700234 if (cd.actual_read_sched_clock == jiffy_sched_clock_read)
Stephen Boyde7e3ff12013-07-18 16:21:17 -0700235 sched_clock_register(jiffy_sched_clock_read, BITS_PER_LONG, HZ);
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100236
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700237 update_sched_clock();
238
239 /*
240 * Start the timer to keep sched_clock() properly updated and
241 * sets the initial epoch.
242 */
Ahmed S. Darwish2c8bd582020-03-09 18:15:29 +0000243 hrtimer_init(&sched_clock_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700244 sched_clock_timer.function = sched_clock_poll;
Ahmed S. Darwish2c8bd582020-03-09 18:15:29 +0000245 hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL_HARD);
Russell King211baa702011-01-11 16:23:04 +0000246}
Russell Kingf153d012012-02-04 12:31:27 +0000247
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700248/*
249 * Clock read function for use when the clock is suspended.
250 *
251 * This function makes it appear to sched_clock() as if the clock
252 * stopped counting at its last update.
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700253 *
254 * This function must only be called from the critical
255 * section in sched_clock(). It relies on the read_seqcount_retry()
256 * at the end of the critical section to be sure we observe the
Ingo Molnar32fea562015-03-27 07:08:06 +0100257 * correct copy of 'epoch_cyc'.
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700258 */
259static u64 notrace suspended_sched_clock_read(void)
260{
Ahmed S. Darwish58faf202020-08-27 13:40:37 +0200261 unsigned int seq = raw_read_seqcount_latch(&cd.seq);
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700262
263 return cd.read_data[seq & 1].epoch_cyc;
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700264}
265
Chang-An Chen3f2552f2019-03-29 10:59:09 +0800266int sched_clock_suspend(void)
Russell Kingf153d012012-02-04 12:31:27 +0000267{
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700268 struct clock_read_data *rd = &cd.read_data[0];
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -0700269
Stephen Boydf723aa12014-07-23 21:03:50 -0700270 update_sched_clock();
271 hrtimer_cancel(&sched_clock_timer);
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700272 rd->read_sched_clock = suspended_sched_clock_read;
Ingo Molnar32fea562015-03-27 07:08:06 +0100273
Russell Kingf153d012012-02-04 12:31:27 +0000274 return 0;
275}
276
Chang-An Chen3f2552f2019-03-29 10:59:09 +0800277void sched_clock_resume(void)
Colin Cross237ec6f2012-08-07 19:05:10 +0100278{
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700279 struct clock_read_data *rd = &cd.read_data[0];
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -0700280
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700281 rd->epoch_cyc = cd.actual_read_sched_clock();
Ahmed S. Darwish2c8bd582020-03-09 18:15:29 +0000282 hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL_HARD);
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700283 rd->read_sched_clock = cd.actual_read_sched_clock;
Colin Cross237ec6f2012-08-07 19:05:10 +0100284}
285
Russell Kingf153d012012-02-04 12:31:27 +0000286static struct syscore_ops sched_clock_ops = {
Ingo Molnar32fea562015-03-27 07:08:06 +0100287 .suspend = sched_clock_suspend,
288 .resume = sched_clock_resume,
Russell Kingf153d012012-02-04 12:31:27 +0000289};
290
291static int __init sched_clock_syscore_init(void)
292{
293 register_syscore_ops(&sched_clock_ops);
Ingo Molnar32fea562015-03-27 07:08:06 +0100294
Russell Kingf153d012012-02-04 12:31:27 +0000295 return 0;
296}
297device_initcall(sched_clock_syscore_init);