xref: /linux/kernel/time/jiffies.c (revision d6ad418763888f617ac5b4849823e4cd670df1dd)
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 
28e2830b5cSTorben Hohn #include "tick-internal.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  */
54734efb46Sjohn stultz #define JIFFIES_SHIFT	8
55734efb46Sjohn stultz 
568e19608eSMagnus Damm static cycle_t jiffies_read(struct clocksource *cs)
57734efb46Sjohn stultz {
58734efb46Sjohn stultz 	return (cycle_t) jiffies;
59734efb46Sjohn stultz }
60734efb46Sjohn stultz 
61f95a9857SLars-Peter Clausen static struct clocksource clocksource_jiffies = {
62734efb46Sjohn stultz 	.name		= "jiffies",
633f4a0b91Sjohn stultz 	.rating		= 1, /* lowest valid rating*/
64734efb46Sjohn stultz 	.read		= jiffies_read,
65734efb46Sjohn stultz 	.mask		= 0xffffffff, /*32bits*/
66734efb46Sjohn stultz 	.mult		= NSEC_PER_JIFFY << JIFFIES_SHIFT, /* details above */
67734efb46Sjohn stultz 	.shift		= JIFFIES_SHIFT,
68734efb46Sjohn stultz };
69734efb46Sjohn stultz 
70*d6ad4187SJohn Stultz __cacheline_aligned_in_smp DEFINE_SEQLOCK(jiffies_lock);
71*d6ad4187SJohn Stultz 
72fbad1ea9STorben Hohn #if (BITS_PER_LONG < 64)
73fbad1ea9STorben Hohn u64 get_jiffies_64(void)
74fbad1ea9STorben Hohn {
75fbad1ea9STorben Hohn 	unsigned long seq;
76fbad1ea9STorben Hohn 	u64 ret;
77fbad1ea9STorben Hohn 
78fbad1ea9STorben Hohn 	do {
79*d6ad4187SJohn Stultz 		seq = read_seqbegin(&jiffies_lock);
80fbad1ea9STorben Hohn 		ret = jiffies_64;
81*d6ad4187SJohn Stultz 	} while (read_seqretry(&jiffies_lock, seq));
82fbad1ea9STorben Hohn 	return ret;
83fbad1ea9STorben Hohn }
84fbad1ea9STorben Hohn EXPORT_SYMBOL(get_jiffies_64);
85fbad1ea9STorben Hohn #endif
86fbad1ea9STorben Hohn 
87fbad1ea9STorben Hohn EXPORT_SYMBOL(jiffies);
88fbad1ea9STorben Hohn 
89734efb46Sjohn stultz static int __init init_jiffies_clocksource(void)
90734efb46Sjohn stultz {
91a2752549Sjohn stultz 	return clocksource_register(&clocksource_jiffies);
92734efb46Sjohn stultz }
93734efb46Sjohn stultz 
9498de9e3bSjohn stultz core_initcall(init_jiffies_clocksource);
95f1b82746SMartin Schwidefsky 
96f1b82746SMartin Schwidefsky struct clocksource * __init __weak clocksource_default_clock(void)
97f1b82746SMartin Schwidefsky {
98f1b82746SMartin Schwidefsky 	return &clocksource_jiffies;
99f1b82746SMartin Schwidefsky }
100b3c869d3SJohn Stultz 
101b3c869d3SJohn Stultz struct clocksource refined_jiffies;
102b3c869d3SJohn Stultz 
103b3c869d3SJohn Stultz int register_refined_jiffies(long cycles_per_second)
104b3c869d3SJohn Stultz {
105b3c869d3SJohn Stultz 	u64 nsec_per_tick, shift_hz;
106b3c869d3SJohn Stultz 	long cycles_per_tick;
107b3c869d3SJohn Stultz 
108b3c869d3SJohn Stultz 
109b3c869d3SJohn Stultz 
110b3c869d3SJohn Stultz 	refined_jiffies = clocksource_jiffies;
111b3c869d3SJohn Stultz 	refined_jiffies.name = "refined-jiffies";
112b3c869d3SJohn Stultz 	refined_jiffies.rating++;
113b3c869d3SJohn Stultz 
114b3c869d3SJohn Stultz 	/* Calc cycles per tick */
115b3c869d3SJohn Stultz 	cycles_per_tick = (cycles_per_second + HZ/2)/HZ;
116b3c869d3SJohn Stultz 	/* shift_hz stores hz<<8 for extra accuracy */
117b3c869d3SJohn Stultz 	shift_hz = (u64)cycles_per_second << 8;
118b3c869d3SJohn Stultz 	shift_hz += cycles_per_tick/2;
119b3c869d3SJohn Stultz 	do_div(shift_hz, cycles_per_tick);
120b3c869d3SJohn Stultz 	/* Calculate nsec_per_tick using shift_hz */
121b3c869d3SJohn Stultz 	nsec_per_tick = (u64)NSEC_PER_SEC << 8;
122b3c869d3SJohn Stultz 	nsec_per_tick += (u32)shift_hz/2;
123b3c869d3SJohn Stultz 	do_div(nsec_per_tick, (u32)shift_hz);
124b3c869d3SJohn Stultz 
125b3c869d3SJohn Stultz 	refined_jiffies.mult = ((u32)nsec_per_tick) << JIFFIES_SHIFT;
126b3c869d3SJohn Stultz 
127b3c869d3SJohn Stultz 	clocksource_register(&refined_jiffies);
128b3c869d3SJohn Stultz 	return 0;
129b3c869d3SJohn Stultz }
130