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