Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * This file contains the jiffies based clocksource.
0004  *
0005  * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com)
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  * The Jiffies based clocksource is the lowest common
0023  * denominator clock source which should function on
0024  * all systems. It has the same coarse resolution as
0025  * the timer interrupt frequency HZ and it suffers
0026  * inaccuracies caused by missed or lost timer
0027  * interrupts and the inability for the timer
0028  * interrupt hardware to accurately tick at the
0029  * requested HZ value. It is also not recommended
0030  * for "tick-less" systems.
0031  */
0032 static struct clocksource clocksource_jiffies = {
0033     .name           = "jiffies",
0034     .rating         = 1, /* lowest valid rating*/
0035     .uncertainty_margin = 32 * NSEC_PER_MSEC,
0036     .read           = jiffies_read,
0037     .mask           = CLOCKSOURCE_MASK(32),
0038     .mult           = TICK_NSEC << JIFFIES_SHIFT, /* details above */
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     /* Calc cycles per tick */
0090     cycles_per_tick = (cycles_per_second + HZ/2)/HZ;
0091     /* shift_hz stores hz<<8 for extra accuracy */
0092     shift_hz = (u64)cycles_per_second << 8;
0093     shift_hz += cycles_per_tick/2;
0094     do_div(shift_hz, cycles_per_tick);
0095     /* Calculate nsec_per_tick using shift_hz */
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 }