/* Minimal ACPI RSDP/RSDT/XSDT/MADT parser. See acpi.h. * * ACPI tables are usually in lowmem, but on larger machines they sit above * it; every table is reached through ioremap() (the identity address in * lowmem, the kernel window otherwise). Anything malformed or unsupported is * treated as "no SMP", never as a fatal error. */ #include "acpi.h" #include "bootinfo.h" #include "paging.h" #include "pci.h" #include "serial.h" #include "sys_fs.h" #include "timer.h" struct acpi_rsdp { char signature[8]; unsigned char checksum; char oem_id[6]; unsigned char revision; unsigned int rsdt; unsigned int length; unsigned long long xsdt; unsigned char ext_checksum; unsigned char reserved[3]; } __attribute__((packed)); struct acpi_sdt { char signature[4]; unsigned int length; unsigned char revision; unsigned char checksum; char oem_id[6]; char oem_table_id[8]; unsigned int oem_revision; unsigned int creator_id; unsigned int creator_revision; } __attribute__((packed)); #define RSDP_SIG "RSD PTR " #define MADT_SIG "APIC" #define LAPIC_DEFAULT 0xFEE00000u static unsigned int ids[ACPI_MAX_CPUS]; static unsigned int ncpu; static unsigned int bsp_apic; static unsigned int lapic_base; static unsigned int ioapic_bases[ACPI_MAX_IOAPICS]; static unsigned int nioapics; static int acpi_ok; /* FADT "century" register (offset 108), 0 when absent. The RTC uses it to * keep the two-digit CMOS year in the right century; rtc.c only writes it * when the firmware points at a real CMOS register. */ static unsigned int rtc_century_reg; /* FADT power-management state for ACPI S5 (soft off). The PM1 control * blocks are I/O ports on every x86 machine this kernel targets; the S5 * sleep type comes from the DSDT's \_S5 package. Without it the only * shutdown path is the emulator's Bochs/QEMU port. */ static int fadt_ok; static unsigned int fadt_pm1a_cnt; static unsigned int fadt_pm1b_cnt; static unsigned int fadt_pm1a_evt; static unsigned int fadt_pm1_cnt_len; static unsigned int fadt_smi_cmd; static unsigned int fadt_acpi_enable; static unsigned int fadt_flags; static unsigned int dsdt_addr; static unsigned int ssdt_addr[4]; static unsigned int nssdt; static unsigned int s5a; static unsigned int s5b; static int s5_ok; static const unsigned char *dsdt_ptr; static unsigned int dsdt_len; static void scan_s5_addr(unsigned int addr, int strict); /* Kernel address of a firmware table range (write-back RAM mapping). */ static void *table_ptr(unsigned int addr, unsigned int len) { return ioremap(addr, len, IOREMAP_RAM); } static int range_ok(unsigned int addr, unsigned int len) { if (addr >= 0xFFFFF000u || len > 0x100000u) return 0; return addr + len >= addr; } static int sum_ok(const unsigned char *p, unsigned int len) { unsigned char sum = 0; unsigned int i; for (i = 0; i < len; i++) sum = (unsigned char)(sum + p[i]); return sum == 0; } static int rsdp_valid(const struct acpi_rsdp *r) { unsigned int i; for (i = 0; i < 8; i++) if (r->signature[i] != RSDP_SIG[i]) return 0; if (!sum_ok((const unsigned char *)r, 20)) return 0; if (r->revision >= 2) { if (r->length < 36 || r->length > 4096) return 0; if (!sum_ok((const unsigned char *)r, r->length)) return 0; } return 1; } static const struct acpi_rsdp *find_rsdp(void) { volatile unsigned short *ebda_seg = (volatile unsigned short *)0x40E; unsigned int ebda, i; unsigned int given = bootinfo_rsdp(); /* The UEFI loader passes the RSDP address from the system table. UEFI * places ACPI tables outside lowmem often enough that the scan below * would never see them, so map it first (4 KB covers the largest * length rsdp_valid() accepts). */ if (given && given < 0xFFF00000u) { const struct acpi_rsdp *r = table_ptr(given, 0x1000); if (r && rsdp_valid(r)) { serial_puts(SERIAL_COM1, "acpi: EFI RSDP at "); serial_puthex(SERIAL_COM1, given); serial_puts(SERIAL_COM1, "\n"); return r; } serial_puts(SERIAL_COM1, "acpi: EFI RSDP invalid, scanning\n"); } /* Opaque asm keeps -Warray-bounds quiet about the fixed BIOS pointer. */ __asm__ volatile ("" : "+r"(ebda_seg)); ebda = (unsigned int)(*ebda_seg) << 4; if (ebda) { for (i = 0; i < 1024; i += 16) { const struct acpi_rsdp *r = (const struct acpi_rsdp *)(ebda + i); if (rsdp_valid(r)) return r; } } for (i = 0xE0000; i < 0x100000; i += 16) { const struct acpi_rsdp *r = (const struct acpi_rsdp *)(i); if (rsdp_valid(r)) return r; } return 0; } /* True when a table signature is one of the four-character ACPI ids. */ static int sig_is(const struct acpi_sdt *t, const char *sig) { return t->signature[0] == sig[0] && t->signature[1] == sig[1] && t->signature[2] == sig[2] && t->signature[3] == sig[3]; } /* Decode and skip an AML PkgLength field (one to four bytes). */ static int aml_pkg_length(const unsigned char *p, unsigned int len, unsigned int *used) { unsigned int follow; if (!len) return -1; follow = (p[0] >> 6) & 3; if (len < follow + 1) return -1; *used = follow + 1; return 0; } /* Decode one integer package element (0, 1, Byte, Word or DWord prefix). */ static int s5_elem(const unsigned char *p, unsigned int len, unsigned int *v) { if (!len) return -1; switch (p[0]) { case 0x00: *v = 0; return 1; case 0x01: *v = 1; return 1; case 0x0A: if (len < 2) return -1; *v = p[1]; return 2; case 0x0B: if (len < 3) return -1; *v = p[1] | ((unsigned int)p[2] << 8); return 3; case 0x0C: if (len < 5) return -1; *v = p[1] | ((unsigned int)p[2] << 8) | ((unsigned int)p[3] << 16) | ((unsigned int)p[4] << 24); return 5; default: return -1; } } /* Find \_S5 in one DSDT/SSDT image and decode its PM1a/PM1b sleep types. * The AML Name string is 08 5F 53 35 5F (optionally with a root prefix * 5C); the package's first two integers are the PM1a/PM1b SLP_TYP values. * Anything that does not decode cleanly is ignored: guessing a type could * enter S3/S4 instead of powering the machine off. */ static void scan_s5(const unsigned char *tbl, unsigned int len) { unsigned int i; if (s5_ok) return; for (i = 0; i + 5 < len; i++) { const unsigned char *p; unsigned int rem, used, nel, v1, v2; int n; if (tbl[i] != 0x08) continue; if (tbl[i + 1] == 0x5C && tbl[i + 2] == '_' && tbl[i + 3] == 'S' && tbl[i + 4] == '5' && tbl[i + 5] == '_') p = tbl + i + 6; else if (tbl[i + 1] == '_' && tbl[i + 2] == 'S' && tbl[i + 3] == '5' && tbl[i + 4] == '_') p = tbl + i + 5; else continue; rem = len - (unsigned int)(p - tbl); if (!rem || *p != 0x12) /* PackageOp */ continue; p++; rem--; if (aml_pkg_length(p, rem, &used) != 0) continue; p += used; rem -= used; if (!rem) continue; nel = *p++; rem--; if (nel < 1) continue; n = s5_elem(p, rem, &v1); if (n < 0 || (v1 & ~7u)) continue; p += n; rem -= (unsigned int)n; v2 = v1; if (nel >= 2 && rem) { n = s5_elem(p, rem, &v2); if (n < 0 || (v2 & ~7u)) continue; } s5a = v1; s5b = v2; s5_ok = 1; return; } } /* Map one DSDT/SSDT by address, validate it and scan for \_S5. The DSDT * pointed to by the FADT is scanned even with a bad checksum (some firmware * ships one and still expects the OS to use the table); SSDTs from the RSDT * were already checksum-validated by the table walk. */ static void scan_s5_addr(unsigned int addr, int strict) { const struct acpi_sdt *t; unsigned int len; if (s5_ok || !addr || !range_ok(addr, sizeof(struct acpi_sdt))) return; t = table_ptr(addr, sizeof(struct acpi_sdt)); if (!t) return; if (!sig_is(t, "DSDT") && !sig_is(t, "SSDT")) return; len = t->length; if (len < sizeof(struct acpi_sdt) || len > 0x100000u || !range_ok(addr, len)) return; t = table_ptr(addr, len); if (!t) return; if (!sum_ok((const unsigned char *)t, len) && strict) return; if (addr == dsdt_addr) { dsdt_ptr = (const unsigned char *)t; dsdt_len = len; } scan_s5((const unsigned char *)t, len); } /* Cache the FADT fields the S5 shutdown needs. */ static void parse_fadt(const unsigned char *t, unsigned int len) { unsigned long long x; if (len < 109 || fadt_ok) return; fadt_ok = 1; fadt_pm1a_cnt = *(const unsigned int *)(t + 64); fadt_pm1b_cnt = *(const unsigned int *)(t + 68); fadt_pm1a_evt = *(const unsigned int *)(t + 56); fadt_pm1_cnt_len = t[89]; fadt_smi_cmd = *(const unsigned int *)(t + 48); fadt_acpi_enable = t[52]; if (len >= 116) fadt_flags = *(const unsigned int *)(t + 112); /* ACPI 2.0+ firmware may publish the DSDT only in X_DSDT. */ if (len >= 148) { x = *(const unsigned long long *)(t + 140); if (x && (unsigned int)(x >> 32) == 0) dsdt_addr = (unsigned int)x; } if (!dsdt_addr) dsdt_addr = *(const unsigned int *)(t + 40); } /* Walk the RSDT/XSDT once: pick the MADT, cache the FADT and scan for \_S5. */ static const struct acpi_sdt *scan_tables(const struct acpi_rsdp *rsdp) { unsigned int addr, len, i, step; const struct acpi_sdt *sdt; const struct acpi_sdt *madt = 0; /* Prefer the 64-bit XSDT when it is reachable from the 32-bit * identity map; otherwise fall back to the ACPI 1.0 RSDT. */ if (rsdp->revision >= 2 && rsdp->xsdt && (unsigned int)(rsdp->xsdt >> 32) == 0) { addr = (unsigned int)rsdp->xsdt; step = 8; } else { addr = rsdp->rsdt; step = 4; } if (!addr || !range_ok(addr, sizeof(struct acpi_sdt))) return 0; sdt = table_ptr(addr, sizeof(struct acpi_sdt)); if (!sdt) return 0; len = sdt->length; if (len < sizeof(struct acpi_sdt) || len > 0x100000u || !range_ok(addr, len)) return 0; sdt = table_ptr(addr, len); if (!sdt || !sum_ok((const unsigned char *)sdt, len)) return 0; for (i = sizeof(struct acpi_sdt); i + step <= len; i += step) { unsigned int taddr; const struct acpi_sdt *t; unsigned int tlen; if (step == 8) { unsigned long long a = *(const unsigned long long *)((const unsigned char *)sdt + i); if ((unsigned int)(a >> 32) != 0) continue; taddr = (unsigned int)a; } else { taddr = *(const unsigned int *)((const unsigned char *)sdt + i); } if (!taddr || !range_ok(taddr, sizeof(struct acpi_sdt))) continue; t = table_ptr(taddr, sizeof(struct acpi_sdt)); if (!t) continue; tlen = t->length; if (tlen < sizeof(struct acpi_sdt) || tlen > 0x100000u || !range_ok(taddr, tlen)) continue; t = table_ptr(taddr, tlen); if (!t || !sum_ok((const unsigned char *)t, tlen)) continue; if (sig_is(t, "FACP")) { /* FADT "century" is at offset 108 (ACPI 2.0+); 0 means * the firmware does not provide a century register. */ if (tlen >= 109) { unsigned char c = ((const unsigned char *)t)[108]; if (c >= 0x0E && c <= 0x7F) rtc_century_reg = c; } parse_fadt((const unsigned char *)t, tlen); continue; } if (sig_is(t, "DSDT")) { if (!dsdt_addr) dsdt_addr = taddr; continue; } if (sig_is(t, "SSDT")) { if (nssdt < 4) ssdt_addr[nssdt++] = taddr; continue; } if (sig_is(t, "APIC") && !madt) madt = t; } /* \_S5 normally lives in the DSDT; some firmware splits it into an * SSDT. Scan after the walk so the FADT's DSDT pointer is known. */ scan_s5_addr(dsdt_addr, 0); for (i = 0; i < nssdt && !s5_ok; i++) scan_s5_addr(ssdt_addr[i], 1); return madt; } static unsigned int cpuid_apic_id(void) { unsigned int eax, ebx, ecx, edx; eax = 1; __asm__ volatile ("cpuid" : "+a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)); if (!(edx & (1u << 9))) /* CPUID.1:EDX.APIC */ return 0xFFFFFFFFu; return ebx >> 24; /* initial local APIC ID */ } static void parse_madt(const struct acpi_sdt *madt) { const unsigned char *p = (const unsigned char *)madt; unsigned int len = madt->length; unsigned int i; lapic_base = LAPIC_DEFAULT; if (len >= 44) { unsigned int base = *(const unsigned int *)(p + 36); if (base) lapic_base = base; } for (i = 44; i + 2 <= len; ) { unsigned char type = p[i]; unsigned char elen = p[i + 1]; if (elen < 2 || i + elen > len) break; if (type == 0 && elen >= 8) { /* Processor Local APIC: flags bit 0 = enabled. */ if ((p[i + 4] & 1) && ncpu < ACPI_MAX_CPUS) ids[ncpu++] = p[i + 3]; } else if (type == 1 && elen >= 12) { /* I/O APIC: id, 32-bit MMIO base, GSI base. */ unsigned int base = *(const unsigned int *)(p + i + 4); if (base && nioapics < ACPI_MAX_IOAPICS) ioapic_bases[nioapics++] = base; } else if (type == 5 && elen >= 12) { /* Local APIC Address Override (64-bit). */ unsigned int lo = *(const unsigned int *)(p + i + 4); unsigned int hi = *(const unsigned int *)(p + i + 8); if (!hi && lo) lapic_base = lo; } /* x2APIC (type 9), IOAPIC (1), NMI (4) and overrides are not * needed for the first SMP step. */ i += elen; } } int acpi_init(void) { const struct acpi_rsdp *rsdp; const struct acpi_sdt *madt; unsigned int i; acpi_ok = 0; ncpu = 0; nioapics = 0; rtc_century_reg = 0; fadt_ok = 0; fadt_pm1a_cnt = 0; fadt_pm1b_cnt = 0; fadt_pm1a_evt = 0; fadt_pm1_cnt_len = 0; fadt_smi_cmd = 0; fadt_acpi_enable = 0; fadt_flags = 0; dsdt_addr = 0; nssdt = 0; dsdt_ptr = 0; dsdt_len = 0; s5a = 0; s5b = 0; s5_ok = 0; bsp_apic = cpuid_apic_id(); rsdp = find_rsdp(); if (!rsdp) { serial_puts(SERIAL_COM1, "acpi: no RSDP, staying UP\n"); return 0; } /* Walk the tables even when the CPU has no APIC: the FADT also * carries the S5 shutdown registers. */ madt = scan_tables(rsdp); if (bsp_apic == 0xFFFFFFFFu) { serial_puts(SERIAL_COM1, "acpi: no CPUID APIC, staying UP\n"); return 0; } if (!madt) { serial_puts(SERIAL_COM1, "acpi: no MADT, staying UP\n"); return 0; } parse_madt(madt); /* Put the BSP first. If firmware omitted it, keep the discovered list * but make sure entry 0 is a real CPU. */ for (i = 0; i < ncpu; i++) if (ids[i] == bsp_apic) break; if (i < ncpu) { unsigned int t = ids[0]; ids[0] = ids[i]; ids[i] = t; } else if (ncpu) { ids[0] = bsp_apic; } if (!ncpu) return 0; acpi_ok = 1; return 1; } unsigned int acpi_cpu_count(void) { return acpi_ok ? ncpu : 0; } unsigned int acpi_cpu_apic_id(unsigned int index) { return (acpi_ok && index < ncpu) ? ids[index] : 0; } unsigned int acpi_bsp_apic_id(void) { return bsp_apic; } unsigned int acpi_lapic_base(void) { return lapic_base; } unsigned int acpi_ioapic_count(void) { return acpi_ok ? nioapics : 0; } unsigned int acpi_ioapic_base(unsigned int index) { return (acpi_ok && index < nioapics) ? ioapic_bases[index] : 0; } unsigned int acpi_rtc_century_reg(void) { return rtc_century_reg; } /* Put the machine into ACPI S5 (soft off) through the FADT PM1 control * blocks. Returns 0 when the SLP_EN sequence was issued, -1 when the * firmware does not provide a usable path. The caller must halt either * way: a machine that ignores the write will just keep running. */ static unsigned int pm1_cnt_read(unsigned int port) { if (fadt_pm1_cnt_len >= 4) return port_inl((unsigned short)port); return port_inw((unsigned short)port); } static void pm1_cnt_put(unsigned int port, unsigned int v) { if (fadt_pm1_cnt_len >= 4) port_outl((unsigned short)port, v); else port_outw((unsigned short)port, (unsigned short)v); } /* Set the sleep type and optionally SLP_EN. SCI_EN and the other live bits * are preserved: clearing SCI_EN can make a real chipset ignore the request. * The type is written first and SLP_EN in a second write because some * chipsets sample SLP_TYP only when SLP_EN is asserted. */ static void pm1_cnt_set(unsigned int port, unsigned int type, int enable) { unsigned int v = pm1_cnt_read(port); v &= ~(7u << 10); v |= type << 10; if (enable) v |= 1u << 13; pm1_cnt_put(port, v); } /* The ION/nForce DSDT's _PTS(5): record the sleep state in the BIOS NVS * (ASSB/AOTB/AAXB/OSTP at 0xCFF9E064, field offsets from the DSDT's BIOS * OperationRegion) and arm PS1S/PS1E in the SMI block at 0x4400. The SMM * reads that state when SLP_EN is written; arming PS1E without it hangs. */ static void nvidia_s5_prepare(unsigned int smi_base) { unsigned char *nvs; unsigned char b; nvs = ioremap(0xCFF9E064u, 0x100, IOREMAP_RAM); if (!nvs) return; nvs[0x31] = 5; /* ASSB: S5 */ nvs[0x32] = 3; /* AOTB: OSFL() for Linux */ *(unsigned int *)(nvs + 0x33) = 0; /* AAXB */ nvs[0x4D] = 1; /* OSTP: OSYS() for Linux */ port_outb(0x80, 5); /* DBG8 */ if (smi_base <= 0xFFC0u) { b = port_inb((unsigned short)(smi_base + 1)); port_outb((unsigned short)(smi_base + 1), (unsigned char)(b | 0x80)); b = port_inb((unsigned short)(smi_base + 5)); port_outb((unsigned short)(smi_base + 5), (unsigned char)(b | 0x80)); } } int acpi_poweroff(void) { unsigned int t0, sci, smi, cmd; if (!fadt_ok || !s5_ok) return -1; if (fadt_flags & (1u << 4)) /* HW_REDUCED_ACPI */ return -1; if (!fadt_pm1a_cnt || fadt_pm1a_cnt > 0xFFFFu) return -1; /* Some firmware still has ACPI in legacy mode (SCI_EN clear), where * PM1_CNT writes are ignored. Do the SMI handshake first; the FADT * SMI_CMD is authoritative, but a few boards leave it zero and still * use the conventional 0xB2/0xF1 pair. */ sci = pm1_cnt_read(fadt_pm1a_cnt) & 1; if (!sci) { smi = fadt_smi_cmd; cmd = fadt_acpi_enable; if (!smi || smi > 0xFFFFu) smi = 0xB2; if (!cmd) cmd = 0xF1; port_outb((unsigned short)smi, (unsigned char)cmd); t0 = get_ticks_ms(); while (get_ticks_ms() - t0 < 3000) { if (pm1_cnt_read(fadt_pm1a_cnt) & 1) break; } sci = pm1_cnt_read(fadt_pm1a_cnt) & 1; } serial_puts(SERIAL_COM1, "poweroff: S5 "); serial_putdec(SERIAL_COM1, s5a); serial_puts(SERIAL_COM1, "/"); serial_putdec(SERIAL_COM1, s5b); serial_puts(SERIAL_COM1, " pm1a "); serial_puthex(SERIAL_COM1, fadt_pm1a_cnt); if (fadt_pm1b_cnt) { serial_puts(SERIAL_COM1, " pm1b "); serial_puthex(SERIAL_COM1, fadt_pm1b_cnt); } serial_puts(SERIAL_COM1, " sci "); serial_putdec(SERIAL_COM1, sci); serial_puts(SERIAL_COM1, "\n"); /* Set SLP_TYP first, then assert SLP_EN in a separate write. */ pm1_cnt_set(fadt_pm1a_cnt, s5a, 0); if (fadt_pm1b_cnt && fadt_pm1b_cnt <= 0xFFFFu) pm1_cnt_set(fadt_pm1b_cnt, s5b, 0); /* Intel-style southbridges route SLP_EN through an SMI while * SMI_EN.SLP_SMI_EN (bit 4 at PMBASE+0x30) is set, so clear it. * NVIDIA nForce/MCP parts go through their firmware SMM as the * DSDT's _PTS(5) describes: nvidia_s5_prepare() records the sleep * state in the BIOS NVS and arms PS1S/PS1E before SLP_EN. */ if (fadt_smi_cmd < 0x4000u) { if (fadt_pm1a_evt && fadt_pm1a_evt <= 0xFFC0u) { unsigned int se = port_inl((unsigned short)(fadt_pm1a_evt + 0x30)); port_outl((unsigned short)(fadt_pm1a_evt + 0x30), se & ~(1u << 4)); } } else { nvidia_s5_prepare(fadt_smi_cmd - 0x2Eu); } pm1_cnt_set(fadt_pm1a_cnt, s5a, 1); if (fadt_pm1b_cnt && fadt_pm1b_cnt <= 0xFFFFu) pm1_cnt_set(fadt_pm1b_cnt, s5b, 1); return 0; } int acpi_dump_dsdt(const char *path) { if (!dsdt_ptr || !dsdt_len) return -1; return sys_write_file(path, dsdt_ptr, dsdt_len); } void acpi_dump(void) { unsigned int i; if (fadt_ok) { serial_puts(SERIAL_COM1, "acpi: fadt pm1a "); serial_puthex(SERIAL_COM1, fadt_pm1a_cnt); serial_puts(SERIAL_COM1, " s5 "); if (s5_ok) { serial_putdec(SERIAL_COM1, s5a); serial_puts(SERIAL_COM1, "/"); serial_putdec(SERIAL_COM1, s5b); } else { serial_puts(SERIAL_COM1, "?"); } serial_puts(SERIAL_COM1, "\n"); } if (!acpi_ok) { serial_puts(SERIAL_COM1, "acpi: unavailable, 1 cpu\n"); return; } serial_puts(SERIAL_COM1, "acpi: lapic "); serial_puthex(SERIAL_COM1, lapic_base); serial_puts(SERIAL_COM1, ", "); serial_putdec(SERIAL_COM1, ncpu); serial_puts(SERIAL_COM1, " cpus:"); for (i = 0; i < ncpu; i++) { serial_puts(SERIAL_COM1, " "); serial_putdec(SERIAL_COM1, ids[i]); } serial_puts(SERIAL_COM1, "\n"); for (i = 0; i < nioapics; i++) { serial_puts(SERIAL_COM1, "acpi: ioapic "); serial_puthex(SERIAL_COM1, ioapic_bases[i]); serial_puts(SERIAL_COM1, "\n"); } }