xref: /linux/tools/power/cpupower/utils/helpers/helpers.h (revision 9dbbc3b9d09d6deba9f3b9e1d5b355032ed46a75)
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 
15 #include "helpers/bitmask.h"
16 #include <cpupower.h>
17 
18 /* Internationalization ****************************/
19 #ifdef NLS
20 
21 #define _(String) gettext(String)
22 #ifndef gettext_noop
23 #define gettext_noop(String) String
24 #endif
25 #define N_(String) gettext_noop(String)
26 
27 #else /* !NLS */
28 
29 #define _(String) String
30 #define N_(String) String
31 
32 #endif
33 /* Internationalization ****************************/
34 
35 extern int run_as_root;
36 extern int base_cpu;
37 extern struct bitmask *cpus_chosen;
38 
39 /* Global verbose (-d) stuff *********************************/
40 /*
41  * define DEBUG via global Makefile variable
42  * Debug output is sent to stderr, do:
43  * cpupower monitor 2>/tmp/debug
44  * to split debug output away from normal output
45 */
46 #ifdef DEBUG
47 extern int be_verbose;
48 
49 #define dprint(fmt, ...) {					\
50 		if (be_verbose) {				\
51 			fprintf(stderr, "%s: " fmt,		\
52 				__func__, ##__VA_ARGS__);	\
53 		}						\
54 	}
55 #else
56 static inline void dprint(const char *fmt, ...) { }
57 #endif
58 extern int be_verbose;
59 /* Global verbose (-v) stuff *********************************/
60 
61 /* cpuid and cpuinfo helpers  **************************/
62 enum cpupower_cpu_vendor {X86_VENDOR_UNKNOWN = 0, X86_VENDOR_INTEL,
63 			  X86_VENDOR_AMD, X86_VENDOR_HYGON, X86_VENDOR_MAX};
64 
65 #define CPUPOWER_CAP_INV_TSC		0x00000001
66 #define CPUPOWER_CAP_APERF		0x00000002
67 #define CPUPOWER_CAP_AMD_CPB		0x00000004
68 #define CPUPOWER_CAP_PERF_BIAS		0x00000008
69 #define CPUPOWER_CAP_HAS_TURBO_RATIO	0x00000010
70 #define CPUPOWER_CAP_IS_SNB		0x00000020
71 #define CPUPOWER_CAP_INTEL_IDA		0x00000040
72 #define CPUPOWER_CAP_AMD_RDPRU		0x00000080
73 #define CPUPOWER_CAP_AMD_HW_PSTATE	0x00000100
74 #define CPUPOWER_CAP_AMD_PSTATEDEF	0x00000200
75 #define CPUPOWER_CAP_AMD_CPB_MSR	0x00000400
76 
77 #define CPUPOWER_AMD_CPBDIS		0x02000000
78 
79 #define MAX_HW_PSTATES 10
80 
81 struct cpupower_cpu_info {
82 	enum cpupower_cpu_vendor vendor;
83 	unsigned int family;
84 	unsigned int model;
85 	unsigned int stepping;
86 	/* CPU capabilities read out from cpuid */
87 	unsigned long long caps;
88 };
89 
90 /* get_cpu_info
91  *
92  * Extract CPU vendor, family, model, stepping info from /proc/cpuinfo
93  *
94  * Returns 0 on success or a negative error code
95  * Only used on x86, below global's struct values are zero/unknown on
96  * other archs
97  */
98 extern int get_cpu_info(struct cpupower_cpu_info *cpu_info);
99 extern struct cpupower_cpu_info cpupower_cpu_info;
100 
101 
102 /* cpuid and cpuinfo helpers  **************************/
103 
104 /* X86 ONLY ****************************************/
105 #if defined(__i386__) || defined(__x86_64__)
106 
107 #include <pci/pci.h>
108 
109 /* Read/Write msr ****************************/
110 extern int read_msr(int cpu, unsigned int idx, unsigned long long *val);
111 extern int write_msr(int cpu, unsigned int idx, unsigned long long val);
112 
113 extern int cpupower_intel_set_perf_bias(unsigned int cpu, unsigned int val);
114 extern int cpupower_intel_get_perf_bias(unsigned int cpu);
115 extern unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu);
116 
117 /* Read/Write msr ****************************/
118 
119 /* PCI stuff ****************************/
120 extern int amd_pci_get_num_boost_states(int *active, int *states);
121 extern struct pci_dev *pci_acc_init(struct pci_access **pacc, int domain,
122 				    int bus, int slot, int func, int vendor,
123 				    int dev);
124 extern struct pci_dev *pci_slot_func_init(struct pci_access **pacc,
125 					      int slot, int func);
126 
127 /* PCI stuff ****************************/
128 
129 /* AMD HW pstate decoding **************************/
130 
131 extern int decode_pstates(unsigned int cpu, int boost_states,
132 			  unsigned long *pstates, int *no);
133 
134 /* AMD HW pstate decoding **************************/
135 
136 extern int cpufreq_has_boost_support(unsigned int cpu, int *support,
137 				     int *active, int * states);
138 /*
139  * CPUID functions returning a single datum
140  */
141 unsigned int cpuid_eax(unsigned int op);
142 unsigned int cpuid_ebx(unsigned int op);
143 unsigned int cpuid_ecx(unsigned int op);
144 unsigned int cpuid_edx(unsigned int op);
145 
146 /* cpuid and cpuinfo helpers  **************************/
147 /* X86 ONLY ********************************************/
148 #else
149 static inline int decode_pstates(unsigned int cpu, int boost_states,
150 				 unsigned long *pstates, int *no)
151 { return -1; };
152 
153 static inline int read_msr(int cpu, unsigned int idx, unsigned long long *val)
154 { return -1; };
155 static inline int write_msr(int cpu, unsigned int idx, unsigned long long val)
156 { return -1; };
157 static inline int cpupower_intel_set_perf_bias(unsigned int cpu, unsigned int val)
158 { return -1; };
159 static inline int cpupower_intel_get_perf_bias(unsigned int cpu)
160 { return -1; };
161 static inline unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu)
162 { return 0; };
163 
164 /* Read/Write msr ****************************/
165 
166 static inline int cpufreq_has_boost_support(unsigned int cpu, int *support,
167 					    int *active, int * states)
168 { return -1; }
169 
170 /* cpuid and cpuinfo helpers  **************************/
171 
172 static inline unsigned int cpuid_eax(unsigned int op) { return 0; };
173 static inline unsigned int cpuid_ebx(unsigned int op) { return 0; };
174 static inline unsigned int cpuid_ecx(unsigned int op) { return 0; };
175 static inline unsigned int cpuid_edx(unsigned int op) { return 0; };
176 #endif /* defined(__i386__) || defined(__x86_64__) */
177 
178 /*
179  * CPU State related functions
180  */
181 extern struct bitmask *online_cpus;
182 extern struct bitmask *offline_cpus;
183 
184 void get_cpustate(void);
185 void print_online_cpus(void);
186 void print_offline_cpus(void);
187 
188 #endif /* __CPUPOWERUTILS_HELPERS__ */
189