blob: fe7f0f043486139c0dc0f7ca6ca0b3e14f827143 [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.
Peter Stugedad1e302008-11-22 17:13:36 +000015 */
16
17#ifndef MSRTOOL_H
18#define MSRTOOL_H
19
20#include <stdio.h>
21#include <stdint.h>
Stefan Reinauer37f39352009-09-01 10:03:01 +000022#if (defined(__MACH__) && defined(__APPLE__))
Stefan Reinauer9018b6e2011-03-14 09:08:27 +000023/* DirectHW is available here: http://www.coreboot.org/DirectHW */
Stefan Reinauer37f39352009-09-01 10:03:01 +000024#define __DARWIN__
Stefan Reinauer9018b6e2011-03-14 09:08:27 +000025#include <DirectHW/DirectHW.h>
Stefan Reinauer37f39352009-09-01 10:03:01 +000026#endif
Andriy Gapond80e57c2009-11-28 05:21:42 +000027#if defined(__FreeBSD__)
28#include <sys/ioctl.h>
29#include <sys/cpuctl.h>
30#endif
Peter Stuge0924dee2008-11-25 02:03:16 +000031#include <pci/pci.h>
Peter Stugedad1e302008-11-22 17:13:36 +000032
33#define HEXCHARS "0123456789abcdefABCDEF"
34
35enum {
36 MSRTYPE_RDONLY,
37 MSRTYPE_RDWR,
38 MSRTYPE_WRONLY,
39 MSRTYPE_EOT
40} MsrTypes;
41
42enum {
43 PRESENT_RSVD,
44 PRESENT_DEC,
45 PRESENT_BIN,
46 PRESENT_OCT,
47 PRESENT_HEX,
48 PRESENT_HEXDEC
49} PresentTypes;
50
51struct msr {
52 uint32_t hi;
53 uint32_t lo;
54};
55
56struct msrbitvalues {
57 const struct msr value;
58 const char *text;
59};
60
61struct msrbits {
62 const uint8_t start;
63 const uint8_t size;
64 const char *name;
65 const char *desc;
66 const uint8_t present;
67 const struct msrbitvalues bitval[32];
68};
69
70struct msrdef {
71 const uint32_t addr;
72 const uint8_t type;
73 const struct msr resetval;
74 const char *symbol;
75 const char *desc;
76 const struct msrbits bits[65];
77};
78
79#define MSR1(lo) { 0, (lo) }
80#define MSR2(hi,lo) { (hi), (lo) }
81
82#define BITVAL_EOT .text = NULL
83#define BITVAL_ISEOT(bv) (NULL == (bv).text)
84
85#define BITS_EOT .size = 0
86#define BITS_ISEOT(b) (0 == (b).size)
87
88#define MSR_EOT .type = MSRTYPE_EOT
89#define MSR_ISEOT(m) (MSRTYPE_EOT == (m).type)
90
91#define NOBITS {{ BITVAL_EOT }}
92#define RESERVED "RSVD", "Reserved", PRESENT_HEXDEC, NOBITS
93
94#define MAX_CORES 8
95
Anton Kochkov59b36f12012-07-21 07:29:48 +040096typedef enum {
97 VENDOR_INTEL = 1,
98 VENDOR_AMD = 2,
99} vendor_t;
100
101struct cpuid_t {
102 uint8_t family;
103 uint8_t model;
104 uint8_t stepping;
105 uint8_t ext_family;
106 uint8_t ext_model;
107 vendor_t vendor;
108};
109
Peter Stugedad1e302008-11-22 17:13:36 +0000110struct targetdef {
111 const char *name;
112 const char *prettyname;
Anton Kochkov59b36f12012-07-21 07:29:48 +0400113 int (*probe)(const struct targetdef *target, const struct cpuid_t *id);
Peter Stugedad1e302008-11-22 17:13:36 +0000114 const struct msrdef *msrs;
115};
116
117#define TARGET_EOT .name = NULL
118#define TARGET_ISEOT(t) (NULL == (t).name)
119
120
121enum SysModes {
122 SYS_RDONLY = 0,
123 SYS_WRONLY,
124 SYS_RDWR
125};
126
127struct sysdef {
128 const char *name;
129 const char *prettyname;
130 int (*probe)(const struct sysdef *system);
131 int (*open)(uint8_t cpu, enum SysModes mode);
132 int (*close)(uint8_t cpu);
133 int (*rdmsr)(uint8_t cpu, uint32_t addr, struct msr *val);
134};
135
136#define SYSTEM_EOT .name = NULL
137#define SYSTEM_ISEOT(s) (NULL == (s).name)
138
Peter Stugedad1e302008-11-22 17:13:36 +0000139extern 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 */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400202extern int geodegx2_probe(const struct targetdef *t, const struct cpuid_t *id);
Nils Jacobs58a901f2010-01-15 10:06:39 +0000203extern const struct msrdef geodegx2_msrs[];
204
Peter Stugedad1e302008-11-22 17:13:36 +0000205/* geodelx.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400206extern int geodelx_probe(const struct targetdef *t, const struct cpuid_t *id);
Peter Stugedad1e302008-11-22 17:13:36 +0000207extern const struct msrdef geodelx_msrs[];
208
209/* cs5536.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400210extern int cs5536_probe(const struct targetdef *t, const struct cpuid_t *id);
Peter Stugedad1e302008-11-22 17:13:36 +0000211extern const struct msrdef cs5536_msrs[];
212
Marc Jones8f210762009-03-08 04:37:39 +0000213/* k8.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400214extern int k8_probe(const struct targetdef *t, const struct cpuid_t *id);
Marc Jones8f210762009-03-08 04:37:39 +0000215extern const struct msrdef k8_msrs[];
216
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400217/* intel_pentium3_early.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400218extern int intel_pentium3_early_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400219extern const struct msrdef intel_pentium3_early_msrs[];
220
221/* intel_pentium3.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400222extern int intel_pentium3_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400223extern const struct msrdef intel_pentium3_msrs[];
224
225/* intel_core1.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400226extern int intel_core1_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400227extern const struct msrdef intel_core1_msrs[];
228
229/* intel_core2_early.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400230extern int intel_core2_early_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400231extern const struct msrdef intel_core2_early_msrs[];
232
233/* intel_core2_later.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400234extern int intel_core2_later_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400235extern const struct msrdef intel_core2_later_msrs[];
236
237/* intel_pentium4_early.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400238extern int intel_pentium4_early_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400239extern const struct msrdef intel_pentium4_early_msrs[];
240
241/* intel_pentium4_later.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400242extern int intel_pentium4_later_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400243extern const struct msrdef intel_pentium4_later_msrs[];
244
Anton Kochkov54c07a62012-07-04 07:35:45 +0400245/* intel_nehalem.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400246extern int intel_nehalem_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov54c07a62012-07-04 07:35:45 +0400247extern const struct msrdef intel_nehalem_msrs[];
248
Olivier Langloisccc7d1f2013-06-03 01:30:25 -0400249/* intel_atom.c */
250extern int intel_atom_probe(const struct targetdef *t, const struct cpuid_t *id);
251extern const struct msrdef intel_atom_msrs[];
252
Peter Stugedad1e302008-11-22 17:13:36 +0000253#endif /* MSRTOOL_H */