]> git.proxmox.com Git - grub2.git/blob - grub-core/kern/i386/tsc.c
Move cpuid code to cpuid.h and TSC code to tsc.c.
[grub2.git] / grub-core / kern / i386 / tsc.c
1 /* kern/i386/tsc.c - x86 TSC time source implementation
2 * Requires Pentium or better x86 CPU that supports the RDTSC instruction.
3 * This module uses the RTC (via grub_get_rtc()) to calibrate the TSC to
4 * real time.
5 *
6 * GRUB -- GRand Unified Bootloader
7 * Copyright (C) 2008 Free Software Foundation, Inc.
8 *
9 * GRUB is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * GRUB is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include <grub/types.h>
24 #include <grub/time.h>
25 #include <grub/misc.h>
26 #include <grub/i386/tsc.h>
27 #include <grub/i386/cpuid.h>
28 #include <grub/i386/pit.h>
29 #include <grub/cpu/io.h>
30
31 /* This defines the value TSC had at the epoch (that is, when we calibrated it). */
32 static grub_uint64_t tsc_boot_time;
33
34 /* Calibrated TSC rate. (In ms per 2^32 ticks) */
35 /* We assume that the tick is less than 1 ms and hence this value fits
36 in 32-bit. */
37 grub_uint32_t grub_tsc_rate;
38
39 /* Read the TSC value, which increments with each CPU clock cycle. */
40 static __inline grub_uint64_t
41 grub_get_tsc (void)
42 {
43 grub_uint32_t lo, hi;
44
45 /* The CPUID instruction is a 'serializing' instruction, and
46 avoids out-of-order execution of the RDTSC instruction. */
47 #ifdef __APPLE__
48 __asm__ __volatile__ ("xorl %%eax, %%eax\n\t"
49 #ifdef __x86_64__
50 "push %%rbx\n"
51 #else
52 "push %%ebx\n"
53 #endif
54 "cpuid\n"
55 #ifdef __x86_64__
56 "pop %%rbx\n"
57 #else
58 "pop %%ebx\n"
59 #endif
60 :::"%rax", "%rcx", "%rdx");
61 #else
62 __asm__ __volatile__ ("xorl %%eax, %%eax\n\t"
63 "cpuid":::"%rax", "%rbx", "%rcx", "%rdx");
64 #endif
65 /* Read TSC value. We cannot use "=A", since this would use
66 %rax on x86_64. */
67 __asm__ __volatile__ ("rdtsc":"=a" (lo), "=d" (hi));
68
69 return (((grub_uint64_t) hi) << 32) | lo;
70 }
71
72 static __inline int
73 grub_cpu_is_tsc_supported (void)
74 {
75 if (! grub_cpu_is_cpuid_supported ())
76 return 0;
77
78 grub_uint32_t features;
79 #ifdef __APPLE__
80 __asm__ ("movl $1, %%eax\n\t"
81 #ifdef __x86_64__
82 "push %%rbx\n"
83 #else
84 "push %%ebx\n"
85 #endif
86 "cpuid\n"
87 #ifdef __x86_64__
88 "pop %%rbx\n"
89 #else
90 "pop %%ebx\n"
91 #endif
92 : "=d" (features)
93 : /* No inputs. */
94 : /* Clobbered: */ "%rax", "%rcx");
95 #else
96 __asm__ ("movl $1, %%eax\n\t"
97 "cpuid\n"
98 : "=d" (features)
99 : /* No inputs. */
100 : /* Clobbered: */ "%rax", "%rbx", "%rcx");
101 #endif
102 return (features & (1 << 4)) != 0;
103 }
104
105 static void
106 grub_pit_wait (grub_uint16_t tics)
107 {
108 /* Disable timer2 gate and speaker. */
109 grub_outb (grub_inb (GRUB_PIT_SPEAKER_PORT)
110 & ~ (GRUB_PIT_SPK_DATA | GRUB_PIT_SPK_TMR2),
111 GRUB_PIT_SPEAKER_PORT);
112
113 /* Set tics. */
114 grub_outb (GRUB_PIT_CTRL_SELECT_2 | GRUB_PIT_CTRL_READLOAD_WORD,
115 GRUB_PIT_CTRL);
116 grub_outb (tics & 0xff, GRUB_PIT_COUNTER_2);
117 grub_outb (tics >> 8, GRUB_PIT_COUNTER_2);
118
119 /* Enable timer2 gate, keep speaker disabled. */
120 grub_outb ((grub_inb (GRUB_PIT_SPEAKER_PORT) & ~ GRUB_PIT_SPK_DATA)
121 | GRUB_PIT_SPK_TMR2,
122 GRUB_PIT_SPEAKER_PORT);
123
124 /* Wait. */
125 while ((grub_inb (GRUB_PIT_SPEAKER_PORT) & GRUB_PIT_SPK_TMR2_LATCH) == 0x00);
126
127 /* Disable timer2 gate and speaker. */
128 grub_outb (grub_inb (GRUB_PIT_SPEAKER_PORT)
129 & ~ (GRUB_PIT_SPK_DATA | GRUB_PIT_SPK_TMR2),
130 GRUB_PIT_SPEAKER_PORT);
131 }
132
133 static grub_uint64_t
134 grub_tsc_get_time_ms (void)
135 {
136 grub_uint64_t a = grub_get_tsc () - tsc_boot_time;
137 grub_uint64_t ah = a >> 32;
138 grub_uint64_t al = a & 0xffffffff;
139
140 return ((al * grub_tsc_rate) >> 32) + ah * grub_tsc_rate;
141 }
142
143 /* Calibrate the TSC based on the RTC. */
144 static void
145 calibrate_tsc (void)
146 {
147 /* First calibrate the TSC rate (relative, not absolute time). */
148 grub_uint64_t end_tsc;
149
150 tsc_boot_time = grub_get_tsc ();
151 grub_pit_wait (0xffff);
152 end_tsc = grub_get_tsc ();
153
154 grub_tsc_rate = grub_divmod64 ((55ULL << 32), end_tsc - tsc_boot_time, 0);
155 }
156
157 void
158 grub_tsc_init (void)
159 {
160 if (grub_cpu_is_tsc_supported ())
161 {
162 calibrate_tsc ();
163 grub_install_get_time_ms (grub_tsc_get_time_ms);
164 }
165 else
166 {
167 #if defined (GRUB_MACHINE_PCBIOS) || defined (GRUB_MACHINE_IEEE1275)
168 grub_install_get_time_ms (grub_rtc_get_time_ms);
169 #else
170 grub_fatal ("no TSC found");
171 #endif
172 }
173 }