Add some basic K8 MSRs.
[coreboot.git] / util / msrtool / msrtool.h
1 /*
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>
25 #include <pci/pci.h>
26
27 #define HEXCHARS "0123456789abcdefABCDEF"
28
29 enum {
30         MSRTYPE_RDONLY,
31         MSRTYPE_RDWR,
32         MSRTYPE_WRONLY,
33         MSRTYPE_EOT
34 } MsrTypes;
35
36 enum {
37         PRESENT_RSVD,
38         PRESENT_DEC,
39         PRESENT_BIN,
40         PRESENT_OCT,
41         PRESENT_HEX,
42         PRESENT_HEXDEC
43 } PresentTypes;
44
45 struct msr {
46         uint32_t hi;
47         uint32_t lo;
48 };
49
50 struct msrbitvalues {
51         const struct msr value;
52         const char *text;
53 };
54
55 struct 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
64 struct 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
90 struct 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
101 enum SysModes {
102         SYS_RDONLY = 0,
103         SYS_WRONLY,
104         SYS_RDWR
105 };
106
107 struct 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
120 struct 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
129 extern const struct sysdef *sys;
130
131 extern uint8_t targets_found;
132 extern const struct targetdef **targets;
133
134 extern uint8_t reserved, verbose, quiet;
135
136 extern struct pci_access *pacc;
137
138 #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 */
150 struct cpuid_t *cpuid(void);
151 struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device);
152
153 /* msrutils.c */
154 void hexprint(FILE *f, const struct msr val, const uint8_t bits);
155 int msr_eq(const struct msr a, const struct msr b);
156 struct msr msr_shl(const struct msr a, const uint8_t bits);
157 struct msr msr_shr(const struct msr a, const uint8_t bits);
158 void msr_and(struct msr *a, const struct msr b);
159 const struct msrdef *findmsrdef(const uint32_t addr);
160 uint32_t msraddrbyname(const char *name);
161 void dumpmsrdefs(const struct targetdef *t);
162 int dumpmsrdefsvals(FILE *f, const struct targetdef *t, const uint8_t cpu);
163 uint8_t str2msr(char *str, struct msr *msr);
164 void decodemsr(const uint8_t cpu, const uint32_t addr, const struct msr val);
165 uint8_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 */
172 extern int linux_probe(const struct sysdef *system);
173 extern int linux_open(uint8_t cpu, enum SysModes mode);
174 extern int linux_close(uint8_t cpu);
175 extern int linux_rdmsr(uint8_t cpu, uint32_t addr, struct msr *val);
176
177
178 /** target externs **/
179
180 /* geodelx.c */
181 extern int geodelx_probe(const struct targetdef *t);
182 extern const struct msrdef geodelx_msrs[];
183
184 /* cs5536.c */
185 extern int cs5536_probe(const struct targetdef *t);
186 extern const struct msrdef cs5536_msrs[];
187
188 /* k8.c */
189 extern int k8_probe(const struct targetdef *t);
190 extern const struct msrdef k8_msrs[];
191
192 #endif /* MSRTOOL_H */