blob: 31aa79f1b573841f68575f49788a0cbbee9d1f0c [file] [log] [blame]
Jens Axboefa80fea2012-12-09 20:29:00 +01001#ifndef FIO_ARCH_X86_COMMON
2#define FIO_ARCH_X86_COMMON
3
Jens Axboe7189c962013-02-03 20:54:26 +01004#include <string.h>
5
Jens Axboe7189c962013-02-03 20:54:26 +01006static inline void cpuid(unsigned int op,
7 unsigned int *eax, unsigned int *ebx,
8 unsigned int *ecx, unsigned int *edx)
9{
10 *eax = op;
11 *ecx = 0;
12 do_cpuid(eax, ebx, ecx, edx);
13}
14
Jens Axboefa80fea2012-12-09 20:29:00 +010015#define ARCH_HAVE_INIT
Jens Axboe7189c962013-02-03 20:54:26 +010016
Jens Axboefa80fea2012-12-09 20:29:00 +010017extern int tsc_reliable;
Jens Axboe7189c962013-02-03 20:54:26 +010018
19static inline int arch_init_intel(unsigned int level)
Jens Axboefa80fea2012-12-09 20:29:00 +010020{
Jens Axboe267339f2012-12-18 19:54:40 +010021 unsigned int eax, ebx, ecx = 0, edx;
Jens Axboefa80fea2012-12-09 20:29:00 +010022
23 /*
24 * Check for TSC
25 */
26 eax = 1;
27 do_cpuid(&eax, &ebx, &ecx, &edx);
28 if (!(edx & (1U << 4)))
29 return 0;
30
31 /*
32 * Check for constant rate and synced (across cores) TSC
33 */
34 eax = 0x80000007;
35 do_cpuid(&eax, &ebx, &ecx, &edx);
Jens Axboe7189c962013-02-03 20:54:26 +010036 return edx & (1U << 8);
37}
38
39static inline int arch_init_amd(unsigned int level)
40{
41 unsigned int eax, ebx, ecx, edx;
42
43 cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
44 if (eax < 0x80000007)
45 return 0;
46
47 cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
48 if (edx & (1 << 8))
49 return 1;
50
51 return 0;
52}
53
54static inline int arch_init(char *envp[])
55{
56 unsigned int level;
Jens Axboe81fa6e02014-02-14 08:48:22 -070057 char str[13];
Jens Axboe7189c962013-02-03 20:54:26 +010058
59 cpuid(0, &level, (unsigned int *) &str[0],
60 (unsigned int *) &str[8],
61 (unsigned int *) &str[4]);
62
Jens Axboe81fa6e02014-02-14 08:48:22 -070063 str[12] = '\0';
Jens Axboe7189c962013-02-03 20:54:26 +010064 if (!strcmp(str, "GenuineIntel"))
65 tsc_reliable = arch_init_intel(level);
66 else if (!strcmp(str, "AuthenticAMD"))
67 tsc_reliable = arch_init_amd(level);
68
Jens Axboefa80fea2012-12-09 20:29:00 +010069 return 0;
70}
71
72#endif