Fix CPUID typo. This caused fid to memory speed calculations to be off.
[coreboot.git] / util / msrtool / msrtool.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 <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <pci/pci.h>
29
30 #include "msrtool.h"
31
32 #define DEFAULT_CPU 0
33 static uint8_t cpu = DEFAULT_CPU;
34
35 uint8_t targets_found = 0;
36 const struct targetdef **targets = NULL;
37 const struct sysdef *sys = NULL;
38 uint8_t reserved = 0, verbose = 0, quiet = 0;
39
40 struct pci_access *pacc = NULL;
41
42 static struct targetdef alltargets[] = {
43         { "geodelx", "AMD Geode(tm) LX", geodelx_probe, geodelx_msrs },
44         { "cs5536", "AMD Geode(tm) CS5536", cs5536_probe, cs5536_msrs },
45         { "K8", "AMD K8 Family", k8_probe, k8_msrs },
46         { TARGET_EOT }
47 };
48
49 static struct sysdef allsystems[] = {
50         { "linux", "Linux with /dev/cpu/*/msr", linux_probe, linux_open, linux_close, linux_rdmsr },
51         { SYSTEM_EOT }
52 };
53
54 static void syntax(char *argv[]) {
55         printf("syntax: %s [-hvqrkl] [-c cpu] [-m system] [-t target ...]\n", argv[0]);
56         printf("\t [-i addr=hi[:]lo] | [-s file] | [-d [:]file] | addr...\n");
57         printf("  -h\t show this help text\n");
58         printf("  -v\t be verbose\n");
59         printf("  -q\t be quiet (overrides -v)\n");
60         printf("  -r\t include [Reserved] values\n");
61         printf("  -k\t list all known systems and targets\n");
62         printf("  -l\t list MSRs and bit fields for current target(s) (-kl for ALL targets!)\n");
63         printf("  -c\t access MSRs on the specified CPU, default=%d\n", DEFAULT_CPU);
64         printf("  -m\t force a system, e.g: -m linux\n");
65         printf("  -t\t force a target, can be used multiple times, e.g: -t geodelx -t cs5536\n");
66         printf("  -i\t immediate mode\n");
67         printf("\t decode hex addr=hi:lo for the target without reading hw value\n");
68         printf("\t e.g: -i 4c00000f=f2f100ff56960004\n");
69         printf("  -s\t stream mode\n");
70         printf("\t read one MSR address per line and append current hw value to the line\n");
71         printf("\t use the filename - for stdin/stdout\n");
72         printf("\t using -l -s ignores input and will output all MSRs with values\n");
73         printf("  -d\t diff mode\n");
74         printf("\t read one address and value per line and compare with current hw value,\n");
75         printf("\t printing differences to stdout. use the filename - to read from stdin\n");
76         printf("\t use :file or :- to reverse diff, normally hw values are considered new\n");
77         printf("  addr.. direct mode, read and decode values for the given MSR address(es)\n");
78 }
79
80 static void *add_target(const struct targetdef *t) {
81         void *tmp;
82         tmp = realloc(targets, (targets_found + 2) * sizeof(struct targetdef *));
83         if (NULL == tmp) {
84                 perror("realloc");
85                 return tmp;
86         }
87         targets = tmp;
88         targets[targets_found++] = t;
89         targets[targets_found] = NULL;
90         return targets;
91 }
92
93 int do_stream(const char *streamfn, uint8_t ignoreinput) {
94         char tmpfn[20], line[256];
95         uint8_t tn;
96         size_t start, len;
97         int ret = 1;
98         int fdout = -1;
99         FILE *fin = NULL, *fout = NULL;
100         uint32_t addr, linenum;
101         struct msr m = MSR1(0);
102
103         if (0 == strcmp(streamfn, "-")) {
104                 fin = stdin;
105                 fout = stdout;
106         } else {
107                 if (!ignoreinput) {
108                         if (NULL == (fin = fopen(streamfn, "r"))) {
109                                 perror("fopen()");
110                                 return 1;
111                         }
112                         if (snprintf(tmpfn, sizeof(tmpfn), "msrtoolXXXXXX") >= sizeof(tmpfn)) {
113                                 perror("snprintf");
114                                 return 1;
115                         }
116                         if (-1 == (fdout = mkstemp(tmpfn))) {
117                                 perror("mkstemp");
118                                 return 1;
119                         }
120                         if (NULL == (fout = fdopen(fdout, "w"))) {
121                                 perror("fdopen");
122                                 return 1;
123                         }
124                 } else {
125                         if (NULL == (fout = fopen(streamfn, "w"))) {
126                                 perror("fopen");
127                                 return 1;
128                         }
129                 }
130         }
131
132         if (!sys->open(cpu, SYS_RDONLY))
133                 goto done;
134         if (ignoreinput) {
135                 for (tn = 0; tn < targets_found; tn++)
136                         if (dumpmsrdefsvals(fout, targets[tn], cpu)) {
137                                 ret = 1;
138                                 break;
139                         }
140         } else {
141                 for (linenum = 1; NULL != fgets(line, sizeof(line), fin); ++linenum) {
142                         start = (0 == strncmp("0x", line, 2)) ? 2 : 0;
143                         if (1 == sscanf(line + start, "%8x", &addr)) {
144                                 if (!sys->rdmsr(cpu, addr, &m))
145                                         goto done;
146                                 fprintf(fout, "0x%08x 0x%08x%08x\n", addr, m.hi, m.lo);
147                                 continue;
148                         }
149                         while (1) {
150                                 fprintf(fout, "%s", line);
151                                 len = strlen(line);
152                                 if (NULL != strchr("\r\n", line[len - 1]))
153                                         break;
154                                 if (NULL == fgets(line, sizeof(line), fin))
155                                         goto read_done;
156                         }
157                 }
158 read_done:
159                 if (!feof(fin)) {
160                         fprintf(stderr, "%s:%d: fgets: %s\n", streamfn, linenum, strerror(errno));
161                         goto done;
162                 }
163         }
164         ret = 0;
165 done:
166         sys->close(cpu);
167         if (strcmp(streamfn, "-")) {
168                 if (ret)
169                         unlink(tmpfn);
170                 else if (!ignoreinput)
171                         rename(tmpfn, streamfn);
172         }
173         if (!ignoreinput)
174                 fclose(fin);
175         fclose(fout);
176         return ret;
177 }
178
179 int do_diff(const char *difffn) {
180         char tmpfn[20], line[512];
181         size_t start, len;
182         int ret = 1, tmp;
183         FILE *fin = NULL, *fout = stdout;
184         uint8_t rev = 0;
185         uint32_t addr, linenum;
186         struct msr mf = MSR1(0), mhw = MSR1(0);
187
188         if (':' == difffn[0]) {
189                 rev = 1;
190                 ++difffn;
191         }
192         if (0 == strcmp(difffn, "-"))
193                 fin = stdin;
194         else if (NULL == (fin = fopen(difffn, "r"))) {
195                 perror("fopen()");
196                 return 1;
197         }
198
199         if (!sys->open(cpu, SYS_RDONLY))
200                 goto done;
201         for (linenum = 1; NULL != fgets(line, sizeof(line), fin); ++linenum) {
202                 start = (0 == strncmp("0x", line, 2)) ? 2 : 0;
203                 if (sscanf(line + start, "%8x %n%*x", &addr, &tmp) >= 1) {
204                         start += tmp;
205                         for (len = strlen(line) - 1; NULL != strchr("\r\n", line[len]); --len)
206                                 line[len] = 0;
207                         if (!str2msr(line + start, &mf)) {
208                                 fprintf(stderr, "%s:%d: invalid MSR value '%s'\n", difffn, linenum, line + start);
209                                 continue;
210                         }
211                         if (!sys->rdmsr(cpu, addr, &mhw))
212                                 goto done;
213                         if (diff_msr(fout, addr, rev ? mhw : mf, rev ? mf : mhw))
214                                 fprintf(fout, "\n");
215                 }
216         }
217         if (!feof(fin))
218                 fprintf(stderr, "%s:%d: fgets: %s\n", difffn, linenum, strerror(errno));
219         else
220                 ret = 0;
221 done:
222         sys->close(cpu);
223         if (strcmp(difffn, "-")) {
224                 if (ret)
225                         unlink(tmpfn);
226                 else
227                         rename(tmpfn, difffn);
228                 fclose(fin);
229                 fclose(fout);
230         }
231         return ret;
232 }
233
234 int main(int argc, char *argv[]) {
235         char c;
236         int ret = 1;
237         const struct sysdef *s;
238         const struct targetdef *t;
239         uint8_t tn, listmsrs = 0, listknown = 0, input = 0;
240         uint32_t addr = 0;
241         const char *streamfn = NULL, *difffn = NULL;
242         struct msr msrval = MSR2(-1, -1);
243         while ((c = getopt(argc, argv, "hqvrklc:m:t:a:i:s:d:")) != -1)
244                 switch (c) {
245                 case 'h':
246                         syntax(argv);
247                         return 0;
248                 case 'q':
249                         quiet = 1;
250                         break;
251                 case 'v':
252                         ++verbose;
253                         break;
254                 case 'r':
255                         reserved = 1;
256                         break;
257                 case 'k':
258                         listknown = 1;
259                         break;
260                 case 'l':
261                         listmsrs = 1;
262                         break;
263                 case 'c':
264                         cpu = atoi(optarg);
265                         break;
266                 case 'm':
267                         for (s = allsystems; !SYSTEM_ISEOT(*s); s++)
268                                 if (!strcmp(s->name, optarg)) {
269                                         sys = s;
270                                         break;
271                                 }
272                         break;
273                 case 't':
274                         for (t = alltargets; !TARGET_ISEOT(*t); t++)
275                                 if (!strcmp(t->name, optarg)) {
276                                         add_target(t);
277                                         break;
278                                 }
279                         break;
280                 case 'i':
281                         input = 1;
282                         addr = msraddrbyname(optarg);
283                         optarg = strchr(optarg, '=');
284                         if (NULL == optarg) {
285                                 fprintf(stderr, "missing value in -i argument!\n");
286                                 break;
287                         }
288                         if (!str2msr(++optarg, &msrval))
289                                 fprintf(stderr, "invalid value in -i argument!\n");
290                         break;
291                 case 's':
292                         streamfn = optarg;
293                         break;
294                 case 'd':
295                         difffn = optarg;
296                         break;
297                 default:
298                         break;
299                 }
300
301         printf_quiet("msrtool %s\n", VERSION);
302
303         pacc = pci_alloc();
304         if (NULL == pacc) {
305                 fprintf(stderr, "Could not initialize PCI library! pci_alloc() failed.\n");
306                 return 1;
307         }
308         pci_init(pacc);
309         pci_scan_bus(pacc);
310
311         if (!sys && !input && !listknown)
312                 for (sys = allsystems; !SYSTEM_ISEOT(*sys); sys++) {
313                         printf_verbose("Probing for system %s: %s\n", sys->name, sys->prettyname);
314                         if (!sys->probe(sys))
315                                 continue;
316                         printf_quiet("Detected system %s: %s\n", sys->name, sys->prettyname);
317                         break;
318                 }
319
320         if (targets)
321                 for (tn = 0; tn < targets_found; tn++)
322                         printf_quiet("Forced target %s: %s\n", targets[tn]->name, targets[tn]->prettyname);
323         else 
324                 for (t = alltargets; !TARGET_ISEOT(*t); t++) {
325                         printf_verbose("Probing for target %s: %s\n", t->name, t->prettyname);
326                         if (!t->probe(t))
327                                 continue;
328                         printf_quiet("Detected target %s: %s\n", t->name, t->prettyname);
329                         add_target(t);
330                 }
331
332         printf_quiet("\n");
333         fflush(stdout);
334
335         if (listknown) {
336                 printf("Known systems:\n");
337                 for (s = allsystems; s->name; s++)
338                         printf("%s: %s\n", s->name, s->prettyname);
339                 printf("\nKnown targets:\n");
340                 for (t = alltargets; t->name; t++) {
341                         if (listmsrs && alltargets != t)
342                                 printf("\n");
343                         printf("%s: %s\n", t->name, t->prettyname);
344                         if (listmsrs)
345                                 dumpmsrdefs(t);
346                 }
347                 printf("\n");
348                 return 0;
349         }
350
351         if (sys && !sys->name) {
352                 fprintf(stderr, "Unable to detect the current operating system!\n");
353                 fprintf(stderr, "Please send a report or patch to coreboot@coreboot.org. Thanks for your help!\n");
354                 fprintf(stderr, "\n");
355         }
356
357         if (!targets_found || !targets) {
358                 fprintf(stderr, "Unable to detect a known target; can not decode any MSRs! (Use -t to force)\n");
359                 fprintf(stderr, "Please send a report or patch to coreboot@coreboot.org. Thanks for your help!\n");
360                 fprintf(stderr, "\n");
361                 return 1;
362         }
363
364         if (input) {
365                 decodemsr(cpu, addr, msrval);
366                 return 0;
367         }
368
369         if (sys && !sys->name)
370                 return 1;
371
372         if (listmsrs) {
373                 if (streamfn)
374                         return do_stream(streamfn, 1);
375                 for (tn = 0; tn < targets_found; tn++) {
376                         if (tn)
377                                 printf("\n");
378                         dumpmsrdefs(targets[tn]);
379                 }
380                 printf("\n");
381                 return 0;
382         }
383
384         if (streamfn)
385                 return do_stream(streamfn, 0);
386
387         if (!sys->open(cpu, SYS_RDONLY))
388                 return 1;
389
390         if (difffn) {
391                 ret = do_diff(difffn);
392                 goto done;
393         }
394
395         if (optind == argc) {
396                 syntax(argv);
397                 printf("\nNo mode or address(es) specified!\n");
398                 goto done;
399         }
400
401         for (; optind < argc; optind++) {
402                 addr = msraddrbyname(argv[optind]);
403                 if (!sys->rdmsr(cpu, addr, &msrval))
404                         break;
405                 decodemsr(cpu, addr, msrval);
406         }
407         ret = 0;
408 done:
409         sys->close(cpu);
410         return ret;
411 }