Debian Codegraph @

src/soc/amd/common/block/cpu/noncar/cpu.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <amdblocks/cpu.h>
4 #include <amdblocks/mca.h>
5 #include <arch/cpuid.h>
6 #include <cpu/cpu.h>
7 #include <cpu/x86/msr.h>
8 #include <cpu/amd/cpuid.h>
9 #include <cpu/amd/microcode.h>
10 #include <cpu/amd/msr.h>
11 #include <cpu/amd/mtrr.h>
12 #include <lib.h>
13 #include <smbios.h>
14 #include <soc/iomap.h>
15 #include <soc/msr.h>
16 #include <types.h>
17
18 #define UNSUPPORTED 0
19
20 #define UNINITIALIZED ((uint64_t)-1)
21
22 struct core_info {
23 /* Core max boost frequency */
24 uint32_t max_frequency;
25
26 /* L3 Cache block unique ID & size (in bytes) */
27 uint16_t l3_cache_uid;
28 size_t l3_cache_size;
29 };
30
31 static struct core_info core_info_list[CONFIG_MAX_CPUS];
32
33 static union pstate_msr get_pstate0_msr(void)
34 {
35 static union pstate_msr pstate_reg = { .raw = UNINITIALIZED };
36
37 struct cpuid_result res;
38
39 /* Check if we have already determined P-state support */
40 if (pstate_reg.raw == UNINITIALIZED) {
41 if (cpu_cpuid_extended_level() < CPUID_EXT_PM) {
42 pstate_reg.pstate_en = UNSUPPORTED;
43 return pstate_reg;
44 }
45
46 res = cpuid(CPUID_EXT_PM);
47
48 if (!(res.edx & BIT(7))) { /* Hardware P-state control */
49 pstate_reg.pstate_en = UNSUPPORTED;
50 return pstate_reg;
51 }
52
53 pstate_reg.raw = rdmsr(PSTATE_MSR(get_pstate_0_reg())).raw;
54 }
55
56 return pstate_reg;
57 }
58
59 unsigned int smbios_cpu_get_current_speed_mhz(void)
60 {
61 union pstate_msr pstate_reg = get_pstate0_msr();
62
63 if (!pstate_reg.pstate_en)
64 return 0;
65
66 return get_pstate_core_freq(pstate_reg);
67 }
68
69 unsigned int smbios_cpu_get_voltage(void)
70 {
71 union pstate_msr pstate_reg = get_pstate0_msr();
72
73 if (!pstate_reg.pstate_en)
74 return 0;
75
76 return get_pstate_core_uvolts(pstate_reg) / 100000; /* uV to (10 * V) */
77 }
78
79 uint32_t get_pstate_0_reg(void)
80 {
81 return 0;
82 }
83
84 uint32_t get_pstate_latency(void)
85 {
86 return 0;
87 }
88
89 unsigned int smbios_processor_family(struct cpuid_result res)
90 {
91 return 0x6b; /* Zen */
92 }
93
94 unsigned int smbios_processor_external_clock(void)
95 {
96 return 100; /* 100 MHz */
97 }
98
99 void set_cstate_io_addr(void)
100 {
101 msr_t cst_addr;
102
103 cst_addr.hi = 0;
104 cst_addr.lo = ACPI_CSTATE_CONTROL;
105 wrmsr(MSR_CSTATE_ADDRESS, cst_addr);
106 }
107
108 /* Number of most significant physical address bits reserved for secure memory encryption */
109 unsigned int get_reserved_phys_addr_bits(void)
110 {
111 if (!(rdmsr(SYSCFG_MSR).raw & SYSCFG_MSR_SMEE))
112 return 0;
113
114 return (cpuid_ebx(CPUID_EBX_MEM_ENCRYPT) & CPUID_EBX_MEM_ENCRYPT_ADDR_BITS_MASK) >>
115 CPUID_EBX_MEM_ENCRYPT_ADDR_BITS_SHIFT;
116 }
117
118 unsigned int smbios_cpu_get_max_speed_mhz(void)
119 {
120 uint32_t max_freq = 0;
121
122 for (int i = 0; i < get_cpu_count(); i++) {
123 struct core_info *core_info = &core_info_list[i];
124
125 if (max_freq < core_info->max_frequency)
126 max_freq = core_info->max_frequency;
127 }
128 return max_freq;
129 }
130
131 /*
132 * On some SoCs like AMD Glinda, multiple unique L3 cache blocks exist,
133 * each with a distinct UID and possibly different sizes. The default
134 * method assumes a single shared L3 cache across all cores, which is
135 * inaccurate. This function aggregates sizes of all uniquely identified
136 * L3 cache blocks to compute the correct total L3 size.
137 */
138 bool soc_fill_cpu_cache_info(uint8_t level, struct cpu_cache_info *info)
139 {
140 if (level != CACHE_L3)
141 return x86_get_cpu_cache_info(level, info);
142
143 if (!info)
144 return false;
145
146 uint32_t total_cache = 0;
147 bool seen_cache_ids[CONFIG_MAX_CPUS] = {false};
148
149 x86_get_cpu_cache_info(level, info);
150
151 /*
152 * To calculate the total L3 cache size, iterate over core_info_list and add up the sizes
153 * of the L3 cache blocks with unique cache ID.
154 */
155 for (int i = 0; i < get_cpu_count(); i++) {
156 struct core_info *cache = &core_info_list[i];
157
158 printk(BIOS_SPEW, "CPU %d: Cache Level: %d, Cache Size: %zu bytes\n",
159 i, cache->l3_cache_uid, cache->l3_cache_size);
160
161 if (!seen_cache_ids[cache->l3_cache_uid]) {
162 total_cache += cache->l3_cache_size;
163 seen_cache_ids[cache->l3_cache_uid] = true;
164 }
165 }
166
167 info->num_cores_shared = get_cpu_count();
168 info->size = total_cache;
169
170 printk(BIOS_SPEW, "Total cache at level: %d is:%zu\n", level, info->size);
171 return true;
172 }
173
174 static uint32_t get_max_boost_frequency(void)
175 {
176 msr_t msr = rdmsr(MSR_CPPC_CAPABILITY_1);
177 uint16_t nominal_perf = (msr.lo >> SHIFT_CPPC_CAPABILITY_1_NOMINAL_PERF) & 0xff;
178 uint16_t max_perf = (msr.lo >> SHIFT_CPPC_CAPABILITY_1_HIGHEST_PERF) & 0xff;
179
180 return (smbios_cpu_get_current_speed_mhz() * max_perf)/nominal_perf;
181 }
182
183 static void ap_stash_core_info(void)
184 {
185 unsigned int cpuid_cpu_id = cpuid_ebx(CPUID_EBX_CORE_ID) & 0xff;
186
187 const uint8_t level = CACHE_L3;
188 struct cpu_cache_info info;
189 x86_get_cpu_cache_info(level, &info);
190
191 struct core_info *core_info = &core_info_list[cpu_index()];
192 core_info->l3_cache_uid = cpuid_cpu_id >> log2(info.num_cores_shared);
193 core_info->l3_cache_size = info.size;
194
195 core_info->max_frequency = get_max_boost_frequency();
196 }
197
198 void amd_cpu_init(struct device *dev)
199 {
200 if (CONFIG(SOC_AMD_COMMON_BLOCK_MCA_COMMON))
201 check_mca();
202
203 set_cstate_io_addr();
204
205 if (CONFIG(SOC_AMD_COMMON_BLOCK_UCODE))
206 amd_apply_microcode_patch();
207
208 if (CONFIG(SOC_FILL_CPU_CACHE_INFO))
209 ap_stash_core_info();
210 }