Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Arthur Heymans | a29498f | 2017-01-24 20:27:43 +0100 | [diff] [blame] | 2 | |
| 3 | #include <getopt.h> |
| 4 | #include <stdio.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <stdint.h> |
| 10 | #include <string.h> |
| 11 | #include <errno.h> |
| 12 | #include <unistd.h> |
| 13 | #include <sys/mman.h> |
| 14 | #if defined(__GLIBC__) |
| 15 | #include <sys/io.h> |
| 16 | #endif |
| 17 | #include <pci/pci.h> |
| 18 | |
| 19 | #if defined(__sun) && (defined(__i386) || defined(__amd64)) |
| 20 | #define MEM_DEV "/dev/xsvc" |
| 21 | #else |
| 22 | #define MEM_DEV "/dev/mem" |
| 23 | #endif |
| 24 | |
| 25 | #define BUC_OFFSET 0x3414 |
| 26 | #define GCS_OFFSET 0x3410 |
| 27 | |
| 28 | #define SPIBAR 0x3800 |
| 29 | #define FDOC 0xb0 |
| 30 | #define FDOD 0xb4 |
| 31 | |
| 32 | enum { |
| 33 | DESCRIPTOR_MAP_SECTION = 0, |
| 34 | COMPONENT_SECTION, |
| 35 | REGION_SECTION, |
| 36 | MASTER_SECTION, |
| 37 | PCHSTRAPS_SECTION, |
| 38 | }; |
| 39 | |
| 40 | enum { |
Martin Roth | 90a4306 | 2021-02-14 14:30:24 -0700 | [diff] [blame] | 41 | BUCTS_UNINITIALIZED = -1, |
Arthur Heymans | a29498f | 2017-01-24 20:27:43 +0100 | [diff] [blame] | 42 | BUCTS_UNSET = 0, |
| 43 | BUCTS_SET |
| 44 | }; |
| 45 | |
| 46 | static int fd_mem = -1; |
| 47 | |
| 48 | static void print_usage(const char *name) |
| 49 | { |
| 50 | printf("usage: %s [-h?]\n", name); |
| 51 | printf("\n" |
| 52 | " -s | --set: Set the top swap bit to invert the bootblock region decoding\n" |
| 53 | " -u | --unset: Unset the top swap bit to have regular bootblock region decoding\n" |
| 54 | " -p | --print: print the current status of the hardware\n" |
| 55 | " -h | --help: print this help\n" |
| 56 | "\n"); |
| 57 | } |
| 58 | |
| 59 | static void *sys_physmap(unsigned long phys_addr, size_t len) |
| 60 | { |
| 61 | void *virt_addr = |
| 62 | mmap(0, len, PROT_WRITE | PROT_READ, MAP_SHARED, fd_mem, |
| 63 | (off_t) phys_addr); |
| 64 | return virt_addr == MAP_FAILED ? NULL : virt_addr; |
| 65 | } |
| 66 | |
| 67 | static void physunmap(void *virt_addr, size_t len) |
| 68 | { |
| 69 | if (!len) { |
| 70 | printf("Not unmapping zero size at %p\n", virt_addr); |
| 71 | return; |
| 72 | } |
| 73 | munmap(virt_addr, len); |
| 74 | } |
| 75 | |
| 76 | static void *physmap(const char *descr, unsigned long phys_addr, size_t len) |
| 77 | { |
| 78 | void *virt_addr; |
| 79 | |
| 80 | if (!descr) |
| 81 | descr = "memory"; |
| 82 | |
| 83 | if (!len) { |
| 84 | printf("Not mapping %s, zero size at 0x%08lx.\n", descr, |
| 85 | phys_addr); |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | if ((getpagesize() - 1) & len) |
| 90 | fprintf(stderr, "Unaligned size 0x%lx for %s at 0x%08lx!\n", |
| 91 | (unsigned long)len, descr, phys_addr); |
| 92 | |
| 93 | if ((getpagesize() - 1) & phys_addr) |
| 94 | fprintf(stderr, "Unaligned address 0x%08lx for %s!\n", |
| 95 | phys_addr, descr); |
| 96 | |
| 97 | virt_addr = sys_physmap(phys_addr, len); |
| 98 | if (!virt_addr) { |
| 99 | fprintf(stderr, "Error accessing 0x%lx bytes %s at 0x%08lx!\n", |
| 100 | (unsigned long)len, descr, phys_addr); |
| 101 | perror("mmap(" MEM_DEV ")"); |
| 102 | if (errno == EINVAL) { |
| 103 | fprintf(stderr, "\n"); |
| 104 | fprintf(stderr, |
| 105 | "In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n"); |
| 106 | fprintf(stderr, |
| 107 | "CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n"); |
| 108 | fprintf(stderr, |
| 109 | "Please check if either is enabled in your kernel before reporting a failure.\n"); |
| 110 | fprintf(stderr, |
| 111 | "You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n"); |
| 112 | fprintf(stderr, |
| 113 | "disabling the other option unfortunately requires a kernel recompile. Sorry!\n"); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return virt_addr; |
| 118 | } |
| 119 | |
| 120 | static int get_platform_info(struct pci_dev *sb, int *has_var_bb, |
| 121 | uint32_t *rcba_addr) |
| 122 | { |
| 123 | *has_var_bb = 0; |
| 124 | switch (sb->device_id) { |
| 125 | case 0x1c44: /* Z68 */ |
| 126 | case 0x1c46: /* P67 */ |
| 127 | case 0x1c47: /* UM67 */ |
| 128 | case 0x1c49: /* HM68 */ |
| 129 | case 0x1c4a: /* H67 */ |
| 130 | case 0x1c4b: /* HM67 */ |
| 131 | case 0x1c4c: /* Q65 */ |
| 132 | case 0x1c4d: /* QS67 */ |
| 133 | case 0x1c4e: /* Q67 */ |
| 134 | case 0x1c4f: /* QM67 */ |
| 135 | case 0x1c50: /* B65 */ |
| 136 | case 0x1c52: /* C202 */ |
| 137 | case 0x1c54: /* C204 */ |
| 138 | case 0x1c56: /* C026 */ |
| 139 | case 0x1c5c: /* H61 */ |
| 140 | case 0x1d40: /* C60x/X79 */ |
| 141 | case 0x1c41: /* C60x/X79 */ |
| 142 | case 0x1e44: /* Z77 */ |
| 143 | case 0x1e46: /* Z75 */ |
| 144 | case 0x1e47: /* Q77 */ |
| 145 | case 0x1e48: /* Q75 */ |
| 146 | case 0x1e49: /* B75 */ |
| 147 | case 0x1e4a: /* H77 */ |
| 148 | case 0x1e53: /* C216 */ |
| 149 | case 0x1e55: /* QM77 */ |
| 150 | case 0x1e56: /* QS77 */ |
| 151 | case 0x1e57: /* HM77 */ |
| 152 | case 0x1e58: /* UM77 */ |
| 153 | case 0x1e59: /* HM76 */ |
| 154 | case 0x1e5d: /* HM75 */ |
| 155 | case 0x1e5e: /* HM70 */ |
| 156 | case 0x1e5f: /* NM70 */ |
| 157 | case 0x3b02: /* P55 */ |
| 158 | case 0x3b03: /* PM55 */ |
| 159 | case 0x3b06: /* H55 */ |
| 160 | case 0x3b07: /* QM55 */ |
| 161 | case 0x3b08: /* H57 */ |
| 162 | case 0x3b09: /* HM55 */ |
| 163 | case 0x3b0a: /* Q57 */ |
| 164 | case 0x3b0b: /* HM57 */ |
| 165 | case 0x3b0d: /* PCH 3400 Mobile SFF */ |
| 166 | case 0x3b0e: /* B55 */ |
| 167 | case 0x3b0f: /* QS57 */ |
| 168 | case 0x3b12: /* PCH 3400 */ |
| 169 | case 0x3b14: /* PCH 3420 */ |
| 170 | case 0x3b16: /* PCH 3450 */ |
| 171 | case 0x3b1e: /* B55 */ |
| 172 | case 0x8c40: /* Lynx Point */ |
| 173 | case 0x8c41: /* Lynx Point Mobile Eng. Sample */ |
| 174 | case 0x8c42: /* Lynx Point Desktop Eng. Sample */ |
| 175 | case 0x8c43: /* Lynx Point */ |
| 176 | case 0x8c44: /* Z87 */ |
| 177 | case 0x8c45: /* Lynx Point */ |
| 178 | *has_var_bb = 1; |
| 179 | /* fallthrough */ |
| 180 | case 0x2640: /* ICH6/ICH6R */ |
| 181 | case 0x2641: /* ICH6-M */ |
| 182 | case 0x2642: /* ICH6W/ICH6RW */ |
| 183 | case 0x2670: /* 631xESB/632xESB/3100 */ |
| 184 | case 0x27b0: /* ICH7DH */ |
| 185 | case 0x27b8: /* ICH7/ICH7R */ |
| 186 | case 0x27b9: /* ICH7M */ |
| 187 | case 0x27bd: /* ICH7MDH */ |
| 188 | case 0x27bc: /* NM10 */ |
| 189 | case 0x2810: /* ICH8/ICH8R */ |
| 190 | case 0x2811: /* ICH8M-E */ |
| 191 | case 0x2812: /* ICH8DH */ |
| 192 | case 0x2814: /* ICH8DO */ |
| 193 | case 0x2815: /* ICH8M */ |
| 194 | case 0x2910: /* ICH9 Eng. Sample */ |
| 195 | case 0x2912: /* ICH9DH */ |
| 196 | case 0x2914: /* ICH9DO */ |
| 197 | case 0x2916: /* ICH9R */ |
| 198 | case 0x2917: /* ICH9M-E */ |
| 199 | case 0x2918: /* ICH9 */ |
| 200 | case 0x2919: /* ICH9M */ |
| 201 | case 0x3a10: /* ICH10R Eng. Sample */ |
| 202 | case 0x3a14: /* ICH10D0 */ |
| 203 | case 0x3a16: /* ICH10R */ |
| 204 | case 0x3a18: /* ICH10 */ |
| 205 | case 0x3a1a: /* ICH10D */ |
| 206 | case 0x3a1e: /* ICH10 Eng. Sample*/ |
| 207 | case 0x3b00: /* PCH 3400 Desktop */ |
| 208 | case 0x3b01: /* PCH 3400 Mobile */ |
| 209 | *rcba_addr = pci_read_long(sb, 0xf0) & ~1; |
| 210 | return 0; |
| 211 | default: |
| 212 | fprintf(stderr, "Unsupported LPC bridge. Sorry.\n"); |
| 213 | return 1; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | int print_status(uint32_t rcba_addr, int has_var_bb) |
| 218 | { |
| 219 | int ret = 0; |
| 220 | volatile uint8_t *rcba; |
| 221 | int bild, ts, ts_size = 64 << 10; |
| 222 | rcba = physmap("RCBA", rcba_addr, 0x4000); |
| 223 | if (!rcba) |
| 224 | return 1; |
| 225 | |
| 226 | bild = rcba[GCS_OFFSET] & 1; |
| 227 | ts = rcba[BUC_OFFSET] & 1; |
| 228 | int spi_is_bootdevice = |
| 229 | ((*(uint16_t *)(rcba + GCS_OFFSET) >> 10) & 3) == 3; |
| 230 | |
| 231 | if (has_var_bb && spi_is_bootdevice) { |
| 232 | *(uint32_t *)(rcba + SPIBAR + FDOC) = |
| 233 | (PCHSTRAPS_SECTION << 12) | 0; |
| 234 | uint32_t pchstrap0 = *(uint32_t *)(rcba + SPIBAR + FDOD); |
| 235 | switch (pchstrap0 >> 29) { |
| 236 | case 0: |
| 237 | ts_size = 64 << 10; |
| 238 | break; |
| 239 | case 1: |
| 240 | ts_size = 128 << 10; |
| 241 | break; |
| 242 | case 2: |
| 243 | ts_size = 256 << 10; |
| 244 | break; |
| 245 | default: |
| 246 | printf("Unknown BIOS Boot-Block size (BBBS)\n"); |
| 247 | ret = 1; |
| 248 | goto out; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | printf("\nSystem status\n=============\n"); |
| 253 | printf("BILD (BIOS Interface Lock-Down) : %s\n", bild |
| 254 | ? "set, changing top swap not possible" : "unset, changing top swap is possible"); |
| 255 | printf("Top Swap : %s\n", ts ? "Enabled" : "Disabled"); |
| 256 | printf("BIOS Boot-block Size (BBDS) : %dK\n", ts_size >> 10); |
| 257 | if (ts) { |
| 258 | printf("Ranges swapped : 0x%llx-0x%08llx <-> 0x%08llx-0x%08llx", |
| 259 | ((4ULL << 30) - ts_size), |
| 260 | (4ULL << 30) - 1, |
| 261 | (4ULL << 30) - 2 * ts_size, |
| 262 | (4ULL << 30) - ts_size - 1); |
| 263 | } |
| 264 | |
| 265 | out: |
| 266 | physunmap((void *)rcba, 0x4000); |
| 267 | return ret; |
| 268 | } |
| 269 | |
| 270 | static int set_bucts(uint32_t rcba_addr, int bucts_state) |
| 271 | { |
| 272 | int ret = 0; |
| 273 | volatile uint8_t *rcba; |
| 274 | int bild; |
| 275 | rcba = physmap("RCBA", rcba_addr, 0x4000); |
| 276 | if (!rcba) |
| 277 | return 1; |
| 278 | |
| 279 | bild = rcba[GCS_OFFSET] & 1; |
| 280 | if (bild) { |
| 281 | printf("BIOS Interface Lock-Down set, Changing Top Swap not possible.\n"); |
| 282 | ret = 1; |
| 283 | goto out; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | rcba[BUC_OFFSET] = bucts_state & 1; |
| 288 | if ((rcba[BUC_OFFSET] & 1) != bucts_state) { |
| 289 | fprintf(stderr, "Hu?! setting the top swap bit failed!\n"); |
| 290 | ret = 1; |
| 291 | } else { |
| 292 | printf("Setting top swap bit succeeded.\n"); |
| 293 | ret = 0; |
| 294 | } |
| 295 | |
| 296 | out: |
| 297 | physunmap((void *)rcba, 0x4000); |
| 298 | return ret; |
| 299 | } |
| 300 | |
Martin Roth | 90a4306 | 2021-02-14 14:30:24 -0700 | [diff] [blame] | 301 | int main(int argc, char *argv[]) |
Arthur Heymans | a29498f | 2017-01-24 20:27:43 +0100 | [diff] [blame] | 302 | { |
| 303 | struct pci_access *pacc; |
| 304 | struct pci_dev *dev, *sb = NULL; |
| 305 | #if defined(__FreeBSD__) |
| 306 | int io_fd; |
| 307 | #endif |
| 308 | int opt, option_index = 0; |
| 309 | int print_state = 0; |
Martin Roth | 90a4306 | 2021-02-14 14:30:24 -0700 | [diff] [blame] | 310 | int bucts_state = BUCTS_UNINITIALIZED; |
Arthur Heymans | a29498f | 2017-01-24 20:27:43 +0100 | [diff] [blame] | 311 | |
| 312 | static struct option long_options[] = { |
| 313 | {"unset", 0, 0, 'u'}, |
| 314 | {"set", 0, 0, 's'}, |
| 315 | {"print", 0, 0, 'p'}, |
| 316 | {"help", 0, 0, 'h'}, |
| 317 | {0, 0, 0, 0} |
| 318 | }; |
| 319 | |
| 320 | |
| 321 | printf("bucts utility version '" VERSION "'\n"); |
| 322 | |
| 323 | if (argv[1] == NULL) { |
| 324 | print_usage(argv[0]); |
| 325 | exit(1); |
| 326 | } |
| 327 | |
| 328 | while ((opt = getopt_long(argc, argv, "usph?:", |
| 329 | long_options, &option_index)) != EOF) { |
| 330 | switch (opt) { |
| 331 | case 'u': |
Martin Roth | 90a4306 | 2021-02-14 14:30:24 -0700 | [diff] [blame] | 332 | if (bucts_state != BUCTS_UNINITIALIZED) { |
Arthur Heymans | a29498f | 2017-01-24 20:27:43 +0100 | [diff] [blame] | 333 | printf("Invalid setting: multiple set/unset arguments\n"); |
| 334 | exit(1); |
| 335 | } |
| 336 | if (print_state) { |
| 337 | printf("Print and Set are mutually exclusive!\n"); |
| 338 | exit(1); |
| 339 | } |
| 340 | bucts_state = BUCTS_UNSET; |
Arthur Heymans | a29498f | 2017-01-24 20:27:43 +0100 | [diff] [blame] | 341 | break; |
| 342 | case 's': |
Martin Roth | 90a4306 | 2021-02-14 14:30:24 -0700 | [diff] [blame] | 343 | if (bucts_state != BUCTS_UNINITIALIZED) { |
Arthur Heymans | a29498f | 2017-01-24 20:27:43 +0100 | [diff] [blame] | 344 | printf("Invalid setting: multiple set/unset arguments\n"); |
| 345 | exit(1); |
| 346 | } |
| 347 | if (print_state) { |
| 348 | printf("Print and Set are mutually exclusive!\n"); |
| 349 | exit(1); |
| 350 | } |
| 351 | bucts_state = BUCTS_SET; |
Arthur Heymans | a29498f | 2017-01-24 20:27:43 +0100 | [diff] [blame] | 352 | break; |
| 353 | case 'p': |
Martin Roth | 90a4306 | 2021-02-14 14:30:24 -0700 | [diff] [blame] | 354 | if (bucts_state != BUCTS_UNINITIALIZED) { |
Arthur Heymans | a29498f | 2017-01-24 20:27:43 +0100 | [diff] [blame] | 355 | printf("Print and Set are mutually exclusive!\n"); |
| 356 | exit(1); |
| 357 | } |
| 358 | print_state = 1; |
| 359 | break; |
| 360 | case 'h': |
| 361 | print_usage(argv[0]); |
| 362 | exit(0); |
| 363 | case '?': |
| 364 | default: |
| 365 | print_usage(argv[0]); |
| 366 | exit(1); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | if (optind < argc) { |
| 371 | fprintf(stderr, "Error: Extra parameter found.\n"); |
| 372 | print_usage(argv[0]); |
| 373 | exit(1); |
| 374 | } |
| 375 | |
| 376 | |
| 377 | #if defined(__FreeBSD__) |
| 378 | if ((io_fd = open("/dev/io", O_RDWR)) < 0) { |
| 379 | perror("open(/dev/io)"); |
| 380 | #else |
| 381 | if (iopl(3)) { |
| 382 | perror("iopl"); |
| 383 | #endif |
| 384 | printf("You need to be root.\n"); |
| 385 | return 1; |
| 386 | } |
| 387 | |
| 388 | if ((fd_mem = open(MEM_DEV, O_RDWR | O_SYNC)) == -1) { |
| 389 | perror("Error: open(" MEM_DEV ")"); |
| 390 | return 1; |
| 391 | } |
| 392 | |
| 393 | pacc = pci_alloc(); |
| 394 | pci_init(pacc); |
| 395 | pci_scan_bus(pacc); |
| 396 | for (dev = pacc->devices; dev && !sb; dev = dev->next) { |
| 397 | pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_CLASS); |
| 398 | if (dev->vendor_id != 0x8086 || dev->device_class != 0x0601) |
| 399 | continue; |
| 400 | sb = dev; |
| 401 | } |
| 402 | if (!sb) { |
| 403 | fprintf(stderr, "Error: LPC bridge not found!\n"); |
| 404 | return 1; |
| 405 | } |
| 406 | |
| 407 | printf("Using LPC bridge %04x:%04x at %02x%02x:%02x.%02x\n", |
| 408 | sb->vendor_id, sb->device_id, sb->domain, sb->bus, sb->dev, |
| 409 | sb->func); |
| 410 | |
| 411 | int has_var_bb; |
| 412 | uint32_t rcba_addr; |
| 413 | |
| 414 | if (get_platform_info(sb, &has_var_bb, &rcba_addr)) |
| 415 | exit(1); |
| 416 | |
| 417 | if (print_state) |
| 418 | print_status(rcba_addr, has_var_bb); |
Martin Roth | 90a4306 | 2021-02-14 14:30:24 -0700 | [diff] [blame] | 419 | if (bucts_state != BUCTS_UNINITIALIZED) { |
Arthur Heymans | a29498f | 2017-01-24 20:27:43 +0100 | [diff] [blame] | 420 | set_bucts(rcba_addr, bucts_state); |
| 421 | print_status(rcba_addr, has_var_bb); |
| 422 | } |
| 423 | |
| 424 | close(fd_mem); |
| 425 | return 0; |
| 426 | } |