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