blob: 9fe820dd048b89ade681b3d45e8ced90efcdf125 [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>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifndef MSRTOOL_H
21#define MSRTOOL_H
22
23#include <stdio.h>
24#include <stdint.h>
Peter Stuge0924dee2008-11-25 02:03:16 +000025#include <pci/pci.h>
Peter Stugedad1e302008-11-22 17:13:36 +000026
27#define HEXCHARS "0123456789abcdefABCDEF"
28
29enum {
30 MSRTYPE_RDONLY,
31 MSRTYPE_RDWR,
32 MSRTYPE_WRONLY,
33 MSRTYPE_EOT
34} MsrTypes;
35
36enum {
37 PRESENT_RSVD,
38 PRESENT_DEC,
39 PRESENT_BIN,
40 PRESENT_OCT,
41 PRESENT_HEX,
42 PRESENT_HEXDEC
43} PresentTypes;
44
45struct msr {
46 uint32_t hi;
47 uint32_t lo;
48};
49
50struct msrbitvalues {
51 const struct msr value;
52 const char *text;
53};
54
55struct msrbits {
56 const uint8_t start;
57 const uint8_t size;
58 const char *name;
59 const char *desc;
60 const uint8_t present;
61 const struct msrbitvalues bitval[32];
62};
63
64struct msrdef {
65 const uint32_t addr;
66 const uint8_t type;
67 const struct msr resetval;
68 const char *symbol;
69 const char *desc;
70 const struct msrbits bits[65];
71};
72
73#define MSR1(lo) { 0, (lo) }
74#define MSR2(hi,lo) { (hi), (lo) }
75
76#define BITVAL_EOT .text = NULL
77#define BITVAL_ISEOT(bv) (NULL == (bv).text)
78
79#define BITS_EOT .size = 0
80#define BITS_ISEOT(b) (0 == (b).size)
81
82#define MSR_EOT .type = MSRTYPE_EOT
83#define MSR_ISEOT(m) (MSRTYPE_EOT == (m).type)
84
85#define NOBITS {{ BITVAL_EOT }}
86#define RESERVED "RSVD", "Reserved", PRESENT_HEXDEC, NOBITS
87
88#define MAX_CORES 8
89
90struct targetdef {
91 const char *name;
92 const char *prettyname;
93 int (*probe)(const struct targetdef *target);
94 const struct msrdef *msrs;
95};
96
97#define TARGET_EOT .name = NULL
98#define TARGET_ISEOT(t) (NULL == (t).name)
99
100
101enum SysModes {
102 SYS_RDONLY = 0,
103 SYS_WRONLY,
104 SYS_RDWR
105};
106
107struct sysdef {
108 const char *name;
109 const char *prettyname;
110 int (*probe)(const struct sysdef *system);
111 int (*open)(uint8_t cpu, enum SysModes mode);
112 int (*close)(uint8_t cpu);
113 int (*rdmsr)(uint8_t cpu, uint32_t addr, struct msr *val);
114};
115
116#define SYSTEM_EOT .name = NULL
117#define SYSTEM_ISEOT(s) (NULL == (s).name)
118
119
120struct cpuid_t {
121 uint8_t family;
122 uint8_t model;
123 uint8_t stepping;
124 uint8_t ext_family;
125 uint8_t ext_model;
126};
127
128
129extern const struct sysdef *sys;
130
131extern uint8_t targets_found;
132extern const struct targetdef **targets;
133
134extern uint8_t reserved, verbose, quiet;
135
Peter Stuge0924dee2008-11-25 02:03:16 +0000136extern struct pci_access *pacc;
137
Peter Stugedad1e302008-11-22 17:13:36 +0000138#define printf_quiet(x...) do { if (!quiet) fprintf(stderr,x); } while(0)
139#define printf_verbose(x...) do { if (verbose && !quiet) fprintf(stderr,x); } while(0)
140
141#define SYSERROR(call, addr) do { \
142 const struct msrdef *m = findmsrdef(addr); \
143 if (m) \
144 fprintf(stderr, "%s: " #call "(0x%08x) %s: %s\n", __func__, addr, m->symbol, strerror(errno)); \
145 else \
146 fprintf(stderr, "%s: " #call "(0x%08x): %s\n", __func__, addr, strerror(errno)); \
147} while (0);
148
149/* sys.c */
150struct cpuid_t *cpuid(void);
Peter Stuge0924dee2008-11-25 02:03:16 +0000151struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device);
Peter Stugedad1e302008-11-22 17:13:36 +0000152
153/* msrutils.c */
154void hexprint(FILE *f, const struct msr val, const uint8_t bits);
155int msr_eq(const struct msr a, const struct msr b);
156struct msr msr_shl(const struct msr a, const uint8_t bits);
157struct msr msr_shr(const struct msr a, const uint8_t bits);
158void msr_and(struct msr *a, const struct msr b);
159const struct msrdef *findmsrdef(const uint32_t addr);
Marc Jones8f210762009-03-08 04:37:39 +0000160uint32_t msraddrbyname(const char *name);
Peter Stugedad1e302008-11-22 17:13:36 +0000161void dumpmsrdefs(const struct targetdef *t);
162int dumpmsrdefsvals(FILE *f, const struct targetdef *t, const uint8_t cpu);
163uint8_t str2msr(char *str, struct msr *msr);
164void decodemsr(const uint8_t cpu, const uint32_t addr, const struct msr val);
165uint8_t diff_msr(FILE *fout, const uint32_t addr, const struct msr a, const struct msr b);
166
167
168
169/** system externs **/
170
171/* linux.c */
172extern int linux_probe(const struct sysdef *system);
173extern int linux_open(uint8_t cpu, enum SysModes mode);
174extern int linux_close(uint8_t cpu);
175extern int linux_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val);
176
177
178/** target externs **/
179
180/* geodelx.c */
181extern int geodelx_probe(const struct targetdef *t);
182extern const struct msrdef geodelx_msrs[];
183
184/* cs5536.c */
185extern int cs5536_probe(const struct targetdef *t);
186extern const struct msrdef cs5536_msrs[];
187
Marc Jones8f210762009-03-08 04:37:39 +0000188/* k8.c */
189extern int k8_probe(const struct targetdef *t);
190extern const struct msrdef k8_msrs[];
191
Peter Stugedad1e302008-11-22 17:13:36 +0000192#endif /* MSRTOOL_H */