1 // SPDX-License-Identifier: GPL-2.0
8 #include "helpers/helpers.h"
9 #include "helpers/sysfs.h"
12 #if defined(__i386__) || defined(__x86_64__)
14 #include "cpupower_intern.h"
16 #define MSR_AMD_HWCR 0xc0010015
18 int cpufreq_has_boost_support(unsigned int cpu, int *support, int *active,
22 unsigned long long val;
24 *support = *active = *states = 0;
26 if (cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_CPB) {
29 /* AMD Family 0x17 does not utilize PCI D18F4 like prior
30 * families and has no fixed discrete boost states but
31 * has Hardware determined variable increments instead.
34 if (cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_CPB_MSR) {
35 if (!read_msr(cpu, MSR_AMD_HWCR, &val)) {
36 if (!(val & CPUPOWER_AMD_CPBDIS))
40 ret = amd_pci_get_num_boost_states(active, states);
44 } else if (cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_PSTATE) {
45 amd_pstate_boost_init(cpu, support, active);
46 } else if (cpupower_cpu_info.caps & CPUPOWER_CAP_INTEL_IDA)
47 *support = *active = 1;
51 int cpupower_intel_get_perf_bias(unsigned int cpu)
53 char linebuf[MAX_LINE_LEN];
54 char path[SYSFS_PATH_MAX];
58 if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS))
61 snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/power/energy_perf_bias", cpu);
63 if (cpupower_read_sysfs(path, linebuf, MAX_LINE_LEN) == 0)
66 val = strtol(linebuf, &endp, 0);
67 if (endp == linebuf || errno == ERANGE)
73 int cpupower_intel_set_perf_bias(unsigned int cpu, unsigned int val)
75 char path[SYSFS_PATH_MAX];
78 if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS))
81 snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/power/energy_perf_bias", cpu);
82 snprintf(linebuf, sizeof(linebuf), "%d", val);
84 if (cpupower_write_sysfs(path, linebuf, 3) <= 0)
90 int cpupower_set_epp(unsigned int cpu, char *epp)
92 char path[SYSFS_PATH_MAX];
93 char linebuf[30] = {};
95 snprintf(path, sizeof(path),
96 PATH_TO_CPU "cpu%u/cpufreq/energy_performance_preference", cpu);
98 if (!is_valid_path(path))
101 snprintf(linebuf, sizeof(linebuf), "%s", epp);
103 if (cpupower_write_sysfs(path, linebuf, 30) <= 0)
109 int cpupower_set_amd_pstate_mode(char *mode)
111 char path[SYSFS_PATH_MAX];
112 char linebuf[20] = {};
114 snprintf(path, sizeof(path), PATH_TO_CPU "amd_pstate/status");
116 if (!is_valid_path(path))
119 snprintf(linebuf, sizeof(linebuf), "%s\n", mode);
121 if (cpupower_write_sysfs(path, linebuf, 20) <= 0)
127 int cpupower_set_turbo_boost(int turbo_boost)
129 char path[SYSFS_PATH_MAX];
130 char linebuf[2] = {};
132 snprintf(path, sizeof(path), PATH_TO_CPU "cpufreq/boost");
134 if (!is_valid_path(path))
137 snprintf(linebuf, sizeof(linebuf), "%d", turbo_boost);
139 if (cpupower_write_sysfs(path, linebuf, 2) <= 0)
145 bool cpupower_amd_pstate_enabled(void)
147 char *driver = cpufreq_get_driver(0);
153 if (!strncmp(driver, "amd", 3))
156 cpufreq_put_driver(driver);
161 #endif /* #if defined(__i386__) || defined(__x86_64__) */
165 * Gather the information of all online CPUs into bitmask struct
167 void get_cpustate(void)
169 unsigned int cpu = 0;
171 bitmask_clearall(online_cpus);
172 bitmask_clearall(offline_cpus);
174 for (cpu = bitmask_first(cpus_chosen);
175 cpu <= bitmask_last(cpus_chosen); cpu++) {
177 if (cpupower_is_cpu_online(cpu) == 1)
178 bitmask_setbit(online_cpus, cpu);
180 bitmask_setbit(offline_cpus, cpu);
188 * Print the CPU numbers of all CPUs that are online currently
190 void print_online_cpus(void)
193 char *online_cpus_str = NULL;
195 str_len = online_cpus->size * 5;
196 online_cpus_str = (void *)malloc(sizeof(char) * str_len);
198 if (!bitmask_isallclear(online_cpus)) {
199 bitmask_displaylist(online_cpus_str, str_len, online_cpus);
200 printf(_("Following CPUs are online:\n%s\n"), online_cpus_str);
204 /* print_offline_cpus
206 * Print the CPU numbers of all CPUs that are offline currently
208 void print_offline_cpus(void)
211 char *offline_cpus_str = NULL;
213 str_len = offline_cpus->size * 5;
214 offline_cpus_str = (void *)malloc(sizeof(char) * str_len);
216 if (!bitmask_isallclear(offline_cpus)) {
217 bitmask_displaylist(offline_cpus_str, str_len, offline_cpus);
218 printf(_("Following CPUs are offline:\n%s\n"), offline_cpus_str);
219 printf(_("cpupower set operation was not performed on them\n"));
226 * Print the exact CPU frequency with appropriate unit
228 void print_speed(unsigned long speed, int no_rounding)
234 printf("%u.%06u GHz", ((unsigned int)speed / 1000000),
235 ((unsigned int)speed % 1000000));
236 else if (speed > 1000)
237 printf("%u.%03u MHz", ((unsigned int)speed / 1000),
238 (unsigned int)(speed % 1000));
240 printf("%lu kHz", speed);
242 if (speed > 1000000) {
246 printf("%u.%02u GHz", ((unsigned int)speed / 1000000),
247 ((unsigned int)(speed % 1000000) / 10000));
248 } else if (speed > 100000) {
252 printf("%u MHz", ((unsigned int)speed / 1000));
253 } else if (speed > 1000) {
257 printf("%u.%01u MHz", ((unsigned int)speed / 1000),
258 ((unsigned int)(speed % 1000) / 100));