135728b82SThomas Gleixner // SPDX-License-Identifier: GPL-2.0+ 26c7811c6SThomas Gleixner /* 3734efb46Sjohn stultz * This file contains the jiffies based clocksource. 4734efb46Sjohn stultz * 5734efb46Sjohn stultz * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com) 66c7811c6SThomas Gleixner */ 7734efb46Sjohn stultz #include <linux/clocksource.h> 8734efb46Sjohn stultz #include <linux/jiffies.h> 9fbad1ea9STorben Hohn #include <linux/module.h> 10734efb46Sjohn stultz #include <linux/init.h> 11734efb46Sjohn stultz 12bfb83b27SThomas Gleixner #include "timekeeping.h" 13e2830b5cSTorben Hohn 14734efb46Sjohn stultz 1593825f2eSFrederic Weisbecker /* Since jiffies uses a simple TICK_NSEC multiplier 16734efb46Sjohn stultz * conversion, the .shift value could be zero. However 17734efb46Sjohn stultz * this would make NTP adjustments impossible as they are 18734efb46Sjohn stultz * in units of 1/2^.shift. Thus we use JIFFIES_SHIFT to 19734efb46Sjohn stultz * shift both the nominator and denominator the same 20734efb46Sjohn stultz * amount, and give ntp adjustments in units of 1/2^8 21734efb46Sjohn stultz * 22734efb46Sjohn stultz * The value 8 is somewhat carefully chosen, as anything 2393825f2eSFrederic Weisbecker * larger can result in overflows. TICK_NSEC grows as HZ 2493825f2eSFrederic Weisbecker * shrinks, so values greater than 8 overflow 32bits when 25734efb46Sjohn stultz * HZ=100. 26734efb46Sjohn stultz */ 2780d767d7SMikulas Patocka #if HZ < 34 2880d767d7SMikulas Patocka #define JIFFIES_SHIFT 6 2980d767d7SMikulas Patocka #elif HZ < 67 3080d767d7SMikulas Patocka #define JIFFIES_SHIFT 7 3180d767d7SMikulas Patocka #else 32734efb46Sjohn stultz #define JIFFIES_SHIFT 8 3380d767d7SMikulas Patocka #endif 34734efb46Sjohn stultz 35a5a1d1c2SThomas Gleixner static u64 jiffies_read(struct clocksource *cs) 36734efb46Sjohn stultz { 37a5a1d1c2SThomas Gleixner return (u64) jiffies; 38734efb46Sjohn stultz } 39734efb46Sjohn stultz 4093825f2eSFrederic Weisbecker /* 4193825f2eSFrederic Weisbecker * The Jiffies based clocksource is the lowest common 4293825f2eSFrederic Weisbecker * denominator clock source which should function on 4393825f2eSFrederic Weisbecker * all systems. It has the same coarse resolution as 4493825f2eSFrederic Weisbecker * the timer interrupt frequency HZ and it suffers 4593825f2eSFrederic Weisbecker * inaccuracies caused by missed or lost timer 4693825f2eSFrederic Weisbecker * interrupts and the inability for the timer 47*4bf07f65SIngo Molnar * interrupt hardware to accurately tick at the 4893825f2eSFrederic Weisbecker * requested HZ value. It is also not recommended 4993825f2eSFrederic Weisbecker * for "tick-less" systems. 5093825f2eSFrederic Weisbecker */ 51f95a9857SLars-Peter Clausen static struct clocksource clocksource_jiffies = { 52734efb46Sjohn stultz .name = "jiffies", 533f4a0b91Sjohn stultz .rating = 1, /* lowest valid rating*/ 54734efb46Sjohn stultz .read = jiffies_read, 55232d2637SAlexander Kuleshov .mask = CLOCKSOURCE_MASK(32), 5693825f2eSFrederic Weisbecker .mult = TICK_NSEC << JIFFIES_SHIFT, /* details above */ 57734efb46Sjohn stultz .shift = JIFFIES_SHIFT, 583c17ad19SJohn Stultz .max_cycles = 10, 59734efb46Sjohn stultz }; 60734efb46Sjohn stultz 61e5d4d175SThomas Gleixner __cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(jiffies_lock); 621a2b85f1SDavidlohr Bueso __cacheline_aligned_in_smp seqcount_raw_spinlock_t jiffies_seq = 631a2b85f1SDavidlohr Bueso SEQCNT_RAW_SPINLOCK_ZERO(jiffies_seq, &jiffies_lock); 64d6ad4187SJohn Stultz 65fbad1ea9STorben Hohn #if (BITS_PER_LONG < 64) 66fbad1ea9STorben Hohn u64 get_jiffies_64(void) 67fbad1ea9STorben Hohn { 68e1e41b6cSRasmus Villemoes unsigned int seq; 69fbad1ea9STorben Hohn u64 ret; 70fbad1ea9STorben Hohn 71fbad1ea9STorben Hohn do { 72e5d4d175SThomas Gleixner seq = read_seqcount_begin(&jiffies_seq); 73fbad1ea9STorben Hohn ret = jiffies_64; 74e5d4d175SThomas Gleixner } while (read_seqcount_retry(&jiffies_seq, seq)); 75fbad1ea9STorben Hohn return ret; 76fbad1ea9STorben Hohn } 77fbad1ea9STorben Hohn EXPORT_SYMBOL(get_jiffies_64); 78fbad1ea9STorben Hohn #endif 79fbad1ea9STorben Hohn 80fbad1ea9STorben Hohn EXPORT_SYMBOL(jiffies); 81fbad1ea9STorben Hohn 82734efb46Sjohn stultz static int __init init_jiffies_clocksource(void) 83734efb46Sjohn stultz { 84f8935983SJohn Stultz return __clocksource_register(&clocksource_jiffies); 85734efb46Sjohn stultz } 86734efb46Sjohn stultz 8798de9e3bSjohn stultz core_initcall(init_jiffies_clocksource); 88f1b82746SMartin Schwidefsky 89f1b82746SMartin Schwidefsky struct clocksource * __init __weak clocksource_default_clock(void) 90f1b82746SMartin Schwidefsky { 91f1b82746SMartin Schwidefsky return &clocksource_jiffies; 92f1b82746SMartin Schwidefsky } 93b3c869d3SJohn Stultz 94e8750053SValdis Kletnieks static struct clocksource refined_jiffies; 95b3c869d3SJohn Stultz 96b3c869d3SJohn Stultz int register_refined_jiffies(long cycles_per_second) 97b3c869d3SJohn Stultz { 98b3c869d3SJohn Stultz u64 nsec_per_tick, shift_hz; 99b3c869d3SJohn Stultz long cycles_per_tick; 100b3c869d3SJohn Stultz 101b3c869d3SJohn Stultz 102b3c869d3SJohn Stultz 103b3c869d3SJohn Stultz refined_jiffies = clocksource_jiffies; 104b3c869d3SJohn Stultz refined_jiffies.name = "refined-jiffies"; 105b3c869d3SJohn Stultz refined_jiffies.rating++; 106b3c869d3SJohn Stultz 107b3c869d3SJohn Stultz /* Calc cycles per tick */ 108b3c869d3SJohn Stultz cycles_per_tick = (cycles_per_second + HZ/2)/HZ; 109b3c869d3SJohn Stultz /* shift_hz stores hz<<8 for extra accuracy */ 110b3c869d3SJohn Stultz shift_hz = (u64)cycles_per_second << 8; 111b3c869d3SJohn Stultz shift_hz += cycles_per_tick/2; 112b3c869d3SJohn Stultz do_div(shift_hz, cycles_per_tick); 113b3c869d3SJohn Stultz /* Calculate nsec_per_tick using shift_hz */ 114fa3aa7a5SFrederic Weisbecker nsec_per_tick = (u64)NSEC_PER_SEC << 8; 115b3c869d3SJohn Stultz nsec_per_tick += (u32)shift_hz/2; 116b3c869d3SJohn Stultz do_div(nsec_per_tick, (u32)shift_hz); 117b3c869d3SJohn Stultz 118b3c869d3SJohn Stultz refined_jiffies.mult = ((u32)nsec_per_tick) << JIFFIES_SHIFT; 119b3c869d3SJohn Stultz 120f8935983SJohn Stultz __clocksource_register(&refined_jiffies); 121b3c869d3SJohn Stultz return 0; 122b3c869d3SJohn Stultz } 123