blob: 68b79937598106918748832a396848f7fd22f870 [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
Marc Zyngier2f0778af2011-12-15 12:19:23 +010021struct clock_data {
Stephen Boyda08ca5d2013-07-18 16:21:16 -070022 ktime_t wrap_kt;
Marc Zyngier2f0778af2011-12-15 12:19:23 +010023 u64 epoch_ns;
Stephen Boyde7e3ff12013-07-18 16:21:17 -070024 u64 epoch_cyc;
Stephen Boyd85c3d2d2013-07-18 16:21:15 -070025 seqcount_t seq;
Rob Herringc1157392013-02-08 16:14:59 -060026 unsigned long rate;
Marc Zyngier2f0778af2011-12-15 12:19:23 +010027 u32 mult;
28 u32 shift;
Colin Cross237ec6f2012-08-07 19:05:10 +010029 bool suspended;
Marc Zyngier2f0778af2011-12-15 12:19:23 +010030};
31
Stephen Boyda08ca5d2013-07-18 16:21:16 -070032static struct hrtimer sched_clock_timer;
Russell Kinga42c3622012-09-09 18:39:28 +010033static int irqtime = -1;
34
35core_param(irqtime, irqtime, int, 0400);
Marc Zyngier2f0778af2011-12-15 12:19:23 +010036
37static struct clock_data cd = {
38 .mult = NSEC_PER_SEC / HZ,
39};
40
Stephen Boyde7e3ff12013-07-18 16:21:17 -070041static u64 __read_mostly sched_clock_mask;
Marc Zyngier2f0778af2011-12-15 12:19:23 +010042
Stephen Boyde7e3ff12013-07-18 16:21:17 -070043static u64 notrace jiffy_sched_clock_read(void)
Marc Zyngier2f0778af2011-12-15 12:19:23 +010044{
Stephen Boyde7e3ff12013-07-18 16:21:17 -070045 /*
46 * We don't need to use get_jiffies_64 on 32-bit arches here
47 * because we register with BITS_PER_LONG
48 */
49 return (u64)(jiffies - INITIAL_JIFFIES);
Marc Zyngier2f0778af2011-12-15 12:19:23 +010050}
51
Stephen Boyde7e3ff12013-07-18 16:21:17 -070052static u32 __read_mostly (*read_sched_clock_32)(void);
53
54static u64 notrace read_sched_clock_32_wrapper(void)
55{
56 return read_sched_clock_32();
57}
58
59static u64 __read_mostly (*read_sched_clock)(void) = jiffy_sched_clock_read;
Marc Zyngier2f0778af2011-12-15 12:19:23 +010060
Stephen Boydcea15092013-04-18 17:33:40 +010061static inline u64 notrace cyc_to_ns(u64 cyc, u32 mult, u32 shift)
Marc Zyngier2f0778af2011-12-15 12:19:23 +010062{
63 return (cyc * mult) >> shift;
64}
65
Stephen Boydb4042ce2013-07-18 16:21:19 -070066unsigned long long notrace sched_clock(void)
Marc Zyngier2f0778af2011-12-15 12:19:23 +010067{
68 u64 epoch_ns;
Stephen Boyde7e3ff12013-07-18 16:21:17 -070069 u64 epoch_cyc;
70 u64 cyc;
Stephen Boyd85c3d2d2013-07-18 16:21:15 -070071 unsigned long seq;
Stephen Boyd336ae112013-06-17 15:40:58 -070072
73 if (cd.suspended)
74 return cd.epoch_ns;
Marc Zyngier2f0778af2011-12-15 12:19:23 +010075
Marc Zyngier2f0778af2011-12-15 12:19:23 +010076 do {
Stephen Boyd85c3d2d2013-07-18 16:21:15 -070077 seq = read_seqcount_begin(&cd.seq);
Marc Zyngier2f0778af2011-12-15 12:19:23 +010078 epoch_cyc = cd.epoch_cyc;
Marc Zyngier2f0778af2011-12-15 12:19:23 +010079 epoch_ns = cd.epoch_ns;
Stephen Boyd85c3d2d2013-07-18 16:21:15 -070080 } while (read_seqcount_retry(&cd.seq, seq));
Marc Zyngier2f0778af2011-12-15 12:19:23 +010081
Stephen Boyd336ae112013-06-17 15:40:58 -070082 cyc = read_sched_clock();
83 cyc = (cyc - epoch_cyc) & sched_clock_mask;
84 return epoch_ns + cyc_to_ns(cyc, cd.mult, cd.shift);
Marc Zyngier2f0778af2011-12-15 12:19:23 +010085}
86
87/*
88 * Atomically update the sched_clock epoch.
89 */
90static void notrace update_sched_clock(void)
91{
92 unsigned long flags;
Stephen Boyde7e3ff12013-07-18 16:21:17 -070093 u64 cyc;
Marc Zyngier2f0778af2011-12-15 12:19:23 +010094 u64 ns;
95
96 cyc = read_sched_clock();
97 ns = cd.epoch_ns +
98 cyc_to_ns((cyc - cd.epoch_cyc) & sched_clock_mask,
99 cd.mult, cd.shift);
Stephen Boyd85c3d2d2013-07-18 16:21:15 -0700100
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100101 raw_local_irq_save(flags);
Stephen Boyd85c3d2d2013-07-18 16:21:15 -0700102 write_seqcount_begin(&cd.seq);
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100103 cd.epoch_ns = ns;
Joonsoo Kim7c4e9ce2013-02-09 05:52:45 +0100104 cd.epoch_cyc = cyc;
Stephen Boyd85c3d2d2013-07-18 16:21:15 -0700105 write_seqcount_end(&cd.seq);
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100106 raw_local_irq_restore(flags);
107}
Russell King112f38a42010-12-15 19:23:07 +0000108
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700109static enum hrtimer_restart sched_clock_poll(struct hrtimer *hrt)
Russell King112f38a42010-12-15 19:23:07 +0000110{
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100111 update_sched_clock();
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700112 hrtimer_forward_now(hrt, cd.wrap_kt);
113 return HRTIMER_RESTART;
Russell King112f38a42010-12-15 19:23:07 +0000114}
115
Stephen Boyde7e3ff12013-07-18 16:21:17 -0700116void __init sched_clock_register(u64 (*read)(void), int bits,
117 unsigned long rate)
Russell King112f38a42010-12-15 19:23:07 +0000118{
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700119 unsigned long r;
Russell King112f38a42010-12-15 19:23:07 +0000120 u64 res, wrap;
121 char r_unit;
122
Rob Herringc1157392013-02-08 16:14:59 -0600123 if (cd.rate > rate)
124 return;
125
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100126 WARN_ON(!irqs_disabled());
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100127 read_sched_clock = read;
Stephen Boyde7e3ff12013-07-18 16:21:17 -0700128 sched_clock_mask = CLOCKSOURCE_MASK(bits);
Rob Herringc1157392013-02-08 16:14:59 -0600129 cd.rate = rate;
Russell King112f38a42010-12-15 19:23:07 +0000130
131 /* calculate the mult/shift to convert counter ticks to ns. */
Stephen Boyde7e3ff12013-07-18 16:21:17 -0700132 clocks_calc_mult_shift(&cd.mult, &cd.shift, rate, NSEC_PER_SEC, 3600);
Russell King112f38a42010-12-15 19:23:07 +0000133
134 r = rate;
135 if (r >= 4000000) {
136 r /= 1000000;
137 r_unit = 'M';
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100138 } else if (r >= 1000) {
Russell King112f38a42010-12-15 19:23:07 +0000139 r /= 1000;
140 r_unit = 'k';
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100141 } else
142 r_unit = ' ';
Russell King112f38a42010-12-15 19:23:07 +0000143
144 /* calculate how many ns until we wrap */
Stephen Boyde7e3ff12013-07-18 16:21:17 -0700145 wrap = clocks_calc_max_nsecs(cd.mult, cd.shift, 0, sched_clock_mask);
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700146 cd.wrap_kt = ns_to_ktime(wrap - (wrap >> 3));
Russell King112f38a42010-12-15 19:23:07 +0000147
148 /* calculate the ns resolution of this counter */
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100149 res = cyc_to_ns(1ULL, cd.mult, cd.shift);
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700150 pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lluns\n",
151 bits, r, r_unit, res, wrap);
Russell King112f38a42010-12-15 19:23:07 +0000152
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100153 update_sched_clock();
Russell King112f38a42010-12-15 19:23:07 +0000154
155 /*
156 * Ensure that sched_clock() starts off at 0ns
157 */
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100158 cd.epoch_ns = 0;
159
Russell Kinga42c3622012-09-09 18:39:28 +0100160 /* Enable IRQ time accounting if we have a fast enough sched_clock */
161 if (irqtime > 0 || (irqtime == -1 && rate >= 1000000))
162 enable_sched_clock_irqtime();
163
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100164 pr_debug("Registered %pF as sched_clock source\n", read);
165}
166
Stephen Boyde7e3ff12013-07-18 16:21:17 -0700167void __init setup_sched_clock(u32 (*read)(void), int bits, unsigned long rate)
168{
169 read_sched_clock_32 = read;
170 sched_clock_register(read_sched_clock_32_wrapper, bits, rate);
171}
172
Russell King211baa702011-01-11 16:23:04 +0000173void __init sched_clock_postinit(void)
174{
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100175 /*
176 * If no sched_clock function has been provided at that point,
177 * make it the final one one.
178 */
179 if (read_sched_clock == jiffy_sched_clock_read)
Stephen Boyde7e3ff12013-07-18 16:21:17 -0700180 sched_clock_register(jiffy_sched_clock_read, BITS_PER_LONG, HZ);
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100181
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700182 update_sched_clock();
183
184 /*
185 * Start the timer to keep sched_clock() properly updated and
186 * sets the initial epoch.
187 */
188 hrtimer_init(&sched_clock_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
189 sched_clock_timer.function = sched_clock_poll;
190 hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
Russell King211baa702011-01-11 16:23:04 +0000191}
Russell Kingf153d012012-02-04 12:31:27 +0000192
193static int sched_clock_suspend(void)
194{
Stephen Boyda08ca5d2013-07-18 16:21:16 -0700195 sched_clock_poll(&sched_clock_timer);
Felipe Balbi 26a4dae52012-10-23 19:00:03 +0100196 cd.suspended = true;
Russell Kingf153d012012-02-04 12:31:27 +0000197 return 0;
198}
199
Colin Cross237ec6f2012-08-07 19:05:10 +0100200static void sched_clock_resume(void)
201{
Felipe Balbi 26a4dae52012-10-23 19:00:03 +0100202 cd.epoch_cyc = read_sched_clock();
Felipe Balbi 26a4dae52012-10-23 19:00:03 +0100203 cd.suspended = false;
Colin Cross237ec6f2012-08-07 19:05:10 +0100204}
205
Russell Kingf153d012012-02-04 12:31:27 +0000206static struct syscore_ops sched_clock_ops = {
207 .suspend = sched_clock_suspend,
Colin Cross237ec6f2012-08-07 19:05:10 +0100208 .resume = sched_clock_resume,
Russell Kingf153d012012-02-04 12:31:27 +0000209};
210
211static int __init sched_clock_syscore_init(void)
212{
213 register_syscore_ops(&sched_clock_ops);
214 return 0;
215}
216device_initcall(sched_clock_syscore_init);