]> nv-tegra.nvidia Code Review - linux-3.10.git/blob - arch/i386/kernel/timers/timer_hpet.c
Linux-2.6.12-rc2
[linux-3.10.git] / arch / i386 / kernel / timers / timer_hpet.c
1 /*
2  * This code largely moved from arch/i386/kernel/time.c.
3  * See comments there for proper credits.
4  */
5
6 #include <linux/spinlock.h>
7 #include <linux/init.h>
8 #include <linux/timex.h>
9 #include <linux/errno.h>
10 #include <linux/string.h>
11 #include <linux/jiffies.h>
12
13 #include <asm/timer.h>
14 #include <asm/io.h>
15 #include <asm/processor.h>
16
17 #include "io_ports.h"
18 #include "mach_timer.h"
19 #include <asm/hpet.h>
20
21 static unsigned long hpet_usec_quotient;        /* convert hpet clks to usec */
22 static unsigned long tsc_hpet_quotient;         /* convert tsc to hpet clks */
23 static unsigned long hpet_last;         /* hpet counter value at last tick*/
24 static unsigned long last_tsc_low;      /* lsb 32 bits of Time Stamp Counter */
25 static unsigned long last_tsc_high;     /* msb 32 bits of Time Stamp Counter */
26 static unsigned long long monotonic_base;
27 static seqlock_t monotonic_lock = SEQLOCK_UNLOCKED;
28
29 /* convert from cycles(64bits) => nanoseconds (64bits)
30  *  basic equation:
31  *              ns = cycles / (freq / ns_per_sec)
32  *              ns = cycles * (ns_per_sec / freq)
33  *              ns = cycles * (10^9 / (cpu_mhz * 10^6))
34  *              ns = cycles * (10^3 / cpu_mhz)
35  *
36  *      Then we use scaling math (suggested by george@mvista.com) to get:
37  *              ns = cycles * (10^3 * SC / cpu_mhz) / SC
38  *              ns = cycles * cyc2ns_scale / SC
39  *
40  *      And since SC is a constant power of two, we can convert the div
41  *  into a shift.
42  *                      -johnstul@us.ibm.com "math is hard, lets go shopping!"
43  */
44 static unsigned long cyc2ns_scale;
45 #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
46
47 static inline void set_cyc2ns_scale(unsigned long cpu_mhz)
48 {
49         cyc2ns_scale = (1000 << CYC2NS_SCALE_FACTOR)/cpu_mhz;
50 }
51
52 static inline unsigned long long cycles_2_ns(unsigned long long cyc)
53 {
54         return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
55 }
56
57 static unsigned long long monotonic_clock_hpet(void)
58 {
59         unsigned long long last_offset, this_offset, base;
60         unsigned seq;
61
62         /* atomically read monotonic base & last_offset */
63         do {
64                 seq = read_seqbegin(&monotonic_lock);
65                 last_offset = ((unsigned long long)last_tsc_high<<32)|last_tsc_low;
66                 base = monotonic_base;
67         } while (read_seqretry(&monotonic_lock, seq));
68
69         /* Read the Time Stamp Counter */
70         rdtscll(this_offset);
71
72         /* return the value in ns */
73         return base + cycles_2_ns(this_offset - last_offset);
74 }
75
76 static unsigned long get_offset_hpet(void)
77 {
78         register unsigned long eax, edx;
79
80         eax = hpet_readl(HPET_COUNTER);
81         eax -= hpet_last;       /* hpet delta */
82
83         /*
84          * Time offset = (hpet delta) * ( usecs per HPET clock )
85          *             = (hpet delta) * ( usecs per tick / HPET clocks per tick)
86          *             = (hpet delta) * ( hpet_usec_quotient ) / (2^32)
87          *
88          * Where,
89          * hpet_usec_quotient = (2^32 * usecs per tick)/HPET clocks per tick
90          *
91          * Using a mull instead of a divl saves some cycles in critical path.
92          */
93         ASM_MUL64_REG(eax, edx, hpet_usec_quotient, eax);
94
95         /* our adjusted time offset in microseconds */
96         return edx;
97 }
98
99 static void mark_offset_hpet(void)
100 {
101         unsigned long long this_offset, last_offset;
102         unsigned long offset;
103
104         write_seqlock(&monotonic_lock);
105         last_offset = ((unsigned long long)last_tsc_high<<32)|last_tsc_low;
106         rdtsc(last_tsc_low, last_tsc_high);
107
108         offset = hpet_readl(HPET_T0_CMP) - hpet_tick;
109         if (unlikely(((offset - hpet_last) > hpet_tick) && (hpet_last != 0))) {
110                 int lost_ticks = (offset - hpet_last) / hpet_tick;
111                 jiffies_64 += lost_ticks;
112         }
113         hpet_last = offset;
114
115         /* update the monotonic base value */
116         this_offset = ((unsigned long long)last_tsc_high<<32)|last_tsc_low;
117         monotonic_base += cycles_2_ns(this_offset - last_offset);
118         write_sequnlock(&monotonic_lock);
119 }
120
121 static void delay_hpet(unsigned long loops)
122 {
123         unsigned long hpet_start, hpet_end;
124         unsigned long eax;
125
126         /* loops is the number of cpu cycles. Convert it to hpet clocks */
127         ASM_MUL64_REG(eax, loops, tsc_hpet_quotient, loops);
128
129         hpet_start = hpet_readl(HPET_COUNTER);
130         do {
131                 rep_nop();
132                 hpet_end = hpet_readl(HPET_COUNTER);
133         } while ((hpet_end - hpet_start) < (loops));
134 }
135
136 static int __init init_hpet(char* override)
137 {
138         unsigned long result, remain;
139
140         /* check clock override */
141         if (override[0] && strncmp(override,"hpet",4))
142                 return -ENODEV;
143
144         if (!is_hpet_enabled())
145                 return -ENODEV;
146
147         printk("Using HPET for gettimeofday\n");
148         if (cpu_has_tsc) {
149                 unsigned long tsc_quotient = calibrate_tsc_hpet(&tsc_hpet_quotient);
150                 if (tsc_quotient) {
151                         /* report CPU clock rate in Hz.
152                          * The formula is (10^6 * 2^32) / (2^32 * 1 / (clocks/us)) =
153                          * clock/second. Our precision is about 100 ppm.
154                          */
155                         {       unsigned long eax=0, edx=1000;
156                                 ASM_DIV64_REG(cpu_khz, edx, tsc_quotient,
157                                                 eax, edx);
158                                 printk("Detected %lu.%03lu MHz processor.\n",
159                                         cpu_khz / 1000, cpu_khz % 1000);
160                         }
161                         set_cyc2ns_scale(cpu_khz/1000);
162                 }
163         }
164
165         /*
166          * Math to calculate hpet to usec multiplier
167          * Look for the comments at get_offset_hpet()
168          */
169         ASM_DIV64_REG(result, remain, hpet_tick, 0, KERNEL_TICK_USEC);
170         if (remain > (hpet_tick >> 1))
171                 result++; /* rounding the result */
172         hpet_usec_quotient = result;
173
174         return 0;
175 }
176
177 /************************************************************/
178
179 /* tsc timer_opts struct */
180 static struct timer_opts timer_hpet = {
181         .name =                 "hpet",
182         .mark_offset =          mark_offset_hpet,
183         .get_offset =           get_offset_hpet,
184         .monotonic_clock =      monotonic_clock_hpet,
185         .delay =                delay_hpet,
186 };
187
188 struct init_timer_opts __initdata timer_hpet_init = {
189         .init = init_hpet,
190         .opts = &timer_hpet,
191 };