blob: 8cba4c74831e777c8bfc683b45972cc0c4231cf3 [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__))
Paul Menzela8843de2017-06-05 12:33:23 +020023/* DirectHW is available here: https://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,
Lubomir Rintel38686f12017-01-22 22:19:33 +010048 PRESENT_HEXDEC,
49 PRESENT_STR,
Peter Stugedad1e302008-11-22 17:13:36 +000050} PresentTypes;
51
52struct msr {
53 uint32_t hi;
54 uint32_t lo;
55};
56
57struct msrbitvalues {
58 const struct msr value;
59 const char *text;
60};
61
62struct msrbits {
63 const uint8_t start;
64 const uint8_t size;
65 const char *name;
66 const char *desc;
67 const uint8_t present;
68 const struct msrbitvalues bitval[32];
69};
70
71struct msrdef {
72 const uint32_t addr;
73 const uint8_t type;
74 const struct msr resetval;
75 const char *symbol;
76 const char *desc;
77 const struct msrbits bits[65];
78};
79
80#define MSR1(lo) { 0, (lo) }
81#define MSR2(hi,lo) { (hi), (lo) }
82
83#define BITVAL_EOT .text = NULL
84#define BITVAL_ISEOT(bv) (NULL == (bv).text)
85
86#define BITS_EOT .size = 0
87#define BITS_ISEOT(b) (0 == (b).size)
88
89#define MSR_EOT .type = MSRTYPE_EOT
90#define MSR_ISEOT(m) (MSRTYPE_EOT == (m).type)
91
92#define NOBITS {{ BITVAL_EOT }}
93#define RESERVED "RSVD", "Reserved", PRESENT_HEXDEC, NOBITS
94
95#define MAX_CORES 8
96
Anton Kochkov59b36f12012-07-21 07:29:48 +040097typedef enum {
Lubomir Rintelfbf57592017-01-22 22:19:21 +010098 VENDOR_INTEL = 0x756e6547,
99 VENDOR_AMD = 0x68747541,
Lubomir Rintel6cc4dea2017-01-22 22:20:04 +0100100 VENDOR_CENTAUR = 0x746e6543,
Anton Kochkov59b36f12012-07-21 07:29:48 +0400101} vendor_t;
102
103struct cpuid_t {
104 uint8_t family;
105 uint8_t model;
106 uint8_t stepping;
107 uint8_t ext_family;
108 uint8_t ext_model;
109 vendor_t vendor;
110};
111
Peter Stugedad1e302008-11-22 17:13:36 +0000112struct targetdef {
113 const char *name;
114 const char *prettyname;
Anton Kochkov59b36f12012-07-21 07:29:48 +0400115 int (*probe)(const struct targetdef *target, const struct cpuid_t *id);
Peter Stugedad1e302008-11-22 17:13:36 +0000116 const struct msrdef *msrs;
117};
118
119#define TARGET_EOT .name = NULL
120#define TARGET_ISEOT(t) (NULL == (t).name)
121
122
123enum SysModes {
124 SYS_RDONLY = 0,
125 SYS_WRONLY,
126 SYS_RDWR
127};
128
129struct sysdef {
130 const char *name;
131 const char *prettyname;
132 int (*probe)(const struct sysdef *system);
133 int (*open)(uint8_t cpu, enum SysModes mode);
134 int (*close)(uint8_t cpu);
135 int (*rdmsr)(uint8_t cpu, uint32_t addr, struct msr *val);
136};
137
138#define SYSTEM_EOT .name = NULL
139#define SYSTEM_ISEOT(s) (NULL == (s).name)
140
Peter Stugedad1e302008-11-22 17:13:36 +0000141extern const struct sysdef *sys;
142
143extern uint8_t targets_found;
144extern const struct targetdef **targets;
145
146extern uint8_t reserved, verbose, quiet;
147
Peter Stuge0924dee2008-11-25 02:03:16 +0000148extern struct pci_access *pacc;
149
Peter Stugedad1e302008-11-22 17:13:36 +0000150#define printf_quiet(x...) do { if (!quiet) fprintf(stderr,x); } while(0)
151#define printf_verbose(x...) do { if (verbose && !quiet) fprintf(stderr,x); } while(0)
152
153#define SYSERROR(call, addr) do { \
154 const struct msrdef *m = findmsrdef(addr); \
155 if (m) \
156 fprintf(stderr, "%s: " #call "(0x%08x) %s: %s\n", __func__, addr, m->symbol, strerror(errno)); \
157 else \
158 fprintf(stderr, "%s: " #call "(0x%08x): %s\n", __func__, addr, strerror(errno)); \
159} while (0);
160
161/* sys.c */
162struct cpuid_t *cpuid(void);
Peter Stuge0924dee2008-11-25 02:03:16 +0000163struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device);
Peter Stugedad1e302008-11-22 17:13:36 +0000164
165/* msrutils.c */
166void hexprint(FILE *f, const struct msr val, const uint8_t bits);
Lubomir Rintel38686f12017-01-22 22:19:33 +0100167void strprint(FILE *f, const struct msr val, const uint8_t bits);
Peter Stugedad1e302008-11-22 17:13:36 +0000168int msr_eq(const struct msr a, const struct msr b);
169struct msr msr_shl(const struct msr a, const uint8_t bits);
170struct msr msr_shr(const struct msr a, const uint8_t bits);
171void msr_and(struct msr *a, const struct msr b);
172const struct msrdef *findmsrdef(const uint32_t addr);
Marc Jones8f210762009-03-08 04:37:39 +0000173uint32_t msraddrbyname(const char *name);
Peter Stugedad1e302008-11-22 17:13:36 +0000174void dumpmsrdefs(const struct targetdef *t);
175int dumpmsrdefsvals(FILE *f, const struct targetdef *t, const uint8_t cpu);
Peter Stuge34f29072010-01-17 18:33:53 +0000176uint8_t str2msr(char *str, struct msr *msr, char **endptr);
Peter Stugedad1e302008-11-22 17:13:36 +0000177void decodemsr(const uint8_t cpu, const uint32_t addr, const struct msr val);
178uint8_t diff_msr(FILE *fout, const uint32_t addr, const struct msr a, const struct msr b);
179
180
181
182/** system externs **/
183
184/* linux.c */
185extern int linux_probe(const struct sysdef *system);
186extern int linux_open(uint8_t cpu, enum SysModes mode);
187extern int linux_close(uint8_t cpu);
188extern int linux_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val);
189
Stefan Reinauer37f39352009-09-01 10:03:01 +0000190/* darwin.c */
191extern int darwin_probe(const struct sysdef *system);
192extern int darwin_open(uint8_t cpu, enum SysModes mode);
193extern int darwin_close(uint8_t cpu);
194extern int darwin_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val);
Peter Stugedad1e302008-11-22 17:13:36 +0000195
Andriy Gapond80e57c2009-11-28 05:21:42 +0000196/* freebsd.c */
197extern int freebsd_probe(const struct sysdef *system);
198extern int freebsd_open(uint8_t cpu, enum SysModes mode);
199extern int freebsd_close(uint8_t cpu);
200extern int freebsd_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val);
201
Peter Stugedad1e302008-11-22 17:13:36 +0000202/** target externs **/
203
Nils Jacobs58a901f2010-01-15 10:06:39 +0000204/* geodegx2.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400205extern int geodegx2_probe(const struct targetdef *t, const struct cpuid_t *id);
Nils Jacobs58a901f2010-01-15 10:06:39 +0000206extern const struct msrdef geodegx2_msrs[];
207
Peter Stugedad1e302008-11-22 17:13:36 +0000208/* geodelx.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400209extern int geodelx_probe(const struct targetdef *t, const struct cpuid_t *id);
Peter Stugedad1e302008-11-22 17:13:36 +0000210extern const struct msrdef geodelx_msrs[];
211
212/* cs5536.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400213extern int cs5536_probe(const struct targetdef *t, const struct cpuid_t *id);
Peter Stugedad1e302008-11-22 17:13:36 +0000214extern const struct msrdef cs5536_msrs[];
215
Marc Jones8f210762009-03-08 04:37:39 +0000216/* k8.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400217extern int k8_probe(const struct targetdef *t, const struct cpuid_t *id);
Marc Jones8f210762009-03-08 04:37:39 +0000218extern const struct msrdef k8_msrs[];
219
Lubomir Rintel6cc4dea2017-01-22 22:20:04 +0100220/* via_c7.c */
221extern int via_c7_probe(const struct targetdef *t, const struct cpuid_t *id);
222extern const struct msrdef via_c7_msrs[];
223
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400224/* intel_pentium3_early.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400225extern int intel_pentium3_early_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400226extern const struct msrdef intel_pentium3_early_msrs[];
227
228/* intel_pentium3.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400229extern int intel_pentium3_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400230extern const struct msrdef intel_pentium3_msrs[];
231
232/* intel_core1.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400233extern int intel_core1_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400234extern const struct msrdef intel_core1_msrs[];
235
236/* intel_core2_early.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400237extern int intel_core2_early_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400238extern const struct msrdef intel_core2_early_msrs[];
239
240/* intel_core2_later.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400241extern int intel_core2_later_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400242extern const struct msrdef intel_core2_later_msrs[];
243
244/* intel_pentium4_early.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400245extern int intel_pentium4_early_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400246extern const struct msrdef intel_pentium4_early_msrs[];
247
248/* intel_pentium4_later.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400249extern int intel_pentium4_later_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov7c634ae2011-06-20 23:14:22 +0400250extern const struct msrdef intel_pentium4_later_msrs[];
251
Elyes HAOUAS6e6b36a2018-05-15 13:28:54 +0200252/* intel_pentium_d.c */
253extern int intel_pentium_d_probe(const struct targetdef *t, const struct cpuid_t *id);
254extern const struct msrdef intel_pentium_d_msrs[];
255
Anton Kochkov54c07a62012-07-04 07:35:45 +0400256/* intel_nehalem.c */
Anton Kochkov59b36f12012-07-21 07:29:48 +0400257extern int intel_nehalem_probe(const struct targetdef *t, const struct cpuid_t *id);
Anton Kochkov54c07a62012-07-04 07:35:45 +0400258extern const struct msrdef intel_nehalem_msrs[];
259
Olivier Langloisccc7d1f2013-06-03 01:30:25 -0400260/* intel_atom.c */
261extern int intel_atom_probe(const struct targetdef *t, const struct cpuid_t *id);
262extern const struct msrdef intel_atom_msrs[];
263
Peter Stugedad1e302008-11-22 17:13:36 +0000264#endif /* MSRTOOL_H */