1 // SPDX-License-Identifier: GPL-2.0
2 #if defined(__i386__) || defined(__x86_64__)
10 #include "helpers/helpers.h"
12 #include "acpi_cppc.h"
14 /* ACPI P-States Helper Functions for AMD Processors ***************/
15 #define MSR_AMD_PSTATE_STATUS 0xc0010063
16 #define MSR_AMD_PSTATE 0xc0010064
17 #define MSR_AMD_PSTATE_LIMIT 0xc0010061
44 unsigned long long val;
47 static int get_did(union core_pstate pstate)
51 if (cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_PSTATEDEF)
52 t = pstate.pstatedef.did;
53 else if (cpupower_cpu_info.family == 0x12)
56 t = pstate.pstate.did;
61 static int get_cof(union core_pstate pstate)
66 did = get_did(pstate);
67 if (cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_PSTATEDEF) {
68 fid = pstate.pstatedef.fid;
69 cof = 200 * fid / did;
72 fid = pstate.pstate.fid;
73 if (cpupower_cpu_info.family == 0x11)
75 cof = (100 * (fid + t)) >> did;
81 * cpu -> the cpu that gets evaluated
82 * boost_states -> how much boost states the machines support
85 * pstates -> a pointer to an array of size MAX_HW_PSTATES
86 * must be initialized with zeros.
87 * All available HW pstates (including boost states)
88 * no -> amount of pstates above array got filled up with
90 * returns zero on success, -1 on failure
92 int decode_pstates(unsigned int cpu, int boost_states,
93 unsigned long *pstates, int *no)
96 union core_pstate pstate;
97 unsigned long long val;
99 /* Only read out frequencies from HW if HW Pstate is supported,
100 * otherwise frequencies are exported via ACPI tables.
102 if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_HW_PSTATE))
105 if (read_msr(cpu, MSR_AMD_PSTATE_LIMIT, &val))
108 psmax = (val >> 4) & 0x7;
109 psmax += boost_states;
110 for (i = 0; i <= psmax; i++) {
111 if (i >= MAX_HW_PSTATES) {
112 fprintf(stderr, "HW pstates [%d] exceeding max [%d]\n",
113 psmax, MAX_HW_PSTATES);
116 if (read_msr(cpu, MSR_AMD_PSTATE + i, &pstate.val))
119 /* The enabled bit (bit 63) is common for all families */
120 if (!pstate.pstatedef.en)
123 pstates[i] = get_cof(pstate);
129 int amd_pci_get_num_boost_states(int *active, int *states)
131 struct pci_access *pci_acc;
132 struct pci_dev *device;
135 *active = *states = 0;
137 device = pci_slot_func_init(&pci_acc, 0x18, 4);
142 val = pci_read_byte(device, 0x15c);
147 *states = (val >> 2) & 7;
149 pci_cleanup(pci_acc);
153 /* ACPI P-States Helper Functions for AMD Processors ***************/
155 /* AMD P-State Helper Functions ************************************/
156 enum amd_pstate_value {
157 AMD_PSTATE_HIGHEST_PERF,
159 AMD_PSTATE_LOWEST_NONLINEAR_FREQ,
160 MAX_AMD_PSTATE_VALUE_READ_FILES,
163 static const char *amd_pstate_value_files[MAX_AMD_PSTATE_VALUE_READ_FILES] = {
164 [AMD_PSTATE_HIGHEST_PERF] = "amd_pstate_highest_perf",
165 [AMD_PSTATE_MAX_FREQ] = "amd_pstate_max_freq",
166 [AMD_PSTATE_LOWEST_NONLINEAR_FREQ] = "amd_pstate_lowest_nonlinear_freq",
169 static unsigned long amd_pstate_get_data(unsigned int cpu,
170 enum amd_pstate_value value)
172 return cpufreq_get_sysfs_value_from_table(cpu,
173 amd_pstate_value_files,
175 MAX_AMD_PSTATE_VALUE_READ_FILES);
178 void amd_pstate_boost_init(unsigned int cpu, int *support, int *active)
180 unsigned long highest_perf, nominal_perf, cpuinfo_min,
181 cpuinfo_max, amd_pstate_max;
183 highest_perf = amd_pstate_get_data(cpu, AMD_PSTATE_HIGHEST_PERF);
184 nominal_perf = acpi_cppc_get_data(cpu, NOMINAL_PERF);
186 *support = highest_perf > nominal_perf ? 1 : 0;
190 cpufreq_get_hardware_limits(cpu, &cpuinfo_min, &cpuinfo_max);
191 amd_pstate_max = amd_pstate_get_data(cpu, AMD_PSTATE_MAX_FREQ);
193 *active = cpuinfo_max == amd_pstate_max ? 1 : 0;
196 void amd_pstate_show_perf_and_freq(unsigned int cpu, int no_rounding)
198 printf(_(" AMD PSTATE Highest Performance: %lu. Maximum Frequency: "),
199 amd_pstate_get_data(cpu, AMD_PSTATE_HIGHEST_PERF));
201 * If boost isn't active, the cpuinfo_max doesn't indicate real max
202 * frequency. So we read it back from amd-pstate sysfs entry.
204 print_speed(amd_pstate_get_data(cpu, AMD_PSTATE_MAX_FREQ), no_rounding);
207 printf(_(" AMD PSTATE Nominal Performance: %lu. Nominal Frequency: "),
208 acpi_cppc_get_data(cpu, NOMINAL_PERF));
209 print_speed(acpi_cppc_get_data(cpu, NOMINAL_FREQ) * 1000,
213 printf(_(" AMD PSTATE Lowest Non-linear Performance: %lu. Lowest Non-linear Frequency: "),
214 acpi_cppc_get_data(cpu, LOWEST_NONLINEAR_PERF));
215 print_speed(amd_pstate_get_data(cpu, AMD_PSTATE_LOWEST_NONLINEAR_FREQ),
219 printf(_(" AMD PSTATE Lowest Performance: %lu. Lowest Frequency: "),
220 acpi_cppc_get_data(cpu, LOWEST_PERF));
221 print_speed(acpi_cppc_get_data(cpu, LOWEST_FREQ) * 1000, no_rounding);
225 /* AMD P-State Helper Functions ************************************/
226 #endif /* defined(__i386__) || defined(__x86_64__) */