blob: 07de1553b89ac4ab0f18da59fcc39a28777d9030 [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Martin Roth9df9e9392016-01-12 15:55:28 -07002
Lee Leahy2da95242015-06-12 17:30:33 -07003#include <cpu/cpu.h>
Lee Leahy2da95242015-06-12 17:30:33 -07004
Stefan Reinauer96938852015-06-18 01:23:48 -07005#ifndef __x86_64__
Lee Leahy2da95242015-06-12 17:30:33 -07006/* Standard macro to see if a specific flag is changeable */
7static inline int flag_is_changeable_p(uint32_t flag)
8{
9 uint32_t f1, f2;
10
11 asm(
12 "pushfl\n\t"
13 "pushfl\n\t"
14 "popl %0\n\t"
15 "movl %0,%1\n\t"
16 "xorl %2,%0\n\t"
17 "pushl %0\n\t"
18 "popfl\n\t"
19 "pushfl\n\t"
20 "popl %0\n\t"
21 "popfl\n\t"
22 : "=&r" (f1), "=&r" (f2)
23 : "ir" (flag));
24 return ((f1^f2) & flag) != 0;
25}
26
27/* Probe for the CPUID instruction */
28int cpu_have_cpuid(void)
29{
30 return flag_is_changeable_p(X86_EFLAGS_ID);
31}
32
Stefan Reinauer96938852015-06-18 01:23:48 -070033#else
34
35int cpu_have_cpuid(void)
36{
37 return 1;
38}
39#endif
40
Elyes HAOUAS1c9bd9c2019-06-26 17:49:31 +020041unsigned int cpu_cpuid_extended_level(void)
Lee Leahy2da95242015-06-12 17:30:33 -070042{
43 return cpuid_eax(0x80000000);
44}
45
46int cpu_phys_address_size(void)
47{
48 if (!(cpu_have_cpuid()))
49 return 32;
50
51 if (cpu_cpuid_extended_level() >= 0x80000008)
52 return cpuid_eax(0x80000008) & 0xff;
53
54 if (cpuid_edx(1) & (CPUID_FEATURE_PAE | CPUID_FEATURE_PSE36))
55 return 36;
56 return 32;
57}
Subrata Banik53b08c32018-12-10 14:11:35 +053058
59/*
60 * Get processor id using cpuid eax=1
61 * return value in EAX register
62 */
63uint32_t cpu_get_cpuid(void)
64{
65 return cpuid_eax(1);
66}
67
68/*
69 * Get processor feature flag using cpuid eax=1
70 * return value in ECX register
71 */
72uint32_t cpu_get_feature_flags_ecx(void)
73{
74 return cpuid_ecx(1);
75}
76
77/*
78 * Get processor feature flag using cpuid eax=1
79 * return value in EDX register
80 */
81uint32_t cpu_get_feature_flags_edx(void)
82{
83 return cpuid_edx(1);
84}