Path: blob/master/tools/power/cpupower/utils/helpers/misc.c
26299 views
// SPDX-License-Identifier: GPL-2.012#include <stdio.h>3#include <errno.h>4#include <stdlib.h>5#include <string.h>67#include "helpers/helpers.h"8#include "helpers/sysfs.h"9#include "cpufreq.h"10#include "cpupower_intern.h"1112#if defined(__i386__) || defined(__x86_64__)1314#define MSR_AMD_HWCR 0xc00100151516int cpufreq_has_x86_boost_support(unsigned int cpu, int *support, int *active,17int *states)18{19int ret;20unsigned long long val;2122*support = *active = *states = 0;2324if (cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_CPB) {25*support = 1;2627/* AMD Family 0x17 does not utilize PCI D18F4 like prior28* families and has no fixed discrete boost states but29* has Hardware determined variable increments instead.30*/3132if (cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_CPB_MSR) {33if (!read_msr(cpu, MSR_AMD_HWCR, &val)) {34if (!(val & CPUPOWER_AMD_CPBDIS))35*active = 1;36}37} else {38ret = amd_pci_get_num_boost_states(active, states);39if (ret)40return ret;41}42} else if (cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_PSTATE) {43amd_pstate_boost_init(cpu, support, active);44} else if (cpupower_cpu_info.caps & CPUPOWER_CAP_INTEL_IDA)45*support = *active = 1;46return 0;47}4849int cpupower_intel_get_perf_bias(unsigned int cpu)50{51char linebuf[MAX_LINE_LEN];52char path[SYSFS_PATH_MAX];53unsigned long val;54char *endp;5556if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS))57return -1;5859snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/power/energy_perf_bias", cpu);6061if (cpupower_read_sysfs(path, linebuf, MAX_LINE_LEN) == 0)62return -1;6364val = strtol(linebuf, &endp, 0);65if (endp == linebuf || errno == ERANGE)66return -1;6768return val;69}7071int cpupower_intel_set_perf_bias(unsigned int cpu, unsigned int val)72{73char path[SYSFS_PATH_MAX];74char linebuf[3] = {};7576if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS))77return -1;7879snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/power/energy_perf_bias", cpu);80snprintf(linebuf, sizeof(linebuf), "%d", val);8182if (cpupower_write_sysfs(path, linebuf, 3) <= 0)83return -1;8485return 0;86}8788int cpupower_set_epp(unsigned int cpu, char *epp)89{90char path[SYSFS_PATH_MAX];91char linebuf[30] = {};9293snprintf(path, sizeof(path),94PATH_TO_CPU "cpu%u/cpufreq/energy_performance_preference", cpu);9596if (!is_valid_path(path))97return -1;9899snprintf(linebuf, sizeof(linebuf), "%s", epp);100101if (cpupower_write_sysfs(path, linebuf, 30) <= 0)102return -1;103104return 0;105}106107int cpupower_set_amd_pstate_mode(char *mode)108{109char path[SYSFS_PATH_MAX];110char linebuf[20] = {};111112snprintf(path, sizeof(path), PATH_TO_CPU "amd_pstate/status");113114if (!is_valid_path(path))115return -1;116117snprintf(linebuf, sizeof(linebuf), "%s\n", mode);118119if (cpupower_write_sysfs(path, linebuf, 20) <= 0)120return -1;121122return 0;123}124125bool cpupower_amd_pstate_enabled(void)126{127char *driver = cpufreq_get_driver(0);128bool ret = false;129130if (!driver)131return ret;132133if (!strncmp(driver, "amd", 3))134ret = true;135136cpufreq_put_driver(driver);137138return ret;139}140141#endif /* #if defined(__i386__) || defined(__x86_64__) */142143int cpufreq_has_generic_boost_support(bool *active)144{145char path[SYSFS_PATH_MAX];146char linebuf[2] = {};147unsigned long val;148char *endp;149150snprintf(path, sizeof(path), PATH_TO_CPU "cpufreq/boost");151152if (!is_valid_path(path))153return -EACCES;154155if (cpupower_read_sysfs(path, linebuf, 2) <= 0)156return -EINVAL;157158val = strtoul(linebuf, &endp, 0);159if (endp == linebuf || errno == ERANGE)160return -EINVAL;161162switch (val) {163case 0:164*active = false;165break;166case 1:167*active = true;168break;169default:170return -EINVAL;171}172173return 0;174}175176/* get_cpustate177*178* Gather the information of all online CPUs into bitmask struct179*/180void get_cpustate(void)181{182unsigned int cpu = 0;183184bitmask_clearall(online_cpus);185bitmask_clearall(offline_cpus);186187for (cpu = bitmask_first(cpus_chosen);188cpu <= bitmask_last(cpus_chosen); cpu++) {189190if (cpupower_is_cpu_online(cpu) == 1)191bitmask_setbit(online_cpus, cpu);192else193bitmask_setbit(offline_cpus, cpu);194195continue;196}197}198199/* print_online_cpus200*201* Print the CPU numbers of all CPUs that are online currently202*/203void print_online_cpus(void)204{205int str_len = 0;206char *online_cpus_str = NULL;207208str_len = online_cpus->size * 5;209online_cpus_str = (void *)malloc(sizeof(char) * str_len);210211if (!bitmask_isallclear(online_cpus)) {212bitmask_displaylist(online_cpus_str, str_len, online_cpus);213printf(_("Following CPUs are online:\n%s\n"), online_cpus_str);214}215}216217/* print_offline_cpus218*219* Print the CPU numbers of all CPUs that are offline currently220*/221void print_offline_cpus(void)222{223int str_len = 0;224char *offline_cpus_str = NULL;225226str_len = offline_cpus->size * 5;227offline_cpus_str = (void *)malloc(sizeof(char) * str_len);228229if (!bitmask_isallclear(offline_cpus)) {230bitmask_displaylist(offline_cpus_str, str_len, offline_cpus);231printf(_("Following CPUs are offline:\n%s\n"), offline_cpus_str);232printf(_("cpupower set operation was not performed on them\n"));233}234}235236/*237* print_speed238*239* Print the exact CPU frequency with appropriate unit240*/241void print_speed(unsigned long speed, int no_rounding)242{243unsigned long tmp;244245if (no_rounding) {246if (speed > 1000000)247printf("%u.%06u GHz", ((unsigned int)speed / 1000000),248((unsigned int)speed % 1000000));249else if (speed > 1000)250printf("%u.%03u MHz", ((unsigned int)speed / 1000),251(unsigned int)(speed % 1000));252else253printf("%lu kHz", speed);254} else {255if (speed > 1000000) {256tmp = speed % 10000;257if (tmp >= 5000)258speed += 10000;259printf("%u.%02u GHz", ((unsigned int)speed / 1000000),260((unsigned int)(speed % 1000000) / 10000));261} else if (speed > 100000) {262tmp = speed % 1000;263if (tmp >= 500)264speed += 1000;265printf("%u MHz", ((unsigned int)speed / 1000));266} else if (speed > 1000) {267tmp = speed % 100;268if (tmp >= 50)269speed += 100;270printf("%u.%01u MHz", ((unsigned int)speed / 1000),271((unsigned int)(speed % 1000) / 100));272}273}274}275276int cpupower_set_turbo_boost(int turbo_boost)277{278char path[SYSFS_PATH_MAX];279char linebuf[2] = {};280281snprintf(path, sizeof(path), PATH_TO_CPU "cpufreq/boost");282283if (!is_valid_path(path))284return -1;285286snprintf(linebuf, sizeof(linebuf), "%d", turbo_boost);287288if (cpupower_write_sysfs(path, linebuf, 2) <= 0)289return -1;290291return 0;292}293294295