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 | struct clock_frequency_info_t␊ |
14 | {␊ |
15 | ␉unsigned long bus_clock_rate_hz;␊ |
16 | ␉unsigned long cpu_clock_rate_hz;␊ |
17 | ␉unsigned long dec_clock_rate_hz;␊ |
18 | ␉unsigned long bus_clock_rate_num;␊ |
19 | ␉unsigned long bus_clock_rate_den;␊ |
20 | ␉unsigned long bus_to_cpu_rate_num;␊ |
21 | ␉unsigned long bus_to_cpu_rate_den;␊ |
22 | ␉unsigned long bus_to_dec_rate_num;␊ |
23 | ␉unsigned long bus_to_dec_rate_den;␊ |
24 | ␉unsigned long timebase_frequency_hz;␊ |
25 | ␉unsigned long timebase_frequency_num;␊ |
26 | ␉unsigned long timebase_frequency_den;␊ |
27 | ␉unsigned long long bus_frequency_hz;␊ |
28 | ␉unsigned long long bus_frequency_min_hz;␊ |
29 | ␉unsigned long long bus_frequency_max_hz;␊ |
30 | ␉unsigned long long cpu_frequency_hz;␊ |
31 | ␉unsigned long long cpu_frequency_min_hz;␊ |
32 | ␉unsigned long long cpu_frequency_max_hz;␊ |
33 | ␉unsigned long long prf_frequency_hz;␊ |
34 | ␉unsigned long long prf_frequency_min_hz;␊ |
35 | ␉unsigned long long prf_frequency_max_hz;␊ |
36 | ␉unsigned long long mem_frequency_hz;␊ |
37 | ␉unsigned long long mem_frequency_min_hz;␊ |
38 | ␉unsigned long long mem_frequency_max_hz;␊ |
39 | ␉unsigned long long fix_frequency_hz;␊ |
40 | };␊ |
41 | ␊ |
42 | typedef struct clock_frequency_info_t clock_frequency_info_t;␊ |
43 | ␊ |
44 | extern clock_frequency_info_t gPEClockFrequencyInfo;␊ |
45 | ␊ |
46 | ␊ |
47 | struct mach_timebase_info␊ |
48 | {␊ |
49 | ␉uint32_t␉numer;␊ |
50 | ␉uint32_t␉denom;␊ |
51 | };␊ |
52 | ␊ |
53 | struct hslock␊ |
54 | {␊ |
55 | ␉int␉␉lock_data;␊ |
56 | };␊ |
57 | typedef struct hslock hw_lock_data_t, *hw_lock_t;␊ |
58 | ␊ |
59 | #define hw_lock_addr(hwl)␉(&((hwl).lock_data))␊ |
60 | ␊ |
61 | typedef struct uslock_debug␊ |
62 | {␊ |
63 | ␉void␉␉*lock_pc;␉/* pc where lock operation began */␊ |
64 | ␉void␉␉*lock_thread;␉/* thread that acquired lock */␊ |
65 | ␉unsigned long␉duration[2];␊ |
66 | ␉unsigned short␉state;␊ |
67 | ␉unsigned char␉lock_cpu;␊ |
68 | ␉void␉␉*unlock_thread;␉/* last thread to release lock */␊ |
69 | ␉unsigned char␉unlock_cpu;␊ |
70 | ␉void␉␉*unlock_pc;␉/* pc where lock operation ended */␊ |
71 | } uslock_debug;␊ |
72 | ␊ |
73 | typedef struct slock␊ |
74 | {␊ |
75 | ␉hw_lock_data_t␉interlock;␉/* must be first... see lock.c */␊ |
76 | ␉unsigned short␉lock_type;␉/* must be second... see lock.c */␊ |
77 | #define USLOCK_TAG␉0x5353␊ |
78 | ␉uslock_debug␉debug;␊ |
79 | } usimple_lock_data_t, *usimple_lock_t;␊ |
80 | ␊ |
81 | #if !defined(decl_simple_lock_data)␊ |
82 | typedef usimple_lock_data_t␉*simple_lock_t;␊ |
83 | typedef usimple_lock_data_t␉simple_lock_data_t;␊ |
84 | ␊ |
85 | #define␉decl_simple_lock_data(class,name) \␊ |
86 | class␉simple_lock_data_t␉name;␊ |
87 | #endif␉/* !defined(decl_simple_lock_data) */␊ |
88 | ␊ |
89 | typedef struct mach_timebase_info␉*mach_timebase_info_t;␊ |
90 | typedef struct mach_timebase_info␉mach_timebase_info_data_t;␊ |
91 | ␊ |
92 | // DFE: These two constants come from Linux except CLOCK_TICK_RATE replaced with CLKNUM␊ |
93 | #define CALIBRATE_TIME_MSEC␉30␉␉/* 30 msecs */␊ |
94 | #define CALIBRATE_LATCH␉␉((CLKNUM * CALIBRATE_TIME_MSEC + 1000/2)/1000)␊ |
95 | ␊ |
96 | static inline uint64_t rdtsc64(void)␊ |
97 | {␊ |
98 | ␉uint64_t ret;␊ |
99 | ␉__asm__ volatile("rdtsc" : "=A" (ret));␊ |
100 | ␉return ret;␊ |
101 | }␊ |
102 | ␊ |
103 | static inline uint64_t rdmsr64(uint32_t msr)␊ |
104 | {␊ |
105 | uint64_t ret;␊ |
106 | __asm__ volatile("rdmsr" : "=A" (ret) : "c" (msr));␊ |
107 | return ret;␊ |
108 | }␊ |
109 | ␊ |
110 | static inline void wrmsr64(uint32_t msr, uint64_t val)␊ |
111 | {␊ |
112 | ␉__asm__ volatile("wrmsr" : : "c" (msr), "A" (val));␊ |
113 | }␊ |
114 | ␊ |
115 | static inline void intel_waitforsts(void) {␊ |
116 | ␉uint32_t inline_timeout = 100000;␊ |
117 | ␉while (rdmsr64(MSR_IA32_PERF_STATUS) & (1 << 21)) { if (!inline_timeout--) break; }␊ |
118 | }␊ |
119 | ␊ |
120 | /* From Apple's cpuid.h */␊ |
121 | typedef enum { eax, ebx, ecx, edx } cpuid_register_t;␊ |
122 | ␊ |
123 | static inline void cpuid(uint32_t *data)␊ |
124 | {␊ |
125 | ␉asm(␊ |
126 | ␉␉"cpuid" : "=a" (data[eax]),␊ |
127 | ␉␉"=b" (data[ebx]),␊ |
128 | ␉␉"=c" (data[ecx]),␊ |
129 | ␉␉"=d" (data[edx]) : "a" (data[eax]),␊ |
130 | ␉␉"b" (data[ebx]),␊ |
131 | ␉␉"c" (data[ecx]),␊ |
132 | ␉␉"d" (data[edx]));␊ |
133 | }␊ |
134 | ␊ |
135 | static inline void do_cpuid(uint32_t selector, uint32_t *data)␊ |
136 | {␊ |
137 | ␉asm(␊ |
138 | ␉␉"cpuid" : "=a" (data[eax]),␊ |
139 | ␉␉"=b" (data[ebx]),␊ |
140 | ␉␉"=c" (data[ecx]),␊ |
141 | ␉␉"=d" (data[edx]) : "a"(selector),␊ |
142 | ␉␉"b" (0),␊ |
143 | ␉␉"c" (0),␊ |
144 | ␉␉"d" (0));␊ |
145 | }␊ |
146 | ␊ |
147 | static inline void do_cpuid2(uint32_t selector, uint32_t selector2, uint32_t *data)␊ |
148 | {␊ |
149 | ␉asm volatile (␊ |
150 | ␉␉"cpuid" : "=a" (data[eax]),␊ |
151 | ␉␉"=b" (data[ebx]),␊ |
152 | ␉␉"=c" (data[ecx]),␊ |
153 | ␉␉"=d" (data[edx]) : "a" (selector),␊ |
154 | ␉␉"b" (0),␊ |
155 | ␉␉"c" (selector2),␊ |
156 | ␉␉"d" (0));␊ |
157 | }␊ |
158 | ␊ |
159 | // DFE: enable_PIT2 and disable_PIT2 come from older xnu␊ |
160 | ␊ |
161 | /*␊ |
162 | * Enable or disable timer 2.␊ |
163 | * Port 0x61 controls timer 2:␊ |
164 | * bit 0 gates the clock,␊ |
165 | * bit 1 gates output to speaker.␊ |
166 | */␊ |
167 | static inline void enable_PIT2(void)␊ |
168 | {␊ |
169 | /* Enable gate, disable speaker */␊ |
170 | __asm__ volatile(␊ |
171 | ␉␉␉␉␉ " inb $0x61,%%al \n\t"␊ |
172 | ␉␉␉␉␉ " and $0xFC,%%al \n\t" /* & ~0x03 */␊ |
173 | ␉␉␉␉␉ " or $1,%%al \n\t"␊ |
174 | ␉␉␉␉␉ " outb %%al,$0x61 \n\t"␊ |
175 | ␉␉␉␉␉ : : : "%al" );␊ |
176 | }␊ |
177 | ␊ |
178 | static inline void disable_PIT2(void)␊ |
179 | {␊ |
180 | /* Disable gate and output to speaker */␊ |
181 | __asm__ volatile(␊ |
182 | ␉␉␉␉␉ " inb $0x61,%%al \n\t"␊ |
183 | ␉␉␉␉␉ " and $0xFC,%%al \n\t"␉/* & ~0x03 */␊ |
184 | ␉␉␉␉␉ " outb %%al,$0x61 \n\t"␊ |
185 | ␉␉␉␉␉ : : : "%al" );␊ |
186 | }␊ |
187 | ␊ |
188 | // DFE: set_PIT2_mode0, poll_PIT2_gate, and measure_tsc_frequency are␊ |
189 | // roughly based on Linux code␊ |
190 | ␊ |
191 | /* Set the 8254 channel 2 to mode 0 with the specified value.␊ |
192 | In mode 0, the counter will initially set its gate low when the␊ |
193 | timer expires. For this to be useful, you ought to set it high␊ |
194 | before calling this function. The enable_PIT2 function does this.␊ |
195 | */␊ |
196 | static inline void set_PIT2_mode0(uint16_t value)␊ |
197 | {␊ |
198 | __asm__ volatile(␊ |
199 | ␉␉␉␉␉ " movb $0xB0,%%al \n\t"␊ |
200 | ␉␉␉␉␉ " outb␉%%al,$0x43␉\n\t"␊ |
201 | ␉␉␉␉␉ " movb␉%%dl,%%al␉\n\t"␊ |
202 | ␉␉␉␉␉ " outb␉%%al,$0x42␉\n\t"␊ |
203 | ␉␉␉␉␉ " movb␉%%dh,%%al␉\n\t"␊ |
204 | ␉␉␉␉␉ " outb␉%%al,$0x42"␊ |
205 | ␉␉␉␉␉ : : "d"(value) /*: no clobber */ );␊ |
206 | }␊ |
207 | ␊ |
208 | /* Returns the number of times the loop ran before the PIT2 signaled */␊ |
209 | static inline unsigned long poll_PIT2_gate(void)␊ |
210 | {␊ |
211 | unsigned long count = 0;␊ |
212 | unsigned char nmi_sc_val;␊ |
213 | do {␊ |
214 | ++count;␊ |
215 | __asm__ volatile(␊ |
216 | ␉␉␉␉␉␉ "inb␉$0x61,%0"␊ |
217 | ␉␉␉␉␉␉ : "=a"(nmi_sc_val) /*:*/ /* no input */ /*:*/ /* no clobber */);␊ |
218 | } while( (nmi_sc_val & 0x20) == 0);␊ |
219 | return count;␊ |
220 | }␊ |
221 | ␊ |
222 | inline static void␊ |
223 | set_PIT2(int value)␊ |
224 | {␊ |
225 | /*␊ |
226 | * First, tell the clock we are going to write 16 bits to the counter␊ |
227 | * and enable one-shot mode (command 0xB8 to port 0x43)␊ |
228 | * Then write the two bytes into the PIT2 clock register (port 0x42).␊ |
229 | * Loop until the value is "realized" in the clock,␊ |
230 | * this happens on the next tick.␊ |
231 | */␊ |
232 | asm volatile(␊ |
233 | " movb $0xB8,%%al \n\t"␊ |
234 | " outb %%al,$0x43 \n\t"␊ |
235 | " movb %%dl,%%al \n\t"␊ |
236 | " outb %%al,$0x42 \n\t"␊ |
237 | " movb %%dh,%%al \n\t"␊ |
238 | " outb %%al,$0x42 \n"␊ |
239 | "1: inb $0x42,%%al \n\t" ␊ |
240 | " inb $0x42,%%al \n\t"␊ |
241 | " cmp %%al,%%dh \n\t"␊ |
242 | " jne 1b"␊ |
243 | : : "d"(value) : "%al");␊ |
244 | }␊ |
245 | ␊ |
246 | inline static uint64_t␊ |
247 | get_PIT2(unsigned int *value)␊ |
248 | {␊ |
249 | register uint64_t result;␊ |
250 | /*␊ |
251 | * This routine first latches the time (command 0x80 to port 0x43),␊ |
252 | * then gets the time stamp so we know how long the read will take later.␊ |
253 | * Read (from port 0x42) and return the current value of the timer.␊ |
254 | */␊ |
255 | #ifdef __i386__␊ |
256 | asm volatile(␊ |
257 | " xorl %%ecx,%%ecx \n\t"␊ |
258 | " movb $0x80,%%al \n\t"␊ |
259 | " outb %%al,$0x43 \n\t"␊ |
260 | " rdtsc \n\t"␊ |
261 | " pushl %%eax \n\t"␊ |
262 | " inb $0x42,%%al \n\t"␊ |
263 | " movb %%al,%%cl \n\t"␊ |
264 | " inb $0x42,%%al \n\t"␊ |
265 | " movb %%al,%%ch \n\t"␊ |
266 | " popl %%eax "␊ |
267 | : "=A"(result), "=c"(*value));␊ |
268 | #else /* __x86_64__ */␊ |
269 | asm volatile(␊ |
270 | ␉␉" xorq %%rcx,%%rcx \n\t"␊ |
271 | ␉␉" movb $0x80,%%al \n\t"␊ |
272 | ␉␉" outb %%al,$0x43 \n\t"␊ |
273 | ␉␉" rdtsc \n\t"␊ |
274 | ␉␉" pushq %%rax \n\t"␊ |
275 | ␉␉" inb $0x42,%%al \n\t"␊ |
276 | ␉␉" movb %%al,%%cl \n\t"␊ |
277 | ␉␉" inb $0x42,%%al \n\t"␊ |
278 | ␉␉" movb %%al,%%ch \n\t"␊ |
279 | ␉␉" popq %%rax "␊ |
280 | ␉␉: "=A"(result), "=c"(*value));␊ |
281 | #endif␊ |
282 | ␊ |
283 | return result;␊ |
284 | }␊ |
285 | ␊ |
286 | /*␊ |
287 | * Timing Functions␊ |
288 | */␊ |
289 | ␊ |
290 | /* REP NOP (PAUSE) is a good thing to insert into busy-wait loops. */␊ |
291 | static inline void CpuPause(void)␊ |
292 | {␊ |
293 | ␉__asm__ volatile ("rep; nop");␊ |
294 | }␊ |
295 | ␊ |
296 | static inline uint32_t DivU64x32(uint64_t dividend, uint32_t divisor)␊ |
297 | {␊ |
298 | ␉__asm__ volatile ("divl %1" : "+A"(dividend) : "r"(divisor));␊ |
299 | ␉return (uint32_t) dividend;␊ |
300 | }␊ |
301 | ␊ |
302 | static inline uint64_t MultU32x32(uint32_t multiplicand, uint32_t multiplier)␊ |
303 | {␊ |
304 | ␉uint64_t result;␊ |
305 | ␉__asm__ volatile ("mull %2" : "=A"(result) : "a"(multiplicand), "r"(multiplier));␊ |
306 | ␉return result;␊ |
307 | }␊ |
308 | ␊ |
309 | #endif /* !__LIBSAIO_CPU_H */␊ |
310 | |