| Nicholas Flintham | 1e3d311 | 2013-04-10 10:48:38 +0100 | [diff] [blame^] | 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/kernel.h> |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/syscore_ops.h> |
| 14 | #include <linux/timer.h> |
| 15 | |
| 16 | #include <asm/sched_clock.h> |
| 17 | |
| 18 | struct clock_data { |
| 19 | u64 epoch_ns; |
| 20 | u32 epoch_cyc; |
| 21 | u32 epoch_cyc_copy; |
| 22 | u32 mult; |
| 23 | u32 shift; |
| 24 | }; |
| 25 | |
| 26 | static void sched_clock_poll(unsigned long wrap_ticks); |
| 27 | static DEFINE_TIMER(sched_clock_timer, sched_clock_poll, 0, 0); |
| 28 | |
| 29 | static struct clock_data cd = { |
| 30 | .mult = NSEC_PER_SEC / HZ, |
| 31 | }; |
| 32 | |
| 33 | static u32 __read_mostly sched_clock_mask = 0xffffffff; |
| 34 | |
| 35 | static u32 notrace jiffy_sched_clock_read(void) |
| 36 | { |
| 37 | return (u32)(jiffies - INITIAL_JIFFIES); |
| 38 | } |
| 39 | |
| 40 | static u32 __read_mostly (*read_sched_clock)(void) = jiffy_sched_clock_read; |
| 41 | |
| 42 | static inline u64 cyc_to_ns(u64 cyc, u32 mult, u32 shift) |
| 43 | { |
| 44 | return (cyc * mult) >> shift; |
| 45 | } |
| 46 | |
| 47 | static unsigned long long cyc_to_sched_clock(u32 cyc, u32 mask) |
| 48 | { |
| 49 | u64 epoch_ns; |
| 50 | u32 epoch_cyc; |
| 51 | |
| 52 | do { |
| 53 | epoch_cyc = cd.epoch_cyc; |
| 54 | smp_rmb(); |
| 55 | epoch_ns = cd.epoch_ns; |
| 56 | smp_rmb(); |
| 57 | } while (epoch_cyc != cd.epoch_cyc_copy); |
| 58 | |
| 59 | return epoch_ns + cyc_to_ns((cyc - epoch_cyc) & mask, cd.mult, cd.shift); |
| 60 | } |
| 61 | |
| 62 | static void notrace update_sched_clock(void) |
| 63 | { |
| 64 | unsigned long flags; |
| 65 | u32 cyc; |
| 66 | u64 ns; |
| 67 | |
| 68 | cyc = read_sched_clock(); |
| 69 | ns = cd.epoch_ns + |
| 70 | cyc_to_ns((cyc - cd.epoch_cyc) & sched_clock_mask, |
| 71 | cd.mult, cd.shift); |
| 72 | raw_local_irq_save(flags); |
| 73 | cd.epoch_cyc = cyc; |
| 74 | smp_wmb(); |
| 75 | cd.epoch_ns = ns; |
| 76 | smp_wmb(); |
| 77 | cd.epoch_cyc_copy = cyc; |
| 78 | raw_local_irq_restore(flags); |
| 79 | } |
| 80 | |
| 81 | static void sched_clock_poll(unsigned long wrap_ticks) |
| 82 | { |
| 83 | mod_timer(&sched_clock_timer, round_jiffies(jiffies + wrap_ticks)); |
| 84 | update_sched_clock(); |
| 85 | } |
| 86 | |
| 87 | void __init setup_sched_clock(u32 (*read)(void), int bits, unsigned long rate) |
| 88 | { |
| 89 | unsigned long r, w; |
| 90 | u64 res, wrap; |
| 91 | char r_unit; |
| 92 | |
| 93 | BUG_ON(bits > 32); |
| 94 | WARN_ON(!irqs_disabled()); |
| 95 | WARN_ON(read_sched_clock != jiffy_sched_clock_read); |
| 96 | read_sched_clock = read; |
| 97 | sched_clock_mask = (1 << bits) - 1; |
| 98 | |
| 99 | |
| 100 | clocks_calc_mult_shift(&cd.mult, &cd.shift, rate, NSEC_PER_SEC, 0); |
| 101 | |
| 102 | r = rate; |
| 103 | if (r >= 4000000) { |
| 104 | r /= 1000000; |
| 105 | r_unit = 'M'; |
| 106 | } else if (r >= 1000) { |
| 107 | r /= 1000; |
| 108 | r_unit = 'k'; |
| 109 | } else |
| 110 | r_unit = ' '; |
| 111 | |
| 112 | |
| 113 | wrap = cyc_to_ns((1ULL << bits) - 1, cd.mult, cd.shift); |
| 114 | do_div(wrap, NSEC_PER_MSEC); |
| 115 | w = wrap; |
| 116 | |
| 117 | |
| 118 | res = cyc_to_ns(1ULL, cd.mult, cd.shift); |
| 119 | pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lums\n", |
| 120 | bits, r, r_unit, res, w); |
| 121 | |
| 122 | sched_clock_timer.data = msecs_to_jiffies(w - (w / 10)); |
| 123 | update_sched_clock(); |
| 124 | |
| 125 | cd.epoch_ns = 0; |
| 126 | |
| 127 | pr_debug("Registered %pF as sched_clock source\n", read); |
| 128 | } |
| 129 | |
| 130 | unsigned long long notrace sched_clock(void) |
| 131 | { |
| 132 | u32 cyc = read_sched_clock(); |
| 133 | return cyc_to_sched_clock(cyc, sched_clock_mask); |
| 134 | } |
| 135 | |
| 136 | void __init sched_clock_postinit(void) |
| 137 | { |
| 138 | if (read_sched_clock == jiffy_sched_clock_read) |
| 139 | setup_sched_clock(jiffy_sched_clock_read, 32, HZ); |
| 140 | |
| 141 | sched_clock_poll(sched_clock_timer.data); |
| 142 | } |
| 143 | |
| 144 | static int sched_clock_suspend(void) |
| 145 | { |
| 146 | sched_clock_poll(sched_clock_timer.data); |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static struct syscore_ops sched_clock_ops = { |
| 151 | .suspend = sched_clock_suspend, |
| 152 | }; |
| 153 | |
| 154 | static int __init sched_clock_syscore_init(void) |
| 155 | { |
| 156 | register_syscore_ops(&sched_clock_ops); |
| 157 | return 0; |
| 158 | } |
| 159 | device_initcall(sched_clock_syscore_init); |