1 | /*␊ |
2 | * Copyright 2008 Islam Ahmed Zaid. All rights reserved. <azismed@gmail.com>␊ |
3 | * AsereBLN: 2009: cleanup and bugfix␊ |
4 | */␊ |
5 | ␊ |
6 | #ifndef __LIBSAIO_CPU_H␊ |
7 | #define __LIBSAIO_CPU_H␊ |
8 | ␊ |
9 | #include "platform.h"␊ |
10 | ␊ |
11 | extern void scan_cpu(PlatformInfo_t *);␊ |
12 | ␊ |
13 | // DFE: These two constants come from Linux except CLOCK_TICK_RATE replaced with CLKNUM␊ |
14 | #define CALIBRATE_TIME_MSEC␉30␉␉/* 30 msecs */␊ |
15 | #define CALIBRATE_LATCH␉␉((CLKNUM * CALIBRATE_TIME_MSEC + 1000/2)/1000)␊ |
16 | ␊ |
17 | static inline uint64_t rdtsc64(void)␊ |
18 | {␊ |
19 | ␉uint64_t ret;␊ |
20 | ␉__asm__ volatile("rdtsc" : "=A" (ret));␊ |
21 | ␉return ret;␊ |
22 | }␊ |
23 | ␊ |
24 | static inline uint64_t rdmsr64(uint32_t msr)␊ |
25 | {␊ |
26 | uint64_t ret;␊ |
27 | __asm__ volatile("rdmsr" : "=A" (ret) : "c" (msr));␊ |
28 | return ret;␊ |
29 | }␊ |
30 | ␊ |
31 | static inline void wrmsr64(uint32_t msr, uint64_t val)␊ |
32 | {␊ |
33 | ␉__asm__ volatile("wrmsr" : : "c" (msr), "A" (val));␊ |
34 | }␊ |
35 | ␊ |
36 | static inline void intel_waitforsts(void) {␊ |
37 | ␉uint32_t inline_timeout = 100000;␊ |
38 | ␉while (rdmsr64(MSR_IA32_PERF_STATUS) & (1 << 21)) { if (!inline_timeout--) break; }␊ |
39 | }␊ |
40 | ␊ |
41 | /* From Apple's cpuid.h */␊ |
42 | typedef enum { eax, ebx, ecx, edx } cpuid_register_t;␊ |
43 | ␊ |
44 | static inline void cpuid(uint32_t *data)␊ |
45 | {␊ |
46 | ␉asm(␊ |
47 | ␉␉"cpuid" : "=a" (data[eax]),␊ |
48 | ␉␉"=b" (data[ebx]),␊ |
49 | ␉␉"=c" (data[ecx]),␊ |
50 | ␉␉"=d" (data[edx]) : "a" (data[eax]),␊ |
51 | ␉␉"b" (data[ebx]),␊ |
52 | ␉␉"c" (data[ecx]),␊ |
53 | ␉␉"d" (data[edx]));␊ |
54 | }␊ |
55 | ␊ |
56 | static inline void do_cpuid(uint32_t selector, uint32_t *data)␊ |
57 | {␊ |
58 | ␉asm(␊ |
59 | ␉␉"cpuid" : "=a" (data[eax]),␊ |
60 | ␉␉"=b" (data[ebx]),␊ |
61 | ␉␉"=c" (data[ecx]),␊ |
62 | ␉␉"=d" (data[edx]) : "a"(selector),␊ |
63 | ␉␉"b" (0),␊ |
64 | ␉␉"c" (0),␊ |
65 | ␉␉"d" (0));␊ |
66 | }␊ |
67 | ␊ |
68 | static inline void do_cpuid2(uint32_t selector, uint32_t selector2, uint32_t *data)␊ |
69 | {␊ |
70 | ␉asm volatile (␊ |
71 | ␉␉"cpuid" : "=a" (data[eax]),␊ |
72 | ␉␉"=b" (data[ebx]),␊ |
73 | ␉␉"=c" (data[ecx]),␊ |
74 | ␉␉"=d" (data[edx]) : "a" (selector),␊ |
75 | ␉␉"b" (0),␊ |
76 | ␉␉"c" (selector2),␊ |
77 | ␉␉"d" (0));␊ |
78 | }␊ |
79 | ␊ |
80 | // DFE: enable_PIT2 and disable_PIT2 come from older xnu␊ |
81 | ␊ |
82 | /*␊ |
83 | * Enable or disable timer 2.␊ |
84 | * Port 0x61 controls timer 2:␊ |
85 | * bit 0 gates the clock,␊ |
86 | * bit 1 gates output to speaker.␊ |
87 | */␊ |
88 | static inline void enable_PIT2(void)␊ |
89 | {␊ |
90 | /* Enable gate, disable speaker */␊ |
91 | __asm__ volatile(␊ |
92 | ␉␉␉␉␉ " inb $0x61,%%al \n\t"␊ |
93 | ␉␉␉␉␉ " and $0xFC,%%al \n\t" /* & ~0x03 */␊ |
94 | ␉␉␉␉␉ " or $1,%%al \n\t"␊ |
95 | ␉␉␉␉␉ " outb %%al,$0x61 \n\t"␊ |
96 | ␉␉␉␉␉ : : : "%al" );␊ |
97 | }␊ |
98 | ␊ |
99 | static inline void disable_PIT2(void)␊ |
100 | {␊ |
101 | /* Disable gate and output to speaker */␊ |
102 | __asm__ volatile(␊ |
103 | ␉␉␉␉␉ " inb $0x61,%%al \n\t"␊ |
104 | ␉␉␉␉␉ " and $0xFC,%%al \n\t"␉/* & ~0x03 */␊ |
105 | ␉␉␉␉␉ " outb %%al,$0x61 \n\t"␊ |
106 | ␉␉␉␉␉ : : : "%al" );␊ |
107 | }␊ |
108 | ␊ |
109 | // DFE: set_PIT2_mode0, poll_PIT2_gate, and measure_tsc_frequency are␊ |
110 | // roughly based on Linux code␊ |
111 | ␊ |
112 | /* Set the 8254 channel 2 to mode 0 with the specified value.␊ |
113 | In mode 0, the counter will initially set its gate low when the␊ |
114 | timer expires. For this to be useful, you ought to set it high␊ |
115 | before calling this function. The enable_PIT2 function does this.␊ |
116 | */␊ |
117 | static inline void set_PIT2_mode0(uint16_t value)␊ |
118 | {␊ |
119 | __asm__ volatile(␊ |
120 | ␉␉␉␉␉ " movb $0xB0,%%al \n\t"␊ |
121 | ␉␉␉␉␉ " outb␉%%al,$0x43␉\n\t"␊ |
122 | ␉␉␉␉␉ " movb␉%%dl,%%al␉\n\t"␊ |
123 | ␉␉␉␉␉ " outb␉%%al,$0x42␉\n\t"␊ |
124 | ␉␉␉␉␉ " movb␉%%dh,%%al␉\n\t"␊ |
125 | ␉␉␉␉␉ " outb␉%%al,$0x42"␊ |
126 | ␉␉␉␉␉ : : "d"(value) /*: no clobber */ );␊ |
127 | }␊ |
128 | ␊ |
129 | /* Returns the number of times the loop ran before the PIT2 signaled */␊ |
130 | static inline unsigned long poll_PIT2_gate(void)␊ |
131 | {␊ |
132 | unsigned long count = 0;␊ |
133 | unsigned char nmi_sc_val;␊ |
134 | do {␊ |
135 | ++count;␊ |
136 | __asm__ volatile(␊ |
137 | ␉␉␉␉␉␉ "inb␉$0x61,%0"␊ |
138 | ␉␉␉␉␉␉ : "=a"(nmi_sc_val) /*:*/ /* no input */ /*:*/ /* no clobber */);␊ |
139 | } while( (nmi_sc_val & 0x20) == 0);␊ |
140 | return count;␊ |
141 | }␊ |
142 | ␊ |
143 | inline static void␊ |
144 | set_PIT2(int value)␊ |
145 | {␊ |
146 | /*␊ |
147 | * First, tell the clock we are going to write 16 bits to the counter␊ |
148 | * and enable one-shot mode (command 0xB8 to port 0x43)␊ |
149 | * Then write the two bytes into the PIT2 clock register (port 0x42).␊ |
150 | * Loop until the value is "realized" in the clock,␊ |
151 | * this happens on the next tick.␊ |
152 | */␊ |
153 | asm volatile(␊ |
154 | " movb $0xB8,%%al \n\t"␊ |
155 | " outb %%al,$0x43 \n\t"␊ |
156 | " movb %%dl,%%al \n\t"␊ |
157 | " outb %%al,$0x42 \n\t"␊ |
158 | " movb %%dh,%%al \n\t"␊ |
159 | " outb %%al,$0x42 \n"␊ |
160 | "1: inb $0x42,%%al \n\t" ␊ |
161 | " inb $0x42,%%al \n\t"␊ |
162 | " cmp %%al,%%dh \n\t"␊ |
163 | " jne 1b"␊ |
164 | : : "d"(value) : "%al");␊ |
165 | }␊ |
166 | ␊ |
167 | ␊ |
168 | inline static uint64_t␊ |
169 | get_PIT2(unsigned int *value)␊ |
170 | {␊ |
171 | register uint64_t result;␊ |
172 | /*␊ |
173 | * This routine first latches the time (command 0x80 to port 0x43),␊ |
174 | * then gets the time stamp so we know how long the read will take later.␊ |
175 | * Read (from port 0x42) and return the current value of the timer.␊ |
176 | */␊ |
177 | #ifdef __i386__␊ |
178 | asm volatile(␊ |
179 | " xorl %%ecx,%%ecx \n\t"␊ |
180 | " movb $0x80,%%al \n\t"␊ |
181 | " outb %%al,$0x43 \n\t"␊ |
182 | " rdtsc \n\t"␊ |
183 | " pushl %%eax \n\t"␊ |
184 | " inb $0x42,%%al \n\t"␊ |
185 | " movb %%al,%%cl \n\t"␊ |
186 | " inb $0x42,%%al \n\t"␊ |
187 | " movb %%al,%%ch \n\t"␊ |
188 | " popl %%eax "␊ |
189 | : "=A"(result), "=c"(*value));␊ |
190 | #else /* __x86_64__ */␊ |
191 | asm volatile(␊ |
192 | ␉␉" xorq %%rcx,%%rcx \n\t"␊ |
193 | ␉␉" movb $0x80,%%al \n\t"␊ |
194 | ␉␉" outb %%al,$0x43 \n\t"␊ |
195 | ␉␉" rdtsc \n\t"␊ |
196 | ␉␉" pushq %%rax \n\t"␊ |
197 | ␉␉" inb $0x42,%%al \n\t"␊ |
198 | ␉␉" movb %%al,%%cl \n\t"␊ |
199 | ␉␉" inb $0x42,%%al \n\t"␊ |
200 | ␉␉" movb %%al,%%ch \n\t"␊ |
201 | ␉␉" popq %%rax "␊ |
202 | ␉␉: "=A"(result), "=c"(*value));␊ |
203 | #endif␊ |
204 | ␊ |
205 | return result;␊ |
206 | }␊ |
207 | ␊ |
208 | #endif /* !__LIBSAIO_CPU_H */␊ |
209 | |