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