0001
0002
0003
0004
0005
0006
0007 #include <linux/clocksource.h>
0008 #include <linux/jiffies.h>
0009 #include <linux/module.h>
0010 #include <linux/init.h>
0011
0012 #include "timekeeping.h"
0013 #include "tick-internal.h"
0014
0015
0016 static u64 jiffies_read(struct clocksource *cs)
0017 {
0018 return (u64) jiffies;
0019 }
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 static struct clocksource clocksource_jiffies = {
0033 .name = "jiffies",
0034 .rating = 1,
0035 .uncertainty_margin = 32 * NSEC_PER_MSEC,
0036 .read = jiffies_read,
0037 .mask = CLOCKSOURCE_MASK(32),
0038 .mult = TICK_NSEC << JIFFIES_SHIFT,
0039 .shift = JIFFIES_SHIFT,
0040 .max_cycles = 10,
0041 };
0042
0043 __cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(jiffies_lock);
0044 __cacheline_aligned_in_smp seqcount_raw_spinlock_t jiffies_seq =
0045 SEQCNT_RAW_SPINLOCK_ZERO(jiffies_seq, &jiffies_lock);
0046
0047 #if (BITS_PER_LONG < 64)
0048 u64 get_jiffies_64(void)
0049 {
0050 unsigned int seq;
0051 u64 ret;
0052
0053 do {
0054 seq = read_seqcount_begin(&jiffies_seq);
0055 ret = jiffies_64;
0056 } while (read_seqcount_retry(&jiffies_seq, seq));
0057 return ret;
0058 }
0059 EXPORT_SYMBOL(get_jiffies_64);
0060 #endif
0061
0062 EXPORT_SYMBOL(jiffies);
0063
0064 static int __init init_jiffies_clocksource(void)
0065 {
0066 return __clocksource_register(&clocksource_jiffies);
0067 }
0068
0069 core_initcall(init_jiffies_clocksource);
0070
0071 struct clocksource * __init __weak clocksource_default_clock(void)
0072 {
0073 return &clocksource_jiffies;
0074 }
0075
0076 static struct clocksource refined_jiffies;
0077
0078 int register_refined_jiffies(long cycles_per_second)
0079 {
0080 u64 nsec_per_tick, shift_hz;
0081 long cycles_per_tick;
0082
0083
0084
0085 refined_jiffies = clocksource_jiffies;
0086 refined_jiffies.name = "refined-jiffies";
0087 refined_jiffies.rating++;
0088
0089
0090 cycles_per_tick = (cycles_per_second + HZ/2)/HZ;
0091
0092 shift_hz = (u64)cycles_per_second << 8;
0093 shift_hz += cycles_per_tick/2;
0094 do_div(shift_hz, cycles_per_tick);
0095
0096 nsec_per_tick = (u64)NSEC_PER_SEC << 8;
0097 nsec_per_tick += (u32)shift_hz/2;
0098 do_div(nsec_per_tick, (u32)shift_hz);
0099
0100 refined_jiffies.mult = ((u32)nsec_per_tick) << JIFFIES_SHIFT;
0101
0102 __clocksource_register(&refined_jiffies);
0103 return 0;
0104 }