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 | #define MSR_IA32_MISC_ENABLE 0x000001A0␊ |
24 | ␊ |
25 | #define DEFAULT_FSB␉␉100000 /* for now, hardcoding 100MHz for old CPUs */␊ |
26 | ␊ |
27 | // DFE: This constant comes from older xnu:␊ |
28 | #define CLKNUM␉␉␉1193182␉␉/* formerly 1193167 */␊ |
29 | ␊ |
30 | // DFE: These two constants come from Linux except CLOCK_TICK_RATE replaced with CLKNUM␊ |
31 | #define CALIBRATE_TIME_MSEC␉30␉␉/* 30 msecs */␊ |
32 | #define CALIBRATE_LATCH␉␉((CLKNUM * CALIBRATE_TIME_MSEC + 1000/2)/1000)␊ |
33 | ␊ |
34 | static inline uint64_t rdtsc64(void)␊ |
35 | {␊ |
36 | ␉uint64_t ret;␊ |
37 | ␉__asm__ volatile("rdtsc" : "=A" (ret));␊ |
38 | ␉return ret;␊ |
39 | }␊ |
40 | ␊ |
41 | static inline uint64_t rdmsr64(uint32_t msr)␊ |
42 | {␊ |
43 | uint64_t ret;␊ |
44 | __asm__ volatile("rdmsr" : "=A" (ret) : "c" (msr));␊ |
45 | return ret;␊ |
46 | }␊ |
47 | ␊ |
48 | static inline void wrmsr64(uint32_t msr, uint64_t val)␊ |
49 | {␊ |
50 | ␉__asm__ volatile("wrmsr" : : "c" (msr), "A" (val));␊ |
51 | }␊ |
52 | ␊ |
53 | static inline void intel_waitforsts(void) {␊ |
54 | ␉uint32_t inline_timeout = 100000;␊ |
55 | ␉while (rdmsr64(MSR_IA32_PERF_STATUS) & (1 << 21)) { if (!inline_timeout--) break; }␊ |
56 | }␊ |
57 | ␊ |
58 | static inline void do_cpuid2(uint32_t selector, uint32_t selector2, uint32_t *data)␊ |
59 | {␊ |
60 | ␉asm volatile ("cpuid"␊ |
61 | ␉␉␉␉ : "=a" (data[0]),␊ |
62 | ␉␉␉␉ "=b" (data[1]),␊ |
63 | ␉␉␉␉ "=c" (data[2]),␊ |
64 | ␉␉␉␉ "=d" (data[3])␊ |
65 | ␉␉␉␉ : "a" (selector), "c" (selector2),␊ |
66 | ␉␉␉␉ "b" (0),␊ |
67 | ␉␉␉␉ "d" (0));␊ |
68 | }␊ |
69 | ␊ |
70 | #endif /* !__LIBSAIO_CPU_H */␊ |
71 | |