Chameleon

Chameleon Svn Source Tree

Root/trunk/i386/libsaio/freq_detect.h

1/*
2 * Copyright 2008 Islam Ahmed Zaid. All rights reserved. <azismed@gmail.com>
3 */
4
5#ifndef __LIBSAIO_FREQ_DETECT_H
6#define __LIBSAIO_FREQ_DETECT_H
7
8#include "libsaio.h"
9#ifndef DEBUG_FREQ
10#define DEBUG_FREQ 0
11#endif
12
13#if DEBUG_FREQ
14#define DBG(x...)printf(x)
15#else
16#define DBG(x...)
17#endif
18
19/* Decimal powers: */
20#define kilo (1000ULL)
21#define Mega (kilo * kilo)
22#define Giga (kilo * Mega)
23#define Tera (kilo * Giga)
24#define Peta (kilo * Tera)
25
26#define bit(n)(1ULL << (n))
27#define bitmask(h,l)((bit(h)|(bit(h)-1)) & ~(bit(l)-1))
28#define bitfield(x,h,l)(((x) & bitmask(h,l)) >> l)
29
30#defineIA32_PERF_STATUS0x198
31#define MSR_FLEX_RATIO0x194
32#defineMSR_PLATFORM_INFO0xCE
33#define K8_FIDVID_STATUS0xC0010042
34#define K10_COFVID_STATUS0xC0010071
35
36#define DEFAULT_FSB100000 /* for now, hardcoding 100MHz for old CPUs */
37
38// DFE: This constant comes from older xnu:
39#define CLKNUM1193182/* formerly 1193167 */
40
41// DFE: These two constants come from Linux except CLOCK_TICK_RATE replaced with CLKNUM
42#define CALIBRATE_TIME_MSEC 30 /* 30 msecs */
43#define CALIBRATE_LATCH\
44((CLKNUM * CALIBRATE_TIME_MSEC + 1000/2)/1000)
45
46extern uint64_t tscFrequency;
47extern uint64_t fsbFrequency;
48extern uint64_t cpuFrequency;
49
50void calculate_freq(void);
51
52static inline uint64_t rdtsc64(void)
53{
54uint64_t ret;
55__asm__ volatile("rdtsc" : "=A" (ret));
56return ret;
57}
58
59static inline uint64_t rdmsr64(uint32_t msr)
60{
61 uint64_t ret;
62 __asm__ volatile("rdmsr" : "=A" (ret) : "c" (msr));
63 return ret;
64}
65
66static inline void do_cpuid(uint32_t selector, uint32_t *data)
67{
68asm volatile ("cpuid"
69 : "=a" (data[0]),
70 "=b" (data[1]),
71 "=c" (data[2]),
72 "=d" (data[3])
73 : "a" (selector)
74 );
75}
76
77#endif /* !__LIBSAIO_FREQ_DETECT_H */
78

Archive Download this file

Revision: 1