2 * sched_clock.h: support for extending counters to full 64-bit ns counter
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.
8 #ifndef ASM_SCHED_CLOCK
9 #define ASM_SCHED_CLOCK
11 #include <linux/kernel.h>
12 #include <linux/types.h>
22 #define DEFINE_CLOCK_DATA(name) struct clock_data name
24 static inline u64 cyc_to_ns(u64 cyc, u32 mult, u32 shift)
26 return (cyc * mult) >> shift;
30 * Atomically update the sched_clock epoch. Your update callback will
31 * be called from a timer before the counter wraps - read the current
32 * counter value, and call this function to safely move the epochs
33 * forward. Only use this from the update callback.
35 static inline void update_sched_clock(struct clock_data *cd, u32 cyc, u32 mask)
38 u64 ns = cd->epoch_ns +
39 cyc_to_ns((cyc - cd->epoch_cyc) & mask, cd->mult, cd->shift);
42 * Write epoch_cyc and epoch_ns in a way that the update is
43 * detectable in cyc_to_fixed_sched_clock().
45 raw_local_irq_save(flags);
50 cd->epoch_cyc_copy = cyc;
51 raw_local_irq_restore(flags);
55 * If your clock rate is known at compile time, using this will allow
56 * you to optimize the mult/shift loads away. This is paired with
57 * init_fixed_sched_clock() to ensure that your mult/shift are correct.
59 static inline unsigned long long cyc_to_fixed_sched_clock(struct clock_data *cd,
60 u32 cyc, u32 mask, u32 mult, u32 shift)
66 * Load the epoch_cyc and epoch_ns atomically. We do this by
67 * ensuring that we always write epoch_cyc, epoch_ns and
68 * epoch_cyc_copy in strict order, and read them in strict order.
69 * If epoch_cyc and epoch_cyc_copy are not equal, then we're in
70 * the middle of an update, and we should repeat the load.
73 epoch_cyc = cd->epoch_cyc;
75 epoch_ns = cd->epoch_ns;
77 } while (epoch_cyc != cd->epoch_cyc_copy);
79 return epoch_ns + cyc_to_ns((cyc - epoch_cyc) & mask, mult, shift);
83 * Otherwise, you need to use this, which will obtain the mult/shift
84 * from the clock_data structure. Use init_sched_clock() with this.
86 static inline unsigned long long cyc_to_sched_clock(struct clock_data *cd,
89 return cyc_to_fixed_sched_clock(cd, cyc, mask, cd->mult, cd->shift);
93 * Initialize the clock data - calculate the appropriate multiplier
94 * and shift. Also setup a timer to ensure that the epoch is refreshed
95 * at the appropriate time interval, which will call your update
98 void init_sched_clock(struct clock_data *, void (*)(void),
99 unsigned int, unsigned long);
102 * Use this initialization function rather than init_sched_clock() if
103 * you're using cyc_to_fixed_sched_clock, which will warn if your
104 * constants are incorrect.
106 static inline void init_fixed_sched_clock(struct clock_data *cd,
107 void (*update)(void), unsigned int bits, unsigned long rate,
110 init_sched_clock(cd, update, bits, rate);
111 if (cd->mult != mult || cd->shift != shift) {
112 pr_crit("sched_clock: wrong multiply/shift: %u>>%u vs calculated %u>>%u\n"
113 "sched_clock: fix multiply/shift to avoid scheduler hiccups\n",
114 mult, shift, cd->mult, cd->shift);
118 extern void sched_clock_postinit(void);