blob: 8adb9d0c969aea6a2cf605935064a2b2299c10b6 [file] [log] [blame]
Russell King112f38a42010-12-15 19:23:07 +00001/*
2 * sched_clock.c: support for extending counters to full 64-bit ns counter
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8#include <linux/clocksource.h>
9#include <linux/init.h>
10#include <linux/jiffies.h>
Stephen Boyda08ca5d2013-07-18 16:21:16 -070011#include <linux/ktime.h>
Russell King112f38a42010-12-15 19:23:07 +000012#include <linux/kernel.h>
Russell Kinga42c3622012-09-09 18:39:28 +010013#include <linux/moduleparam.h>
Russell King112f38a42010-12-15 19:23:07 +000014#include <linux/sched.h>
Russell Kingf153d012012-02-04 12:31:27 +000015#include <linux/syscore_ops.h>
Stephen Boyda08ca5d2013-07-18 16:21:16 -070016#include <linux/hrtimer.h>
Stephen Boyd38ff87f2013-06-01 23:39:40 -070017#include <linux/sched_clock.h>
Stephen Boyd85c3d2d2013-07-18 16:21:15 -070018#include <linux/seqlock.h>
Stephen Boyde7e3ff12013-07-18 16:21:17 -070019#include <linux/bitops.h>
Russell King112f38a42010-12-15 19:23:07 +000020
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070021/**
22 * struct clock_read_data - data required to read from sched_clock
23 *
24 * @epoch_ns: sched_clock value at last update
25 * @epoch_cyc: Clock cycle value at last update
26 * @sched_clock_mask: Bitmask for two's complement subtraction of non 64bit
27 * clocks
28 * @read_sched_clock: Current clock source (or dummy source when suspended)
29 * @mult: Multipler for scaled math conversion
30 * @shift: Shift value for scaled math conversion
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070031 *
32 * Care must be taken when updating this structure; it is read by
Daniel Thompson13dbeb32015-03-26 12:23:24 -070033 * some very hot code paths. It occupies <=40 bytes and, when combined
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070034 * with the seqcount used to synchronize access, comfortably fits into
35 * a 64 byte cache line.
36 */
37struct clock_read_data {
Marc Zyngier2f0778af2011-12-15 12:19:23 +010038 u64 epoch_ns;
Stephen Boyde7e3ff12013-07-18 16:21:17 -070039 u64 epoch_cyc;
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070040 u64 sched_clock_mask;
41 u64 (*read_sched_clock)(void);
Marc Zyngier2f0778af2011-12-15 12:19:23 +010042 u32 mult;
43 u32 shift;
44};
45
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070046/**
47 * struct clock_data - all data needed for sched_clock (including
48 * registration of a new clock source)
49 *
50 * @seq: Sequence counter for protecting updates.
51 * @read_data: Data required to read from sched_clock.
52 * @wrap_kt: Duration for which clock can run before wrapping
53 * @rate: Tick rate of the registered clock
54 * @actual_read_sched_clock: Registered clock read function
55 *
56 * The ordering of this structure has been chosen to optimize cache
57 * performance. In particular seq and read_data (combined) should fit
58 * into a single 64 byte cache line.
59 */
60struct clock_data {
61 seqcount_t seq;
62 struct clock_read_data read_data;
63 ktime_t wrap_kt;
64 unsigned long rate;
Daniel Thompson13dbeb32015-03-26 12:23:24 -070065 u64 (*actual_read_sched_clock)(void);
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070066};
67
Stephen Boyda08ca5d2013-07-18 16:21:16 -070068static struct hrtimer sched_clock_timer;
Russell Kinga42c3622012-09-09 18:39:28 +010069static int irqtime = -1;
70
71core_param(irqtime, irqtime, int, 0400);
Marc Zyngier2f0778af2011-12-15 12:19:23 +010072
Stephen Boyde7e3ff12013-07-18 16:21:17 -070073static u64 notrace jiffy_sched_clock_read(void)
Marc Zyngier2f0778af2011-12-15 12:19:23 +010074{
Stephen Boyde7e3ff12013-07-18 16:21:17 -070075 /*
76 * We don't need to use get_jiffies_64 on 32-bit arches here
77 * because we register with BITS_PER_LONG
78 */
79 return (u64)(jiffies - INITIAL_JIFFIES);
Marc Zyngier2f0778af2011-12-15 12:19:23 +010080}
81
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070082static struct clock_data cd ____cacheline_aligned = {
83 .read_data = { .mult = NSEC_PER_SEC / HZ,
84 .read_sched_clock = jiffy_sched_clock_read, },
Daniel Thompson13dbeb32015-03-26 12:23:24 -070085 .actual_read_sched_clock = jiffy_sched_clock_read,
86
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070087};
Marc Zyngier2f0778af2011-12-15 12:19:23 +010088
Stephen Boydcea15092013-04-18 17:33:40 +010089static inline u64 notrace cyc_to_ns(u64 cyc, u32 mult, u32 shift)
Marc Zyngier2f0778af2011-12-15 12:19:23 +010090{
91 return (cyc * mult) >> shift;
92}
93
Stephen Boydb4042ce2013-07-18 16:21:19 -070094unsigned long long notrace sched_clock(void)
Marc Zyngier2f0778af2011-12-15 12:19:23 +010095{
Daniel Thompson8710e912015-03-26 12:23:22 -070096 u64 cyc, res;
Stephen Boyd85c3d2d2013-07-18 16:21:15 -070097 unsigned long seq;
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -070098 struct clock_read_data *rd = &cd.read_data;
Stephen Boyd336ae112013-06-17 15:40:58 -070099
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100100 do {
John Stultz7a06c412014-01-02 15:11:14 -0800101 seq = raw_read_seqcount_begin(&cd.seq);
Daniel Thompson8710e912015-03-26 12:23:22 -0700102
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700103 cyc = (rd->read_sched_clock() - rd->epoch_cyc) &
104 rd->sched_clock_mask;
105 res = rd->epoch_ns + cyc_to_ns(cyc, rd->mult, rd->shift);
Stephen Boyd85c3d2d2013-07-18 16:21:15 -0700106 } while (read_seqcount_retry(&cd.seq, seq));
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100107
Daniel Thompson8710e912015-03-26 12:23:22 -0700108 return res;
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100109}
110
111/*
112 * Atomically update the sched_clock epoch.
113 */
Daniel Thompson9fee69a2015-03-26 12:23:25 -0700114static void update_sched_clock(void)
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100115{
116 unsigned long flags;
Stephen Boyde7e3ff12013-07-18 16:21:17 -0700117 u64 cyc;
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100118 u64 ns;
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -0700119 struct clock_read_data *rd = &cd.read_data;
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100120
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700121 cyc = cd.actual_read_sched_clock();
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -0700122 ns = rd->epoch_ns +
123 cyc_to_ns((cyc - rd->epoch_cyc) & rd->sched_clock_mask,
124 rd->mult, rd->shift);
Stephen Boyd85c3d2d2013-07-18 16:21:15 -0700125
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100126 raw_local_irq_save(flags);
John Stultz7a06c412014-01-02 15:11:14 -0800127 raw_write_seqcount_begin(&cd.seq);
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -0700128 rd->epoch_ns = ns;
129 rd->epoch_cyc = cyc;
John Stultz7a06c412014-01-02 15:11:14 -0800130 raw_write_seqcount_end(&cd.seq);
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100131 raw_local_irq_restore(flags);
132}
Russell King112f38a42010-12-15 19:23:07 +0000133
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700134static enum hrtimer_restart sched_clock_poll(struct hrtimer *hrt)
Russell King112f38a42010-12-15 19:23:07 +0000135{
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100136 update_sched_clock();
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700137 hrtimer_forward_now(hrt, cd.wrap_kt);
138 return HRTIMER_RESTART;
Russell King112f38a42010-12-15 19:23:07 +0000139}
140
Stephen Boyde7e3ff12013-07-18 16:21:17 -0700141void __init sched_clock_register(u64 (*read)(void), int bits,
142 unsigned long rate)
Russell King112f38a42010-12-15 19:23:07 +0000143{
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800144 u64 res, wrap, new_mask, new_epoch, cyc, ns;
145 u32 new_mult, new_shift;
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700146 unsigned long r;
Russell King112f38a42010-12-15 19:23:07 +0000147 char r_unit;
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -0700148 struct clock_read_data *rd = &cd.read_data;
Russell King112f38a42010-12-15 19:23:07 +0000149
Rob Herringc1157392013-02-08 16:14:59 -0600150 if (cd.rate > rate)
151 return;
152
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100153 WARN_ON(!irqs_disabled());
Russell King112f38a42010-12-15 19:23:07 +0000154
155 /* calculate the mult/shift to convert counter ticks to ns. */
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800156 clocks_calc_mult_shift(&new_mult, &new_shift, rate, NSEC_PER_SEC, 3600);
157
158 new_mask = CLOCKSOURCE_MASK(bits);
Daniel Thompson8710e912015-03-26 12:23:22 -0700159 cd.rate = rate;
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800160
John Stultz362fde02015-03-11 21:16:30 -0700161 /* calculate how many nanosecs until we risk wrapping */
John Stultzfb82fe22015-03-11 21:16:31 -0700162 wrap = clocks_calc_max_nsecs(new_mult, new_shift, 0, new_mask, NULL);
Daniel Thompson8710e912015-03-26 12:23:22 -0700163 cd.wrap_kt = ns_to_ktime(wrap);
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800164
165 /* update epoch for new counter and update epoch_ns from old counter*/
166 new_epoch = read();
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700167 cyc = cd.actual_read_sched_clock();
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -0700168 ns = rd->epoch_ns +
169 cyc_to_ns((cyc - rd->epoch_cyc) & rd->sched_clock_mask,
170 rd->mult, rd->shift);
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700171 cd.actual_read_sched_clock = read;
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800172
173 raw_write_seqcount_begin(&cd.seq);
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -0700174 rd->read_sched_clock = read;
175 rd->sched_clock_mask = new_mask;
176 rd->mult = new_mult;
177 rd->shift = new_shift;
178 rd->epoch_cyc = new_epoch;
179 rd->epoch_ns = ns;
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800180 raw_write_seqcount_end(&cd.seq);
Russell King112f38a42010-12-15 19:23:07 +0000181
182 r = rate;
183 if (r >= 4000000) {
184 r /= 1000000;
185 r_unit = 'M';
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100186 } else if (r >= 1000) {
Russell King112f38a42010-12-15 19:23:07 +0000187 r /= 1000;
188 r_unit = 'k';
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100189 } else
190 r_unit = ' ';
Russell King112f38a42010-12-15 19:23:07 +0000191
Russell King112f38a42010-12-15 19:23:07 +0000192 /* calculate the ns resolution of this counter */
Stephen Boyd5ae8aab2014-02-17 10:45:36 -0800193 res = cyc_to_ns(1ULL, new_mult, new_shift);
194
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700195 pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lluns\n",
196 bits, r, r_unit, res, wrap);
Russell King112f38a42010-12-15 19:23:07 +0000197
Russell Kinga42c3622012-09-09 18:39:28 +0100198 /* Enable IRQ time accounting if we have a fast enough sched_clock */
199 if (irqtime > 0 || (irqtime == -1 && rate >= 1000000))
200 enable_sched_clock_irqtime();
201
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100202 pr_debug("Registered %pF as sched_clock source\n", read);
203}
204
Russell King211baa702011-01-11 16:23:04 +0000205void __init sched_clock_postinit(void)
206{
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100207 /*
208 * If no sched_clock function has been provided at that point,
209 * make it the final one one.
210 */
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700211 if (cd.actual_read_sched_clock == jiffy_sched_clock_read)
Stephen Boyde7e3ff12013-07-18 16:21:17 -0700212 sched_clock_register(jiffy_sched_clock_read, BITS_PER_LONG, HZ);
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100213
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700214 update_sched_clock();
215
216 /*
217 * Start the timer to keep sched_clock() properly updated and
218 * sets the initial epoch.
219 */
220 hrtimer_init(&sched_clock_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
221 sched_clock_timer.function = sched_clock_poll;
222 hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
Russell King211baa702011-01-11 16:23:04 +0000223}
Russell Kingf153d012012-02-04 12:31:27 +0000224
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700225/*
226 * Clock read function for use when the clock is suspended.
227 *
228 * This function makes it appear to sched_clock() as if the clock
229 * stopped counting at its last update.
230 */
231static u64 notrace suspended_sched_clock_read(void)
232{
233 return cd.read_data.epoch_cyc;
234}
235
Russell Kingf153d012012-02-04 12:31:27 +0000236static int sched_clock_suspend(void)
237{
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -0700238 struct clock_read_data *rd = &cd.read_data;
239
Stephen Boydf723aa12014-07-23 21:03:50 -0700240 update_sched_clock();
241 hrtimer_cancel(&sched_clock_timer);
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700242 rd->read_sched_clock = suspended_sched_clock_read;
Russell Kingf153d012012-02-04 12:31:27 +0000243 return 0;
244}
245
Colin Cross237ec6f2012-08-07 19:05:10 +0100246static void sched_clock_resume(void)
247{
Daniel Thompsoncf7c9c12015-03-26 12:23:23 -0700248 struct clock_read_data *rd = &cd.read_data;
249
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700250 rd->epoch_cyc = cd.actual_read_sched_clock();
Stephen Boydf723aa12014-07-23 21:03:50 -0700251 hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
Daniel Thompson13dbeb32015-03-26 12:23:24 -0700252 rd->read_sched_clock = cd.actual_read_sched_clock;
Colin Cross237ec6f2012-08-07 19:05:10 +0100253}
254
Russell Kingf153d012012-02-04 12:31:27 +0000255static struct syscore_ops sched_clock_ops = {
256 .suspend = sched_clock_suspend,
Colin Cross237ec6f2012-08-07 19:05:10 +0100257 .resume = sched_clock_resume,
Russell Kingf153d012012-02-04 12:31:27 +0000258};
259
260static int __init sched_clock_syscore_init(void)
261{
262 register_syscore_ops(&sched_clock_ops);
263 return 0;
264}
265device_initcall(sched_clock_syscore_init);