summaryrefslogtreecommitdiff
path: root/arch/arch-x86-common.h
blob: 179572d482fa550093084b55b5bd2b3d0f75c291 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef FIO_ARCH_X86_COMMON
#define FIO_ARCH_X86_COMMON

#include <string.h>

static inline void cpuid(unsigned int op,
			 unsigned int *eax, unsigned int *ebx,
			 unsigned int *ecx, unsigned int *edx)
{
	*eax = op;
	*ecx = 0;
	do_cpuid(eax, ebx, ecx, edx);
}

#define ARCH_HAVE_INIT

extern int tsc_reliable;

static inline int arch_init_intel(unsigned int level)
{
	unsigned int eax, ebx, ecx = 0, edx;

	/*
	 * Check for TSC
	 */
	eax = 1;
	do_cpuid(&eax, &ebx, &ecx, &edx);
	if (!(edx & (1U << 4)))
		return 0;

	/*
	 * Check for constant rate and synced (across cores) TSC
	 */
	eax = 0x80000007;
	do_cpuid(&eax, &ebx, &ecx, &edx);
	return edx & (1U << 8);
}

static inline int arch_init_amd(unsigned int level)
{
	unsigned int eax, ebx, ecx, edx;

	cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
	if (eax < 0x80000007)
		return 0;

	cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
	if (edx & (1 << 8))
		return 1;

	return 0;
}

static inline int arch_init(char *envp[])
{
	unsigned int level;
	char str[12];

	cpuid(0, &level, (unsigned int *) &str[0],
			 (unsigned int *) &str[8],
			 (unsigned int *) &str[4]);

	if (!strcmp(str, "GenuineIntel"))
		tsc_reliable = arch_init_intel(level);
	else if (!strcmp(str, "AuthenticAMD"))
		tsc_reliable = arch_init_amd(level);

	return 0;
}

#endif