Chameleon

Chameleon Svn Source Tree

Root/trunk/i386/libsaio/cpu.h

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

Archive Download this file

Revision: 827