blob: 0441e3f1d48966649df139313889b4ea6ada408d [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
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
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
100struct targetdef {
101 const char *name;
102 const char *prettyname;
103 int (*probe)(const struct targetdef *target);
104 const struct msrdef *msrs;
105};
106
107#define TARGET_EOT .name = NULL
108#define TARGET_ISEOT(t) (NULL == (t).name)
109
110
111enum SysModes {
112 SYS_RDONLY = 0,
113 SYS_WRONLY,
114 SYS_RDWR
115};
116
117struct sysdef {
118 const char *name;
119 const char *prettyname;
120 int (*probe)(const struct sysdef *system);
121 int (*open)(uint8_t cpu, enum SysModes mode);
122 int (*close)(uint8_t cpu);
123 int (*rdmsr)(uint8_t cpu, uint32_t addr, struct msr *val);
124};
125
126#define SYSTEM_EOT .name = NULL
127#define SYSTEM_ISEOT(s) (NULL == (s).name)
128
129
130struct cpuid_t {
131 uint8_t family;
132 uint8_t model;
133 uint8_t stepping;
134 uint8_t ext_family;
135 uint8_t ext_model;
136};
137
138
139extern const struct sysdef *sys;
140
141extern uint8_t targets_found;
142extern const struct targetdef **targets;
143
144extern uint8_t reserved, verbose, quiet;
145
Peter Stuge0924dee2008-11-25 02:03:16 +0000146extern struct pci_access *pacc;
147
Peter Stugedad1e302008-11-22 17:13:36 +0000148#define printf_quiet(x...) do { if (!quiet) fprintf(stderr,x); } while(0)
149#define printf_verbose(x...) do { if (verbose && !quiet) fprintf(stderr,x); } while(0)
150
151#define SYSERROR(call, addr) do { \
152 const struct msrdef *m = findmsrdef(addr); \
153 if (m) \
154 fprintf(stderr, "%s: " #call "(0x%08x) %s: %s\n", __func__, addr, m->symbol, strerror(errno)); \
155 else \
156 fprintf(stderr, "%s: " #call "(0x%08x): %s\n", __func__, addr, strerror(errno)); \
157} while (0);
158
159/* sys.c */
160struct cpuid_t *cpuid(void);
Peter Stuge0924dee2008-11-25 02:03:16 +0000161struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device);
Peter Stugedad1e302008-11-22 17:13:36 +0000162
163/* msrutils.c */
164void hexprint(FILE *f, const struct msr val, const uint8_t bits);
165int msr_eq(const struct msr a, const struct msr b);
166struct msr msr_shl(const struct msr a, const uint8_t bits);
167struct msr msr_shr(const struct msr a, const uint8_t bits);
168void msr_and(struct msr *a, const struct msr b);
169const struct msrdef *findmsrdef(const uint32_t addr);
Marc Jones8f210762009-03-08 04:37:39 +0000170uint32_t msraddrbyname(const char *name);
Peter Stugedad1e302008-11-22 17:13:36 +0000171void dumpmsrdefs(const struct targetdef *t);
172int dumpmsrdefsvals(FILE *f, const struct targetdef *t, const uint8_t cpu);
Peter Stuge34f29072010-01-17 18:33:53 +0000173uint8_t str2msr(char *str, struct msr *msr, char **endptr);
Peter Stugedad1e302008-11-22 17:13:36 +0000174void decodemsr(const uint8_t cpu, const uint32_t addr, const struct msr val);
175uint8_t diff_msr(FILE *fout, const uint32_t addr, const struct msr a, const struct msr b);
176
177
178
179/** system externs **/
180
181/* linux.c */
182extern int linux_probe(const struct sysdef *system);
183extern int linux_open(uint8_t cpu, enum SysModes mode);
184extern int linux_close(uint8_t cpu);
185extern int linux_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val);
186
Stefan Reinauer37f39352009-09-01 10:03:01 +0000187/* darwin.c */
188extern int darwin_probe(const struct sysdef *system);
189extern int darwin_open(uint8_t cpu, enum SysModes mode);
190extern int darwin_close(uint8_t cpu);
191extern int darwin_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val);
Peter Stugedad1e302008-11-22 17:13:36 +0000192
Andriy Gapond80e57c2009-11-28 05:21:42 +0000193/* freebsd.c */
194extern int freebsd_probe(const struct sysdef *system);
195extern int freebsd_open(uint8_t cpu, enum SysModes mode);
196extern int freebsd_close(uint8_t cpu);
197extern int freebsd_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val);
198
Peter Stugedad1e302008-11-22 17:13:36 +0000199/** target externs **/
200
Nils Jacobs58a901f2010-01-15 10:06:39 +0000201/* geodegx2.c */
202extern int geodegx2_probe(const struct targetdef *t);
203extern const struct msrdef geodegx2_msrs[];
204
Peter Stugedad1e302008-11-22 17:13:36 +0000205/* geodelx.c */
206extern int geodelx_probe(const struct targetdef *t);
207extern const struct msrdef geodelx_msrs[];
208
209/* cs5536.c */
210extern int cs5536_probe(const struct targetdef *t);
211extern const struct msrdef cs5536_msrs[];
212
Marc Jones8f210762009-03-08 04:37:39 +0000213/* k8.c */
214extern int k8_probe(const struct targetdef *t);
215extern const struct msrdef k8_msrs[];
216
Peter Stugedad1e302008-11-22 17:13:36 +0000217#endif /* MSRTOOL_H */