Path: blob/master/tools/power/cpupower/utils/helpers/helpers.h
26299 views
/* SPDX-License-Identifier: GPL-2.0-only */1/*2* (C) 2010,2011 Thomas Renninger <[email protected]>, Novell Inc.3*4* Miscellaneous helpers which do not fit or are worth5* to put into separate headers6*/78#ifndef __CPUPOWERUTILS_HELPERS__9#define __CPUPOWERUTILS_HELPERS__1011#include <libintl.h>12#include <locale.h>13#include <stdbool.h>1415#include "helpers/bitmask.h"16#include <cpupower.h>1718/* Internationalization ****************************/19#ifdef NLS2021#define _(String) gettext(String)22#ifndef gettext_noop23#define gettext_noop(String) String24#endif25#define N_(String) gettext_noop(String)2627#else /* !NLS */2829#define _(String) String30#define N_(String) String3132#endif33/* Internationalization ****************************/3435extern int run_as_root;36extern int base_cpu;37extern struct bitmask *cpus_chosen;3839/* Global verbose (-d) stuff *********************************/40/*41* define DEBUG via global Makefile variable42* Debug output is sent to stderr, do:43* cpupower monitor 2>/tmp/debug44* to split debug output away from normal output45*/46#ifdef DEBUG47extern int be_verbose;4849#define dprint(fmt, ...) { \50if (be_verbose) { \51fprintf(stderr, "%s: " fmt, \52__func__, ##__VA_ARGS__); \53} \54}55#else56static inline void dprint(const char *fmt, ...) { }57#endif58extern int be_verbose;59/* Global verbose (-v) stuff *********************************/6061/* cpuid and cpuinfo helpers **************************/62enum cpupower_cpu_vendor {X86_VENDOR_UNKNOWN = 0, X86_VENDOR_INTEL,63X86_VENDOR_AMD, X86_VENDOR_HYGON, X86_VENDOR_MAX};6465#define CPUPOWER_CAP_INV_TSC 0x0000000166#define CPUPOWER_CAP_APERF 0x0000000267#define CPUPOWER_CAP_AMD_CPB 0x0000000468#define CPUPOWER_CAP_PERF_BIAS 0x0000000869#define CPUPOWER_CAP_HAS_TURBO_RATIO 0x0000001070#define CPUPOWER_CAP_IS_SNB 0x0000002071#define CPUPOWER_CAP_INTEL_IDA 0x0000004072#define CPUPOWER_CAP_AMD_RDPRU 0x0000008073#define CPUPOWER_CAP_AMD_HW_PSTATE 0x0000010074#define CPUPOWER_CAP_AMD_PSTATEDEF 0x0000020075#define CPUPOWER_CAP_AMD_CPB_MSR 0x0000040076#define CPUPOWER_CAP_AMD_PSTATE 0x000008007778#define CPUPOWER_AMD_CPBDIS 0x020000007980#define MAX_HW_PSTATES 108182struct cpupower_cpu_info {83enum cpupower_cpu_vendor vendor;84unsigned int family;85unsigned int model;86unsigned int stepping;87/* CPU capabilities read out from cpuid */88unsigned long long caps;89};9091/* get_cpu_info92*93* Extract CPU vendor, family, model, stepping info from /proc/cpuinfo94*95* Returns 0 on success or a negative error code96* Only used on x86, below global's struct values are zero/unknown on97* other archs98*/99extern int get_cpu_info(struct cpupower_cpu_info *cpu_info);100extern struct cpupower_cpu_info cpupower_cpu_info;101102103/* cpuid and cpuinfo helpers **************************/104105int cpufreq_has_generic_boost_support(bool *active);106int cpupower_set_turbo_boost(int turbo_boost);107108/* X86 ONLY ****************************************/109#if defined(__i386__) || defined(__x86_64__)110111#include <pci/pci.h>112113/* Read/Write msr ****************************/114extern int read_msr(int cpu, unsigned int idx, unsigned long long *val);115extern int write_msr(int cpu, unsigned int idx, unsigned long long val);116117extern int cpupower_intel_set_perf_bias(unsigned int cpu, unsigned int val);118extern int cpupower_intel_get_perf_bias(unsigned int cpu);119extern unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu);120121extern int cpupower_set_epp(unsigned int cpu, char *epp);122extern int cpupower_set_amd_pstate_mode(char *mode);123124/* Read/Write msr ****************************/125126/* PCI stuff ****************************/127extern int amd_pci_get_num_boost_states(int *active, int *states);128extern struct pci_dev *pci_acc_init(struct pci_access **pacc, int domain,129int bus, int slot, int func, int vendor,130int dev);131extern struct pci_dev *pci_slot_func_init(struct pci_access **pacc,132int slot, int func);133134/* PCI stuff ****************************/135136/* AMD HW pstate decoding **************************/137138extern int decode_pstates(unsigned int cpu, int boost_states,139unsigned long *pstates, int *no);140141/* AMD HW pstate decoding **************************/142143int cpufreq_has_x86_boost_support(unsigned int cpu, int *support,144int *active, int *states);145146/* AMD P-State stuff **************************/147bool cpupower_amd_pstate_enabled(void);148void amd_pstate_boost_init(unsigned int cpu,149int *support, int *active);150void amd_pstate_show_perf_and_freq(unsigned int cpu,151int no_rounding);152153/* AMD P-State stuff **************************/154155/*156* CPUID functions returning a single datum157*/158unsigned int cpuid_eax(unsigned int op);159unsigned int cpuid_ebx(unsigned int op);160unsigned int cpuid_ecx(unsigned int op);161unsigned int cpuid_edx(unsigned int op);162163/* cpuid and cpuinfo helpers **************************/164/* X86 ONLY ********************************************/165#else166static inline int decode_pstates(unsigned int cpu, int boost_states,167unsigned long *pstates, int *no)168{ return -1; };169170static inline int read_msr(int cpu, unsigned int idx, unsigned long long *val)171{ return -1; };172static inline int write_msr(int cpu, unsigned int idx, unsigned long long val)173{ return -1; };174static inline int cpupower_intel_set_perf_bias(unsigned int cpu, unsigned int val)175{ return -1; };176static inline int cpupower_intel_get_perf_bias(unsigned int cpu)177{ return -1; };178static inline unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu)179{ return 0; };180181static inline int cpupower_set_epp(unsigned int cpu, char *epp)182{ return -1; };183static inline int cpupower_set_amd_pstate_mode(char *mode)184{ return -1; };185186/* Read/Write msr ****************************/187188static inline int cpufreq_has_x86_boost_support(unsigned int cpu, int *support,189int *active, int *states)190{ return -1; }191192static inline bool cpupower_amd_pstate_enabled(void)193{ return false; }194static inline void amd_pstate_boost_init(unsigned int cpu, int *support,195int *active)196{}197static inline void amd_pstate_show_perf_and_freq(unsigned int cpu,198int no_rounding)199{}200201/* cpuid and cpuinfo helpers **************************/202203static inline unsigned int cpuid_eax(unsigned int op) { return 0; };204static inline unsigned int cpuid_ebx(unsigned int op) { return 0; };205static inline unsigned int cpuid_ecx(unsigned int op) { return 0; };206static inline unsigned int cpuid_edx(unsigned int op) { return 0; };207#endif /* defined(__i386__) || defined(__x86_64__) */208209/*210* CPU State related functions211*/212extern struct bitmask *online_cpus;213extern struct bitmask *offline_cpus;214215void get_cpustate(void);216void print_online_cpus(void);217void print_offline_cpus(void);218void print_speed(unsigned long speed, int no_rounding);219220#endif /* __CPUPOWERUTILS_HELPERS__ */221222223