blob: 4776585d785a63e51df1be3580790eff8903e8a9 [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;
Srinivas Ramana20ee7cc2016-11-23 16:43:42 +053073static u64 suspend_ns;
74static u64 suspend_cycles;
75static u64 resume_cycles;
Russell Kinga42c3622012-09-09 18:39:28 +010076
77core_param(irqtime, irqtime, int, 0400);
Marc Zyngier2f0778af2011-12-15 12:19:23 +010078
Stephen Boyde7e3ff12013-07-18 16:21:17 -070079static u64 notrace jiffy_sched_clock_read(void)
Marc Zyngier2f0778af2011-12-15 12:19:23 +010080{
Stephen Boyde7e3ff12013-07-18 16:21:17 -070081 /*
82 * We don't need to use get_jiffies_64 on 32-bit arches here
83 * because we register with BITS_PER_LONG
84 */
85 return (u64)(jiffies - INITIAL_JIFFIES);
Marc Zyngier2f0778af2011-12-15 12:19:23 +010086}
87
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070088static struct clock_data cd ____cacheline_aligned = {
Daniel Thompson1809bfa2015-03-26 12:23:26 -070089 .read_data[0] = { .mult = NSEC_PER_SEC / HZ,
90 .read_sched_clock = jiffy_sched_clock_read, },
Daniel Thompson13dbeb32015-03-26 12:23:24 -070091 .actual_read_sched_clock = jiffy_sched_clock_read,
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070092};
Marc Zyngier2f0778af2011-12-15 12:19:23 +010093
Stephen Boydcea15092013-04-18 17:33:40 +010094static inline u64 notrace cyc_to_ns(u64 cyc, u32 mult, u32 shift)
Marc Zyngier2f0778af2011-12-15 12:19:23 +010095{
96 return (cyc * mult) >> shift;
97}
98
Stephen Boydb4042ce2013-07-18 16:21:19 -070099unsigned long long notrace sched_clock(void)
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100100{
Daniel Thompson8710e912015-03-26 12:23:22 -0700101 u64 cyc, res;
Stephen Boyd85c3d2d2013-07-18 16:21:15 -0700102 unsigned long seq;
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700103 struct clock_read_data *rd;
Stephen Boyd336ae112013-06-17 15:40:58 -0700104
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100105 do {
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700106 seq = raw_read_seqcount(&cd.seq);
107 rd = cd.read_data + (seq & 1);
Daniel Thompson8710e912015-03-26 12:23:22 -0700108
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700109 cyc = (rd->read_sched_clock() - rd->epoch_cyc) &
110 rd->sched_clock_mask;
111 res = rd->epoch_ns + cyc_to_ns(cyc, rd->mult, rd->shift);
Stephen Boyd85c3d2d2013-07-18 16:21:15 -0700112 } while (read_seqcount_retry(&cd.seq, seq));
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100113
Daniel Thompson8710e912015-03-26 12:23:22 -0700114 return res;
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100115}
116
117/*
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700118 * Updating the data required to read the clock.
119 *
Ingo Molnar32fea562015-03-27 07:08:06 +0100120 * sched_clock() will never observe mis-matched data even if called from
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700121 * an NMI. We do this by maintaining an odd/even copy of the data and
Ingo Molnar32fea562015-03-27 07:08:06 +0100122 * steering sched_clock() to one or the other using a sequence counter.
123 * In order to preserve the data cache profile of sched_clock() as much
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700124 * as possible the system reverts back to the even copy when the update
125 * completes; the odd copy is used *only* during an update.
126 */
127static void update_clock_read_data(struct clock_read_data *rd)
128{
129 /* update the backup (odd) copy with the new data */
130 cd.read_data[1] = *rd;
131
132 /* steer readers towards the odd copy */
133 raw_write_seqcount_latch(&cd.seq);
134
135 /* now its safe for us to update the normal (even) copy */
136 cd.read_data[0] = *rd;
137
138 /* switch readers back to the even copy */
139 raw_write_seqcount_latch(&cd.seq);
140}
141
142/*
Ingo Molnar32fea562015-03-27 07:08:06 +0100143 * Atomically update the sched_clock() epoch.
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100144 */
Daniel Thompson9fee69a2015-03-26 12:23:25 -0700145static void update_sched_clock(void)
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100146{
Stephen Boyde7e3ff12013-07-18 16:21:17 -0700147 u64 cyc;
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100148 u64 ns;
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700149 struct clock_read_data rd;
150
151 rd = cd.read_data[0];
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100152
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700153 cyc = cd.actual_read_sched_clock();
Ingo Molnar32fea562015-03-27 07:08:06 +0100154 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 -0700155
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700156 rd.epoch_ns = ns;
157 rd.epoch_cyc = cyc;
158
159 update_clock_read_data(&rd);
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100160}
Russell King112f38a42010-12-15 19:23:07 +0000161
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700162static enum hrtimer_restart sched_clock_poll(struct hrtimer *hrt)
Russell King112f38a42010-12-15 19:23:07 +0000163{
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100164 update_sched_clock();
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700165 hrtimer_forward_now(hrt, cd.wrap_kt);
Ingo Molnar32fea562015-03-27 07:08:06 +0100166
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700167 return HRTIMER_RESTART;
Russell King112f38a42010-12-15 19:23:07 +0000168}
169
Ingo Molnar32fea562015-03-27 07:08:06 +0100170void __init
171sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
Russell King112f38a42010-12-15 19:23:07 +0000172{
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800173 u64 res, wrap, new_mask, new_epoch, cyc, ns;
174 u32 new_mult, new_shift;
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700175 unsigned long r;
Russell King112f38a42010-12-15 19:23:07 +0000176 char r_unit;
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700177 struct clock_read_data rd;
Russell King112f38a42010-12-15 19:23:07 +0000178
Rob Herringc1157392013-02-08 16:14:59 -0600179 if (cd.rate > rate)
180 return;
181
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100182 WARN_ON(!irqs_disabled());
Russell King112f38a42010-12-15 19:23:07 +0000183
Ingo Molnar32fea562015-03-27 07:08:06 +0100184 /* Calculate the mult/shift to convert counter ticks to ns. */
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800185 clocks_calc_mult_shift(&new_mult, &new_shift, rate, NSEC_PER_SEC, 3600);
186
187 new_mask = CLOCKSOURCE_MASK(bits);
Daniel Thompson8710e912015-03-26 12:23:22 -0700188 cd.rate = rate;
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800189
Ingo Molnar32fea562015-03-27 07:08:06 +0100190 /* Calculate how many nanosecs until we risk wrapping */
John Stultzfb82fe22015-03-11 21:16:31 -0700191 wrap = clocks_calc_max_nsecs(new_mult, new_shift, 0, new_mask, NULL);
Daniel Thompson8710e912015-03-26 12:23:22 -0700192 cd.wrap_kt = ns_to_ktime(wrap);
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800193
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700194 rd = cd.read_data[0];
195
Ingo Molnar32fea562015-03-27 07:08:06 +0100196 /* Update epoch for new counter and update 'epoch_ns' from old counter*/
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800197 new_epoch = read();
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700198 cyc = cd.actual_read_sched_clock();
Ingo Molnar32fea562015-03-27 07:08:06 +0100199 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 -0700200 cd.actual_read_sched_clock = read;
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800201
Ingo Molnar32fea562015-03-27 07:08:06 +0100202 rd.read_sched_clock = read;
203 rd.sched_clock_mask = new_mask;
204 rd.mult = new_mult;
205 rd.shift = new_shift;
206 rd.epoch_cyc = new_epoch;
207 rd.epoch_ns = ns;
208
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700209 update_clock_read_data(&rd);
Russell King112f38a42010-12-15 19:23:07 +0000210
David Engrafae0258a2017-02-17 08:51:03 +0100211 if (sched_clock_timer.function != NULL) {
212 /* update timeout for clock wrap */
213 hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
214 }
215
Russell King112f38a42010-12-15 19:23:07 +0000216 r = rate;
217 if (r >= 4000000) {
218 r /= 1000000;
219 r_unit = 'M';
Ingo Molnar32fea562015-03-27 07:08:06 +0100220 } else {
221 if (r >= 1000) {
222 r /= 1000;
223 r_unit = 'k';
224 } else {
225 r_unit = ' ';
226 }
227 }
Russell King112f38a42010-12-15 19:23:07 +0000228
Ingo Molnar32fea562015-03-27 07:08:06 +0100229 /* Calculate the ns resolution of this counter */
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800230 res = cyc_to_ns(1ULL, new_mult, new_shift);
231
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700232 pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lluns\n",
233 bits, r, r_unit, res, wrap);
Russell King112f38a42010-12-15 19:23:07 +0000234
Ingo Molnar32fea562015-03-27 07:08:06 +0100235 /* Enable IRQ time accounting if we have a fast enough sched_clock() */
Russell Kinga42c3622012-09-09 18:39:28 +0100236 if (irqtime > 0 || (irqtime == -1 && rate >= 1000000))
237 enable_sched_clock_irqtime();
238
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100239 pr_debug("Registered %pF as sched_clock source\n", read);
240}
241
Russell King211baa702011-01-11 16:23:04 +0000242void __init sched_clock_postinit(void)
243{
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100244 /*
Ingo Molnar32fea562015-03-27 07:08:06 +0100245 * If no sched_clock() function has been provided at that point,
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100246 * make it the final one one.
247 */
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700248 if (cd.actual_read_sched_clock == jiffy_sched_clock_read)
Stephen Boyde7e3ff12013-07-18 16:21:17 -0700249 sched_clock_register(jiffy_sched_clock_read, BITS_PER_LONG, HZ);
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100250
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700251 update_sched_clock();
252
253 /*
254 * Start the timer to keep sched_clock() properly updated and
255 * sets the initial epoch.
256 */
257 hrtimer_init(&sched_clock_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
258 sched_clock_timer.function = sched_clock_poll;
259 hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
Russell King211baa702011-01-11 16:23:04 +0000260}
Russell Kingf153d012012-02-04 12:31:27 +0000261
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700262/*
263 * Clock read function for use when the clock is suspended.
264 *
265 * This function makes it appear to sched_clock() as if the clock
266 * stopped counting at its last update.
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700267 *
268 * This function must only be called from the critical
269 * section in sched_clock(). It relies on the read_seqcount_retry()
270 * at the end of the critical section to be sure we observe the
Ingo Molnar32fea562015-03-27 07:08:06 +0100271 * correct copy of 'epoch_cyc'.
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700272 */
273static u64 notrace suspended_sched_clock_read(void)
274{
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700275 unsigned long seq = raw_read_seqcount(&cd.seq);
276
277 return cd.read_data[seq & 1].epoch_cyc;
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700278}
279
Stephen Boydb71f8dc2017-08-11 14:23:19 -0700280int sched_clock_suspend(void)
Russell Kingf153d012012-02-04 12:31:27 +0000281{
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700282 struct clock_read_data *rd = &cd.read_data[0];
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -0700283
Stephen Boydf723aa12014-07-23 21:03:50 -0700284 update_sched_clock();
Srinivas Ramana20ee7cc2016-11-23 16:43:42 +0530285
286 suspend_ns = rd->epoch_ns;
287 suspend_cycles = rd->epoch_cyc;
288 pr_info("suspend ns:%17llu suspend cycles:%17llu\n",
289 rd->epoch_ns, rd->epoch_cyc);
Stephen Boydf723aa12014-07-23 21:03:50 -0700290 hrtimer_cancel(&sched_clock_timer);
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700291 rd->read_sched_clock = suspended_sched_clock_read;
Ingo Molnar32fea562015-03-27 07:08:06 +0100292
Russell Kingf153d012012-02-04 12:31:27 +0000293 return 0;
294}
295
Stephen Boydb71f8dc2017-08-11 14:23:19 -0700296void sched_clock_resume(void)
Colin Cross237ec6f2012-08-07 19:05:10 +0100297{
Daniel Thompson1809bfa2015-03-26 12:23:26 -0700298 struct clock_read_data *rd = &cd.read_data[0];
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -0700299
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700300 rd->epoch_cyc = cd.actual_read_sched_clock();
Srinivas Ramana20ee7cc2016-11-23 16:43:42 +0530301 resume_cycles = rd->epoch_cyc;
302 pr_info("resume cycles:%17llu\n", rd->epoch_cyc);
Stephen Boydf723aa12014-07-23 21:03:50 -0700303 hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700304 rd->read_sched_clock = cd.actual_read_sched_clock;
Colin Cross237ec6f2012-08-07 19:05:10 +0100305}
306
Russell Kingf153d012012-02-04 12:31:27 +0000307static struct syscore_ops sched_clock_ops = {
Ingo Molnar32fea562015-03-27 07:08:06 +0100308 .suspend = sched_clock_suspend,
309 .resume = sched_clock_resume,
Russell Kingf153d012012-02-04 12:31:27 +0000310};
311
312static int __init sched_clock_syscore_init(void)
313{
314 register_syscore_ops(&sched_clock_ops);
Ingo Molnar32fea562015-03-27 07:08:06 +0100315
Russell Kingf153d012012-02-04 12:31:27 +0000316 return 0;
317}
318device_initcall(sched_clock_syscore_init);