1734efb46Sjohn stultz /*********************************************************************** 2734efb46Sjohn stultz * linux/kernel/time/jiffies.c 3734efb46Sjohn stultz * 4734efb46Sjohn stultz * This file contains the jiffies based clocksource. 5734efb46Sjohn stultz * 6734efb46Sjohn stultz * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com) 7734efb46Sjohn stultz * 8734efb46Sjohn stultz * This program is free software; you can redistribute it and/or modify 9734efb46Sjohn stultz * it under the terms of the GNU General Public License as published by 10734efb46Sjohn stultz * the Free Software Foundation; either version 2 of the License, or 11734efb46Sjohn stultz * (at your option) any later version. 12734efb46Sjohn stultz * 13734efb46Sjohn stultz * This program is distributed in the hope that it will be useful, 14734efb46Sjohn stultz * but WITHOUT ANY WARRANTY; without even the implied warranty of 15734efb46Sjohn stultz * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16734efb46Sjohn stultz * GNU General Public License for more details. 17734efb46Sjohn stultz * 18734efb46Sjohn stultz * You should have received a copy of the GNU General Public License 19734efb46Sjohn stultz * along with this program; if not, write to the Free Software 20734efb46Sjohn stultz * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21734efb46Sjohn stultz * 22734efb46Sjohn stultz ************************************************************************/ 23734efb46Sjohn stultz #include <linux/clocksource.h> 24734efb46Sjohn stultz #include <linux/jiffies.h> 25fbad1ea9STorben Hohn #include <linux/module.h> 26734efb46Sjohn stultz #include <linux/init.h> 27734efb46Sjohn stultz 28bfb83b27SThomas Gleixner #include "timekeeping.h" 29e2830b5cSTorben Hohn 30734efb46Sjohn stultz 3193825f2eSFrederic Weisbecker /* Since jiffies uses a simple TICK_NSEC multiplier 32734efb46Sjohn stultz * conversion, the .shift value could be zero. However 33734efb46Sjohn stultz * this would make NTP adjustments impossible as they are 34734efb46Sjohn stultz * in units of 1/2^.shift. Thus we use JIFFIES_SHIFT to 35734efb46Sjohn stultz * shift both the nominator and denominator the same 36734efb46Sjohn stultz * amount, and give ntp adjustments in units of 1/2^8 37734efb46Sjohn stultz * 38734efb46Sjohn stultz * The value 8 is somewhat carefully chosen, as anything 3993825f2eSFrederic Weisbecker * larger can result in overflows. TICK_NSEC grows as HZ 4093825f2eSFrederic Weisbecker * shrinks, so values greater than 8 overflow 32bits when 41734efb46Sjohn stultz * HZ=100. 42734efb46Sjohn stultz */ 4380d767d7SMikulas Patocka #if HZ < 34 4480d767d7SMikulas Patocka #define JIFFIES_SHIFT 6 4580d767d7SMikulas Patocka #elif HZ < 67 4680d767d7SMikulas Patocka #define JIFFIES_SHIFT 7 4780d767d7SMikulas Patocka #else 48734efb46Sjohn stultz #define JIFFIES_SHIFT 8 4980d767d7SMikulas Patocka #endif 50734efb46Sjohn stultz 51a5a1d1c2SThomas Gleixner static u64 jiffies_read(struct clocksource *cs) 52734efb46Sjohn stultz { 53a5a1d1c2SThomas Gleixner return (u64) jiffies; 54734efb46Sjohn stultz } 55734efb46Sjohn stultz 5693825f2eSFrederic Weisbecker /* 5793825f2eSFrederic Weisbecker * The Jiffies based clocksource is the lowest common 5893825f2eSFrederic Weisbecker * denominator clock source which should function on 5993825f2eSFrederic Weisbecker * all systems. It has the same coarse resolution as 6093825f2eSFrederic Weisbecker * the timer interrupt frequency HZ and it suffers 6193825f2eSFrederic Weisbecker * inaccuracies caused by missed or lost timer 6293825f2eSFrederic Weisbecker * interrupts and the inability for the timer 6393825f2eSFrederic Weisbecker * interrupt hardware to accuratly tick at the 6493825f2eSFrederic Weisbecker * requested HZ value. It is also not recommended 6593825f2eSFrederic Weisbecker * for "tick-less" systems. 6693825f2eSFrederic Weisbecker */ 67f95a9857SLars-Peter Clausen static struct clocksource clocksource_jiffies = { 68734efb46Sjohn stultz .name = "jiffies", 693f4a0b91Sjohn stultz .rating = 1, /* lowest valid rating*/ 70734efb46Sjohn stultz .read = jiffies_read, 71232d2637SAlexander Kuleshov .mask = CLOCKSOURCE_MASK(32), 7293825f2eSFrederic Weisbecker .mult = TICK_NSEC << JIFFIES_SHIFT, /* details above */ 73734efb46Sjohn stultz .shift = JIFFIES_SHIFT, 743c17ad19SJohn Stultz .max_cycles = 10, 75734efb46Sjohn stultz }; 76734efb46Sjohn stultz 77d6ad4187SJohn Stultz __cacheline_aligned_in_smp DEFINE_SEQLOCK(jiffies_lock); 78d6ad4187SJohn Stultz 79fbad1ea9STorben Hohn #if (BITS_PER_LONG < 64) 80fbad1ea9STorben Hohn u64 get_jiffies_64(void) 81fbad1ea9STorben Hohn { 82fbad1ea9STorben Hohn unsigned long seq; 83fbad1ea9STorben Hohn u64 ret; 84fbad1ea9STorben Hohn 85fbad1ea9STorben Hohn do { 86d6ad4187SJohn Stultz seq = read_seqbegin(&jiffies_lock); 87fbad1ea9STorben Hohn ret = jiffies_64; 88d6ad4187SJohn Stultz } while (read_seqretry(&jiffies_lock, seq)); 89fbad1ea9STorben Hohn return ret; 90fbad1ea9STorben Hohn } 91fbad1ea9STorben Hohn EXPORT_SYMBOL(get_jiffies_64); 92fbad1ea9STorben Hohn #endif 93fbad1ea9STorben Hohn 94fbad1ea9STorben Hohn EXPORT_SYMBOL(jiffies); 95fbad1ea9STorben Hohn 96734efb46Sjohn stultz static int __init init_jiffies_clocksource(void) 97734efb46Sjohn stultz { 98f8935983SJohn Stultz return __clocksource_register(&clocksource_jiffies); 99734efb46Sjohn stultz } 100734efb46Sjohn stultz 10198de9e3bSjohn stultz core_initcall(init_jiffies_clocksource); 102f1b82746SMartin Schwidefsky 103f1b82746SMartin Schwidefsky struct clocksource * __init __weak clocksource_default_clock(void) 104f1b82746SMartin Schwidefsky { 105f1b82746SMartin Schwidefsky return &clocksource_jiffies; 106f1b82746SMartin Schwidefsky } 107b3c869d3SJohn Stultz 108b3c869d3SJohn Stultz struct clocksource refined_jiffies; 109b3c869d3SJohn Stultz 110b3c869d3SJohn Stultz int register_refined_jiffies(long cycles_per_second) 111b3c869d3SJohn Stultz { 112b3c869d3SJohn Stultz u64 nsec_per_tick, shift_hz; 113b3c869d3SJohn Stultz long cycles_per_tick; 114b3c869d3SJohn Stultz 115b3c869d3SJohn Stultz 116b3c869d3SJohn Stultz 117b3c869d3SJohn Stultz refined_jiffies = clocksource_jiffies; 118b3c869d3SJohn Stultz refined_jiffies.name = "refined-jiffies"; 119b3c869d3SJohn Stultz refined_jiffies.rating++; 120b3c869d3SJohn Stultz 121b3c869d3SJohn Stultz /* Calc cycles per tick */ 122b3c869d3SJohn Stultz cycles_per_tick = (cycles_per_second + HZ/2)/HZ; 123b3c869d3SJohn Stultz /* shift_hz stores hz<<8 for extra accuracy */ 124b3c869d3SJohn Stultz shift_hz = (u64)cycles_per_second << 8; 125b3c869d3SJohn Stultz shift_hz += cycles_per_tick/2; 126b3c869d3SJohn Stultz do_div(shift_hz, cycles_per_tick); 127b3c869d3SJohn Stultz /* Calculate nsec_per_tick using shift_hz */ 128*fa3aa7a5SFrederic Weisbecker nsec_per_tick = (u64)NSEC_PER_SEC << 8; 129b3c869d3SJohn Stultz nsec_per_tick += (u32)shift_hz/2; 130b3c869d3SJohn Stultz do_div(nsec_per_tick, (u32)shift_hz); 131b3c869d3SJohn Stultz 132b3c869d3SJohn Stultz refined_jiffies.mult = ((u32)nsec_per_tick) << JIFFIES_SHIFT; 133b3c869d3SJohn Stultz 134f8935983SJohn Stultz __clocksource_register(&refined_jiffies); 135b3c869d3SJohn Stultz return 0; 136b3c869d3SJohn Stultz } 137