2 * This file is part of msrtool.
4 * Copyright (c) 2008 Peter Stuge <peter@stuge.se>
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.
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.
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
26 static void print_bitdef(FILE *f, const struct msrbits *mb, const char *tail) {
28 if (!reserved && 0 == strcmp(mb->name, "RSVD"))
31 fprintf(f, "# %5d", mb->start);
33 endbit = mb->start - mb->size + 1;
34 fprintf(f, "# %*d:%d", endbit < 10 ? 3 : 2, mb->start, endbit);
36 if (0 == strcmp(mb->name, "RSVD"))
37 fprintf(f, " [%s]", mb->desc);
39 fprintf(f, " %s %s", mb->name, mb->desc);
40 fprintf(f, "%s", tail);
43 static void print_bitval(FILE *f, const struct msrbits *mb, const struct msr val) {
45 struct msr tmp, mask = MSR1(1);
46 const struct msrbitvalues *mbv = mb->bitval;
47 while (mbv->text && !msr_eq(mbv->value, val))
49 switch (mb->present) {
51 mask = msr_shl(mask, mb->size - 1);
52 for (i = 0; i < mb->size; i++) {
53 memcpy(&tmp, &val, sizeof(val));
55 fprintf(f, "%d", (tmp.hi || tmp.lo) ? 1 : 0);
56 mask = msr_shr(mask, 1);
60 fprintf(f, "%d", val.lo);
63 fprintf(f, "0%o", val.lo);
66 hexprint(f, val, mb->size);
69 hexprint(f, val, mb->size);
70 fprintf(f, " %d", val.lo);
74 fprintf(f, ": %s", mbv->text);
78 void hexprint(FILE *f, const struct msr val, const uint8_t bits) {
80 fprintf(f, "0x%x", (uint8_t)(val.lo & 0x0f));
82 fprintf(f, "0x%02x", (uint8_t)(val.lo & 0xff));
84 fprintf(f, "0x%04x", (uint16_t)(val.lo & 0xffff));
86 fprintf(f, "0x%08x", val.lo);
88 fprintf(f, "0x%08x%08x", val.hi, val.lo);
91 int msr_eq(const struct msr a, const struct msr b) {
92 return a.hi == b.hi && a.lo == b.lo;
95 struct msr msr_shl(const struct msr a, const uint8_t bits) {
98 ret.hi = bits < 32 ? a.hi << bits : 0;
99 ret.lo = bits < 32 ? a.lo << bits : 0;
102 ret.hi |= bits ? a.lo >> (32 - bits) : 0;
104 ret.hi |= a.lo << (bits - 32);
109 struct msr msr_shr(const struct msr a, const uint8_t bits) {
112 ret.hi = bits < 32 ? a.hi >> bits : 0;
113 ret.lo = bits < 32 ? a.lo >> bits : 0;
116 ret.lo |= bits ? a.hi << (32 - bits) : 0;
118 ret.lo |= a.hi >> (bits - 32);
123 void msr_and(struct msr *a, const struct msr b) {
128 const struct msrdef *findmsrdef(const uint32_t addr) {
130 const struct msrdef *m;
133 for (t = 0; t < targets_found; t++)
134 for (m = targets[t]->msrs; !MSR_ISEOT(*m); m++)
140 uint32_t msraddrbyname(const char *name) {
142 const uint32_t addr = strtoul(name, NULL, 16);
143 const struct msrdef *m;
146 for (t = 0; t < targets_found; t++)
147 for (m = targets[t]->msrs; !MSR_ISEOT(*m); m++) {
150 if (!strcasecmp(name, m->symbol))
156 void dumpmsrdefs(const struct targetdef *t) {
157 const struct msrdef *m;
158 const struct msrbits *mb;
161 printf("# %s MSRs:\n", t->name);
162 for (m = t->msrs; !MSR_ISEOT(*m); m++) {
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);
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;
178 fprintf(f, "# %s MSRs:\n", t->name);
179 for (m = t->msrs; !MSR_ISEOT(*m); m++) {
182 if (!sys->rdmsr(cpu, m->addr, &val))
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);
193 * Parse a hexadecimal string into an MSR value.
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
202 * Parsing fails when there is a separator and it is followed by another
203 * non-hexadecimal character.
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.
210 uint8_t str2msr(char *str, struct msr *msr) {
213 if (0 == strncmp(str, "0x", 2) || 0 == strncmp(str, "0X", 2))
215 len = strspn(str, HEXCHARS);
216 if (len <= 8 && 0 == str[len]) {
219 } else if (len <= 8) {
220 lo = len + strcspn(str + len, HEXCHARS);
221 if (0 == len && 0 == strspn(str + lo, HEXCHARS))
225 msr->hi = strtoul(str, NULL, 16);
231 msr->hi = strtoul(str, NULL, 16);
234 msr->lo = strtoul(str + lo, NULL, 16);
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;
244 printf("# %s ", m->symbol);
245 printf("0x%08x = 0x%08x%08x\n", addr, val.hi, val.lo);
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");
252 for (mb = m->bits; mb->size; mb++) {
253 if (!reserved && 0 == strcmp(mb->name, "RSVD"))
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);
265 * Compare two MSR values and print any differences with field definitions and
266 * both old and new values decoded.
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).
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;
281 if (a.hi == b.hi && a.lo == b.lo)
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");
290 for (mb = m->bits; mb->size; mb++) {
291 if (!reserved && 0 == strcmp(mb->name, "RSVD"))
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))
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);
308 print_bitdef(f, mb, "\n-");
309 print_bitval(f, mb, aval);
311 print_bitval(f, mb, bval);