1 /*********************************************************************** 2 * This file contains the jiffies based clocksource. 3 * 4 * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com) 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 * 20 ************************************************************************/ 21 #include <linux/clocksource.h> 22 #include <linux/jiffies.h> 23 #include <linux/module.h> 24 #include <linux/init.h> 25 26 #include "timekeeping.h" 27 28 29 /* Since jiffies uses a simple TICK_NSEC multiplier 30 * conversion, the .shift value could be zero. However 31 * this would make NTP adjustments impossible as they are 32 * in units of 1/2^.shift. Thus we use JIFFIES_SHIFT to 33 * shift both the nominator and denominator the same 34 * amount, and give ntp adjustments in units of 1/2^8 35 * 36 * The value 8 is somewhat carefully chosen, as anything 37 * larger can result in overflows. TICK_NSEC grows as HZ 38 * shrinks, so values greater than 8 overflow 32bits when 39 * HZ=100. 40 */ 41 #if HZ < 34 42 #define JIFFIES_SHIFT 6 43 #elif HZ < 67 44 #define JIFFIES_SHIFT 7 45 #else 46 #define JIFFIES_SHIFT 8 47 #endif 48 49 static u64 jiffies_read(struct clocksource *cs) 50 { 51 return (u64) jiffies; 52 } 53 54 /* 55 * The Jiffies based clocksource is the lowest common 56 * denominator clock source which should function on 57 * all systems. It has the same coarse resolution as 58 * the timer interrupt frequency HZ and it suffers 59 * inaccuracies caused by missed or lost timer 60 * interrupts and the inability for the timer 61 * interrupt hardware to accuratly tick at the 62 * requested HZ value. It is also not recommended 63 * for "tick-less" systems. 64 */ 65 static struct clocksource clocksource_jiffies = { 66 .name = "jiffies", 67 .rating = 1, /* lowest valid rating*/ 68 .read = jiffies_read, 69 .mask = CLOCKSOURCE_MASK(32), 70 .mult = TICK_NSEC << JIFFIES_SHIFT, /* details above */ 71 .shift = JIFFIES_SHIFT, 72 .max_cycles = 10, 73 }; 74 75 __cacheline_aligned_in_smp DEFINE_SEQLOCK(jiffies_lock); 76 77 #if (BITS_PER_LONG < 64) 78 u64 get_jiffies_64(void) 79 { 80 unsigned long seq; 81 u64 ret; 82 83 do { 84 seq = read_seqbegin(&jiffies_lock); 85 ret = jiffies_64; 86 } while (read_seqretry(&jiffies_lock, seq)); 87 return ret; 88 } 89 EXPORT_SYMBOL(get_jiffies_64); 90 #endif 91 92 EXPORT_SYMBOL(jiffies); 93 94 static int __init init_jiffies_clocksource(void) 95 { 96 return __clocksource_register(&clocksource_jiffies); 97 } 98 99 core_initcall(init_jiffies_clocksource); 100 101 struct clocksource * __init __weak clocksource_default_clock(void) 102 { 103 return &clocksource_jiffies; 104 } 105 106 struct clocksource refined_jiffies; 107 108 int register_refined_jiffies(long cycles_per_second) 109 { 110 u64 nsec_per_tick, shift_hz; 111 long cycles_per_tick; 112 113 114 115 refined_jiffies = clocksource_jiffies; 116 refined_jiffies.name = "refined-jiffies"; 117 refined_jiffies.rating++; 118 119 /* Calc cycles per tick */ 120 cycles_per_tick = (cycles_per_second + HZ/2)/HZ; 121 /* shift_hz stores hz<<8 for extra accuracy */ 122 shift_hz = (u64)cycles_per_second << 8; 123 shift_hz += cycles_per_tick/2; 124 do_div(shift_hz, cycles_per_tick); 125 /* Calculate nsec_per_tick using shift_hz */ 126 nsec_per_tick = (u64)NSEC_PER_SEC << 8; 127 nsec_per_tick += (u32)shift_hz/2; 128 do_div(nsec_per_tick, (u32)shift_hz); 129 130 refined_jiffies.mult = ((u32)nsec_per_tick) << JIFFIES_SHIFT; 131 132 __clocksource_register(&refined_jiffies); 133 return 0; 134 } 135