eeea1e950b72c133726bcc38deb1909df714741e
[linux-2.6-block.git] / kernel / time / sched_clock.c
1 /*
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>
11 #include <linux/ktime.h>
12 #include <linux/kernel.h>
13 #include <linux/moduleparam.h>
14 #include <linux/sched.h>
15 #include <linux/syscore_ops.h>
16 #include <linux/hrtimer.h>
17 #include <linux/sched_clock.h>
18 #include <linux/seqlock.h>
19 #include <linux/bitops.h>
20
21 /**
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
31  *
32  * Care must be taken when updating this structure; it is read by
33  * some very hot code paths. It occupies <=40 bytes and, when combined
34  * with the seqcount used to synchronize access, comfortably fits into
35  * a 64 byte cache line.
36  */
37 struct clock_read_data {
38         u64 epoch_ns;
39         u64 epoch_cyc;
40         u64 sched_clock_mask;
41         u64 (*read_sched_clock)(void);
42         u32 mult;
43         u32 shift;
44 };
45
46 /**
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. The lowest
51  *                      bit is the index for @read_data.
52  * @read_data:          Data required to read from sched_clock.
53  * @wrap_kt:            Duration for which clock can run before wrapping
54  * @rate:               Tick rate of the registered clock
55  * @actual_read_sched_clock: Registered clock read function
56  *
57  * The ordering of this structure has been chosen to optimize cache
58  * performance. In particular seq and read_data[0] (combined) should fit
59  * into a single 64 byte cache line.
60  */
61 struct clock_data {
62         seqcount_t seq;
63         struct clock_read_data read_data[2];
64         ktime_t wrap_kt;
65         unsigned long rate;
66         u64 (*actual_read_sched_clock)(void);
67 };
68
69 static struct hrtimer sched_clock_timer;
70 static int irqtime = -1;
71
72 core_param(irqtime, irqtime, int, 0400);
73
74 static u64 notrace jiffy_sched_clock_read(void)
75 {
76         /*
77          * We don't need to use get_jiffies_64 on 32-bit arches here
78          * because we register with BITS_PER_LONG
79          */
80         return (u64)(jiffies - INITIAL_JIFFIES);
81 }
82
83 static struct clock_data cd ____cacheline_aligned = {
84         .read_data[0] = { .mult = NSEC_PER_SEC / HZ,
85                           .read_sched_clock = jiffy_sched_clock_read, },
86         .actual_read_sched_clock = jiffy_sched_clock_read,
87 };
88
89 static inline u64 notrace cyc_to_ns(u64 cyc, u32 mult, u32 shift)
90 {
91         return (cyc * mult) >> shift;
92 }
93
94 unsigned long long notrace sched_clock(void)
95 {
96         u64 cyc, res;
97         unsigned long seq;
98         struct clock_read_data *rd;
99
100         do {
101                 seq = raw_read_seqcount(&cd.seq);
102                 rd = cd.read_data + (seq & 1);
103
104                 cyc = (rd->read_sched_clock() - rd->epoch_cyc) &
105                       rd->sched_clock_mask;
106                 res = rd->epoch_ns + cyc_to_ns(cyc, rd->mult, rd->shift);
107         } while (read_seqcount_retry(&cd.seq, seq));
108
109         return res;
110 }
111
112 /*
113  * Updating the data required to read the clock.
114  *
115  * sched_clock will never observe mis-matched data even if called from
116  * an NMI. We do this by maintaining an odd/even copy of the data and
117  * steering sched_clock to one or the other using a sequence counter.
118  * In order to preserve the data cache profile of sched_clock as much
119  * as possible the system reverts back to the even copy when the update
120  * completes; the odd copy is used *only* during an update.
121  */
122 static void update_clock_read_data(struct clock_read_data *rd)
123 {
124         /* update the backup (odd) copy with the new data */
125         cd.read_data[1] = *rd;
126
127         /* steer readers towards the odd copy */
128         raw_write_seqcount_latch(&cd.seq);
129
130         /* now its safe for us to update the normal (even) copy */
131         cd.read_data[0] = *rd;
132
133         /* switch readers back to the even copy */
134         raw_write_seqcount_latch(&cd.seq);
135 }
136
137 /*
138  * Atomically update the sched_clock epoch.
139  */
140 static void update_sched_clock(void)
141 {
142         u64 cyc;
143         u64 ns;
144         struct clock_read_data rd;
145
146         rd = cd.read_data[0];
147
148         cyc = cd.actual_read_sched_clock();
149         ns = rd.epoch_ns +
150              cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask,
151                        rd.mult, rd.shift);
152
153         rd.epoch_ns = ns;
154         rd.epoch_cyc = cyc;
155
156         update_clock_read_data(&rd);
157 }
158
159 static enum hrtimer_restart sched_clock_poll(struct hrtimer *hrt)
160 {
161         update_sched_clock();
162         hrtimer_forward_now(hrt, cd.wrap_kt);
163         return HRTIMER_RESTART;
164 }
165
166 void __init sched_clock_register(u64 (*read)(void), int bits,
167                                  unsigned long rate)
168 {
169         u64 res, wrap, new_mask, new_epoch, cyc, ns;
170         u32 new_mult, new_shift;
171         unsigned long r;
172         char r_unit;
173         struct clock_read_data rd;
174
175         if (cd.rate > rate)
176                 return;
177
178         WARN_ON(!irqs_disabled());
179
180         /* calculate the mult/shift to convert counter ticks to ns. */
181         clocks_calc_mult_shift(&new_mult, &new_shift, rate, NSEC_PER_SEC, 3600);
182
183         new_mask = CLOCKSOURCE_MASK(bits);
184         cd.rate = rate;
185
186         /* calculate how many nanosecs until we risk wrapping */
187         wrap = clocks_calc_max_nsecs(new_mult, new_shift, 0, new_mask, NULL);
188         cd.wrap_kt = ns_to_ktime(wrap);
189
190         rd = cd.read_data[0];
191
192         /* update epoch for new counter and update epoch_ns from old counter*/
193         new_epoch = read();
194         cyc = cd.actual_read_sched_clock();
195         ns = rd.epoch_ns +
196              cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask,
197                        rd.mult, rd.shift);
198         cd.actual_read_sched_clock = read;
199
200         rd.read_sched_clock = read;
201         rd.sched_clock_mask = new_mask;
202         rd.mult = new_mult;
203         rd.shift = new_shift;
204         rd.epoch_cyc = new_epoch;
205         rd.epoch_ns = ns;
206         update_clock_read_data(&rd);
207
208         r = rate;
209         if (r >= 4000000) {
210                 r /= 1000000;
211                 r_unit = 'M';
212         } else if (r >= 1000) {
213                 r /= 1000;
214                 r_unit = 'k';
215         } else
216                 r_unit = ' ';
217
218         /* calculate the ns resolution of this counter */
219         res = cyc_to_ns(1ULL, new_mult, new_shift);
220
221         pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lluns\n",
222                 bits, r, r_unit, res, wrap);
223
224         /* Enable IRQ time accounting if we have a fast enough sched_clock */
225         if (irqtime > 0 || (irqtime == -1 && rate >= 1000000))
226                 enable_sched_clock_irqtime();
227
228         pr_debug("Registered %pF as sched_clock source\n", read);
229 }
230
231 void __init sched_clock_postinit(void)
232 {
233         /*
234          * If no sched_clock function has been provided at that point,
235          * make it the final one one.
236          */
237         if (cd.actual_read_sched_clock == jiffy_sched_clock_read)
238                 sched_clock_register(jiffy_sched_clock_read, BITS_PER_LONG, HZ);
239
240         update_sched_clock();
241
242         /*
243          * Start the timer to keep sched_clock() properly updated and
244          * sets the initial epoch.
245          */
246         hrtimer_init(&sched_clock_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
247         sched_clock_timer.function = sched_clock_poll;
248         hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
249 }
250
251 /*
252  * Clock read function for use when the clock is suspended.
253  *
254  * This function makes it appear to sched_clock() as if the clock
255  * stopped counting at its last update.
256  *
257  * This function must only be called from the critical
258  * section in sched_clock(). It relies on the read_seqcount_retry()
259  * at the end of the critical section to be sure we observe the
260  * correct copy of epoch_cyc.
261  */
262 static u64 notrace suspended_sched_clock_read(void)
263 {
264         unsigned long seq = raw_read_seqcount(&cd.seq);
265
266         return cd.read_data[seq & 1].epoch_cyc;
267 }
268
269 static int sched_clock_suspend(void)
270 {
271         struct clock_read_data *rd = &cd.read_data[0];
272
273         update_sched_clock();
274         hrtimer_cancel(&sched_clock_timer);
275         rd->read_sched_clock = suspended_sched_clock_read;
276         return 0;
277 }
278
279 static void sched_clock_resume(void)
280 {
281         struct clock_read_data *rd = &cd.read_data[0];
282
283         rd->epoch_cyc = cd.actual_read_sched_clock();
284         hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
285         rd->read_sched_clock = cd.actual_read_sched_clock;
286 }
287
288 static struct syscore_ops sched_clock_ops = {
289         .suspend = sched_clock_suspend,
290         .resume = sched_clock_resume,
291 };
292
293 static int __init sched_clock_syscore_init(void)
294 {
295         register_syscore_ops(&sched_clock_ops);
296         return 0;
297 }
298 device_initcall(sched_clock_syscore_init);