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(PlatformInfo_t *);␊ |
12 | ␊ |
13 | #define bit(n)␉␉␉(1UL << (n))␊ |
14 | #define bitmask(h,l)␉␉((bit(h)|(bit(h)-1)) & ~(bit(l)-1))␊ |
15 | #define bitfield(x,h,l)␉␉(((x) & bitmask(h,l)) >> l)␊ |
16 | ␊ |
17 | #define CPU_STRING_UNKNOWN␉␉"Unknown CPU Typ"␊ |
18 | ␊ |
19 | #define␉MSR_IA32_PERF_STATUS␉0x198␊ |
20 | #define MSR_IA32_PERF_CONTROL␉0x199␊ |
21 | #define MSR_IA32_EXT_CONFIG␉␉0x00EE␊ |
22 | #define MSR_FLEX_RATIO␉␉␉0x194␊ |
23 | #define MSR_TURBO_RATIO_LIMIT␉0x1AD␊ |
24 | #define␉MSR_PLATFORM_INFO␉␉0xCE␊ |
25 | #define MSR_CORE_THREAD_COUNT␉0x35␉␉␉// Undocumented␊ |
26 | #define MSR_IA32_PLATFORM_ID␉0x17␊ |
27 | ␊ |
28 | #define K8_FIDVID_STATUS␉␉0xC0010042␊ |
29 | #define K10_COFVID_STATUS␉␉0xC0010071␊ |
30 | ␊ |
31 | #define DEFAULT_FSB␉␉100000 /* for now, hardcoding 100MHz for old CPUs */␊ |
32 | ␊ |
33 | // DFE: This constant comes from older xnu:␊ |
34 | #define CLKNUM␉␉␉1193182␉␉/* formerly 1193167 */␊ |
35 | ␊ |
36 | // DFE: These two constants come from Linux except CLOCK_TICK_RATE replaced with CLKNUM␊ |
37 | #define CALIBRATE_TIME_MSEC␉30␉␉/* 30 msecs */␊ |
38 | #define CALIBRATE_LATCH␉␉((CLKNUM * CALIBRATE_TIME_MSEC + 1000/2)/1000)␊ |
39 | ␊ |
40 | static inline uint64_t rdtsc64(void)␊ |
41 | {␊ |
42 | ␉uint64_t ret;␊ |
43 | ␉__asm__ volatile("rdtsc" : "=A" (ret));␊ |
44 | ␉return ret;␊ |
45 | }␊ |
46 | ␊ |
47 | static inline uint64_t rdmsr64(uint32_t msr)␊ |
48 | {␊ |
49 | uint64_t ret;␊ |
50 | __asm__ volatile("rdmsr" : "=A" (ret) : "c" (msr));␊ |
51 | return ret;␊ |
52 | }␊ |
53 | ␊ |
54 | static inline void wrmsr64(uint32_t msr, uint64_t val)␊ |
55 | {␊ |
56 | ␉__asm__ volatile("wrmsr" : : "c" (msr), "A" (val));␊ |
57 | }␊ |
58 | ␊ |
59 | static inline void intel_waitforsts(void) {␊ |
60 | ␉uint32_t inline_timeout = 100000;␊ |
61 | ␉while (rdmsr64(MSR_IA32_PERF_STATUS) & (1 << 21)) { if (!inline_timeout--) break; }␊ |
62 | }␊ |
63 | ␊ |
64 | static inline void do_cpuid(uint32_t selector, uint32_t *data)␊ |
65 | {␊ |
66 | ␉asm volatile ("cpuid"␊ |
67 | ␉␉␉␉ : "=a" (data[0]),␊ |
68 | ␉␉␉␉ "=b" (data[1]),␊ |
69 | ␉␉␉␉ "=c" (data[2]),␊ |
70 | ␉␉␉␉ "=d" (data[3])␊ |
71 | ␉␉␉␉ : "a" (selector));␊ |
72 | }␊ |
73 | ␊ |
74 | static inline void do_cpuid2(uint32_t selector, uint32_t selector2, uint32_t *data)␊ |
75 | {␊ |
76 | ␉asm volatile ("cpuid"␊ |
77 | ␉␉␉␉ : "=a" (data[0]),␊ |
78 | ␉␉␉␉ "=b" (data[1]),␊ |
79 | ␉␉␉␉ "=c" (data[2]),␊ |
80 | ␉␉␉␉ "=d" (data[3])␊ |
81 | ␉␉␉␉ : "a" (selector), "c" (selector2));␊ |
82 | }␊ |
83 | ␊ |
84 | // DFE: enable_PIT2 and disable_PIT2 come from older xnu␊ |
85 | ␊ |
86 | /*␊ |
87 | * Enable or disable timer 2.␊ |
88 | * Port 0x61 controls timer 2:␊ |
89 | * bit 0 gates the clock,␊ |
90 | * bit 1 gates output to speaker.␊ |
91 | */␊ |
92 | static inline void enable_PIT2(void)␊ |
93 | {␊ |
94 | /* Enable gate, disable speaker */␊ |
95 | __asm__ volatile(␊ |
96 | ␉␉␉␉␉ " inb $0x61,%%al \n\t"␊ |
97 | ␉␉␉␉␉ " and $0xFC,%%al \n\t" /* & ~0x03 */␊ |
98 | ␉␉␉␉␉ " or $1,%%al \n\t"␊ |
99 | ␉␉␉␉␉ " outb %%al,$0x61 \n\t"␊ |
100 | ␉␉␉␉␉ : : : "%al" );␊ |
101 | }␊ |
102 | ␊ |
103 | static inline void disable_PIT2(void)␊ |
104 | {␊ |
105 | /* Disable gate and output to speaker */␊ |
106 | __asm__ volatile(␊ |
107 | ␉␉␉␉␉ " inb $0x61,%%al \n\t"␊ |
108 | ␉␉␉␉␉ " and $0xFC,%%al \n\t"␉/* & ~0x03 */␊ |
109 | ␉␉␉␉␉ " outb %%al,$0x61 \n\t"␊ |
110 | ␉␉␉␉␉ : : : "%al" );␊ |
111 | }␊ |
112 | ␊ |
113 | // DFE: set_PIT2_mode0, poll_PIT2_gate, and measure_tsc_frequency are␊ |
114 | // roughly based on Linux code␊ |
115 | ␊ |
116 | /* Set the 8254 channel 2 to mode 0 with the specified value.␊ |
117 | In mode 0, the counter will initially set its gate low when the␊ |
118 | timer expires. For this to be useful, you ought to set it high␊ |
119 | before calling this function. The enable_PIT2 function does this.␊ |
120 | */␊ |
121 | static inline void set_PIT2_mode0(uint16_t value)␊ |
122 | {␊ |
123 | __asm__ volatile(␊ |
124 | ␉␉␉␉␉ " movb $0xB0,%%al \n\t"␊ |
125 | ␉␉␉␉␉ " outb␉%%al,$0x43␉\n\t"␊ |
126 | ␉␉␉␉␉ " movb␉%%dl,%%al␉\n\t"␊ |
127 | ␉␉␉␉␉ " outb␉%%al,$0x42␉\n\t"␊ |
128 | ␉␉␉␉␉ " movb␉%%dh,%%al␉\n\t"␊ |
129 | ␉␉␉␉␉ " outb␉%%al,$0x42"␊ |
130 | ␉␉␉␉␉ : : "d"(value) /*: no clobber */ );␊ |
131 | }␊ |
132 | ␊ |
133 | /* Returns the number of times the loop ran before the PIT2 signaled */␊ |
134 | static inline unsigned long poll_PIT2_gate(void)␊ |
135 | {␊ |
136 | unsigned long count = 0;␊ |
137 | unsigned char nmi_sc_val;␊ |
138 | do {␊ |
139 | ++count;␊ |
140 | __asm__ volatile(␊ |
141 | ␉␉␉␉␉␉ "inb␉$0x61,%0"␊ |
142 | ␉␉␉␉␉␉ : "=q"(nmi_sc_val) /*:*/ /* no input */ /*:*/ /* no clobber */);␊ |
143 | } while( (nmi_sc_val & 0x20) == 0);␊ |
144 | return count;␊ |
145 | }␊ |
146 | ␊ |
147 | #endif /* !__LIBSAIO_CPU_H */␊ |
148 | |