util/: Replace GPLv2 boiler plate with SPDX header
[coreboot.git] / util / msrtool / msrtool.h
blobd1f2eb9a8271bdac8e7d9b8fa4ee7a3d8e9b8ac3
1 /* This file is part of msrtool. */
2 /* SPDX-License-Identifier: GPL-2.0-only */
4 #ifndef MSRTOOL_H
5 #define MSRTOOL_H
7 #include <stdio.h>
8 #include <stdint.h>
9 #if (defined(__MACH__) && defined(__APPLE__))
10 /* DirectHW is available here: https://www.coreboot.org/DirectHW */
11 #define __DARWIN__
12 #include <DirectHW/DirectHW.h>
13 #endif
14 #if defined(__FreeBSD__)
15 #include <sys/ioctl.h>
16 #include <sys/cpuctl.h>
17 #endif
18 #include <pci/pci.h>
20 #define HEXCHARS "0123456789abcdefABCDEF"
22 enum {
23 MSRTYPE_RDONLY,
24 MSRTYPE_RDWR,
25 MSRTYPE_WRONLY,
26 MSRTYPE_EOT
27 } MsrTypes;
29 enum {
30 PRESENT_RSVD,
31 PRESENT_DEC,
32 PRESENT_BIN,
33 PRESENT_OCT,
34 PRESENT_HEX,
35 PRESENT_HEXDEC,
36 PRESENT_STR,
37 } PresentTypes;
39 struct msr {
40 uint32_t hi;
41 uint32_t lo;
44 struct msrbitvalues {
45 const struct msr value;
46 const char *text;
49 struct msrbits {
50 const uint8_t start;
51 const uint8_t size;
52 const char *name;
53 const char *desc;
54 const uint8_t present;
55 const struct msrbitvalues bitval[32];
58 struct msrdef {
59 const uint32_t addr;
60 const uint8_t type;
61 const struct msr resetval;
62 const char *symbol;
63 const char *desc;
64 const struct msrbits bits[65];
67 #define MSR1(lo) { 0, (lo) }
68 #define MSR2(hi,lo) { (hi), (lo) }
70 #define BITVAL_EOT .text = NULL
71 #define BITVAL_ISEOT(bv) (NULL == (bv).text)
73 #define BITS_EOT .size = 0
74 #define BITS_ISEOT(b) (0 == (b).size)
76 #define MSR_EOT .type = MSRTYPE_EOT
77 #define MSR_ISEOT(m) (MSRTYPE_EOT == (m).type)
79 #define NOBITS {{ BITVAL_EOT }}
80 #define RESERVED "RSVD", "Reserved", PRESENT_HEXDEC, NOBITS
82 #define MAX_CORES 8
84 typedef enum {
85 VENDOR_INTEL = 0x756e6547,
86 VENDOR_AMD = 0x68747541,
87 VENDOR_CENTAUR = 0x746e6543,
88 } vendor_t;
90 struct cpuid_t {
91 uint8_t family;
92 uint8_t model;
93 uint8_t stepping;
94 uint8_t ext_family;
95 uint8_t ext_model;
96 vendor_t vendor;
99 struct targetdef {
100 const char *name;
101 const char *prettyname;
102 int (*probe)(const struct targetdef *target, const struct cpuid_t *id);
103 const struct msrdef *msrs;
106 #define TARGET_EOT .name = NULL
107 #define TARGET_ISEOT(t) (NULL == (t).name)
110 enum SysModes {
111 SYS_RDONLY = 0,
112 SYS_WRONLY,
113 SYS_RDWR
116 struct sysdef {
117 const char *name;
118 const char *prettyname;
119 int (*probe)(const struct sysdef *system);
120 int (*open)(uint8_t cpu, enum SysModes mode);
121 int (*close)(uint8_t cpu);
122 int (*rdmsr)(uint8_t cpu, uint32_t addr, struct msr *val);
125 #define SYSTEM_EOT .name = NULL
126 #define SYSTEM_ISEOT(s) (NULL == (s).name)
128 extern const struct sysdef *sys;
130 extern uint8_t targets_found;
131 extern const struct targetdef **targets;
133 extern uint8_t reserved, verbose, quiet;
135 extern struct pci_access *pacc;
137 #define printf_quiet(x...) do { if (!quiet) fprintf(stderr,x); } while(0)
138 #define printf_verbose(x...) do { if (verbose && !quiet) fprintf(stderr,x); } while(0)
140 #define SYSERROR(call, addr) do { \
141 const struct msrdef *m = findmsrdef(addr); \
142 if (m) \
143 fprintf(stderr, "%s: " #call "(0x%08x) %s: %s\n", __func__, addr, m->symbol, strerror(errno)); \
144 else \
145 fprintf(stderr, "%s: " #call "(0x%08x): %s\n", __func__, addr, strerror(errno)); \
146 } while (0);
148 /* sys.c */
149 struct cpuid_t *cpuid(void);
150 struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device);
152 /* msrutils.c */
153 void hexprint(FILE *f, const struct msr val, const uint8_t bits);
154 void strprint(FILE *f, const struct msr val, const uint8_t bits);
155 int msr_eq(const struct msr a, const struct msr b);
156 struct msr msr_shl(const struct msr a, const uint8_t bits);
157 struct msr msr_shr(const struct msr a, const uint8_t bits);
158 void msr_and(struct msr *a, const struct msr b);
159 const struct msrdef *findmsrdef(const uint32_t addr);
160 uint32_t msraddrbyname(const char *name);
161 void dumpmsrdefs(const struct targetdef *t);
162 int dumpmsrdefsvals(FILE *f, const struct targetdef *t, const uint8_t cpu);
163 uint8_t str2msr(char *str, struct msr *msr, char **endptr);
164 void decodemsr(const uint8_t cpu, const uint32_t addr, const struct msr val);
165 uint8_t diff_msr(FILE *fout, const uint32_t addr, const struct msr a, const struct msr b);
169 /** system externs **/
171 /* linux.c */
172 extern int linux_probe(const struct sysdef *system);
173 extern int linux_open(uint8_t cpu, enum SysModes mode);
174 extern int linux_close(uint8_t cpu);
175 extern int linux_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val);
177 /* darwin.c */
178 extern int darwin_probe(const struct sysdef *system);
179 extern int darwin_open(uint8_t cpu, enum SysModes mode);
180 extern int darwin_close(uint8_t cpu);
181 extern int darwin_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val);
183 /* freebsd.c */
184 extern int freebsd_probe(const struct sysdef *system);
185 extern int freebsd_open(uint8_t cpu, enum SysModes mode);
186 extern int freebsd_close(uint8_t cpu);
187 extern int freebsd_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val);
189 /** target externs **/
191 /* geodegx2.c */
192 extern int geodegx2_probe(const struct targetdef *t, const struct cpuid_t *id);
193 extern const struct msrdef geodegx2_msrs[];
195 /* geodelx.c */
196 extern int geodelx_probe(const struct targetdef *t, const struct cpuid_t *id);
197 extern const struct msrdef geodelx_msrs[];
199 /* cs5536.c */
200 extern int cs5536_probe(const struct targetdef *t, const struct cpuid_t *id);
201 extern const struct msrdef cs5536_msrs[];
203 /* k8.c */
204 extern int k8_probe(const struct targetdef *t, const struct cpuid_t *id);
205 extern const struct msrdef k8_msrs[];
207 /* via_c7.c */
208 extern int via_c7_probe(const struct targetdef *t, const struct cpuid_t *id);
209 extern const struct msrdef via_c7_msrs[];
211 /* intel_pentium3_early.c */
212 extern int intel_pentium3_early_probe(const struct targetdef *t, const struct cpuid_t *id);
213 extern const struct msrdef intel_pentium3_early_msrs[];
215 /* intel_pentium3.c */
216 extern int intel_pentium3_probe(const struct targetdef *t, const struct cpuid_t *id);
217 extern const struct msrdef intel_pentium3_msrs[];
219 /* intel_core1.c */
220 extern int intel_core1_probe(const struct targetdef *t, const struct cpuid_t *id);
221 extern const struct msrdef intel_core1_msrs[];
223 /* intel_core2_early.c */
224 extern int intel_core2_early_probe(const struct targetdef *t, const struct cpuid_t *id);
225 extern const struct msrdef intel_core2_early_msrs[];
227 /* intel_core2_later.c */
228 extern int intel_core2_later_probe(const struct targetdef *t, const struct cpuid_t *id);
229 extern const struct msrdef intel_core2_later_msrs[];
231 /* intel_pentium4_early.c */
232 extern int intel_pentium4_early_probe(const struct targetdef *t, const struct cpuid_t *id);
233 extern const struct msrdef intel_pentium4_early_msrs[];
235 /* intel_pentium4_later.c */
236 extern int intel_pentium4_later_probe(const struct targetdef *t, const struct cpuid_t *id);
237 extern const struct msrdef intel_pentium4_later_msrs[];
239 /* intel_pentium_d.c */
240 extern int intel_pentium_d_probe(const struct targetdef *t, const struct cpuid_t *id);
241 extern const struct msrdef intel_pentium_d_msrs[];
243 /* intel_nehalem.c */
244 extern int intel_nehalem_probe(const struct targetdef *t, const struct cpuid_t *id);
245 extern const struct msrdef intel_nehalem_msrs[];
247 /* intel_atom.c */
248 extern int intel_atom_probe(const struct targetdef *t, const struct cpuid_t *id);
249 extern const struct msrdef intel_atom_msrs[];
251 #endif /* MSRTOOL_H */