| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | #if defined(__i386__) || defined(__x86_64__) |
| 3 | #include <unistd.h> |
| 4 | #include <errno.h> |
| 5 | #include <stdio.h> |
| 6 | #include <stdint.h> |
| 7 | |
| 8 | #include <pci/pci.h> |
| 9 | |
| 10 | #include "helpers/helpers.h" |
| 11 | #include "cpufreq.h" |
| 12 | #include "acpi_cppc.h" |
| 13 | |
| 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 |
| 18 | |
| 19 | union core_pstate { |
| 20 | /* pre fam 17h: */ |
| 21 | struct { |
| 22 | unsigned fid:6; |
| 23 | unsigned did:3; |
| 24 | unsigned vid:7; |
| 25 | unsigned res1:6; |
| 26 | unsigned nbdid:1; |
| 27 | unsigned res2:2; |
| 28 | unsigned nbvid:7; |
| 29 | unsigned iddval:8; |
| 30 | unsigned idddiv:2; |
| 31 | unsigned res3:21; |
| 32 | unsigned en:1; |
| 33 | } pstate; |
| 34 | /* since fam 17h: */ |
| 35 | struct { |
| 36 | unsigned fid:8; |
| 37 | unsigned did:6; |
| 38 | unsigned vid:8; |
| 39 | unsigned iddval:8; |
| 40 | unsigned idddiv:2; |
| 41 | unsigned res1:31; |
| 42 | unsigned en:1; |
| 43 | } pstatedef; |
| 44 | /* since fam 1Ah: */ |
| 45 | struct { |
| 46 | unsigned fid:12; |
| 47 | unsigned res1:2; |
| 48 | unsigned vid:8; |
| 49 | unsigned iddval:8; |
| 50 | unsigned idddiv:2; |
| 51 | unsigned res2:31; |
| 52 | unsigned en:1; |
| 53 | } pstatedef2; |
| 54 | unsigned long long val; |
| 55 | }; |
| 56 | |
| 57 | static int get_did(union core_pstate pstate) |
| 58 | { |
| 59 | int t; |
| 60 | |
| 61 | /* Fam 1Ah onward do not use did */ |
| 62 | if (cpupower_cpu_info.family >= 0x1A) |
| 63 | return 0; |
| 64 | |
| 65 | if (cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_PSTATEDEF) |
| 66 | t = pstate.pstatedef.did; |
| 67 | else if (cpupower_cpu_info.family == 0x12) |
| 68 | t = pstate.val & 0xf; |
| 69 | else |
| 70 | t = pstate.pstate.did; |
| 71 | |
| 72 | return t; |
| 73 | } |
| 74 | |
| 75 | static int get_cof(union core_pstate pstate) |
| 76 | { |
| 77 | int t; |
| 78 | int fid, did, cof = 0; |
| 79 | |
| 80 | did = get_did(pstate); |
| 81 | if (cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_PSTATEDEF) { |
| 82 | if (cpupower_cpu_info.family >= 0x1A) { |
| 83 | fid = pstate.pstatedef2.fid; |
| 84 | if (fid > 0x0f) |
| 85 | cof = (fid * 5); |
| 86 | } else { |
| 87 | fid = pstate.pstatedef.fid; |
| 88 | cof = 200 * fid / did; |
| 89 | } |
| 90 | } else { |
| 91 | t = 0x10; |
| 92 | fid = pstate.pstate.fid; |
| 93 | if (cpupower_cpu_info.family == 0x11) |
| 94 | t = 0x8; |
| 95 | cof = (100 * (fid + t)) >> did; |
| 96 | } |
| 97 | return cof; |
| 98 | } |
| 99 | |
| 100 | /* Needs: |
| 101 | * cpu -> the cpu that gets evaluated |
| 102 | * boost_states -> how much boost states the machines support |
| 103 | * |
| 104 | * Fills up: |
| 105 | * pstates -> a pointer to an array of size MAX_HW_PSTATES |
| 106 | * must be initialized with zeros. |
| 107 | * All available HW pstates (including boost states) |
| 108 | * no -> amount of pstates above array got filled up with |
| 109 | * |
| 110 | * returns zero on success, -1 on failure |
| 111 | */ |
| 112 | int decode_pstates(unsigned int cpu, int boost_states, |
| 113 | unsigned long *pstates, int *no) |
| 114 | { |
| 115 | int i, psmax; |
| 116 | union core_pstate pstate; |
| 117 | unsigned long long val; |
| 118 | |
| 119 | /* Only read out frequencies from HW if HW Pstate is supported, |
| 120 | * otherwise frequencies are exported via ACPI tables. |
| 121 | */ |
| 122 | if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_HW_PSTATE)) |
| 123 | return -1; |
| 124 | |
| 125 | if (read_msr(cpu, MSR_AMD_PSTATE_LIMIT, &val)) |
| 126 | return -1; |
| 127 | |
| 128 | psmax = (val >> 4) & 0x7; |
| 129 | psmax += boost_states; |
| 130 | for (i = 0; i <= psmax; i++) { |
| 131 | if (i >= MAX_HW_PSTATES) { |
| 132 | fprintf(stderr, "HW pstates [%d] exceeding max [%d]\n" , |
| 133 | psmax, MAX_HW_PSTATES); |
| 134 | return -1; |
| 135 | } |
| 136 | if (read_msr(cpu, MSR_AMD_PSTATE + i, &pstate.val)) |
| 137 | return -1; |
| 138 | |
| 139 | /* The enabled bit (bit 63) is common for all families */ |
| 140 | if (!pstate.pstatedef.en) |
| 141 | continue; |
| 142 | |
| 143 | pstates[i] = get_cof(pstate); |
| 144 | } |
| 145 | *no = i; |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | int amd_pci_get_num_boost_states(int *active, int *states) |
| 150 | { |
| 151 | struct pci_access *pci_acc; |
| 152 | struct pci_dev *device; |
| 153 | uint8_t val = 0; |
| 154 | |
| 155 | *active = *states = 0; |
| 156 | |
| 157 | device = pci_slot_func_init(&pci_acc, 0x18, 4); |
| 158 | |
| 159 | if (device == NULL) |
| 160 | return -ENODEV; |
| 161 | |
| 162 | val = pci_read_byte(device, 0x15c); |
| 163 | if (val & 3) |
| 164 | *active = 1; |
| 165 | else |
| 166 | *active = 0; |
| 167 | *states = (val >> 2) & 7; |
| 168 | |
| 169 | pci_cleanup(pci_acc); |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | /* ACPI P-States Helper Functions for AMD Processors ***************/ |
| 174 | |
| 175 | /* AMD P-State Helper Functions ************************************/ |
| 176 | enum amd_pstate_value { |
| 177 | AMD_PSTATE_HIGHEST_PERF, |
| 178 | AMD_PSTATE_MAX_FREQ, |
| 179 | AMD_PSTATE_LOWEST_NONLINEAR_FREQ, |
| 180 | AMD_PSTATE_HW_PREFCORE, |
| 181 | AMD_PSTATE_PREFCORE_RANKING, |
| 182 | MAX_AMD_PSTATE_VALUE_READ_FILES, |
| 183 | }; |
| 184 | |
| 185 | static const char *amd_pstate_value_files[MAX_AMD_PSTATE_VALUE_READ_FILES] = { |
| 186 | [AMD_PSTATE_HIGHEST_PERF] = "amd_pstate_highest_perf" , |
| 187 | [AMD_PSTATE_MAX_FREQ] = "amd_pstate_max_freq" , |
| 188 | [AMD_PSTATE_LOWEST_NONLINEAR_FREQ] = "amd_pstate_lowest_nonlinear_freq" , |
| 189 | [AMD_PSTATE_HW_PREFCORE] = "amd_pstate_hw_prefcore" , |
| 190 | [AMD_PSTATE_PREFCORE_RANKING] = "amd_pstate_prefcore_ranking" , |
| 191 | }; |
| 192 | |
| 193 | static unsigned long amd_pstate_get_data(unsigned int cpu, |
| 194 | enum amd_pstate_value value) |
| 195 | { |
| 196 | return cpufreq_get_sysfs_value_from_table(cpu, |
| 197 | amd_pstate_value_files, |
| 198 | value, |
| 199 | MAX_AMD_PSTATE_VALUE_READ_FILES); |
| 200 | } |
| 201 | |
| 202 | void amd_pstate_boost_init(unsigned int cpu, int *support, int *active) |
| 203 | { |
| 204 | unsigned long highest_perf, nominal_perf, cpuinfo_min, |
| 205 | cpuinfo_max, amd_pstate_max; |
| 206 | |
| 207 | highest_perf = amd_pstate_get_data(cpu, value: AMD_PSTATE_HIGHEST_PERF); |
| 208 | nominal_perf = acpi_cppc_get_data(cpu, NOMINAL_PERF); |
| 209 | |
| 210 | *support = highest_perf > nominal_perf ? 1 : 0; |
| 211 | if (!(*support)) |
| 212 | return; |
| 213 | |
| 214 | cpufreq_get_hardware_limits(cpu, &cpuinfo_min, &cpuinfo_max); |
| 215 | amd_pstate_max = amd_pstate_get_data(cpu, value: AMD_PSTATE_MAX_FREQ); |
| 216 | |
| 217 | *active = cpuinfo_max == amd_pstate_max ? 1 : 0; |
| 218 | } |
| 219 | |
| 220 | void amd_pstate_show_perf_and_freq(unsigned int cpu, int no_rounding) |
| 221 | { |
| 222 | |
| 223 | printf(_(" amd-pstate limits:\n" )); |
| 224 | printf(_(" Highest Performance: %lu. Maximum Frequency: " ), |
| 225 | amd_pstate_get_data(cpu, value: AMD_PSTATE_HIGHEST_PERF)); |
| 226 | /* |
| 227 | * If boost isn't active, the cpuinfo_max doesn't indicate real max |
| 228 | * frequency. So we read it back from amd-pstate sysfs entry. |
| 229 | */ |
| 230 | print_speed(amd_pstate_get_data(cpu, value: AMD_PSTATE_MAX_FREQ), no_rounding); |
| 231 | printf(".\n" ); |
| 232 | |
| 233 | printf(_(" Nominal Performance: %lu. Nominal Frequency: " ), |
| 234 | acpi_cppc_get_data(cpu, NOMINAL_PERF)); |
| 235 | print_speed(acpi_cppc_get_data(cpu, NOMINAL_FREQ) * 1000, |
| 236 | no_rounding); |
| 237 | printf(".\n" ); |
| 238 | |
| 239 | printf(_(" Lowest Non-linear Performance: %lu. Lowest Non-linear Frequency: " ), |
| 240 | acpi_cppc_get_data(cpu, LOWEST_NONLINEAR_PERF)); |
| 241 | print_speed(amd_pstate_get_data(cpu, value: AMD_PSTATE_LOWEST_NONLINEAR_FREQ), |
| 242 | no_rounding); |
| 243 | printf(".\n" ); |
| 244 | |
| 245 | printf(_(" Lowest Performance: %lu. Lowest Frequency: " ), |
| 246 | acpi_cppc_get_data(cpu, LOWEST_PERF)); |
| 247 | print_speed(acpi_cppc_get_data(cpu, LOWEST_FREQ) * 1000, no_rounding); |
| 248 | printf(".\n" ); |
| 249 | |
| 250 | printf(_(" Preferred Core Support: %lu. Preferred Core Ranking: %lu.\n" ), |
| 251 | amd_pstate_get_data(cpu, value: AMD_PSTATE_HW_PREFCORE), |
| 252 | amd_pstate_get_data(cpu, value: AMD_PSTATE_PREFCORE_RANKING)); |
| 253 | } |
| 254 | |
| 255 | /* AMD P-State Helper Functions ************************************/ |
| 256 | #endif /* defined(__i386__) || defined(__x86_64__) */ |
| 257 | |