1 /*
2 * (C) 2010,2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
3 *
4 * Licensed under the terms of the GNU GPL License version 2.
5 *
6 * Miscellaneous helpers which do not fit or are worth
7 * to put into separate headers
8 */
9
10 #ifndef __CPUPOWERUTILS_HELPERS__
11 #define __CPUPOWERUTILS_HELPERS__
12
13 #include <libintl.h>
14 #include <locale.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 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
dprint(const char * fmt,...)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_MAX};
64
65 #define CPUPOWER_CAP_INV_TSC 0x00000001
66 #define CPUPOWER_CAP_APERF 0x00000002
67 #define CPUPOWER_CAP_AMD_CBP 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
73 #define MAX_HW_PSTATES 10
74
75 struct cpupower_cpu_info {
76 enum cpupower_cpu_vendor vendor;
77 unsigned int family;
78 unsigned int model;
79 unsigned int stepping;
80 /* CPU capabilities read out from cpuid */
81 unsigned long long caps;
82 };
83
84 /* get_cpu_info
85 *
86 * Extract CPU vendor, family, model, stepping info from /proc/cpuinfo
87 *
88 * Returns 0 on success or a negativ error code
89 * Only used on x86, below global's struct values are zero/unknown on
90 * other archs
91 */
92 extern int get_cpu_info(unsigned int cpu, struct cpupower_cpu_info *cpu_info);
93 extern struct cpupower_cpu_info cpupower_cpu_info;
94 /* cpuid and cpuinfo helpers **************************/
95
96 /* X86 ONLY ****************************************/
97 #if defined(__i386__) || defined(__x86_64__)
98
99 #include <pci/pci.h>
100
101 /* Read/Write msr ****************************/
102 extern int read_msr(int cpu, unsigned int idx, unsigned long long *val);
103 extern int write_msr(int cpu, unsigned int idx, unsigned long long val);
104
105 extern int msr_intel_set_perf_bias(unsigned int cpu, unsigned int val);
106 extern int msr_intel_get_perf_bias(unsigned int cpu);
107 extern unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu);
108
109 /* Read/Write msr ****************************/
110
111 /* PCI stuff ****************************/
112 extern int amd_pci_get_num_boost_states(int *active, int *states);
113 extern struct pci_dev *pci_acc_init(struct pci_access **pacc, int domain,
114 int bus, int slot, int func, int vendor,
115 int dev);
116 extern struct pci_dev *pci_slot_func_init(struct pci_access **pacc,
117 int slot, int func);
118
119 /* PCI stuff ****************************/
120
121 /* AMD HW pstate decoding **************************/
122
123 extern int decode_pstates(unsigned int cpu, unsigned int cpu_family,
124 int boost_states, unsigned long *pstates, int *no);
125
126 /* AMD HW pstate decoding **************************/
127
128 extern int cpufreq_has_boost_support(unsigned int cpu, int *support,
129 int *active, int * states);
130 /*
131 * CPUID functions returning a single datum
132 */
133 unsigned int cpuid_eax(unsigned int op);
134 unsigned int cpuid_ebx(unsigned int op);
135 unsigned int cpuid_ecx(unsigned int op);
136 unsigned int cpuid_edx(unsigned int op);
137
138 /* cpuid and cpuinfo helpers **************************/
139 /* X86 ONLY ********************************************/
140 #else
decode_pstates(unsigned int cpu,unsigned int cpu_family,int boost_states,unsigned long * pstates,int * no)141 static inline int decode_pstates(unsigned int cpu, unsigned int cpu_family,
142 int boost_states, unsigned long *pstates,
143 int *no)
144 { return -1; };
145
read_msr(int cpu,unsigned int idx,unsigned long long * val)146 static inline int read_msr(int cpu, unsigned int idx, unsigned long long *val)
147 { return -1; };
write_msr(int cpu,unsigned int idx,unsigned long long val)148 static inline int write_msr(int cpu, unsigned int idx, unsigned long long val)
149 { return -1; };
msr_intel_set_perf_bias(unsigned int cpu,unsigned int val)150 static inline int msr_intel_set_perf_bias(unsigned int cpu, unsigned int val)
151 { return -1; };
msr_intel_get_perf_bias(unsigned int cpu)152 static inline int msr_intel_get_perf_bias(unsigned int cpu)
153 { return -1; };
msr_intel_get_turbo_ratio(unsigned int cpu)154 static inline unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu)
155 { return 0; };
156
157 /* Read/Write msr ****************************/
158
cpufreq_has_boost_support(unsigned int cpu,int * support,int * active,int * states)159 static inline int cpufreq_has_boost_support(unsigned int cpu, int *support,
160 int *active, int * states)
161 { return -1; }
162
163 /* cpuid and cpuinfo helpers **************************/
164
cpuid_eax(unsigned int op)165 static inline unsigned int cpuid_eax(unsigned int op) { return 0; };
cpuid_ebx(unsigned int op)166 static inline unsigned int cpuid_ebx(unsigned int op) { return 0; };
cpuid_ecx(unsigned int op)167 static inline unsigned int cpuid_ecx(unsigned int op) { return 0; };
cpuid_edx(unsigned int op)168 static inline unsigned int cpuid_edx(unsigned int op) { return 0; };
169 #endif /* defined(__i386__) || defined(__x86_64__) */
170
171 #endif /* __CPUPOWERUTILS_HELPERS__ */
172