blob: 382b159d8592ee9b0c6fddcdd783e72d29e4add7 [file] [log] [blame]
Russell King112f38a42010-12-15 19:23:07 +00001/*
Ingo Molnar32fea562015-03-27 07:08:06 +01002 * sched_clock.c: Generic sched_clock() support, to extend low level
3 * hardware time counters to full 64-bit ns values.
Russell King112f38a42010-12-15 19:23:07 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9#include <linux/clocksource.h>
10#include <linux/init.h>
11#include <linux/jiffies.h>
Stephen Boyda08ca5d2013-07-18 16:21:16 -070012#include <linux/ktime.h>
Russell King112f38a42010-12-15 19:23:07 +000013#include <linux/kernel.h>
Russell Kinga42c3622012-09-09 18:39:28 +010014#include <linux/moduleparam.h>
Russell King112f38a42010-12-15 19:23:07 +000015#include <linux/sched.h>
Russell Kingf153d012012-02-04 12:31:27 +000016#include <linux/syscore_ops.h>
Stephen Boyda08ca5d2013-07-18 16:21:16 -070017#include <linux/hrtimer.h>
Stephen Boyd38ff87f2013-06-01 23:39:40 -070018#include <linux/sched_clock.h>
Stephen Boyd85c3d2d2013-07-18 16:21:15 -070019#include <linux/seqlock.h>
Stephen Boyde7e3ff12013-07-18 16:21:17 -070020#include <linux/bitops.h>
Russell King112f38a42010-12-15 19:23:07 +000021
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070022/**
Ingo Molnar32fea562015-03-27 07:08:06 +010023 * struct clock_read_data - data required to read from sched_clock()
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070024 *
Ingo Molnar32fea562015-03-27 07:08:06 +010025 * @epoch_ns: sched_clock() value at last update
26 * @epoch_cyc: Clock cycle value at last update.
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070027 * @sched_clock_mask: Bitmask for two's complement subtraction of non 64bit
Ingo Molnar32fea562015-03-27 07:08:06 +010028 * clocks.
29 * @read_sched_clock: Current clock source (or dummy source when suspended).
30 * @mult: Multipler for scaled math conversion.
31 * @shift: Shift value for scaled math conversion.
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070032 *
33 * Care must be taken when updating this structure; it is read by
Daniel Thompson13dbeb32015-03-26 12:23:24 -070034 * some very hot code paths. It occupies <=40 bytes and, when combined
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070035 * with the seqcount used to synchronize access, comfortably fits into
36 * a 64 byte cache line.
37 */
38struct clock_read_data {
Marc Zyngier2f0778af2011-12-15 12:19:23 +010039 u64 epoch_ns;
Stephen Boyde7e3ff12013-07-18 16:21:17 -070040 u64 epoch_cyc;
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070041 u64 sched_clock_mask;
42 u64 (*read_sched_clock)(void);
Marc Zyngier2f0778af2011-12-15 12:19:23 +010043 u32 mult;
44 u32 shift;
45};
46
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070047/**
Ingo Molnar32fea562015-03-27 07:08:06 +010048 * struct clock_data - all data needed for sched_clock() (including
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070049 * registration of a new clock source)
50 *
Daniel Thompson1809bfa2015-03-26 12:23:26 -070051 * @seq: Sequence counter for protecting updates. The lowest
52 * bit is the index for @read_data.
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070053 * @read_data: Data required to read from sched_clock.
Ingo Molnar32fea562015-03-27 07:08:06 +010054 * @wrap_kt: Duration for which clock can run before wrapping.
55 * @rate: Tick rate of the registered clock.
56 * @actual_read_sched_clock: Registered hardware level clock read function.
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070057 *
58 * The ordering of this structure has been chosen to optimize cache
Ingo Molnar32fea562015-03-27 07:08:06 +010059 * performance. In particular 'seq' and 'read_data[0]' (combined) should fit
60 * into a single 64-byte cache line.
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070061 */
62struct clock_data {
Ingo Molnar32fea562015-03-27 07:08:06 +010063 seqcount_t seq;
64 struct clock_read_data read_data[2];
65 ktime_t wrap_kt;
66 unsigned long rate;
67
Daniel Thompson13dbeb32015-03-26 12:23:24 -070068 u64 (*actual_read_sched_clock)(void);
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070069};
70
Stephen Boyda08ca5d2013-07-18 16:21:16 -070071static struct hrtimer sched_clock_timer;
Russell Kinga42c3622012-09-09 18:39:28 +010072static int irqtime = -1;
73
74core_param(irqtime, irqtime, int, 0400);
Marc Zyngier2f0778af2011-12-15 12:19:23 +010075
Stephen Boyde7e3ff12013-07-18 16:21:17 -070076static u64 notrace jiffy_sched_clock_read(void)
Marc Zyngier2f0778af2011-12-15 12:19:23 +010077{
Stephen Boyde7e3ff12013-07-18 16:21:17 -070078 /*
79 * We don't need to use get_jiffies_64 on 32-bit arches here
80 * because we register with BITS_PER_LONG
81 */
82 return (u64)(jiffies - INITIAL_JIFFIES);
Marc Zyngier2f0778af2011-12-15 12:19:23 +010083}
84
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070085static struct clock_data cd ____cacheline_aligned = {
Daniel Thompson1809bfa2015-03-26 12:23:26 -070086 .read_data[0] = { .mult = NSEC_PER_SEC / HZ,
87 .read_sched_clock = jiffy_sched_clock_read, },
Daniel Thompson13dbeb32015-03-26 12:23:24 -070088 .actual_read_sched_clock = jiffy_sched_clock_read,
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070089};
Marc Zyngier2f0778af2011-12-15 12:19:23 +010090
Stephen Boydcea15092013-04-18 17:33:40 +010091static inline u64 notrace cyc_to_ns(u64 cyc, u32 mult, u32 shift)
Marc Zyngier2f0778af2011-12-15 12:19:23 +010092{
93 return (cyc * mult) >> shift;
94}
95
Stephen Boydb4042ce2013-07-18 16:21:19 -070096unsigned long long notrace sched_clock(void)
Marc Zyngier2f0778af2011-12-15 12:19:23 +010097{
Daniel Thompson8710e912015-03-26 12:23:22 -070098 u64 cyc, res;
Stephen Boyd85c3d2d2013-07-18 16:21:15 -070099 unsigned long seq;
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700100 struct clock_read_data *rd;
Stephen Boyd336ae112013-06-17 15:40:58 -0700101
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100102 do {
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700103 seq = raw_read_seqcount(&cd.seq);
104 rd = cd.read_data + (seq & 1);
Daniel Thompson8710e912015-03-26 12:23:22 -0700105
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700106 cyc = (rd->read_sched_clock() - rd->epoch_cyc) &
107 rd->sched_clock_mask;
108 res = rd->epoch_ns + cyc_to_ns(cyc, rd->mult, rd->shift);
Stephen Boyd85c3d2d2013-07-18 16:21:15 -0700109 } while (read_seqcount_retry(&cd.seq, seq));
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100110
Daniel Thompson8710e912015-03-26 12:23:22 -0700111 return res;
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100112}
113
114/*
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700115 * Updating the data required to read the clock.
116 *
Ingo Molnar32fea562015-03-27 07:08:06 +0100117 * sched_clock() will never observe mis-matched data even if called from
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700118 * an NMI. We do this by maintaining an odd/even copy of the data and
Ingo Molnar32fea562015-03-27 07:08:06 +0100119 * steering sched_clock() to one or the other using a sequence counter.
120 * In order to preserve the data cache profile of sched_clock() as much
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700121 * as possible the system reverts back to the even copy when the update
122 * completes; the odd copy is used *only* during an update.
123 */
124static void update_clock_read_data(struct clock_read_data *rd)
125{
126 /* update the backup (odd) copy with the new data */
127 cd.read_data[1] = *rd;
128
129 /* steer readers towards the odd copy */
130 raw_write_seqcount_latch(&cd.seq);
131
132 /* now its safe for us to update the normal (even) copy */
133 cd.read_data[0] = *rd;
134
135 /* switch readers back to the even copy */
136 raw_write_seqcount_latch(&cd.seq);
137}
138
139/*
Ingo Molnar32fea562015-03-27 07:08:06 +0100140 * Atomically update the sched_clock() epoch.
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100141 */
Daniel Thompson9fee69a2015-03-26 12:23:25 -0700142static void update_sched_clock(void)
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100143{
Stephen Boyde7e3ff12013-07-18 16:21:17 -0700144 u64 cyc;
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100145 u64 ns;
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700146 struct clock_read_data rd;
147
148 rd = cd.read_data[0];
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100149
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700150 cyc = cd.actual_read_sched_clock();
Ingo Molnar32fea562015-03-27 07:08:06 +0100151 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 -0700152
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700153 rd.epoch_ns = ns;
154 rd.epoch_cyc = cyc;
155
156 update_clock_read_data(&rd);
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100157}
Russell King112f38a42010-12-15 19:23:07 +0000158
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700159static enum hrtimer_restart sched_clock_poll(struct hrtimer *hrt)
Russell King112f38a42010-12-15 19:23:07 +0000160{
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100161 update_sched_clock();
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700162 hrtimer_forward_now(hrt, cd.wrap_kt);
Ingo Molnar32fea562015-03-27 07:08:06 +0100163
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700164 return HRTIMER_RESTART;
Russell King112f38a42010-12-15 19:23:07 +0000165}
166
Ingo Molnar32fea562015-03-27 07:08:06 +0100167void __init
168sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
Russell King112f38a42010-12-15 19:23:07 +0000169{
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800170 u64 res, wrap, new_mask, new_epoch, cyc, ns;
171 u32 new_mult, new_shift;
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700172 unsigned long r;
Russell King112f38a42010-12-15 19:23:07 +0000173 char r_unit;
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700174 struct clock_read_data rd;
Russell King112f38a42010-12-15 19:23:07 +0000175
Rob Herringc1157392013-02-08 16:14:59 -0600176 if (cd.rate > rate)
177 return;
178
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100179 WARN_ON(!irqs_disabled());
Russell King112f38a42010-12-15 19:23:07 +0000180
Ingo Molnar32fea562015-03-27 07:08:06 +0100181 /* Calculate the mult/shift to convert counter ticks to ns. */
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800182 clocks_calc_mult_shift(&new_mult, &new_shift, rate, NSEC_PER_SEC, 3600);
183
184 new_mask = CLOCKSOURCE_MASK(bits);
Daniel Thompson8710e912015-03-26 12:23:22 -0700185 cd.rate = rate;
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800186
Ingo Molnar32fea562015-03-27 07:08:06 +0100187 /* Calculate how many nanosecs until we risk wrapping */
John Stultzfb82fe22015-03-11 21:16:31 -0700188 wrap = clocks_calc_max_nsecs(new_mult, new_shift, 0, new_mask, NULL);
Daniel Thompson8710e912015-03-26 12:23:22 -0700189 cd.wrap_kt = ns_to_ktime(wrap);
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800190
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700191 rd = cd.read_data[0];
192
Ingo Molnar32fea562015-03-27 07:08:06 +0100193 /* Update epoch for new counter and update 'epoch_ns' from old counter*/
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800194 new_epoch = read();
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700195 cyc = cd.actual_read_sched_clock();
Ingo Molnar32fea562015-03-27 07:08:06 +0100196 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 -0700197 cd.actual_read_sched_clock = read;
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800198
Ingo Molnar32fea562015-03-27 07:08:06 +0100199 rd.read_sched_clock = read;
200 rd.sched_clock_mask = new_mask;
201 rd.mult = new_mult;
202 rd.shift = new_shift;
203 rd.epoch_cyc = new_epoch;
204 rd.epoch_ns = ns;
205
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700206 update_clock_read_data(&rd);
Russell King112f38a42010-12-15 19:23:07 +0000207
David Engrafae0258a2017-02-17 08:51:03 +0100208 if (sched_clock_timer.function != NULL) {
209 /* update timeout for clock wrap */
210 hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
211 }
212
Russell King112f38a42010-12-15 19:23:07 +0000213 r = rate;
214 if (r >= 4000000) {
215 r /= 1000000;
216 r_unit = 'M';
Ingo Molnar32fea562015-03-27 07:08:06 +0100217 } else {
218 if (r >= 1000) {
219 r /= 1000;
220 r_unit = 'k';
221 } else {
222 r_unit = ' ';
223 }
224 }
Russell King112f38a42010-12-15 19:23:07 +0000225
Ingo Molnar32fea562015-03-27 07:08:06 +0100226 /* Calculate the ns resolution of this counter */
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800227 res = cyc_to_ns(1ULL, new_mult, new_shift);
228
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700229 pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lluns\n",
230 bits, r, r_unit, res, wrap);
Russell King112f38a42010-12-15 19:23:07 +0000231
Ingo Molnar32fea562015-03-27 07:08:06 +0100232 /* Enable IRQ time accounting if we have a fast enough sched_clock() */
Russell Kinga42c3622012-09-09 18:39:28 +0100233 if (irqtime > 0 || (irqtime == -1 && rate >= 1000000))
234 enable_sched_clock_irqtime();
235
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100236 pr_debug("Registered %pF as sched_clock source\n", read);
237}
238
Russell King211baa702011-01-11 16:23:04 +0000239void __init sched_clock_postinit(void)
240{
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100241 /*
Ingo Molnar32fea562015-03-27 07:08:06 +0100242 * If no sched_clock() function has been provided at that point,
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100243 * make it the final one one.
244 */
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700245 if (cd.actual_read_sched_clock == jiffy_sched_clock_read)
Stephen Boyde7e3ff12013-07-18 16:21:17 -0700246 sched_clock_register(jiffy_sched_clock_read, BITS_PER_LONG, HZ);
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100247
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700248 update_sched_clock();
249
250 /*
251 * Start the timer to keep sched_clock() properly updated and
252 * sets the initial epoch.
253 */
254 hrtimer_init(&sched_clock_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
255 sched_clock_timer.function = sched_clock_poll;
256 hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
Russell King211baa702011-01-11 16:23:04 +0000257}
Russell Kingf153d012012-02-04 12:31:27 +0000258
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700259/*
260 * Clock read function for use when the clock is suspended.
261 *
262 * This function makes it appear to sched_clock() as if the clock
263 * stopped counting at its last update.
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700264 *
265 * This function must only be called from the critical
266 * section in sched_clock(). It relies on the read_seqcount_retry()
267 * at the end of the critical section to be sure we observe the
Ingo Molnar32fea562015-03-27 07:08:06 +0100268 * correct copy of 'epoch_cyc'.
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700269 */
270static u64 notrace suspended_sched_clock_read(void)
271{
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700272 unsigned long seq = raw_read_seqcount(&cd.seq);
273
274 return cd.read_data[seq & 1].epoch_cyc;
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700275}
276
Russell Kingf153d012012-02-04 12:31:27 +0000277static int sched_clock_suspend(void)
278{
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
Stephen Boydf723aa12014-07-23 21:03:50 -0700281 update_sched_clock();
282 hrtimer_cancel(&sched_clock_timer);
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700283 rd->read_sched_clock = suspended_sched_clock_read;
Ingo Molnar32fea562015-03-27 07:08:06 +0100284
Russell Kingf153d012012-02-04 12:31:27 +0000285 return 0;
286}
287
Colin Cross237ec6f2012-08-07 19:05:10 +0100288static void sched_clock_resume(void)
289{
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700290 struct clock_read_data *rd = &cd.read_data[0];
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -0700291
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700292 rd->epoch_cyc = cd.actual_read_sched_clock();
Stephen Boydf723aa12014-07-23 21:03:50 -0700293 hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700294 rd->read_sched_clock = cd.actual_read_sched_clock;
Colin Cross237ec6f2012-08-07 19:05:10 +0100295}
296
Russell Kingf153d012012-02-04 12:31:27 +0000297static struct syscore_ops sched_clock_ops = {
Ingo Molnar32fea562015-03-27 07:08:06 +0100298 .suspend = sched_clock_suspend,
299 .resume = sched_clock_resume,
Russell Kingf153d012012-02-04 12:31:27 +0000300};
301
302static int __init sched_clock_syscore_init(void)
303{
304 register_syscore_ops(&sched_clock_ops);
Ingo Molnar32fea562015-03-27 07:08:06 +0100305
Russell Kingf153d012012-02-04 12:31:27 +0000306 return 0;
307}
308device_initcall(sched_clock_syscore_init);