blob: 0fde455c877a1334bf24ec7d65b77f22f4a24e2b [file] [log] [blame]
Alexandru Gagniuc67f556c2012-08-10 03:55:42 -05001/*
2 * viatool - dump all registers on an Intel CPU + chipset based system.
3 *
4 * Copyright (C) 2008-2010 by coresystems GmbH
5 * written by Stefan Reinauer <stepan@coresystems.de>
6 * Copyright (C) 2009 Carl-Daniel Hailfinger
7 * Copyright (C) 2013 Alexandru Gagniuc
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Alexandru Gagniuc67f556c2012-08-10 03:55:42 -050017 */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <inttypes.h>
22#include <getopt.h>
23#include <fcntl.h>
24#include <sys/mman.h>
25#include <unistd.h>
26#include "viatool.h"
27
28/*
29 * http://pci-ids.ucw.cz/read/PC/8086
30 * http://en.wikipedia.org/wiki/Intel_Tick-Tock
31 * http://en.wikipedia.org/wiki/List_of_Intel_chipsets
32 * http://en.wikipedia.org/wiki/Intel_Xeon_chipsets
33 */
34static const struct {
35 uint16_t vendor_id, device_id;
36 char *name;
37} supported_chips_list[] = {
38 { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VX900_SATA, "VX900 SATA"},
39 /* Host bridges/DRAM controllers (Northbridges) */
40 { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VX900, "VX900"},
41 /* Southbridges (LPC controllers) */
42 { PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_VX900_LPC, "VX900" },
43};
44
45#ifndef __DARWIN__
46static int fd_mem;
47
48void *map_physical(uint64_t phys_addr, size_t len)
49{
50 void *virt_addr;
51
52 virt_addr = mmap(0, len, PROT_WRITE | PROT_READ, MAP_SHARED,
53 fd_mem, (off_t) phys_addr);
54
55 if (virt_addr == MAP_FAILED) {
56 printf("Error mapping physical memory 0x%08" PRIx64 "[0x%zx]\n",
57 phys_addr, len);
58 return NULL;
59 }
60
61 return virt_addr;
62}
63
64void unmap_physical(void *virt_addr, size_t len)
65{
66 munmap(virt_addr, len);
67}
68#endif
69
70void print_version(void)
71{
72 printf("inteltool v%s -- ", VIATOOL_VERSION);
73 printf("Copyright (C) 2013 Alexandru Gagniuc\n\n");
74 printf(
75 "This program is free software: you can redistribute it and/or modify\n"
76 "it under the terms of the GNU General Public License as published by\n"
77 "the Free Software Foundation, version 2 of the License.\n\n"
78 "This program is distributed in the hope that it will be useful,\n"
79 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
80 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
81 "GNU General Public License for more details.\n\n"
82 "You should have received a copy of the GNU General Public License\n"
83 "along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n");
84}
85
86void print_usage(const char *name)
87{
88 printf("usage: %s [-vh?gGrpmedPMa]\n", name);
89 printf("\n"
90 " -v | --version: print the version\n"
91 " -h | --help: print this help\n\n"
92 " -M | --msrs: dump CPU MSRs\n"
93 " -a | --all: dump all known registers\n"
94 " -q | --quirks: dump hierarchical configs\n"
95 "\n");
96 exit(1);
97}
98
99int main(int argc, char *argv[])
100{
101 struct pci_access *pacc;
102 struct pci_dev *sb = NULL, *nb, *dev;
103 int i, opt, option_index = 0;
104 unsigned int id;
105
106 char *sbname = "unknown", *nbname = "unknown";
107
108 int dump_coremsrs = 0, dump_quirks = 0;
109
110 static struct option long_options[] = {
111 {"version", 0, 0, 'v'},
112 {"help", 0, 0, 'h'},
113 {"mchbar", 0, 0, 'm'},
114 {"msrs", 0, 0, 'M'},
115 {"quirks", 0, 0, 'q'},
116 {"all", 0, 0, 'a'},
117 {0, 0, 0, 0}
118 };
119
120 while ((opt = getopt_long(argc, argv, "vh?gGrpmedPMaA",
121 long_options, &option_index)) != EOF) {
122 switch (opt) {
123 case 'v':
124 print_version();
125 exit(0);
126 break;
127 case 'M':
128 dump_coremsrs = 1;
129 break;
130 case 'q':
131 dump_quirks = 1;
132 break;
133 case 'a':
134 dump_coremsrs = 1;
135 dump_quirks = 1;
136 break;
137 case 'h':
138 case '?':
139 default:
140 print_usage(argv[0]);
141 exit(0);
142 break;
143 }
144 }
145
146#if defined(__FreeBSD__)
147 if (open("/dev/io", O_RDWR) < 0) {
148 perror("/dev/io");
149#else
150 if (iopl(3)) {
151 perror("iopl");
152#endif
153 printf("You need to be root.\n");
154 exit(1);
155 }
156
157#ifndef __DARWIN__
158 if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
159 perror("Can not open /dev/mem");
160 exit(1);
161 }
162#endif
163
164 pacc = pci_alloc();
165 pci_init(pacc);
166 pci_scan_bus(pacc);
167
168 /* Find the required devices */
169 for (dev = pacc->devices; dev; dev = dev->next) {
170 pci_fill_info(dev, PCI_FILL_CLASS);
171 /* The ISA/LPC bridge can be 0x1f, 0x07, or 0x04 so we probe. */
172 if (dev->device_class == 0x0601) { /* ISA/LPC bridge */
173 if (sb == NULL)
174 sb = dev;
175 else
176 fprintf(stderr, "Multiple devices with class ID"
177 " 0x0601, using %02x%02x:%02x.%02x\n",
178 dev->domain, dev->bus, dev->dev,
179 dev->func);
180 }
181 }
182
183 if (!sb) {
184 printf("No southbridge found.\n");
185 exit(1);
186 }
187
188 pci_fill_info(sb, PCI_FILL_IDENT|PCI_FILL_BASES|PCI_FILL_SIZES|PCI_FILL_CLASS);
189
190 if (sb->vendor_id != PCI_VENDOR_ID_VIA) {
191 printf("Not a VIA southbridge.\n");
192 exit(1);
193 }
194
195 nb = pci_get_dev(pacc, 0, 0, 0x00, 0);
196 if (!nb) {
197 printf("No northbridge found.\n");
198 exit(1);
199 }
200
201 pci_fill_info(nb, PCI_FILL_IDENT|PCI_FILL_BASES|PCI_FILL_SIZES|PCI_FILL_CLASS);
202
203 if (nb->vendor_id != PCI_VENDOR_ID_VIA) {
204 printf("Not a VIA northbridge.\n");
205 exit(1);
206 }
207
208 id = cpuid(1);
209
210 /* Intel has suggested applications to display the family of a CPU as
211 * the sum of the "Family" and the "Extended Family" fields shown
212 * above, and the model as the sum of the "Model" and the 4-bit
213 * left-shifted "Extended Model" fields.
214 * http://download.intel.com/design/processor/applnots/24161832.pdf
215 */
216 printf("CPU: Processor Type: %x, Family %x, Model %x, Stepping %x\n",
217 (id >> 12) & 0x3, ((id >> 8) & 0xf) + ((id >> 20) & 0xff),
218 ((id >> 12) & 0xf0) + ((id >> 4) & 0xf), (id & 0xf));
219
220 /* Determine names */
221 for (i = 0; i < ARRAY_SIZE(supported_chips_list); i++)
222 if (nb->device_id == supported_chips_list[i].device_id)
223 nbname = supported_chips_list[i].name;
224 for (i = 0; i < ARRAY_SIZE(supported_chips_list); i++)
225 if (sb->device_id == supported_chips_list[i].device_id)
226 sbname = supported_chips_list[i].name;
227
228 printf("Northbridge: %04x:%04x (%s)\n",
229 nb->vendor_id, nb->device_id, nbname);
230
231 printf("Southbridge: %04x:%04x (%s)\n",
232 sb->vendor_id, sb->device_id, sbname);
233
234 /* Now do the deed */
235
236 if (dump_coremsrs) {
237 print_intel_core_msrs();
238 printf("\n\n");
239 }
240
241 if (dump_quirks) {
242 print_quirks_north(nb, pacc);
243 print_quirks_south(sb, pacc);
244 }
245
246 /* Clean up */
247 pci_free_dev(nb);
248 // pci_free_dev(sb); // TODO: glibc detected "double free or corruption"
249 pci_cleanup(pacc);
250
251 return 0;
252}