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> |
Elyes Haouas | ae1ca82 | 2022-10-07 10:02:38 +0200 | [diff] [blame] | 4 | #include <types.h> |
Lee Leahy | 2da9524 | 2015-06-12 17:30:33 -0700 | [diff] [blame] | 5 | |
Patrick Rudolph | adcf782 | 2020-08-27 20:50:18 +0200 | [diff] [blame] | 6 | #if ENV_X86_32 |
Lee Leahy | 2da9524 | 2015-06-12 17:30:33 -0700 | [diff] [blame] | 7 | /* Standard macro to see if a specific flag is changeable */ |
| 8 | static inline int flag_is_changeable_p(uint32_t flag) |
| 9 | { |
| 10 | uint32_t f1, f2; |
| 11 | |
| 12 | asm( |
| 13 | "pushfl\n\t" |
| 14 | "pushfl\n\t" |
| 15 | "popl %0\n\t" |
| 16 | "movl %0,%1\n\t" |
| 17 | "xorl %2,%0\n\t" |
| 18 | "pushl %0\n\t" |
| 19 | "popfl\n\t" |
| 20 | "pushfl\n\t" |
| 21 | "popl %0\n\t" |
| 22 | "popfl\n\t" |
| 23 | : "=&r" (f1), "=&r" (f2) |
| 24 | : "ir" (flag)); |
| 25 | return ((f1^f2) & flag) != 0; |
| 26 | } |
| 27 | |
| 28 | /* Probe for the CPUID instruction */ |
| 29 | int cpu_have_cpuid(void) |
| 30 | { |
| 31 | return flag_is_changeable_p(X86_EFLAGS_ID); |
| 32 | } |
| 33 | |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 34 | #else |
| 35 | |
| 36 | int cpu_have_cpuid(void) |
| 37 | { |
| 38 | return 1; |
| 39 | } |
| 40 | #endif |
| 41 | |
Elyes HAOUAS | 1c9bd9c | 2019-06-26 17:49:31 +0200 | [diff] [blame] | 42 | unsigned int cpu_cpuid_extended_level(void) |
Lee Leahy | 2da9524 | 2015-06-12 17:30:33 -0700 | [diff] [blame] | 43 | { |
| 44 | return cpuid_eax(0x80000000); |
| 45 | } |
| 46 | |
Felix Held | ff4d6be | 2023-09-12 14:18:49 +0200 | [diff] [blame^] | 47 | unsigned int cpu_phys_address_size(void) |
Lee Leahy | 2da9524 | 2015-06-12 17:30:33 -0700 | [diff] [blame] | 48 | { |
| 49 | if (!(cpu_have_cpuid())) |
| 50 | return 32; |
| 51 | |
Jeremy Compostella | 1eff77b | 2023-09-07 10:33:30 -0700 | [diff] [blame] | 52 | if (cpu_cpuid_extended_level() >= 0x80000008) { |
| 53 | int size = cpuid_eax(0x80000008) & 0xff; |
| 54 | size -= get_reserved_phys_addr_bits(); |
| 55 | return size; |
| 56 | } |
Lee Leahy | 2da9524 | 2015-06-12 17:30:33 -0700 | [diff] [blame] | 57 | |
| 58 | if (cpuid_edx(1) & (CPUID_FEATURE_PAE | CPUID_FEATURE_PSE36)) |
| 59 | return 36; |
| 60 | return 32; |
| 61 | } |
Subrata Banik | 53b08c3 | 2018-12-10 14:11:35 +0530 | [diff] [blame] | 62 | |
| 63 | /* |
| 64 | * Get processor id using cpuid eax=1 |
| 65 | * return value in EAX register |
| 66 | */ |
| 67 | uint32_t cpu_get_cpuid(void) |
| 68 | { |
| 69 | return cpuid_eax(1); |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * Get processor feature flag using cpuid eax=1 |
| 74 | * return value in ECX register |
| 75 | */ |
| 76 | uint32_t cpu_get_feature_flags_ecx(void) |
| 77 | { |
| 78 | return cpuid_ecx(1); |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Get processor feature flag using cpuid eax=1 |
| 83 | * return value in EDX register |
| 84 | */ |
| 85 | uint32_t cpu_get_feature_flags_edx(void) |
| 86 | { |
| 87 | return cpuid_edx(1); |
| 88 | } |
Subrata Banik | 0c6c0da | 2021-07-23 21:00:58 +0530 | [diff] [blame] | 89 | |
| 90 | enum cpu_type cpu_check_deterministic_cache_cpuid_supported(void) |
| 91 | { |
Subrata Banik | 0c6c0da | 2021-07-23 21:00:58 +0530 | [diff] [blame] | 92 | if (cpu_is_intel()) { |
Felix Held | 3581a68 | 2023-09-23 03:44:55 +0200 | [diff] [blame] | 93 | if (cpuid_get_max_func() < 4) |
Subrata Banik | 0c6c0da | 2021-07-23 21:00:58 +0530 | [diff] [blame] | 94 | return CPUID_COMMAND_UNSUPPORTED; |
| 95 | return CPUID_TYPE_INTEL; |
| 96 | } else if (cpu_is_amd()) { |
Felix Held | 9acae39 | 2023-09-21 20:33:10 +0200 | [diff] [blame] | 97 | if (cpu_cpuid_extended_level() < 0x80000001) |
Subrata Banik | 0c6c0da | 2021-07-23 21:00:58 +0530 | [diff] [blame] | 98 | return CPUID_COMMAND_UNSUPPORTED; |
| 99 | |
Felix Held | 1d466f2 | 2023-09-21 20:34:43 +0200 | [diff] [blame] | 100 | if (!(cpuid_ecx(0x80000001) & (1 << 22))) |
Subrata Banik | 0c6c0da | 2021-07-23 21:00:58 +0530 | [diff] [blame] | 101 | return CPUID_COMMAND_UNSUPPORTED; |
| 102 | |
| 103 | return CPUID_TYPE_AMD; |
| 104 | } else { |
| 105 | return CPUID_TYPE_INVALID; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static uint32_t cpu_get_cache_info_leaf(void) |
| 110 | { |
Subrata Banik | 72f1e62 | 2021-09-01 12:35:15 +0530 | [diff] [blame] | 111 | uint32_t leaf = (cpu_check_deterministic_cache_cpuid_supported() == CPUID_TYPE_AMD) ? |
| 112 | DETERMINISTIC_CACHE_PARAMETERS_CPUID_AMD : |
| 113 | DETERMINISTIC_CACHE_PARAMETERS_CPUID_IA; |
| 114 | |
| 115 | return leaf; |
Subrata Banik | 0c6c0da | 2021-07-23 21:00:58 +0530 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | size_t cpu_get_cache_ways_assoc_info(const struct cpu_cache_info *info) |
| 119 | { |
| 120 | if (!info) |
| 121 | return 0; |
| 122 | |
| 123 | return info->num_ways; |
| 124 | } |
| 125 | |
| 126 | uint8_t cpu_get_cache_type(const struct cpu_cache_info *info) |
| 127 | { |
| 128 | if (!info) |
| 129 | return 0; |
| 130 | |
| 131 | return info->type; |
| 132 | } |
| 133 | |
| 134 | uint8_t cpu_get_cache_level(const struct cpu_cache_info *info) |
| 135 | { |
| 136 | if (!info) |
| 137 | return 0; |
| 138 | |
| 139 | return info->level; |
| 140 | } |
| 141 | |
| 142 | size_t cpu_get_cache_phy_partition_info(const struct cpu_cache_info *info) |
| 143 | { |
| 144 | if (!info) |
| 145 | return 0; |
| 146 | |
| 147 | return info->physical_partitions; |
| 148 | } |
| 149 | |
| 150 | size_t cpu_get_cache_line_size(const struct cpu_cache_info *info) |
| 151 | { |
| 152 | if (!info) |
| 153 | return 0; |
| 154 | |
| 155 | return info->line_size; |
| 156 | } |
| 157 | |
| 158 | size_t cpu_get_cache_sets(const struct cpu_cache_info *info) |
| 159 | { |
| 160 | if (!info) |
| 161 | return 0; |
| 162 | |
| 163 | return info->num_sets; |
| 164 | } |
| 165 | |
| 166 | bool cpu_is_cache_full_assoc(const struct cpu_cache_info *info) |
| 167 | { |
| 168 | if (!info) |
| 169 | return false; |
| 170 | |
| 171 | return info->fully_associative; |
| 172 | } |
| 173 | |
| 174 | size_t cpu_get_max_cache_share(const struct cpu_cache_info *info) |
| 175 | { |
| 176 | if (!info) |
| 177 | return 0; |
| 178 | |
| 179 | return info->num_cores_shared; |
| 180 | } |
| 181 | |
| 182 | size_t get_cache_size(const struct cpu_cache_info *info) |
| 183 | { |
| 184 | if (!info) |
| 185 | return 0; |
| 186 | |
| 187 | return info->num_ways * info->physical_partitions * info->line_size * info->num_sets; |
| 188 | } |
| 189 | |
| 190 | bool fill_cpu_cache_info(uint8_t level, struct cpu_cache_info *info) |
| 191 | { |
| 192 | if (!info) |
| 193 | return false; |
| 194 | |
| 195 | uint32_t leaf = cpu_get_cache_info_leaf(); |
| 196 | if (!leaf) |
| 197 | return false; |
| 198 | |
| 199 | struct cpuid_result cache_info_res = cpuid_ext(leaf, level); |
| 200 | |
| 201 | info->type = CPUID_CACHE_TYPE(cache_info_res); |
| 202 | info->level = CPUID_CACHE_LEVEL(cache_info_res); |
| 203 | info->num_ways = CPUID_CACHE_WAYS_OF_ASSOC(cache_info_res) + 1; |
| 204 | info->num_sets = CPUID_CACHE_NO_OF_SETS(cache_info_res) + 1; |
| 205 | info->line_size = CPUID_CACHE_COHER_LINE(cache_info_res) + 1; |
| 206 | info->physical_partitions = CPUID_CACHE_PHYS_LINE(cache_info_res) + 1; |
| 207 | info->num_cores_shared = CPUID_CACHE_SHARING_CACHE(cache_info_res) + 1; |
| 208 | info->fully_associative = CPUID_CACHE_FULL_ASSOC(cache_info_res); |
| 209 | info->size = get_cache_size(info); |
| 210 | |
| 211 | return true; |
| 212 | } |