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 "libsaio.h"␊ |
10 | ␊ |
11 | extern void scan_cpu(void);␊ |
12 | ␊ |
13 | #define␉MSR_IA32_PERF_STATUS␉0x198␊ |
14 | #define MSR_IA32_PERF_CONTROL␉0x199␊ |
15 | #define MSR_IA32_EXT_CONFIG␉␉0x00EE␊ |
16 | #define MSR_FLEX_RATIO␉␉␉0x194␊ |
17 | #define␉MSR_PLATFORM_INFO␉␉0xCE␊ |
18 | #define MSR_TURBO_RATIO_LIMIT␉0x1AD␊ |
19 | #define MSR_IA32_BIOS_SIGN_ID 0x08B␊ |
20 | #define MSR_CORE_THREAD_COUNT 0x035␊ |
21 | #define K8_FIDVID_STATUS␉␉0xC0010042␊ |
22 | #define K10_COFVID_STATUS␉␉0xC0010071␊ |
23 | ␊ |
24 | #define DEFAULT_FSB␉␉100000 /* for now, hardcoding 100MHz for old CPUs */␊ |
25 | ␊ |
26 | // DFE: This constant comes from older xnu:␊ |
27 | #define CLKNUM␉␉␉1193182␉␉/* formerly 1193167 */␊ |
28 | ␊ |
29 | // DFE: These two constants come from Linux except CLOCK_TICK_RATE replaced with CLKNUM␊ |
30 | #define CALIBRATE_TIME_MSEC␉30␉␉/* 30 msecs */␊ |
31 | #define CALIBRATE_LATCH␉␉((CLKNUM * CALIBRATE_TIME_MSEC + 1000/2)/1000)␊ |
32 | ␊ |
33 | static inline uint64_t rdtsc64(void)␊ |
34 | {␊ |
35 | ␉uint64_t ret;␊ |
36 | ␉__asm__ volatile("rdtsc" : "=A" (ret));␊ |
37 | ␉return ret;␊ |
38 | }␊ |
39 | ␊ |
40 | static inline uint64_t rdmsr64(uint32_t msr)␊ |
41 | {␊ |
42 | uint64_t ret;␊ |
43 | __asm__ volatile("rdmsr" : "=A" (ret) : "c" (msr));␊ |
44 | return ret;␊ |
45 | }␊ |
46 | ␊ |
47 | static inline void wrmsr64(uint32_t msr, uint64_t val)␊ |
48 | {␊ |
49 | ␉__asm__ volatile("wrmsr" : : "c" (msr), "A" (val));␊ |
50 | }␊ |
51 | ␊ |
52 | static inline void intel_waitforsts(void) {␊ |
53 | ␉uint32_t inline_timeout = 100000;␊ |
54 | ␉while (rdmsr64(MSR_IA32_PERF_STATUS) & (1 << 21)) { if (!inline_timeout--) break; }␊ |
55 | }␊ |
56 | ␊ |
57 | static inline void do_cpuid2(uint32_t selector, uint32_t selector2, uint32_t *data)␊ |
58 | {␊ |
59 | ␉asm volatile ("cpuid"␊ |
60 | ␉␉␉␉ : "=a" (data[0]),␊ |
61 | ␉␉␉␉ "=b" (data[1]),␊ |
62 | ␉␉␉␉ "=c" (data[2]),␊ |
63 | ␉␉␉␉ "=d" (data[3])␊ |
64 | ␉␉␉␉ : "a" (selector), "c" (selector2),␊ |
65 | ␉␉␉␉ "b" (0),␊ |
66 | ␉␉␉␉ "d" (0));␊ |
67 | }␊ |
68 | ␊ |
69 | #endif /* !__LIBSAIO_CPU_H */␊ |
70 | |