xref: /linux/tools/power/cpupower/utils/helpers/helpers.h (revision 0257f64bdac7fdca30fa3cae0df8b9ecbec7733a)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  *  (C) 2010,2011       Thomas Renninger <trenn@suse.de>, Novell Inc.
4  *
5  * Miscellaneous helpers which do not fit or are worth
6  * to put into separate headers
7  */
8 
9 #ifndef __CPUPOWERUTILS_HELPERS__
10 #define __CPUPOWERUTILS_HELPERS__
11 
12 #include <libintl.h>
13 #include <locale.h>
14 #include <stdbool.h>
15 
16 #include "helpers/bitmask.h"
17 #include <cpupower.h>
18 
19 /* Internationalization ****************************/
20 #ifdef NLS
21 
22 #define _(String) gettext(String)
23 #ifndef gettext_noop
24 #define gettext_noop(String) String
25 #endif
26 #define N_(String) gettext_noop(String)
27 
28 #else /* !NLS */
29 
30 #define _(String) String
31 #define N_(String) String
32 
33 #endif
34 /* Internationalization ****************************/
35 
36 extern int run_as_root;
37 extern int base_cpu;
38 extern struct bitmask *cpus_chosen;
39 
40 /* Global verbose (-d) stuff *********************************/
41 /*
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
46 */
47 #ifdef DEBUG
48 extern int be_verbose;
49 
50 #define dprint(fmt, ...) {					\
51 		if (be_verbose) {				\
52 			fprintf(stderr, "%s: " fmt,		\
53 				__func__, ##__VA_ARGS__);	\
54 		}						\
55 	}
56 #else
dprint(const char * fmt,...)57 static inline void dprint(const char *fmt, ...) { }
58 #endif
59 extern int be_verbose;
60 /* Global verbose (-v) stuff *********************************/
61 
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};
65 
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
78 
79 #define CPUPOWER_AMD_CPBDIS		0x02000000
80 
81 #define MAX_HW_PSTATES 10
82 
83 struct cpupower_cpu_info {
84 	enum cpupower_cpu_vendor vendor;
85 	unsigned int family;
86 	unsigned int model;
87 	unsigned int stepping;
88 	/* CPU capabilities read out from cpuid */
89 	unsigned long long caps;
90 };
91 
92 /* get_cpu_info
93  *
94  * Extract CPU vendor, family, model, stepping info from /proc/cpuinfo
95  *
96  * Returns 0 on success or a negative error code
97  * Only used on x86, below global's struct values are zero/unknown on
98  * other archs
99  */
100 extern int get_cpu_info(struct cpupower_cpu_info *cpu_info);
101 extern struct cpupower_cpu_info cpupower_cpu_info;
102 
103 
104 /* cpuid and cpuinfo helpers  **************************/
105 
106 int cpufreq_has_generic_boost_support(bool *active);
107 int cpupower_set_generic_turbo_boost(int turbo_boost);
108 
109 /* X86 ONLY ****************************************/
110 #if defined(__i386__) || defined(__x86_64__)
111 
112 #include <pci/pci.h>
113 
114 /* Read/Write msr ****************************/
115 extern int read_msr(int cpu, unsigned int idx, unsigned long long *val);
116 extern int write_msr(int cpu, unsigned int idx, unsigned long long val);
117 
118 extern int cpupower_intel_set_perf_bias(unsigned int cpu, unsigned int val);
119 extern int cpupower_intel_get_perf_bias(unsigned int cpu);
120 extern unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu);
121 
122 extern int cpupower_set_epp(unsigned int cpu, char *epp);
123 extern int cpupower_set_amd_pstate_mode(char *mode);
124 
125 /* Read/Write msr ****************************/
126 
127 /* PCI stuff ****************************/
128 extern int amd_pci_get_num_boost_states(int *active, int *states);
129 extern struct pci_dev *pci_acc_init(struct pci_access **pacc, int domain,
130 				    int bus, int slot, int func, int vendor,
131 				    int dev);
132 extern struct pci_dev *pci_slot_func_init(struct pci_access **pacc,
133 					      int slot, int func);
134 
135 /* PCI stuff ****************************/
136 
137 /* AMD HW pstate decoding **************************/
138 
139 extern int decode_pstates(unsigned int cpu, int boost_states,
140 			  unsigned long *pstates, int *no);
141 
142 /* AMD HW pstate decoding **************************/
143 
144 int cpufreq_has_x86_boost_support(unsigned int cpu, int *support,
145 				  int *active, int *states);
146 int cpupower_set_intel_turbo_boost(int turbo_boost);
147 
148 /* AMD P-State stuff **************************/
149 bool cpupower_amd_pstate_enabled(void);
150 void amd_pstate_boost_init(unsigned int cpu,
151 			   int *support, int *active);
152 void amd_pstate_show_perf_and_freq(unsigned int cpu,
153 				   int no_rounding);
154 
155 /* AMD P-State stuff **************************/
156 
157 /*
158  * CPUID functions returning a single datum
159  */
160 unsigned int cpuid_eax(unsigned int op);
161 unsigned int cpuid_ebx(unsigned int op);
162 unsigned int cpuid_ecx(unsigned int op);
163 unsigned int cpuid_edx(unsigned int op);
164 
165 /* cpuid and cpuinfo helpers  **************************/
166 /* X86 ONLY ********************************************/
167 #else
decode_pstates(unsigned int cpu,int boost_states,unsigned long * pstates,int * no)168 static inline int decode_pstates(unsigned int cpu, int boost_states,
169 				 unsigned long *pstates, int *no)
170 { return -1; };
171 
read_msr(int cpu,unsigned int idx,unsigned long long * val)172 static inline int read_msr(int cpu, unsigned int idx, unsigned long long *val)
173 { return -1; };
write_msr(int cpu,unsigned int idx,unsigned long long val)174 static inline int write_msr(int cpu, unsigned int idx, unsigned long long val)
175 { return -1; };
cpupower_intel_set_perf_bias(unsigned int cpu,unsigned int val)176 static inline int cpupower_intel_set_perf_bias(unsigned int cpu, unsigned int val)
177 { return -1; };
cpupower_intel_get_perf_bias(unsigned int cpu)178 static inline int cpupower_intel_get_perf_bias(unsigned int cpu)
179 { return -1; };
msr_intel_get_turbo_ratio(unsigned int cpu)180 static inline unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu)
181 { return 0; };
182 
cpupower_set_epp(unsigned int cpu,char * epp)183 static inline int cpupower_set_epp(unsigned int cpu, char *epp)
184 { return -1; };
cpupower_set_amd_pstate_mode(char * mode)185 static inline int cpupower_set_amd_pstate_mode(char *mode)
186 { return -1; };
187 
188 /* Read/Write msr ****************************/
189 
cpufreq_has_x86_boost_support(unsigned int cpu,int * support,int * active,int * states)190 static inline int cpufreq_has_x86_boost_support(unsigned int cpu, int *support,
191 						int *active, int *states)
192 { return -1; }
cpupower_set_intel_turbo_boost(int turbo_boost)193 static inline int cpupower_set_intel_turbo_boost(int turbo_boost)
194 { return -1; }
195 
cpupower_amd_pstate_enabled(void)196 static inline bool cpupower_amd_pstate_enabled(void)
197 { return false; }
amd_pstate_boost_init(unsigned int cpu,int * support,int * active)198 static inline void amd_pstate_boost_init(unsigned int cpu, int *support,
199 					 int *active)
200 {}
amd_pstate_show_perf_and_freq(unsigned int cpu,int no_rounding)201 static inline void amd_pstate_show_perf_and_freq(unsigned int cpu,
202 						 int no_rounding)
203 {}
204 
205 /* cpuid and cpuinfo helpers  **************************/
206 
cpuid_eax(unsigned int op)207 static inline unsigned int cpuid_eax(unsigned int op) { return 0; };
cpuid_ebx(unsigned int op)208 static inline unsigned int cpuid_ebx(unsigned int op) { return 0; };
cpuid_ecx(unsigned int op)209 static inline unsigned int cpuid_ecx(unsigned int op) { return 0; };
cpuid_edx(unsigned int op)210 static inline unsigned int cpuid_edx(unsigned int op) { return 0; };
211 #endif /* defined(__i386__) || defined(__x86_64__) */
212 
213 /*
214  * CPU State related functions
215  */
216 extern struct bitmask *online_cpus;
217 extern struct bitmask *offline_cpus;
218 
219 void get_cpustate(void);
220 void print_online_cpus(void);
221 void print_offline_cpus(void);
222 void print_speed(unsigned long speed, int no_rounding);
223 
224 #endif /* __CPUPOWERUTILS_HELPERS__ */
225