/* SPDX-License-Identifier: MIT OR Apache-2.0 * * amd-ras: fstart's AMD RAS checks, for a disposable RAM-only Linux on a * test bench (tools/hwtest/linux-ras). One program for every AMD board: * SP3 (Rome, Milan), SP4r2 (Zeppelin) and the RS SoCs (Genoa, Siena). * * amd-ras info the RAS tables, BERT's records, the SMM journal * amd-ras smm every CPU: the SMI trigger, SMM's thresholds or PFEH * amd-ras probe one benign SEL event through SMM to the BMC * amd-ras einj TYPE ADDR [N] N memory injections of TYPE at ADDR, each consumed * amd-ras einj-pcie TYPE SBDF one PCIe injection at seg:bus:dev.fn * amd-ras aer SBDF a function's AER status registers * amd-ras sp3-einj [N] N of SP3's correctable injections on each of its * own targets * amd-ras plant CPU BANK STATUS [ADDR] * write MCA_STATUS (HWCR.McStatusWrEn), which a * warm reset keeps for the next boot's BERT * amd-ras plant-reset CPU BANK STATUS [ADDR] * the same, a mark in the RTC's CMOS of what was * planted, then at once a warm reset (CF9h) * amd-ras plant-check after a plant-reset: 0 if the BERT holds the * planted bank and status, 1 if not; 2 if there * was no plant (no mark). Clears the mark: one * plant per mark * amd-ras mark-ue / ue-check the same mark around an uncorrectable * injection: ue-check 0 if the BERT has a record, * 1 if not, 2 without a mark * amd-ras espi the FCH eSPI decodes and the KCS status ports * amd-ras misc BANK BANK's thresholding counter on every CPU * amd-ras kcs PORT SPACING one IPMI Get Device ID on a KCS channel * amd-ras sel PORT SPACING the SMM probe's SEL record, sent from the OS * amd-ras smn ADDR one SMN register (read only) * amd-ras storm SMM's storm state: interrupts or polling, the * periodic SMI, deferred reports * amd-ras ce-burst ADDR N N correctable injections at ADDR back to back * (a storm), then until SMM interrupts again * amd-ras ce-flood ADDR N one correctable injection at ADDR, then N uncached * reads of it (a storm at memory speed) * amd-ras ipmi-load SECONDS [PAUSE_MS] * the OS's IPMI driver busy: Get Device ID through * /dev/ipmi0 back to back (or PAUSE_MS apart); * every one must answer * amd-ras ipmi-sel Get SEL Info through /dev/ipmi0 * * It never mounts or writes a disk. `einj` with an uncorrectable TYPE and * `plant` are meant to take the machine down; run them last. */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define EINJ "/sys/kernel/debug/apei/einj/" #define DIAG_REQUEST 0x46535241530000d1ULL #define HWCR 0xc0010015 #define MCSTATUS_WREN (1ULL << 18) static unsigned failures; #define FAIL(...) do { printf("RAS FAIL " __VA_ARGS__); putchar('\n'); ++failures; } while (0) /* --- MSRs, CPUs and the SMM diagnostic ABI ------------------------------ */ static int msr_open(unsigned cpu, int flags) { char path[64]; snprintf(path, sizeof(path), "/dev/cpu/%u/msr", cpu); return open(path, flags); } static int rdmsr(int fd, uint32_t reg, uint64_t *value) { return pread(fd, value, sizeof(*value), reg) == sizeof(*value) ? 0 : -1; } static int wrmsr(int fd, uint32_t reg, uint64_t value) { return pwrite(fd, &value, sizeof(value), reg) == sizeof(value) ? 0 : -1; } static int pin(unsigned cpu) { cpu_set_t cpus; CPU_ZERO(&cpus); CPU_SET(cpu, &cpus); return sched_setaffinity(0, sizeof(cpus), &cpus); } /* The online CPUs (/sys/devices/system/cpu/online, "0-3,8,10-11"): what * an every-CPU check must have observed. Returns their count, -1 if the * list cannot be read. */ #define MAX_CPUS 1024 static int online_cpus(unsigned char online[MAX_CPUS]) { memset(online, 0, MAX_CPUS); FILE *f = fopen("/sys/devices/system/cpu/online", "r"); if (!f) return -1; char list[4096]; int count = 0; if (!fgets(list, sizeof(list), f)) { fclose(f); return -1; } fclose(f); for (char *range = strtok(list, ",\n"); range; range = strtok(NULL, ",\n")) { unsigned first, last; int n = sscanf(range, "%u-%u", &first, &last); if (n == 1) last = first; else if (n != 2 || last < first) return -1; for (unsigned cpu = first; cpu <= last; ++cpu) { if (cpu >= MAX_CPUS) return -1; count += !online[cpu]; online[cpu] = 1; } } return count; } struct diag { uint64_t a, c, d; }; /* The SMM handler's register-only diagnostic protocol (SW SMI D1, * fstart_platform_amd::smm::ras::DIAG_COMMAND): only the context carrying * the full request token is written. The SMI may arrive a few instructions * after the OUT, so the registers stay live in a spin after it; a request * that found no SMM handler is retried. */ static struct diag diag(uint64_t selector) { struct diag r = {0}; for (int attempt = 0; attempt < 20; ++attempt) { usleep(1000); /* let the FCH's previous SMI edge deassert */ r = (struct diag){DIAG_REQUEST, selector, 0xb2}; uint64_t spin = 100000; __asm__ volatile("outb %%al, %%dx\n" "1: pause\n" "dec %3\n" "jnz 1b" : "+a"(r.a), "+c"(r.c), "+d"(r.d), "+r"(spin) : : "memory"); if (r.a != DIAG_REQUEST || r.d != 0xb2) break; } return r; } static int smi_port(void) { if (ioperm(0xb2, 1, 1)) { perror("RAS ioperm 0xb2"); return -1; } return 0; } /* --- EDAC and EINJ ------------------------------------------------------- */ static long edac(const char *what) { char pattern[64]; snprintf(pattern, sizeof(pattern), "/sys/devices/system/edac/mc/mc*/%s", what); glob_t paths = {0}; if (glob(pattern, 0, NULL, &paths)) return -1; long total = 0; for (size_t i = 0; i < paths.gl_pathc; ++i) { FILE *f = fopen(paths.gl_pathv[i], "r"); long count; if (!f || fscanf(f, "%ld", &count) != 1) { if (f) fclose(f); globfree(&paths); return -1; } total += count; fclose(f); } globfree(&paths); return total; } static void debugfs(void) { mount("debugfs", "/sys/kernel/debug", "debugfs", 0, NULL); } static int put(const char *name, const char *value) { char path[128]; snprintf(path, sizeof(path), EINJ "%s", name); int fd = open(path, O_WRONLY); if (fd < 0) { perror(path); return -1; } usleep(1000); /* busybox printf to debugfs fails silently; write(2) does not. */ ssize_t n = write(fd, value, strlen(value)); int error = errno; close(fd); if (n != (ssize_t)strlen(value)) { printf("RAS EINJ %s=%s: %s\n", name, value, strerror(error)); errno = error; return -1; } return 0; } static unsigned available_types(void) { FILE *f = fopen(EINJ "available_error_type", "r"); if (!f) { perror("RAS EINJ available_error_type"); return 0; } char line[256]; unsigned types = 0; while (fgets(line, sizeof(line), f)) { unsigned type; if (sscanf(line, "%x", &type) == 1) types |= type; } fclose(f); printf("RAS EINJ available types %#x\n", types); return types; } /* One read of the cache line at `address` through /dev/mem: the access * that consumes an injected error. */ static void touch(uint64_t address) { int fd = open("/dev/mem", O_RDONLY | O_SYNC); if (fd < 0) { perror("RAS /dev/mem"); return; } uint64_t line[8]; if (pread(fd, line, sizeof(line), address & ~63ULL) != sizeof(line)) perror("RAS /dev/mem read"); close(fd); } static int inject(unsigned type, const char *flags, const char *p1, const char *p2, const char *p4) { char t[16]; snprintf(t, sizeof(t), "%#x", type); if (put("error_type", t) || put("flags", flags)) return -1; if (p1 && put("param1", p1)) return -1; if (p2 && put("param2", p2)) return -1; if (p4 && put("param4", p4)) return -1; return put("error_inject", "1"); } /* --- ACPI tables and BERT ------------------------------------------------ */ static long table(const char *path, unsigned char *buffer, size_t size) { FILE *f = fopen(path, "rb"); if (!f) return -1; long n = (long)fread(buffer, 1, size, f); fclose(f); return n; } static uint32_t le32(const unsigned char *p) { return p[0] | p[1] << 8 | p[2] << 16 | (uint32_t)p[3] << 24; } static uint64_t le64(const unsigned char *p) { return le32(p) | (uint64_t)le32(p + 4) << 32; } static const unsigned char PROC_IA[16] = {0xb0, 0xa0, 0x3e, 0xdc, 0x44, 0xa1, 0x97, 0x47, 0xb9, 0x5b, 0x53, 0xfa, 0x24, 0x2b, 0x6e, 0x1d}; /* The boot error region (ACPI 6.5 §18.3.1): generic error status blocks, * each of generic error data entries. Prints the MCA banks of the x86 * processor sections. Returns the records found. Linux clears a block's * status once it printed it: after boot, its log is the evidence. */ static unsigned bert_match(unsigned bank, uint64_t status, unsigned *matches); static unsigned bert(void) { unsigned matches; return bert_match(~0u, 0, &matches); } /* bert(), also counting the MCA records of `bank` with `status`. */ static unsigned bert_match(unsigned want_bank, uint64_t want_status, unsigned *matches) { *matches = 0; static unsigned char region[1 << 20]; long length = table("/sys/firmware/acpi/tables/data/BERT", region, sizeof(region)); if (length < 0) { puts("RAS BERT none"); return 0; } unsigned records = 0; for (long block = 0; block + 20 <= length;) { const unsigned char *b = region + block; uint32_t status = le32(b), data = le32(b + 12), severity = le32(b + 16); if (!status && !data) break; printf("RAS BERT block @%ld status=%#x data=%u severity=%u\n", block, status, data, severity); for (uint32_t entry = 20; entry + 72 <= 20 + data && block + entry + 72 <= length;) { const unsigned char *e = b + entry; uint32_t sev = le32(e + 16), len = le32(e + 24); const unsigned char *section = e + 72; if (!memcmp(e, PROC_IA, 16) && block + entry + 72 + len <= length && len >= 64) { uint64_t valid = le64(section), lapic = le64(section + 8); unsigned errors = valid >> 2 & 0x3f, contexts = valid >> 8 & 0x3f; const unsigned char *ctx = section + 64 + 64 * errors; for (unsigned c = 0; c < contexts && ctx + 16 <= section + len; ++c) { unsigned type = ctx[0] | ctx[1] << 8, size = ctx[2] | ctx[3] << 8; uint32_t msr = le32(ctx + 4); if (type == 1 && size >= 40) { printf("RAS BERT mca severity=%u apic=%#" PRIx64 " bank=%u status=%016" PRIx64 " addr=%016" PRIx64 " ipid=%016" PRIx64 "\n", sev, lapic, (msr >> 4) & 0xff, le64(ctx + 16), le64(ctx + 24), le64(ctx + 48)); *matches += ((msr >> 4) & 0xff) == want_bank && le64(ctx + 16) == want_status; } ctx += (16 + size + 15) & ~15u; } ++records; } else { printf("RAS BERT section severity=%u length=%u (not x86)\n", sev, len); ++records; } entry += 72 + len; } block += 20 + data; } printf("RAS BERT records=%u\n", records); return records; } static int info(void) { static unsigned char buffer[1 << 20]; const char *names[] = {"HEST", "BERT", "EINJ", "ERST", "SPMI"}; for (unsigned i = 0; i < sizeof(names) / sizeof(*names); ++i) { char path[64]; snprintf(path, sizeof(path), "/sys/firmware/acpi/tables/%s", names[i]); long n = table(path, buffer, sizeof(buffer)); if (n < 0) printf("RAS table %s absent\n", names[i]); else printf("RAS table %s length=%ld oem=%.6s/%.8s\n", names[i], n, buffer + 10, buffer + 16); } long n = table("/sys/firmware/acpi/tables/HEST", buffer, sizeof(buffer)); if (n >= 40) { uint32_t count = le32(buffer + 36); printf("RAS HEST sources=%u\n", count); for (long off = 40; off + 4 <= n && count--;) { unsigned type = buffer[off] | buffer[off + 1] << 8, id = buffer[off + 2] | buffer[off + 3] << 8; long size; switch (type) { case 0: size = 40 + 28 * buffer[off + 32]; printf("RAS HEST %u: MCE flags=%#x banks=%u\n", id, buffer[off + 6], buffer[off + 32]); break; case 1: size = 48 + 28 * buffer[off + 44]; printf("RAS HEST %u: CMC flags=%#x threshold=%u banks=%u\n", id, buffer[off + 6], le32(buffer + off + 36), buffer[off + 44]); break; case 9: size = 64; printf("RAS HEST %u: GHES related=%#x notify=%u block=%#" PRIx64 "\n", id, buffer[off + 4] | buffer[off + 5] << 8, buffer[off + 32], le64(buffer + off + 24)); break; case 10: size = 92; printf("RAS HEST %u: GHESv2 notify=%u\n", id, buffer[off + 32]); break; default: printf("RAS HEST %u: type %u\n", id, type); size = n; break; } off += size; } } bert(); return 0; } /* --- the subcommands ---------------------------------------------------- */ /* On every CPU: the trigger I/O cycle B2/80h armed, the cloak and deferred * masks clear (the OS keeps every bank), and either PFEH on in SMM with * #MC redirected, or SMM's thresholding counters locked in the banks' * MCA_MISC0 (IntType SMI). */ static int smm_check(void) { if (smi_port()) return 1; static unsigned char online[MAX_CPUS]; int expected = online_cpus(online); if (expected <= 0) { FAIL("the online CPUs"); return 1; } unsigned present = 0, armed_cpus = 0, pfeh_cpus = 0; for (unsigned cpu = 0; cpu < MAX_CPUS; ++cpu) { if (!online[cpu]) continue; int fd = msr_open(cpu, O_RDONLY); if (fd < 0) { FAIL("cpu %u online, its MSRs unreadable", cpu); continue; } if (pin(cpu)) { FAIL("cpu %u online, not schedulable", cpu); close(fd); continue; } ++present; uint64_t redir = 0, trigger = 0, cap = 0; rdmsr(fd, 0xc0010022, &redir); rdmsr(fd, 0xc0010056, &trigger); rdmsr(fd, 0x179, &cap); unsigned locked = 0, banks = cap & 0xff; for (unsigned bank = 0; bank < banks && bank < 64; ++bank) { uint64_t misc = 0; rdmsr(fd, 0xc0002003 + 16 * bank, &misc); /* IntType SMI, or none while SMM polls a storm. */ locked += (misc >> 61 & 1) && (misc >> 49 & 3) != 1 && (misc >> 51 & 1); } struct diag smm = diag(0); int pfeh = (smm.a & 1) && (redir & 0x300) == 0x200; int armed = (trigger & 0x06ffffff) == 0x028000b2 && smm.c == 0 && smm.d == 0 && (pfeh || locked); if (cpu < 4 || !armed) printf("RAS cpu=%u redir=%016" PRIx64 " trigger=%016" PRIx64 " locked_thresholds=%u/%u" " pfeh_smm=%016" PRIx64 " cloak=%016" PRIx64 " mask=%016" PRIx64 " %s\n", cpu, redir, trigger, locked, banks, smm.a, smm.c, smm.d, armed ? "ARMED" : "NOT_ARMED"); armed_cpus += armed; pfeh_cpus += pfeh; close(fd); } pin(0); struct diag journal = diag(4), layout = diag(3); printf("RAS SMM online=%d observed=%u armed=%u pfeh=%u journal=%" PRIu64 " dropped=%" PRIu64 " refused=%" PRIu64 " save_state=%" PRIu64 "/%" PRIu64 "/%#" PRIx64 "\n", expected, present, armed_cpus, pfeh_cpus, journal.a, journal.c, journal.d, layout.a, layout.c, layout.d); if (present != (unsigned)expected || armed_cpus != (unsigned)expected) FAIL("not every online CPU is observed and armed"); return failures ? 1 : 0; } static int probe(void) { if (smi_port() || pin(0)) return 1; struct timespec start, end; clock_gettime(CLOCK_MONOTONIC, &start); /* On a channel the OS shares, a busy interface defers the probe to * SMM's periodic SMI (a second later): poll for up to 10 s. */ struct diag result = diag(1); for (unsigned i = 0; i < 100 && (result.a == 1 || result.a == 4); ++i) { if (i >= 8) usleep(100000); result = diag(2); } clock_gettime(CLOCK_MONOTONIC, &end); uint64_t us = (end.tv_sec - start.tv_sec) * 1000000ULL + (end.tv_nsec - start.tv_nsec) / 1000; printf("RAS SEL_PROBE result=%" PRIu64 " elapsed_us=%" PRIu64 " (2=success,3=failure)\n", result.a, us); return result.a == 2 ? 0 : 1; } static uint64_t journal(void) { if (smi_port() || pin(0)) return UINT64_MAX; return diag(4).a; } /* N memory injections at ADDR, each consumed by a read. A correctable * TYPE must raise EDAC's CE count each time (Linux polls the banks: set * check_interval to 1); with N at least the board's threshold, the SMM * journal must grow by the thresholding events. An uncorrectable TYPE * takes the machine down at the read. */ static int einj(unsigned type, uint64_t address, unsigned count) { debugfs(); if (!(available_types() & type)) { FAIL("EINJ type %#x not available", type); return 1; } char p1[32]; snprintf(p1, sizeof(p1), "%#" PRIx64, address); uint64_t before_journal = journal(); long total = edac("ce_count"); if ((type & 0x8) && total < 0) { FAIL("no EDAC CE counters to check the injections with"); return 1; } unsigned seen = 0; for (unsigned i = 0; i < count; ++i) { long before = edac("ce_count"); if (inject(type, "2", p1, "0xfffffffffffff000", NULL)) { FAIL("EINJ inject %u", i); return 1; } printf("RAS EINJ %u type=%#x address=%s injected, consuming\n", i, type, p1); fflush(stdout); touch(address); if (type & 0x8) { long after = before; for (int t = 0; t < 40 && before >= 0 && after >= 0 && after <= before; ++t) { usleep(100000); after = edac("ce_count"); } /* Both counters read: a failed read is no observation. */ if (before < 0 || after < 0) FAIL("EINJ %u: EDAC CE count unreadable (%ld -> %ld)", i, before, after); else seen += after > before; printf("RAS EINJ %u ce %ld -> %ld\n", i, before, after); } } sleep(1); uint64_t after_journal = journal(); printf("RAS EINJ done type=%#x count=%u ce_total %ld -> %ld seen=%u journal %" PRIu64 " -> %" PRIu64 "\n", type, count, total, edac("ce_count"), seen, before_journal, after_journal); if ((type & 0x8) && seen != count) FAIL("EDAC missed %u of %u corrected errors", count - seen, count); return failures ? 1 : 0; } /* The AER status of a PCI function (its extended capability 0001h): * uncorrectable and correctable status, root error status (ports). */ static void aer_status(unsigned seg, unsigned bus, unsigned dev, unsigned fn, const char *when) { char path[96]; snprintf(path, sizeof(path), "/sys/bus/pci/devices/%04x:%02x:%02x.%x/config", seg, bus, dev, fn); static unsigned char config[4096]; if (table(path, config, sizeof(config)) < 4096) { printf("RAS AER %s: no extended config space\n", path); return; } for (unsigned off = 0x100, n = 0; off && off < 4096 && n < 64; ++n) { uint32_t header = le32(config + off); if ((header & 0xffff) == 1) { printf("RAS AER %s %04x:%02x:%02x.%x @%#x uncorrectable=%08x mask=%08x severity=%08x" " correctable=%08x mask=%08x root_command=%08x root_status=%08x source=%08x\n", when, seg, bus, dev, fn, off, le32(config + off + 4), le32(config + off + 8), le32(config + off + 12), le32(config + off + 16), le32(config + off + 20), le32(config + off + 0x2c), le32(config + off + 0x30), le32(config + off + 0x34)); return; } off = header >> 20; } printf("RAS AER %04x:%02x:%02x.%x: no AER capability\n", seg, bus, dev, fn); } /* The kernel log from now on (/dev/kmsg, at its end). */ static int kmsg_from_now(void) { int fd = open("/dev/kmsg", O_RDONLY | O_NONBLOCK); if (fd >= 0) lseek(fd, 0, SEEK_END); return fd; } /* Whether a record after `fd`'s start holds `needle` (and `also`, if * given) within `seconds`. */ static int kmsg_wait(int fd, const char *needle, const char *also, int seconds) { char record[2048]; for (int waited = 0; waited < seconds * 10;) { ssize_t n = read(fd, record, sizeof(record) - 1); if (n < 0 && errno == EAGAIN) { usleep(100000); ++waited; continue; } if (n <= 0) continue; record[n] = 0; if (strstr(record, needle) && (!also || strstr(record, also))) { printf("RAS kmsg: %s", strchr(record, ';') ? strchr(record, ';') + 1 : record); return 1; } } return 0; } /* One PCIe injection at SBDF: the EINJ write must succeed, and the AER * driver must report an error message from that function after it (a * correctable one for TYPE 40h), not one an earlier test left in the log. */ static int einj_pcie(unsigned type, const char *where) { unsigned seg, bus, dev, fn; if (sscanf(where, "%x:%x:%x.%x", &seg, &bus, &dev, &fn) != 4) { FAIL("SBDF %s", where); return 1; } debugfs(); if (!(available_types() & type)) { FAIL("EINJ type %#x not available", type); return 1; } /* ACPI 6.5 table 18.30: segment, bus, device/function, reserved. */ char p4[32]; snprintf(p4, sizeof(p4), "%#x", seg << 24 | bus << 16 | (dev << 3 | fn) << 8); aer_status(seg, bus, dev, fn, "before"); int log = kmsg_from_now(); if (log < 0) { FAIL("/dev/kmsg"); return 1; } if (inject(type, "4", NULL, NULL, p4)) { FAIL("EINJ PCIe inject"); close(log); return 1; } printf("RAS EINJ PCIe type=%#x sbdf=%s param4=%s injected\n", type, where, p4); /* The injected error hits a later transaction of the function, and its * report can take seconds (the Onyx: up to about ten): read its config * space once a second, for traffic, while waiting for the report. */ char needle[64], path[96]; snprintf(needle, sizeof(needle), "rror message received from %04x:%02x:%02x.%x", seg, bus, dev, fn); snprintf(path, sizeof(path), "/sys/bus/pci/devices/%04x:%02x:%02x.%x/config", seg, bus, dev, fn); int reported = 0; for (int second = 0; second < 30 && !reported; ++second) { unsigned char header[64]; table(path, header, sizeof(header)); reported = kmsg_wait(log, needle, type == 0x40 ? "Correctable" : NULL, 1); if (reported) printf("RAS EINJ PCIe reported after about %d s\n", second + 1); } if (!reported) FAIL("no AER report from %04x:%02x:%02x.%x in 30 s after the injection", seg, bus, dev, fn); close(log); aer_status(seg, bus, dev, fn, "after"); return failures ? 1 : 0; } /* The fixed SP3 EINJ page of fstart's Rome firmware: one firmware-owned * correctable target per socket (crates/platform-amd/src/sp3/einj.rs). */ #define SP3_BASE 0x75bf0000ULL static int sp3_einj(unsigned count) { unsigned char header[16]; if (table("/sys/firmware/acpi/tables/FACP", header, sizeof(header)) < 16 || memcmp(header + 10, "FSTART", 6)) { puts("RAS SP3 EINJ: not fstart firmware; refusing"); return 1; } int fd = open("/dev/mem", O_RDONLY | O_SYNC); if (fd < 0) return 1; volatile uint64_t *p = mmap(NULL, 4096, PROT_READ, MAP_SHARED, fd, SP3_BASE); if (p == MAP_FAILED) { close(fd); return 1; } uint64_t sockets = p[66] <= 2 ? p[66] : 0, targets[2] = {p[64], p[65]}; printf("RAS SP3 EINJ supported=%" PRIx64 " sockets=%" PRIu64 "\n", p[0], sockets); munmap((void *)p, 4096); close(fd); for (unsigned s = 0; s < sockets; ++s) { if (!targets[s] || targets[s] & 63) { FAIL("socket %u target", s); return 1; } if (einj(8, targets[s], count)) return 1; } if (!sockets) FAIL("no SP3 EINJ targets"); return failures ? 1 : 0; } /* A mark across the warm reset, in the RTC's upper CMOS bank (ports * 72h/73h; nothing of fstart's uses it): a magic byte, what was done, the * bank and the status as the bank read it back. Battery backed: a run * that never came back leaves it, and the next check reports it. */ #define MARK 0xe0 #define MARK_MAGIC 0xf5 enum { MARK_PLANT = 1, MARK_UE = 2 }; static int cmos(void) { if (ioperm(0x72, 2, 1)) { perror("RAS ioperm 0x72"); return -1; } return 0; } static unsigned char cmos_get(unsigned index) { outb(index, 0x72); return inb(0x73); } static void cmos_set(unsigned index, unsigned char value) { outb(index, 0x72); outb(value, 0x73); } static int mark(unsigned what, unsigned bank, uint64_t status) { if (cmos()) return -1; cmos_set(MARK + 1, what); cmos_set(MARK + 2, bank); for (unsigned i = 0; i < 8; ++i) cmos_set(MARK + 3 + i, status >> (8 * i)); cmos_set(MARK, MARK_MAGIC); return cmos_get(MARK) == MARK_MAGIC ? 0 : -1; } /* The mark, cleared: 0 none, else what was done, with its bank and status. */ static unsigned take_mark(unsigned *bank, uint64_t *status) { if (cmos() || cmos_get(MARK) != MARK_MAGIC) return 0; unsigned what = cmos_get(MARK + 1); *bank = cmos_get(MARK + 2); *status = 0; for (unsigned i = 0; i < 8; ++i) *status |= (uint64_t)cmos_get(MARK + 3 + i) << (8 * i); cmos_set(MARK, 0); return what; } /* A synthetic MCA_STATUS (and ADDR) in BANK of CPU, written with * HWCR.McStatusWrEn; with PFEH the write traps to SMM first, which hands * it back (and stops observing that CPU). The bank keeps it across a warm * reset, for the next boot's BERT. */ static int plant(unsigned cpu, unsigned bank, uint64_t status, uint64_t address, int reset) { int fd = msr_open(cpu, O_RDWR); if (fd < 0 || pin(cpu)) { FAIL("cpu %u msr", cpu); return 1; } uint64_t hwcr, readback = 0; if (rdmsr(fd, HWCR, &hwcr) || wrmsr(fd, HWCR, hwcr | MCSTATUS_WREN)) { FAIL("HWCR"); return 1; } uint32_t base = 0xc0002000 + bank * 16; int ok = !wrmsr(fd, base + 2, address) && !wrmsr(fd, base + 1, status); wrmsr(fd, HWCR, hwcr); rdmsr(fd, base + 1, &readback); close(fd); /* Some status bits are read-only (Zeppelin's UMC keeps bit 24 clear): * what counts is a valid, uncorrected entry. */ ok = ok && (readback >> 63 & 1) && (readback >> 61 & 1); printf("RAS PLANT cpu=%u bank=%u status=%016" PRIx64 " addr=%016" PRIx64 " readback=%016" PRIx64 " %s\n", cpu, bank, status, address, readback, ok ? "OK" : "FAILED"); if (reset && ok && mark(MARK_PLANT, bank, readback)) { FAIL("the CMOS mark"); return 1; } if (reset && ok && !ioperm(0xcf9, 1, 1)) { /* At once: Linux's poller would log and clear the bank. CF9h * 06h, the FADT's reset: a warm reset, which keeps MCA_STATUS. */ puts("RAS PLANT warm reset"); outb(0x06, 0xcf9); sleep(5); puts("RAS PLANT reset did not happen"); return 1; } return ok ? 0 : 1; } /* The FCH eSPI controller's I/O decodes (FEC20000h: 40h enables, 44h/48h * the four I/O bases, 4Ch their sizes) and what the KCS status ports read. */ static int espi(void) { int fd = open("/dev/mem", O_RDONLY | O_SYNC); if (fd >= 0) { volatile uint32_t *r = mmap(NULL, 4096, PROT_READ, MAP_SHARED, fd, 0xfec20000); if (r != MAP_FAILED) { printf("RAS ESPI 30=%08x 40=%08x 44=%08x 48=%08x 4c=%08x 50=%08x 54=%08x 68=%08x\n", r[0x30 / 4], r[0x40 / 4], r[0x44 / 4], r[0x48 / 4], r[0x4c / 4], r[0x50 / 4], r[0x54 / 4], r[0x68 / 4]); printf("RAS ESPI 80=%08x 84=%08x 88=%08x 8c=%08x 90=%08x 94=%08x 98=%08x 9c=%08x a0=%08x\n", r[0x80 / 4], r[0x84 / 4], r[0x88 / 4], r[0x8c / 4], r[0x90 / 4], r[0x94 / 4], r[0x98 / 4], r[0x9c / 4], r[0xa0 / 4]); munmap((void *)r, 4096); } close(fd); } if (ioperm(0xca0, 16, 1)) { perror("RAS ioperm 0xca0"); return 1; } printf("RAS KCS ca2/ca3=%02x/%02x ca8/cac=%02x/%02x ca4/ca5=%02x/%02x\n", inb(0xca2), inb(0xca3), inb(0xca8), inb(0xcac), inb(0xca4), inb(0xca5)); return 0; } /* One IPMI Get Device ID over the KCS interface at PORT (status/command * SPACING ports above), from the OS: does the channel answer, and how fast. * Only on a channel no OS driver uses (the test kernel has none). */ static int kcs_wait(unsigned port, int mask, int value, struct timespec *start, uint64_t *polls) { for (;;) { int status = inb(port); ++*polls; if ((status & mask) == value) return status; struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); if ((now.tv_sec - start->tv_sec) * 1000000000LL + (now.tv_nsec - start->tv_nsec) > 2000000000LL) return -1; usleep(10); } } /* One request over the KCS interface (IPMI 2.0 §9.15); prints the response * and returns its completion code (-1: transport failure). */ static int kcs_request(unsigned data, unsigned spacing, const unsigned char *request, int length) { unsigned cmd = data + spacing; if (ioperm(data < cmd ? data : cmd, spacing + 1, 1)) { perror("RAS ioperm kcs"); return -1; } struct timespec start, end; uint64_t polls = 0; clock_gettime(CLOCK_MONOTONIC, &start); printf("RAS KCS %#x/%#x status=%02x\n", data, cmd, inb(cmd)); unsigned char response[32]; int n = 0, status; if (kcs_wait(cmd, 2, 0, &start, &polls) < 0) goto timeout; outb(0x61, cmd); /* WRITE_START */ for (int i = 0; i < length; ++i) { if (kcs_wait(cmd, 2, 0, &start, &polls) < 0) goto timeout; if (inb(cmd) & 1) inb(data); if (i == length - 1) { outb(0x62, cmd); /* WRITE_END */ if (kcs_wait(cmd, 2, 0, &start, &polls) < 0) goto timeout; if (inb(cmd) & 1) inb(data); } outb(request[i], data); } for (;;) { if ((status = kcs_wait(cmd, 2, 0, &start, &polls)) < 0) goto timeout; if ((status >> 6) == 0) break; /* IDLE */ if ((status >> 6) != 1) { printf("RAS KCS state %02x\n", status); return -1; } if (kcs_wait(cmd, 1, 1, &start, &polls) < 0) goto timeout; int byte = inb(data); if (n < (int)sizeof(response)) response[n++] = byte; outb(0x68, data); /* READ */ } if (inb(cmd) & 1) inb(data); clock_gettime(CLOCK_MONOTONIC, &end); printf("RAS KCS response %d bytes in %lld us, %" PRIu64 " polls:", n, ((end.tv_sec - start.tv_sec) * 1000000000LL + (end.tv_nsec - start.tv_nsec)) / 1000, polls); for (int i = 0; i < n; ++i) printf(" %02x", response[i]); putchar('\n'); return n >= 3 ? response[2] : -1; timeout: printf("RAS KCS timeout, status=%02x after %" PRIu64 " polls\n", inb(cmd), polls); return -1; } /* Get Device ID: does the channel answer, and how fast. Only on a channel * no OS driver uses (the test kernel has none). */ static int kcs(unsigned data, unsigned spacing) { const unsigned char request[] = {0x06 << 2, 0x01}; return kcs_request(data, spacing, request, sizeof(request)) == 0 ? 0 : 1; } /* Platform Event Message (IPMI 2.0 §29.3) of the SMM probe's event, from * the OS: the generator, then the event message. */ static int event(unsigned data, unsigned spacing) { const unsigned char request[] = {0x04 << 2, 0x02, 0x21, 4, 0x12, 0xff, 0x6f, 0xa3, 'F', 'S'}; return kcs_request(data, spacing, request, sizeof(request)) == 0 ? 0 : 1; } /* Get SEL Info (IPMI 2.0 §31.2), from the OS. */ static int sel_info(unsigned data, unsigned spacing) { const unsigned char request[] = {0x0a << 2, 0x40}; return kcs_request(data, spacing, request, sizeof(request)) == 0 ? 0 : 1; } /* Add SEL Entry of the record the SMM handler's probe sends * (fstart_platform_amd::smm::ras::sel), from the OS. */ static int sel(unsigned data, unsigned spacing) { const unsigned char request[] = {0x0a << 2, 0x44, 0, 0, 2, 0, 0, 0, 0, 0x21, 0, 4, 0x12, 0xff, 0x6f, 0xa3, 'F', 'S'}; return kcs_request(data, spacing, request, sizeof(request)) == 0 ? 0 : 1; } /* After plant-reset's warm reset: the BERT must hold the planted bank with * the status it read back. 2: no mark, nothing was planted. One plant per * mark: the mark is gone afterwards whatever the result. */ static int plant_check(void) { unsigned bank, matches; uint64_t status; unsigned what = take_mark(&bank, &status); if (what != MARK_PLANT) { printf("RAS PLANT no mark%s\n", what ? " of a plant" : ""); return what ? 1 : 2; } printf("RAS PLANT marked bank=%u status=%016" PRIx64 "\n", bank, status); bert_match(bank, status, &matches); if (!matches) FAIL("the BERT has no record of bank %u with status %016" PRIx64, bank, status); return failures ? 1 : 0; } static int mark_ue(void) { return mark(MARK_UE, 0, 0) ? 1 : 0; } /* After an uncorrectable injection took the machine down: the BERT must * have a record. 2: no mark. */ static int ue_check(void) { unsigned bank, matches; uint64_t status; unsigned what = take_mark(&bank, &status); if (what != MARK_UE) return what ? 1 : 2; if (!bert_match(~0u, 0, &matches)) FAIL("the BERT is empty after the uncorrectable injection"); return failures ? 1 : 0; } /* --- storms and the OS's IPMI ------------------------------------------- */ struct storm { int polling, periodic, cpu_polling; uint64_t switches, ticks, deferred, hold_timeouts; }; /* SMM's storm state (diagnostic selector 5) as CPU 0 sees it. */ static struct storm storm_state(void) { struct storm s = {0}; if (smi_port() || pin(0)) return s; struct diag d = diag(5); s.polling = d.a & 1; s.periodic = d.a >> 1 & 1; s.cpu_polling = d.a >> 2 & 1; s.switches = d.c & 0xffffffff; s.ticks = d.c >> 32; s.deferred = d.d & 0xffffffff; s.hold_timeouts = d.d >> 32; return s; } static void print_storm(const char *when, struct storm s) { printf("RAS STORM %s polling=%d periodic=%d cpu0_polling=%d switches=%" PRIu64 " periodic_smis=%" PRIu64 " deferred=%" PRIu64 " hold_timeouts=%" PRIu64 "\n", when, s.polling, s.periodic, s.cpu_polling, s.switches, s.ticks, s.deferred, s.hold_timeouts); } static int storm(void) { print_storm("now", storm_state()); struct diag hold = diag(6); printf("RAS HOLD cpus=%" PRIu64 " last_timeout_counted=%" PRIu64 " longest_wait_us=%" PRIu64 "\n", hold.d, hold.a, hold.c); return 0; } static double seconds(const struct timespec *a, const struct timespec *b) { return (b->tv_sec - a->tv_sec) + (b->tv_nsec - a->tv_nsec) / 1e9; } /* After a storm: SMM must have left interrupts for polling (its leaky * bucket), counted on (journal records), and return to interrupts after * a quiet bucket. */ static int storm_settles(struct storm before, uint64_t journal_before, long ce_before) { struct storm during = storm_state(); print_storm("after-burst", during); if (during.switches <= before.switches) FAIL("SMM did not leave interrupts for polling"); struct storm s = during; unsigned waited = 0; for (; waited < 60 && (s.polling || s.periodic); ++waited) { sleep(1); s = storm_state(); } print_storm("settled", s); printf("RAS BURST settled after %u s; journal %" PRIu64 " -> %" PRIu64 " ce_count %ld -> %ld\n", waited, journal_before, journal(), ce_before, edac("ce_count")); if (s.polling || s.periodic) FAIL("SMM still polls after %u s", waited); if (s.ticks <= during.ticks) FAIL("no periodic SMI ran"); return failures ? 1 : 0; } /* N correctable injections at ADDR back to back, each consumed, without * waiting for Linux: a storm if EINJ is fast enough. Linux's poll sees * what is left in the bank (one error per poll, MCA_STATUS[Overflow] for * the rest). */ static int ce_burst(uint64_t address, unsigned count) { debugfs(); if (!(available_types() & 0x8)) { FAIL("EINJ type 0x8 not available"); return 1; } char p1[32]; snprintf(p1, sizeof(p1), "%#" PRIx64, address); struct storm before = storm_state(); print_storm("before", before); uint64_t journal_before = journal(); long ce_before = edac("ce_count"); struct timespec start, end; clock_gettime(CLOCK_MONOTONIC, &start); for (unsigned i = 0; i < count; ++i) { if (inject(0x8, "2", p1, "0xfffffffffffff000", NULL)) { FAIL("EINJ inject %u", i); return 1; } touch(address); } clock_gettime(CLOCK_MONOTONIC, &end); double took = seconds(&start, &end); printf("RAS BURST %u corrected errors in %.2f s (%.0f/s)\n", count, took, count / took); return storm_settles(before, journal_before, ce_before); } /* One correctable injection at ADDR, then N reads of its cache line * through an uncached mapping: while the injected error stays in DRAM * (a corrected read does not write back), each read is a corrected * error, a storm at memory speed. */ static int ce_flood(uint64_t address, unsigned count) { debugfs(); if (!(available_types() & 0x8)) { FAIL("EINJ type 0x8 not available"); return 1; } char p1[32]; snprintf(p1, sizeof(p1), "%#" PRIx64, address); int fd = open("/dev/mem", O_RDONLY | O_SYNC); if (fd < 0) { perror("RAS /dev/mem"); return 1; } volatile uint64_t *line = mmap(NULL, 4096, PROT_READ, MAP_SHARED, fd, address & ~4095ULL); close(fd); if (line == MAP_FAILED) { perror("RAS mmap"); return 1; } line += (address & 4095) / 8; struct storm before = storm_state(); print_storm("before", before); uint64_t journal_before = journal(); long ce_before = edac("ce_count"); if (inject(0x8, "2", p1, "0xfffffffffffff000", NULL)) { FAIL("EINJ inject"); return 1; } struct timespec start, end; clock_gettime(CLOCK_MONOTONIC, &start); /* Each read from DRAM: the line flushed first (/dev/mem maps RAM * write-back whatever O_SYNC asks). */ uint64_t sum = 0; for (unsigned i = 0; i < count; ++i) { __builtin_ia32_clflush((const void *)line); __builtin_ia32_mfence(); sum += *line; } clock_gettime(CLOCK_MONOTONIC, &end); double took = seconds(&start, &end); printf("RAS FLOOD %u uncached reads in %.3f s (%.0f/s) sum=%" PRIx64 "\n", count, took, count / took, sum); return storm_settles(before, journal_before, ce_before); } static int ipmi_open(void) { int fd = open("/dev/ipmi0", O_RDWR); if (fd < 0) perror("RAS /dev/ipmi0"); return fd; } /* One request to the BMC through the OS's driver; returns the completion * code, -1 on a driver error, -2 on a timeout. */ static int ipmi_request(int fd, unsigned char netfn, unsigned char cmd, unsigned char *data, unsigned short length, unsigned char *response, unsigned short *size, long id) { struct ipmi_system_interface_addr bmc = { .addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE, .channel = IPMI_BMC_CHANNEL, .lun = 0}; struct ipmi_req req = { .addr = (unsigned char *)&bmc, .addr_len = sizeof(bmc), .msgid = id, .msg = {.netfn = netfn, .cmd = cmd, .data_len = length, .data = data}}; if (ioctl(fd, IPMICTL_SEND_COMMAND, &req) < 0) return -1; struct pollfd p = {.fd = fd, .events = POLLIN}; if (poll(&p, 1, 10000) != 1) return -2; struct ipmi_addr from; struct ipmi_recv recv = { .addr = (unsigned char *)&from, .addr_len = sizeof(from), .msg = {.data = response, .data_len = *size}}; if (ioctl(fd, IPMICTL_RECEIVE_MSG_TRUNC, &recv) < 0) return -1; *size = recv.msg.data_len; return *size ? response[0] : -1; } /* Get Device ID through the OS's driver, back to back for SECONDS: its * KCS state machine is in a transaction nearly all the time. Every * request must complete (an SMM report in the middle of one would fail * it). */ static int ipmi_load(unsigned duration, unsigned pause_ms) { int fd = ipmi_open(); if (fd < 0) return 1; struct timespec start, now; clock_gettime(CLOCK_MONOTONIC, &start); unsigned long ok = 0, errors = 0, timeouts = 0, codes = 0; double slowest = 0; long id = 0; do { unsigned char response[64]; unsigned short size = sizeof(response); struct timespec t0; clock_gettime(CLOCK_MONOTONIC, &t0); int code = ipmi_request(fd, 0x06, 0x01, NULL, 0, response, &size, ++id); clock_gettime(CLOCK_MONOTONIC, &now); double took = seconds(&t0, &now); if (took > slowest) slowest = took; if (code == 0) ++ok; else if (code == -2) ++timeouts; else if (code < 0) ++errors; else { ++codes; printf("RAS IPMI request %ld completion %#x\n", id, code); } if (pause_ms) usleep(pause_ms * 1000); } while (seconds(&start, &now) < duration); close(fd); printf("RAS IPMI_LOAD pause %u ms, %.1f s: %lu ok, %lu completion errors, %lu driver errors, %lu timeouts," " slowest %.1f ms\n", pause_ms, seconds(&start, &now), ok, codes, errors, timeouts, slowest * 1e3); if (!ok || codes || errors || timeouts) FAIL("the OS's IPMI requests did not all complete"); return failures ? 1 : 0; } /* Get SEL Info through the OS's driver: the entries and the last add. */ static int ipmi_sel(void) { int fd = ipmi_open(); if (fd < 0) return 1; unsigned char r[64]; unsigned short size = sizeof(r); int code = ipmi_request(fd, 0x0a, 0x40, NULL, 0, r, &size, 1); close(fd); if (code || size < 10) { FAIL("Get SEL Info: %d", code); return 1; } printf("RAS IPMI_SEL entries=%u last_add=%08x\n", r[2] | r[3] << 8, le32(r + 6)); return 0; } /* MCA_MISC0 of BANK on every CPU that sees a counter there: SMM's * thresholding counter (Locked, IntType SMI) and its count. */ static int misc(unsigned bank) { for (unsigned cpu = 0; cpu < 1024; ++cpu) { int fd = msr_open(cpu, O_RDONLY); if (fd < 0) continue; uint64_t value = 0, status = 0; rdmsr(fd, 0xc0002003 + 16 * bank, &value); rdmsr(fd, 0xc0002001 + 16 * bank, &status); close(fd); if (value >> 62 & 1) printf("RAS MISC0 cpu=%u bank=%u misc0=%016" PRIx64 " locked=%u int_type=%u errcnt=%03x overflow=%u status=%016" PRIx64 "\n", cpu, bank, value, (unsigned)(value >> 61 & 1), (unsigned)(value >> 49 & 3), (unsigned)(value >> 32 & 0xfff), (unsigned)(value >> 48 & 1), status); } return 0; } /* One SMN register through the root complex's index/data pair (00:00.0 * 60h/64h), read only. Races the kernel's own SMN users: a test bench * only. */ static int smn(uint32_t address) { int fd = open("/sys/bus/pci/devices/0000:00:00.0/config", O_RDWR); if (fd < 0) { perror("RAS 00:00.0 config"); return 1; } uint32_t value = 0; int ok = pwrite(fd, &address, 4, 0x60) == 4 && pread(fd, &value, 4, 0x64) == 4; close(fd); printf("RAS SMN %08" PRIx32 " = %08" PRIx32 "%s\n", address, value, ok ? "" : " (failed)"); return ok ? 0 : 1; } static uint64_t number(const char *s) { return strtoull(s, NULL, 0); } int main(int argc, char **argv) { setbuf(stdout, NULL); const char *command = argc > 1 ? argv[1] : ""; int result = 2; if (!strcmp(command, "info")) result = info(); else if (!strcmp(command, "smm")) result = smm_check(); else if (!strcmp(command, "probe")) result = probe(); else if (!strcmp(command, "einj") && argc >= 4) result = einj(number(argv[2]), number(argv[3]), argc > 4 ? number(argv[4]) : 1); else if (!strcmp(command, "einj-pcie") && argc == 4) result = einj_pcie(number(argv[2]), argv[3]); else if (!strcmp(command, "aer") && argc == 3) { unsigned seg, bus, dev, fn; result = sscanf(argv[2], "%x:%x:%x.%x", &seg, &bus, &dev, &fn) != 4; if (!result) aer_status(seg, bus, dev, fn, "now"); } else if (!strcmp(command, "sp3-einj")) result = sp3_einj(argc > 2 ? number(argv[2]) : 1); else if (!strcmp(command, "plant") && argc >= 5) result = plant(number(argv[2]), number(argv[3]), number(argv[4]), argc > 5 ? number(argv[5]) : 0, 0); else if (!strcmp(command, "plant-reset") && argc >= 5) result = plant(number(argv[2]), number(argv[3]), number(argv[4]), argc > 5 ? number(argv[5]) : 0, 1); else if (!strcmp(command, "plant-check")) result = plant_check(); else if (!strcmp(command, "mark-ue")) result = mark_ue(); else if (!strcmp(command, "ue-check")) result = ue_check(); else if (!strcmp(command, "espi")) result = espi(); else if (!strcmp(command, "kcs") && argc == 4) result = kcs(number(argv[2]), number(argv[3])); else if (!strcmp(command, "sel") && argc == 4) result = sel(number(argv[2]), number(argv[3])); else if (!strcmp(command, "event") && argc == 4) result = event(number(argv[2]), number(argv[3])); else if (!strcmp(command, "sel-info") && argc == 4) result = sel_info(number(argv[2]), number(argv[3])); else if (!strcmp(command, "misc") && argc == 3) result = misc(number(argv[2])); else if (!strcmp(command, "smn") && argc == 3) result = smn(number(argv[2])); else if (!strcmp(command, "storm")) result = storm(); else if (!strcmp(command, "ce-burst") && argc == 4) result = ce_burst(number(argv[2]), number(argv[3])); else if (!strcmp(command, "ce-flood") && argc == 4) result = ce_flood(number(argv[2]), number(argv[3])); else if (!strcmp(command, "ipmi-load") && argc >= 3) result = ipmi_load(number(argv[2]), argc > 3 ? number(argv[3]) : 0); else if (!strcmp(command, "ipmi-sel")) result = ipmi_sel(); else fprintf(stderr, "usage: see the head of tools/hwtest/amd-ras.c\n"); printf("RAS %s %s\n", command, result ? "FAILED" : "PASSED"); return result; }