Add some basic K8 MSRs.
[coreboot.git] / util / msrtool / msrutils.c
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 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23
24 #include "msrtool.h"
25
26 static void print_bitdef(FILE *f, const struct msrbits *mb, const char *tail) {
27         uint8_t endbit;
28         if (!reserved && 0 == strcmp(mb->name, "RSVD"))
29                 return;
30         if (1 == mb->size)
31                 fprintf(f, "# %5d", mb->start);
32         else {
33                 endbit = mb->start - mb->size + 1;
34                 fprintf(f, "# %*d:%d", endbit < 10 ? 3 : 2, mb->start, endbit);
35         }
36         if (0 == strcmp(mb->name, "RSVD"))
37                 fprintf(f, " [%s]", mb->desc);
38         else
39                 fprintf(f, " %s %s", mb->name, mb->desc);
40         fprintf(f, "%s", tail);
41 }
42
43 static void print_bitval(FILE *f, const struct msrbits *mb, const struct msr val) {
44         uint8_t i;
45         struct msr tmp, mask = MSR1(1);
46         const struct msrbitvalues *mbv = mb->bitval;
47         while (mbv->text && !msr_eq(mbv->value, val))
48                 mbv++;
49         switch (mb->present) {
50         case PRESENT_BIN:
51                 mask = msr_shl(mask, mb->size - 1);
52                 for (i = 0; i < mb->size; i++) {
53                         memcpy(&tmp, &val, sizeof(val));
54                         msr_and(&tmp, mask);
55                         fprintf(f, "%d", (tmp.hi || tmp.lo) ? 1 : 0);
56                         mask = msr_shr(mask, 1);
57                 }
58                 break;
59         case PRESENT_DEC:
60                 fprintf(f, "%d", val.lo);
61                 break;
62         case PRESENT_OCT:
63                 fprintf(f, "0%o", val.lo);
64                 break;
65         case PRESENT_HEX:
66                 hexprint(f, val, mb->size);
67                 break;
68         case PRESENT_HEXDEC:
69                 hexprint(f, val, mb->size);
70                 fprintf(f, " %d", val.lo);
71                 break;
72         }
73         if (mbv->text)
74                 fprintf(f, ": %s", mbv->text);
75         fprintf(f, "\n");
76 }
77
78 void hexprint(FILE *f, const struct msr val, const uint8_t bits) {
79         if (bits <= 4)
80                 fprintf(f, "0x%x", (uint8_t)(val.lo & 0x0f));
81         else if (bits <= 8)
82                 fprintf(f, "0x%02x", (uint8_t)(val.lo & 0xff));
83         else if (bits <= 16)
84                 fprintf(f, "0x%04x", (uint16_t)(val.lo & 0xffff));
85         else if (bits <= 32)
86                 fprintf(f, "0x%08x", val.lo);
87         else
88                 fprintf(f, "0x%08x%08x", val.hi, val.lo);
89 }
90
91 int msr_eq(const struct msr a, const struct msr b) {
92         return a.hi == b.hi && a.lo == b.lo;
93 }
94
95 struct msr msr_shl(const struct msr a, const uint8_t bits) {
96         struct msr ret;
97
98         ret.hi = bits < 32 ? a.hi << bits : 0;
99         ret.lo = bits < 32 ? a.lo << bits : 0;
100
101         if (bits < 32)
102                 ret.hi |= bits ? a.lo >> (32 - bits) : 0;
103         else
104                 ret.hi |= a.lo << (bits - 32);
105
106         return ret;
107 }
108
109 struct msr msr_shr(const struct msr a, const uint8_t bits) {
110         struct msr ret;
111
112         ret.hi = bits < 32 ? a.hi >> bits : 0;
113         ret.lo = bits < 32 ? a.lo >> bits : 0;
114
115         if (bits < 32)
116                 ret.lo |= bits ? a.hi << (32 - bits) : 0;
117         else
118                 ret.lo |= a.hi >> (bits - 32);
119
120         return ret;
121 }
122
123 void msr_and(struct msr *a, const struct msr b) {
124         a->hi &= b.hi;
125         a->lo &= b.lo;
126 }
127
128 const struct msrdef *findmsrdef(const uint32_t addr) {
129         uint8_t t;
130         const struct msrdef *m;
131         if (!targets)
132                 return NULL;
133         for (t = 0; t < targets_found; t++)
134                 for (m = targets[t]->msrs; !MSR_ISEOT(*m); m++)
135                         if (addr == m->addr)
136                                 return m;
137         return NULL;
138 }
139
140 uint32_t msraddrbyname(const char *name) {
141         uint8_t t;
142         const uint32_t addr = strtoul(name, NULL, 16);
143         const struct msrdef *m;
144         if (!targets)
145                 return 0;
146         for (t = 0; t < targets_found; t++)
147                 for (m = targets[t]->msrs; !MSR_ISEOT(*m); m++) {
148                         if (addr == m->addr)
149                                 return m->addr;
150                         if (!strcasecmp(name, m->symbol))
151                                 return m->addr;
152                 }
153         return 0;
154 }
155
156 void dumpmsrdefs(const struct targetdef *t) {
157         const struct msrdef *m;
158         const struct msrbits *mb;
159         if (NULL == t)
160                 return;
161         printf("# %s MSRs:\n", t->name);
162         for (m = t->msrs; !MSR_ISEOT(*m); m++) {
163                 if (t->msrs != m)
164                         printf("\n");
165                 printf("# %s\n", m->symbol);
166                 for (mb = m->bits; mb->size; mb++)
167                         print_bitdef(stdout, mb, "\n");
168                 printf("0x%08x\n", m->addr);
169         }
170 }
171
172 int dumpmsrdefsvals(FILE *f, const struct targetdef *t, const uint8_t cpu) {
173         struct msr val = MSR1(0);
174         const struct msrdef *m;
175         const struct msrbits *mb;
176         if (NULL == t)
177                 return 1;
178         fprintf(f, "# %s MSRs:\n", t->name);
179         for (m = t->msrs; !MSR_ISEOT(*m); m++) {
180                 if (t->msrs != m)
181                         fprintf(f, "\n");
182                 if (!sys->rdmsr(cpu, m->addr, &val))
183                         return 1;
184                 fprintf(f, "# %s\n", m->symbol);
185                 for (mb = m->bits; mb->size; mb++)
186                         print_bitdef(f, mb, "\n");
187                 fprintf(f, "0x%08x 0x%08x%08x\n", m->addr, val.hi, val.lo);
188         }
189         return 0;
190 }
191
192 /**
193  * Parse a hexadecimal string into an MSR value.
194  * 
195  * Leading 0x or 0X is optional, the string is always parsed as hexadecimal.
196  * Any non-hexadecimal character can be used to separate the high 32 bits and
197  * the low 32 bits. If there is such a separator, high and low values do not
198  * need to be zero padded. If there is no separator, the last <=8 digits are
199  * the low 32 bits and any characters before them are the high 32 bits.
200  * When there is no separator and less than eight digits, the high 32 bits
201  * are set to 0.
202  * Parsing fails when there is a separator and it is followed by another
203  * non-hexadecimal character.
204  *
205  * @param str The string to parse. The string must be writable but will be
206  * restored before return.
207  * @param msr Pointer to the struct msr where the value will be stored.
208  * @return 1 on success, 0 on parse failure. msr is unchanged on failure.
209  */
210 uint8_t str2msr(char *str, struct msr *msr) {
211         char c;
212         size_t len, lo;
213         if (0 == strncmp(str, "0x", 2) || 0 == strncmp(str, "0X", 2))
214                 str += 2;
215         len = strspn(str, HEXCHARS);
216         if (len <= 8 && 0 == str[len]) {
217                 msr->hi = 0;
218                 lo = 0;
219         } else if (len <= 8) {
220                 lo = len + strcspn(str + len, HEXCHARS);
221                 if (0 == len && 0 == strspn(str + lo, HEXCHARS))
222                         return 0;
223                 c = str[len];
224                 str[len] = 0;
225                 msr->hi = strtoul(str, NULL, 16);
226                 str[len] = c;
227         } else {
228                 lo = len - 8;
229                 c = str[lo];
230                 str[lo] = 0;
231                 msr->hi = strtoul(str, NULL, 16);
232                 str[lo] = c;
233         }
234         msr->lo = strtoul(str + lo, NULL, 16);
235         return 1;
236 }
237
238 void decodemsr(const uint8_t cpu, const uint32_t addr, const struct msr val) {
239         struct msr bitval, mask;
240         const struct msrdef *m = findmsrdef(addr);
241         const struct msrbits *mb;
242
243         if (NULL != m)
244                 printf("# %s ", m->symbol);
245         printf("0x%08x = 0x%08x%08x\n", addr, val.hi, val.lo);
246         if (NULL == m) {
247                 fprintf(stderr, "Sorry - no definition exists for this MSR! Please add it and send a signed-off\n");
248                 fprintf(stderr, "patch to coreboot@coreboot.org. Thanks for your help!\n");
249                 return;
250         }
251
252         for (mb = m->bits; mb->size; mb++) {
253                 if (!reserved && 0 == strcmp(mb->name, "RSVD"))
254                         continue;
255                 print_bitdef(stdout, mb, " = ");
256                 mask.hi = mask.lo = 0xffffffff;
257                 mask = msr_shr(mask, 64 - mb->size);
258                 bitval = msr_shr(val, mb->start - mb->size + 1);
259                 msr_and(&bitval, mask);
260                 print_bitval(stdout, mb, bitval);
261         }
262 }
263
264 /**
265  * Compare two MSR values and print any differences with field definitions and
266  * both old and new values decoded.
267  *
268  * @param f Output stream.
269  * @param addr MSR address.
270  * @param a Left value.
271  * @param b Right value.
272  * @return 1 when a and b differ, 0 when they are equal or only reserved bits
273  * differ and processing of reserved bits was not requested (with -r).
274  */
275 uint8_t diff_msr(FILE *f, const uint32_t addr, const struct msr a, const struct msr b) {
276         uint8_t ret = 0, first = 1;
277         struct msr aval, bval, mask;
278         const struct msrdef *m = findmsrdef(addr);
279         const struct msrbits *mb;
280
281         if (a.hi == b.hi && a.lo == b.lo)
282                 return 0;
283
284         if (NULL == m) {
285                 fprintf(stderr, "MSR 0x%08x has no definition! Please add it and send a Signed-off-by patch\n", addr);
286                 fprintf(stderr, "to coreboot@coreboot.org. Thank you for your help!\n");
287                 return 1;
288         }
289
290         for (mb = m->bits; mb->size; mb++) {
291                 if (!reserved && 0 == strcmp(mb->name, "RSVD"))
292                         continue;
293                 mask.hi = mask.lo = 0xffffffff;
294                 mask = msr_shr(mask, 64 - mb->size);
295                 aval = msr_shr(a, mb->start - mb->size + 1);
296                 bval = msr_shr(b, mb->start - mb->size + 1);
297                 msr_and(&aval, mask);
298                 msr_and(&bval, mask);
299                 if (msr_eq(aval, bval))
300                         continue;
301                 if (first) {
302                         fprintf(f, "# %s\n", m->symbol);
303                         fprintf(f, "-0x%08x 0x%08x%08x\n", addr, a.hi, a.lo);
304                         fprintf(f, "+0x%08x 0x%08x%08x\n", addr, b.hi, b.lo);
305                         first = 0;
306                         ret = 1;
307                 }
308                 print_bitdef(f, mb, "\n-");
309                 print_bitval(f, mb, aval);
310                 fprintf(f, "+");
311                 print_bitval(f, mb, bval);
312         }
313         return ret;
314 }