blob: e99921ac76ff9bb51de1e05dc1c570b9b3c79111 [file] [log] [blame]
Peter Stugedad1e302008-11-22 17:13:36 +00001/*
2 * This file is part of msrtool.
3 *
4 * Copyright (c) 2008 Peter Stuge <peter@stuge.se>
Stefan Reinauer37f39352009-09-01 10:03:01 +00005 * Copyright (c) 2009 coresystems GmbH
Peter Stugedad1e302008-11-22 17:13:36 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
Paul Menzela46a7122013-02-23 18:37:27 +010018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Peter Stugedad1e302008-11-22 17:13:36 +000019 */
20
21#ifndef MSRTOOL_H
22#define MSRTOOL_H
23
24#include <stdio.h>
25#include <stdint.h>
Stefan Reinauer37f39352009-09-01 10:03:01 +000026#if (defined(__MACH__) && defined(__APPLE__))
Stefan Reinauer9018b6e2011-03-14 09:08:27 +000027/* DirectHW is available here: http://www.coreboot.org/DirectHW */
Stefan Reinauer37f39352009-09-01 10:03:01 +000028#define __DARWIN__
Stefan Reinauer9018b6e2011-03-14 09:08:27 +000029#include <DirectHW/DirectHW.h>
Stefan Reinauer37f39352009-09-01 10:03:01 +000030#endif
Andriy Gapond80e57c2009-11-28 05:21:42 +000031#if defined(__FreeBSD__)
32#include <sys/ioctl.h>
33#include <sys/cpuctl.h>
34#endif
Peter Stuge0924dee2008-11-25 02:03:16 +000035#include <pci/pci.h>
Peter Stugedad1e302008-11-22 17:13:36 +000036
37#define HEXCHARS "0123456789abcdefABCDEF"
38
39enum {
40 MSRTYPE_RDONLY,
41 MSRTYPE_RDWR,
42 MSRTYPE_WRONLY,
43 MSRTYPE_EOT
44} MsrTypes;
45
46enum {
47 PRESENT_RSVD,
48 PRESENT_DEC,
49 PRESENT_BIN,
50 PRESENT_OCT,
51 PRESENT_HEX,
52 PRESENT_HEXDEC
53} PresentTypes;
54
55struct msr {
56 uint32_t hi;
57 uint32_t lo;
58};
59
60struct msrbitvalues {
61 const struct msr value;
62 const char *text;
63};
64
65struct msrbits {
66 const uint8_t start;
67 const uint8_t size;
68 const char *name;
69 const char *desc;
70 const uint8_t present;
71 const struct msrbitvalues bitval[32];
72};
73
74struct msrdef {
75 const uint32_t addr;
76 const uint8_t type;
77 const struct msr resetval;
78 const char *symbol;
79 const char *desc;
80 const struct msrbits bits[65];
81};
82
83#define MSR1(lo) { 0, (lo) }
84#define MSR2(hi,lo) { (hi), (lo) }
85
86#define BITVAL_EOT .text = NULL
87#define BITVAL_ISEOT(bv) (NULL == (bv).text)
88
89#define BITS_EOT .size = 0
90#define BITS_ISEOT(b) (0 == (b).size)
91
92#define MSR_EOT .type = MSRTYPE_EOT
93#define MSR_ISEOT(m) (MSRTYPE_EOT == (m).type)
94
95#define NOBITS {{ BITVAL_EOT }}
96#define RESERVED "RSVD", "Reserved", PRESENT_HEXDEC, NOBITS
97
98#define MAX_CORES 8
99
Anton Kochkov59b36f12012-07-21 07:29:48 +0400100typedef enum {
101 VENDOR_INTEL = 1,
102 VENDOR_AMD = 2,
103} vendor_t;
104
105struct cpuid_t {
106 uint8_t family;
107 uint8_t model;
108 uint8_t stepping;
109 uint8_t ext_family;
110 uint8_t ext_model;
111 vendor_t vendor;
112};
113
Peter Stugedad1e302008-11-22 17:13:36 +0000114struct targetdef {
115 const char *name;
116 const char *prettyname;
Anton Kochkov59b36f12012-07-21 07:29:48 +0400117 int (*probe)(const struct targetdef *target, const struct cpuid_t *id);
Peter Stugedad1e302008-11-22 17:13:36 +0000118 const struct msrdef *msrs;
119};
120
121#define TARGET_EOT .name = NULL
122#define TARGET_ISEOT(t) (NULL == (t).name)
123
124
125enum SysModes {
126 SYS_RDONLY = 0,
127 SYS_WRONLY,
128 SYS_RDWR
129};
130
131struct sysdef {
132 const char *name;
133 const char *prettyname;
134 int (*probe)(const struct sysdef *system);
135 int (*open)(uint8_t cpu, enum SysModes mode);
136 int (*close)(uint8_t cpu);
137 int (*rdmsr)(uint8_t cpu, uint32_t addr, struct msr *val);
138};
139
140#define SYSTEM_EOT .name = NULL
141#define SYSTEM_ISEOT(s) (NULL == (s).name)
142
Peter Stugedad1e302008-11-22 17:13:36 +0000143extern const struct sysdef *sys;
144
145extern uint8_t targets_found;
146extern const struct targetdef **targets;
147
148extern uint8_t reserved, verbose, quiet;
149
Peter Stuge0924dee2008-11-25 02:03:16 +0000150extern struct pci_access *pacc;
151
Peter Stugedad1e302008-11-22 17:13:36 +0000152#define printf_quiet(x...) do { if (!quiet) fprintf(stderr,x); } while(0)
153#define printf_verbose(x...) do { if (verbose && !quiet) fprintf(stderr,x); } while(0)
154
155#define SYSERROR(call, addr) do { \
156 const struct msrdef *m = findmsrdef(addr); \
157 if (m) \
158 fprintf(stderr, "%s: " #call "(0x%08x) %s: %s\n", __func__, addr, m->symbol, strerror(errno)); \
159 else \
160 fprintf(stderr, "%s: " #call "(0x%08x): %s\n", __func__, addr, strerror(errno)); \
161} while (0);
162
163/* sys.c */
164struct cpuid_t *cpuid(void);
Peter Stuge0924dee2008-11-25 02:03:16 +0000165struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device);
Peter Stugedad1e302008-11-22 17:13:36 +0000166
167/* msrutils.c */
168void hexprint(FILE *f, const struct msr val, const uint8_t bits);
169int msr_eq(const struct msr a, const struct msr b);
170struct msr msr_shl(const struct msr a, const uint8_t bits);
171struct msr msr_shr(const struct msr a, const uint8_t bits);
172void msr_and(struct msr *a, const struct msr b);
173const struct msrdef *findmsrdef(const uint32_t addr);
Marc Jones8f210762009-03-08 04:37:39 +0000174uint32_t msraddrbyname(const char *name);
Peter Stugedad1e302008-11-22 17:13:36 +0000175void dumpmsrdefs(const struct targetdef *t);
176int dumpmsrdefsvals(FILE *f, const struct targetdef *t, const uint8_t cpu);
Peter Stuge34f29072010-01-17 18:33:53 +0000177uint8_t str2msr(char *str, struct msr *msr, char **endptr);
Peter Stugedad1e302008-11-22 17:13:36 +0000178void decodemsr(const uint8_t cpu, const uint32_t addr, const struct msr val);
179uint8_t diff_msr(FILE *fout, const uint32_t addr, const struct msr a, const struct msr b);
180
181
182
183/** system externs **/
184
185/* linux.c */
186extern int linux_probe(const struct sysdef *system);
187extern int linux_open(uint8_t cpu, enum SysModes mode);
188extern int linux_close(uint8_t cpu);
189extern int linux_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val);
190
Stefan Reinauer37f39352009-09-01 10:03:01 +0000191/* darwin.c */
192extern int darwin_probe(const struct sysdef *system);
193extern int darwin_open(uint8_t cpu, enum SysModes mode);
194extern int darwin_close(uint8_t cpu);
195extern int darwin_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val);
Peter Stugedad1e302008-11-22 17:13:36 +0000196
Andriy Gapond80e57c2009-11-28 05:21:42 +0000197/* freebsd.c */
198extern int freebsd_probe(const struct sysdef *system);
199extern int freebsd_open(uint8_t cpu, enum SysModes mode);
200extern int freebsd_close(uint8_t cpu);
201extern int freebsd_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val);
202
Peter Stugedad1e302008-11-22 17:13:36 +0000203/** target externs **/
204
Nils Jacobs58a901f2010-01-15 10:06:39 +0000205/* geodegx2.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400206extern int geodegx2_probe(const struct targetdef *t, const struct cpuid_t *id);
Nils Jacobs58a901f2010-01-15 10:06:39 +0000207extern const struct msrdef geodegx2_msrs[];
208
Peter Stugedad1e302008-11-22 17:13:36 +0000209/* geodelx.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400210extern int geodelx_probe(const struct targetdef *t, const struct cpuid_t *id);
Peter Stugedad1e302008-11-22 17:13:36 +0000211extern const struct msrdef geodelx_msrs[];
212
213/* cs5536.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400214extern int cs5536_probe(const struct targetdef *t, const struct cpuid_t *id);
Peter Stugedad1e302008-11-22 17:13:36 +0000215extern const struct msrdef cs5536_msrs[];
216
Marc Jones8f210762009-03-08 04:37:39 +0000217/* k8.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400218extern int k8_probe(const struct targetdef *t, const struct cpuid_t *id);
Marc Jones8f210762009-03-08 04:37:39 +0000219extern const struct msrdef k8_msrs[];
220
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400221/* intel_pentium3_early.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400222extern int intel_pentium3_early_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400223extern const struct msrdef intel_pentium3_early_msrs[];
224
225/* intel_pentium3.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400226extern int intel_pentium3_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400227extern const struct msrdef intel_pentium3_msrs[];
228
229/* intel_core1.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400230extern int intel_core1_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400231extern const struct msrdef intel_core1_msrs[];
232
233/* intel_core2_early.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400234extern int intel_core2_early_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400235extern const struct msrdef intel_core2_early_msrs[];
236
237/* intel_core2_later.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400238extern int intel_core2_later_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400239extern const struct msrdef intel_core2_later_msrs[];
240
241/* intel_pentium4_early.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400242extern int intel_pentium4_early_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400243extern const struct msrdef intel_pentium4_early_msrs[];
244
245/* intel_pentium4_later.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400246extern int intel_pentium4_later_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400247extern const struct msrdef intel_pentium4_later_msrs[];
248
Anton Kochkov54c07a62012-07-04 07:35:45 +0400249/* intel_nehalem.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400250extern int intel_nehalem_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov54c07a62012-07-04 07:35:45 +0400251extern const struct msrdef intel_nehalem_msrs[];
252
Olivier Langloisccc7d1f2013-06-03 01:30:25 -0400253/* intel_atom.c */
254extern int intel_atom_probe(const struct targetdef *t, const struct cpuid_t *id);
255extern const struct msrdef intel_atom_msrs[];
256
Peter Stugedad1e302008-11-22 17:13:36 +0000257#endif /* MSRTOOL_H */