Patrick Georgi | 11f0079 | 2020-03-04 15:10:45 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Martin Roth | 9df9e939 | 2016-01-12 15:55:28 -0700 | [diff] [blame] | 2 | |
Lee Leahy | 2da9524 | 2015-06-12 17:30:33 -0700 | [diff] [blame] | 3 | #include <cpu/cpu.h> |
Lee Leahy | 2da9524 | 2015-06-12 17:30:33 -0700 | [diff] [blame] | 4 | |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 5 | #ifndef __x86_64__ |
Lee Leahy | 2da9524 | 2015-06-12 17:30:33 -0700 | [diff] [blame] | 6 | /* Standard macro to see if a specific flag is changeable */ |
| 7 | static 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 */ |
| 28 | int cpu_have_cpuid(void) |
| 29 | { |
| 30 | return flag_is_changeable_p(X86_EFLAGS_ID); |
| 31 | } |
| 32 | |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 33 | #else |
| 34 | |
| 35 | int cpu_have_cpuid(void) |
| 36 | { |
| 37 | return 1; |
| 38 | } |
| 39 | #endif |
| 40 | |
Elyes HAOUAS | 1c9bd9c | 2019-06-26 17:49:31 +0200 | [diff] [blame] | 41 | unsigned int cpu_cpuid_extended_level(void) |
Lee Leahy | 2da9524 | 2015-06-12 17:30:33 -0700 | [diff] [blame] | 42 | { |
| 43 | return cpuid_eax(0x80000000); |
| 44 | } |
| 45 | |
| 46 | int 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 Banik | 53b08c3 | 2018-12-10 14:11:35 +0530 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * Get processor id using cpuid eax=1 |
| 61 | * return value in EAX register |
| 62 | */ |
| 63 | uint32_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 | */ |
| 72 | uint32_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 | */ |
| 81 | uint32_t cpu_get_feature_flags_edx(void) |
| 82 | { |
| 83 | return cpuid_edx(1); |
| 84 | } |