Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 323