xref: /linux/kernel/time/jiffies.c (revision a5a1d1c2914b5316924c7893eb683a5420ebd3be)
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 /* The Jiffies based clocksource is the lowest common
31734efb46Sjohn stultz  * denominator clock source which should function on
32734efb46Sjohn stultz  * all systems. It has the same coarse resolution as
33734efb46Sjohn stultz  * the timer interrupt frequency HZ and it suffers
34734efb46Sjohn stultz  * inaccuracies caused by missed or lost timer
35734efb46Sjohn stultz  * interrupts and the inability for the timer
36734efb46Sjohn stultz  * interrupt hardware to accuratly tick at the
3725985edcSLucas De Marchi  * requested HZ value. It is also not recommended
38734efb46Sjohn stultz  * for "tick-less" systems.
39734efb46Sjohn stultz  */
40b3c869d3SJohn Stultz #define NSEC_PER_JIFFY	((NSEC_PER_SEC+HZ/2)/HZ)
41734efb46Sjohn stultz 
42734efb46Sjohn stultz /* Since jiffies uses a simple NSEC_PER_JIFFY multiplier
43734efb46Sjohn stultz  * conversion, the .shift value could be zero. However
44734efb46Sjohn stultz  * this would make NTP adjustments impossible as they are
45734efb46Sjohn stultz  * in units of 1/2^.shift. Thus we use JIFFIES_SHIFT to
46734efb46Sjohn stultz  * shift both the nominator and denominator the same
47734efb46Sjohn stultz  * amount, and give ntp adjustments in units of 1/2^8
48734efb46Sjohn stultz  *
49734efb46Sjohn stultz  * The value 8 is somewhat carefully chosen, as anything
50734efb46Sjohn stultz  * larger can result in overflows. NSEC_PER_JIFFY grows as
51025dfdafSFrederik Schwarzer  * HZ shrinks, so values greater than 8 overflow 32bits when
52734efb46Sjohn stultz  * HZ=100.
53734efb46Sjohn stultz  */
5480d767d7SMikulas Patocka #if HZ < 34
5580d767d7SMikulas Patocka #define JIFFIES_SHIFT	6
5680d767d7SMikulas Patocka #elif HZ < 67
5780d767d7SMikulas Patocka #define JIFFIES_SHIFT	7
5880d767d7SMikulas Patocka #else
59734efb46Sjohn stultz #define JIFFIES_SHIFT	8
6080d767d7SMikulas Patocka #endif
61734efb46Sjohn stultz 
62*a5a1d1c2SThomas Gleixner static u64 jiffies_read(struct clocksource *cs)
63734efb46Sjohn stultz {
64*a5a1d1c2SThomas Gleixner 	return (u64) jiffies;
65734efb46Sjohn stultz }
66734efb46Sjohn stultz 
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),
72734efb46Sjohn stultz 	.mult		= NSEC_PER_JIFFY << 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 */
128b3c869d3SJohn Stultz 	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