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 4793825f2eSFrederic Weisbecker * interrupt hardware to accuratly 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 61d6ad4187SJohn Stultz __cacheline_aligned_in_smp DEFINE_SEQLOCK(jiffies_lock); 62d6ad4187SJohn Stultz 63fbad1ea9STorben Hohn #if (BITS_PER_LONG < 64) 64fbad1ea9STorben Hohn u64 get_jiffies_64(void) 65fbad1ea9STorben Hohn { 66*e1e41b6cSRasmus Villemoes unsigned int seq; 67fbad1ea9STorben Hohn u64 ret; 68fbad1ea9STorben Hohn 69fbad1ea9STorben Hohn do { 70d6ad4187SJohn Stultz seq = read_seqbegin(&jiffies_lock); 71fbad1ea9STorben Hohn ret = jiffies_64; 72d6ad4187SJohn Stultz } while (read_seqretry(&jiffies_lock, seq)); 73fbad1ea9STorben Hohn return ret; 74fbad1ea9STorben Hohn } 75fbad1ea9STorben Hohn EXPORT_SYMBOL(get_jiffies_64); 76fbad1ea9STorben Hohn #endif 77fbad1ea9STorben Hohn 78fbad1ea9STorben Hohn EXPORT_SYMBOL(jiffies); 79fbad1ea9STorben Hohn 80734efb46Sjohn stultz static int __init init_jiffies_clocksource(void) 81734efb46Sjohn stultz { 82f8935983SJohn Stultz return __clocksource_register(&clocksource_jiffies); 83734efb46Sjohn stultz } 84734efb46Sjohn stultz 8598de9e3bSjohn stultz core_initcall(init_jiffies_clocksource); 86f1b82746SMartin Schwidefsky 87f1b82746SMartin Schwidefsky struct clocksource * __init __weak clocksource_default_clock(void) 88f1b82746SMartin Schwidefsky { 89f1b82746SMartin Schwidefsky return &clocksource_jiffies; 90f1b82746SMartin Schwidefsky } 91b3c869d3SJohn Stultz 92b3c869d3SJohn Stultz struct clocksource refined_jiffies; 93b3c869d3SJohn Stultz 94b3c869d3SJohn Stultz int register_refined_jiffies(long cycles_per_second) 95b3c869d3SJohn Stultz { 96b3c869d3SJohn Stultz u64 nsec_per_tick, shift_hz; 97b3c869d3SJohn Stultz long cycles_per_tick; 98b3c869d3SJohn Stultz 99b3c869d3SJohn Stultz 100b3c869d3SJohn Stultz 101b3c869d3SJohn Stultz refined_jiffies = clocksource_jiffies; 102b3c869d3SJohn Stultz refined_jiffies.name = "refined-jiffies"; 103b3c869d3SJohn Stultz refined_jiffies.rating++; 104b3c869d3SJohn Stultz 105b3c869d3SJohn Stultz /* Calc cycles per tick */ 106b3c869d3SJohn Stultz cycles_per_tick = (cycles_per_second + HZ/2)/HZ; 107b3c869d3SJohn Stultz /* shift_hz stores hz<<8 for extra accuracy */ 108b3c869d3SJohn Stultz shift_hz = (u64)cycles_per_second << 8; 109b3c869d3SJohn Stultz shift_hz += cycles_per_tick/2; 110b3c869d3SJohn Stultz do_div(shift_hz, cycles_per_tick); 111b3c869d3SJohn Stultz /* Calculate nsec_per_tick using shift_hz */ 112fa3aa7a5SFrederic Weisbecker nsec_per_tick = (u64)NSEC_PER_SEC << 8; 113b3c869d3SJohn Stultz nsec_per_tick += (u32)shift_hz/2; 114b3c869d3SJohn Stultz do_div(nsec_per_tick, (u32)shift_hz); 115b3c869d3SJohn Stultz 116b3c869d3SJohn Stultz refined_jiffies.mult = ((u32)nsec_per_tick) << JIFFIES_SHIFT; 117b3c869d3SJohn Stultz 118f8935983SJohn Stultz __clocksource_register(&refined_jiffies); 119b3c869d3SJohn Stultz return 0; 120b3c869d3SJohn Stultz } 121