1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <commonlib/helpers.h>
8 /* Standard macro to see if a specific flag is changeable */
9 static inline int flag_is_changeable_p(uint32_t flag
)
24 : "=&r" (f1
), "=&r" (f2
)
26 return ((f1
^f2
) & flag
) != 0;
29 /* Probe for the CPUID instruction */
30 int cpu_have_cpuid(void)
32 return flag_is_changeable_p(X86_EFLAGS_ID
);
37 int cpu_have_cpuid(void)
43 unsigned int cpu_cpuid_extended_level(void)
45 return cpuid_eax(0x80000000);
48 unsigned int cpu_phys_address_size(void)
50 if (!(cpu_have_cpuid()))
53 if (cpu_cpuid_extended_level() >= 0x80000008) {
54 int size
= cpuid_eax(0x80000008) & 0xff;
55 size
-= get_reserved_phys_addr_bits();
59 if (cpuid_edx(1) & (CPUID_FEATURE_PAE
| CPUID_FEATURE_PSE36
))
64 unsigned int soc_phys_address_size(void)
66 if (CONFIG_SOC_PHYSICAL_ADDRESS_WIDTH
)
67 return CONFIG_SOC_PHYSICAL_ADDRESS_WIDTH
;
69 return cpu_phys_address_size();
73 * Get processor id using cpuid eax=1
74 * return value in EAX register
76 uint32_t cpu_get_cpuid(void)
82 * Get processor feature flag using cpuid eax=1
83 * return value in ECX register
85 uint32_t cpu_get_feature_flags_ecx(void)
91 * Get processor feature flag using cpuid eax=1
92 * return value in EDX register
94 uint32_t cpu_get_feature_flags_edx(void)
99 enum cpu_type
cpu_check_deterministic_cache_cpuid_supported(void)
101 if (cpu_is_intel()) {
102 if (cpuid_get_max_func() < 4)
103 return CPUID_COMMAND_UNSUPPORTED
;
104 return CPUID_TYPE_INTEL
;
105 } else if (cpu_is_amd()) {
106 if (cpu_cpuid_extended_level() < 0x80000001)
107 return CPUID_COMMAND_UNSUPPORTED
;
109 if (!(cpuid_ecx(0x80000001) & (1 << 22)))
110 return CPUID_COMMAND_UNSUPPORTED
;
112 return CPUID_TYPE_AMD
;
114 return CPUID_TYPE_INVALID
;
118 static uint32_t cpu_get_cache_info_leaf(void)
120 uint32_t leaf
= (cpu_check_deterministic_cache_cpuid_supported() == CPUID_TYPE_AMD
) ?
121 DETERMINISTIC_CACHE_PARAMETERS_CPUID_AMD
:
122 DETERMINISTIC_CACHE_PARAMETERS_CPUID_IA
;
127 size_t cpu_get_cache_ways_assoc_info(const struct cpu_cache_info
*info
)
132 return info
->num_ways
;
135 uint8_t cpu_get_cache_type(const struct cpu_cache_info
*info
)
143 uint8_t cpu_get_cache_level(const struct cpu_cache_info
*info
)
151 size_t cpu_get_cache_phy_partition_info(const struct cpu_cache_info
*info
)
156 return info
->physical_partitions
;
159 size_t cpu_get_cache_line_size(const struct cpu_cache_info
*info
)
164 return info
->line_size
;
167 size_t cpu_get_cache_sets(const struct cpu_cache_info
*info
)
172 return info
->num_sets
;
175 bool cpu_is_cache_full_assoc(const struct cpu_cache_info
*info
)
180 return info
->fully_associative
;
183 size_t cpu_get_max_cache_share(const struct cpu_cache_info
*info
)
188 return info
->num_cores_shared
;
191 size_t get_cache_size(const struct cpu_cache_info
*info
)
196 return info
->num_ways
* info
->physical_partitions
* info
->line_size
* info
->num_sets
;
200 * Returns the sub-states supported by the specified CPU
203 * Level 0 corresponds to the lowest C-state (C0).
204 * Higher levels are processor specific.
206 uint8_t cpu_get_c_substate_support(const int state
)
208 if ((cpuid_get_max_func() < 5) ||
209 !(cpuid_ecx(5) & CPUID_FEATURE_MONITOR_MWAIT
) || (state
> 4))
212 return (cpuid_edx(5) >> (state
* 4)) & 0xf;
215 bool fill_cpu_cache_info(uint8_t level
, struct cpu_cache_info
*info
)
220 uint32_t leaf
= cpu_get_cache_info_leaf();
224 struct cpuid_result cache_info_res
= cpuid_ext(leaf
, level
);
226 info
->type
= CPUID_CACHE_TYPE(cache_info_res
);
227 info
->level
= CPUID_CACHE_LEVEL(cache_info_res
);
228 info
->num_ways
= CPUID_CACHE_WAYS_OF_ASSOC(cache_info_res
) + 1;
229 info
->num_sets
= CPUID_CACHE_NO_OF_SETS(cache_info_res
) + 1;
230 info
->line_size
= CPUID_CACHE_COHER_LINE(cache_info_res
) + 1;
231 info
->physical_partitions
= CPUID_CACHE_PHYS_LINE(cache_info_res
) + 1;
232 info
->num_cores_shared
= CPUID_CACHE_SHARING_CACHE(cache_info_res
) + 1;
233 info
->fully_associative
= CPUID_CACHE_FULL_ASSOC(cache_info_res
);
234 info
->size
= get_cache_size(info
);
239 bool is_cache_sets_power_of_two(void)
241 struct cpu_cache_info info
;
243 if (!fill_cpu_cache_info(CACHE_L3
, &info
))
246 size_t cache_sets
= cpu_get_cache_sets(&info
);
248 return IS_POWER_OF_2(cache_sets
);