1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * (C) 2010,2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
5 * Miscellaneous helpers which do not fit or are worth
6 * to put into separate headers
9 #ifndef __CPUPOWERUTILS_HELPERS__
10 #define __CPUPOWERUTILS_HELPERS__
16 #include "helpers/bitmask.h"
19 /* Internationalization ****************************/
22 #define _(String) gettext(String)
24 #define gettext_noop(String) String
26 #define N_(String) gettext_noop(String)
30 #define _(String) String
31 #define N_(String) String
34 /* Internationalization ****************************/
36 extern int run_as_root;
38 extern struct bitmask *cpus_chosen;
40 /* Global verbose (-d) stuff *********************************/
42 * define DEBUG via global Makefile variable
43 * Debug output is sent to stderr, do:
44 * cpupower monitor 2>/tmp/debug
45 * to split debug output away from normal output
48 extern int be_verbose;
50 #define dprint(fmt, ...) { \
52 fprintf(stderr, "%s: " fmt, \
53 __func__, ##__VA_ARGS__); \
57 static inline void dprint(const char *fmt, ...) { }
59 extern int be_verbose;
60 /* Global verbose (-v) stuff *********************************/
62 /* cpuid and cpuinfo helpers **************************/
63 enum cpupower_cpu_vendor {X86_VENDOR_UNKNOWN = 0, X86_VENDOR_INTEL,
64 X86_VENDOR_AMD, X86_VENDOR_HYGON, X86_VENDOR_MAX};
66 #define CPUPOWER_CAP_INV_TSC 0x00000001
67 #define CPUPOWER_CAP_APERF 0x00000002
68 #define CPUPOWER_CAP_AMD_CPB 0x00000004
69 #define CPUPOWER_CAP_PERF_BIAS 0x00000008
70 #define CPUPOWER_CAP_HAS_TURBO_RATIO 0x00000010
71 #define CPUPOWER_CAP_IS_SNB 0x00000020
72 #define CPUPOWER_CAP_INTEL_IDA 0x00000040
73 #define CPUPOWER_CAP_AMD_RDPRU 0x00000080
74 #define CPUPOWER_CAP_AMD_HW_PSTATE 0x00000100
75 #define CPUPOWER_CAP_AMD_PSTATEDEF 0x00000200
76 #define CPUPOWER_CAP_AMD_CPB_MSR 0x00000400
77 #define CPUPOWER_CAP_AMD_PSTATE 0x00000800
79 #define CPUPOWER_AMD_CPBDIS 0x02000000
81 #define MAX_HW_PSTATES 10
83 struct cpupower_cpu_info {
84 enum cpupower_cpu_vendor vendor;
87 unsigned int stepping;
88 /* CPU capabilities read out from cpuid */
89 unsigned long long caps;
94 * Extract CPU vendor, family, model, stepping info from /proc/cpuinfo
96 * Returns 0 on success or a negative error code
97 * Only used on x86, below global's struct values are zero/unknown on
100 extern int get_cpu_info(struct cpupower_cpu_info *cpu_info);
101 extern struct cpupower_cpu_info cpupower_cpu_info;
104 /* cpuid and cpuinfo helpers **************************/
106 /* X86 ONLY ****************************************/
107 #if defined(__i386__) || defined(__x86_64__)
111 /* Read/Write msr ****************************/
112 extern int read_msr(int cpu, unsigned int idx, unsigned long long *val);
113 extern int write_msr(int cpu, unsigned int idx, unsigned long long val);
115 extern int cpupower_intel_set_perf_bias(unsigned int cpu, unsigned int val);
116 extern int cpupower_intel_get_perf_bias(unsigned int cpu);
117 extern unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu);
119 extern int cpupower_set_epp(unsigned int cpu, char *epp);
120 extern int cpupower_set_amd_pstate_mode(char *mode);
121 extern int cpupower_set_turbo_boost(int turbo_boost);
123 /* Read/Write msr ****************************/
125 /* PCI stuff ****************************/
126 extern int amd_pci_get_num_boost_states(int *active, int *states);
127 extern struct pci_dev *pci_acc_init(struct pci_access **pacc, int domain,
128 int bus, int slot, int func, int vendor,
130 extern struct pci_dev *pci_slot_func_init(struct pci_access **pacc,
133 /* PCI stuff ****************************/
135 /* AMD HW pstate decoding **************************/
137 extern int decode_pstates(unsigned int cpu, int boost_states,
138 unsigned long *pstates, int *no);
140 /* AMD HW pstate decoding **************************/
142 extern int cpufreq_has_boost_support(unsigned int cpu, int *support,
143 int *active, int * states);
145 /* AMD P-State stuff **************************/
146 bool cpupower_amd_pstate_enabled(void);
147 void amd_pstate_boost_init(unsigned int cpu,
148 int *support, int *active);
149 void amd_pstate_show_perf_and_freq(unsigned int cpu,
152 /* AMD P-State stuff **************************/
155 * CPUID functions returning a single datum
157 unsigned int cpuid_eax(unsigned int op);
158 unsigned int cpuid_ebx(unsigned int op);
159 unsigned int cpuid_ecx(unsigned int op);
160 unsigned int cpuid_edx(unsigned int op);
162 /* cpuid and cpuinfo helpers **************************/
163 /* X86 ONLY ********************************************/
165 static inline int decode_pstates(unsigned int cpu, int boost_states,
166 unsigned long *pstates, int *no)
169 static inline int read_msr(int cpu, unsigned int idx, unsigned long long *val)
171 static inline int write_msr(int cpu, unsigned int idx, unsigned long long val)
173 static inline int cpupower_intel_set_perf_bias(unsigned int cpu, unsigned int val)
175 static inline int cpupower_intel_get_perf_bias(unsigned int cpu)
177 static inline unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu)
180 static inline int cpupower_set_epp(unsigned int cpu, char *epp)
182 static inline int cpupower_set_amd_pstate_mode(char *mode)
184 static inline int cpupower_set_turbo_boost(int turbo_boost)
187 /* Read/Write msr ****************************/
189 static inline int cpufreq_has_boost_support(unsigned int cpu, int *support,
190 int *active, int * states)
193 static inline bool cpupower_amd_pstate_enabled(void)
195 static inline void amd_pstate_boost_init(unsigned int cpu, int *support,
198 static inline void amd_pstate_show_perf_and_freq(unsigned int cpu,
202 /* cpuid and cpuinfo helpers **************************/
204 static inline unsigned int cpuid_eax(unsigned int op) { return 0; };
205 static inline unsigned int cpuid_ebx(unsigned int op) { return 0; };
206 static inline unsigned int cpuid_ecx(unsigned int op) { return 0; };
207 static inline unsigned int cpuid_edx(unsigned int op) { return 0; };
208 #endif /* defined(__i386__) || defined(__x86_64__) */
211 * CPU State related functions
213 extern struct bitmask *online_cpus;
214 extern struct bitmask *offline_cpus;
216 void get_cpustate(void);
217 void print_online_cpus(void);
218 void print_offline_cpus(void);
219 void print_speed(unsigned long speed, int no_rounding);
221 #endif /* __CPUPOWERUTILS_HELPERS__ */