acpi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /* Minimal ACPI RSDP/RSDT/XSDT/MADT parser. See acpi.h.
  2. *
  3. * ACPI tables are usually in lowmem, but on larger machines they sit above
  4. * it; every table is reached through ioremap() (the identity address in
  5. * lowmem, the kernel window otherwise). Anything malformed or unsupported is
  6. * treated as "no SMP", never as a fatal error.
  7. */
  8. #include "acpi.h"
  9. #include "bootinfo.h"
  10. #include "paging.h"
  11. #include "pci.h"
  12. #include "serial.h"
  13. #include "sys_fs.h"
  14. #include "timer.h"
  15. struct acpi_rsdp {
  16. char signature[8];
  17. unsigned char checksum;
  18. char oem_id[6];
  19. unsigned char revision;
  20. unsigned int rsdt;
  21. unsigned int length;
  22. unsigned long long xsdt;
  23. unsigned char ext_checksum;
  24. unsigned char reserved[3];
  25. } __attribute__((packed));
  26. struct acpi_sdt {
  27. char signature[4];
  28. unsigned int length;
  29. unsigned char revision;
  30. unsigned char checksum;
  31. char oem_id[6];
  32. char oem_table_id[8];
  33. unsigned int oem_revision;
  34. unsigned int creator_id;
  35. unsigned int creator_revision;
  36. } __attribute__((packed));
  37. #define RSDP_SIG "RSD PTR "
  38. #define MADT_SIG "APIC"
  39. #define LAPIC_DEFAULT 0xFEE00000u
  40. static unsigned int ids[ACPI_MAX_CPUS];
  41. static unsigned int ncpu;
  42. static unsigned int bsp_apic;
  43. static unsigned int lapic_base;
  44. static unsigned int ioapic_bases[ACPI_MAX_IOAPICS];
  45. static unsigned int nioapics;
  46. static int acpi_ok;
  47. /* FADT "century" register (offset 108), 0 when absent. The RTC uses it to
  48. * keep the two-digit CMOS year in the right century; rtc.c only writes it
  49. * when the firmware points at a real CMOS register. */
  50. static unsigned int rtc_century_reg;
  51. /* FADT power-management state for ACPI S5 (soft off). The PM1 control
  52. * blocks are I/O ports on every x86 machine this kernel targets; the S5
  53. * sleep type comes from the DSDT's \_S5 package. Without it the only
  54. * shutdown path is the emulator's Bochs/QEMU port. */
  55. static int fadt_ok;
  56. static unsigned int fadt_pm1a_cnt;
  57. static unsigned int fadt_pm1b_cnt;
  58. static unsigned int fadt_pm1a_evt;
  59. static unsigned int fadt_pm1_cnt_len;
  60. static unsigned int fadt_smi_cmd;
  61. static unsigned int fadt_acpi_enable;
  62. static unsigned int fadt_flags;
  63. static unsigned int dsdt_addr;
  64. static unsigned int ssdt_addr[4];
  65. static unsigned int nssdt;
  66. static unsigned int s5a;
  67. static unsigned int s5b;
  68. static int s5_ok;
  69. static const unsigned char *dsdt_ptr;
  70. static unsigned int dsdt_len;
  71. static void scan_s5_addr(unsigned int addr, int strict);
  72. /* Kernel address of a firmware table range (write-back RAM mapping). */
  73. static void *table_ptr(unsigned int addr, unsigned int len)
  74. {
  75. return ioremap(addr, len, IOREMAP_RAM);
  76. }
  77. static int range_ok(unsigned int addr, unsigned int len)
  78. {
  79. if (addr >= 0xFFFFF000u || len > 0x100000u)
  80. return 0;
  81. return addr + len >= addr;
  82. }
  83. static int sum_ok(const unsigned char *p, unsigned int len)
  84. {
  85. unsigned char sum = 0;
  86. unsigned int i;
  87. for (i = 0; i < len; i++)
  88. sum = (unsigned char)(sum + p[i]);
  89. return sum == 0;
  90. }
  91. static int rsdp_valid(const struct acpi_rsdp *r)
  92. {
  93. unsigned int i;
  94. for (i = 0; i < 8; i++)
  95. if (r->signature[i] != RSDP_SIG[i])
  96. return 0;
  97. if (!sum_ok((const unsigned char *)r, 20))
  98. return 0;
  99. if (r->revision >= 2) {
  100. if (r->length < 36 || r->length > 4096)
  101. return 0;
  102. if (!sum_ok((const unsigned char *)r, r->length))
  103. return 0;
  104. }
  105. return 1;
  106. }
  107. static const struct acpi_rsdp *find_rsdp(void)
  108. {
  109. volatile unsigned short *ebda_seg = (volatile unsigned short *)0x40E;
  110. unsigned int ebda, i;
  111. unsigned int given = bootinfo_rsdp();
  112. /* The UEFI loader passes the RSDP address from the system table. UEFI
  113. * places ACPI tables outside lowmem often enough that the scan below
  114. * would never see them, so map it first (4 KB covers the largest
  115. * length rsdp_valid() accepts). */
  116. if (given && given < 0xFFF00000u) {
  117. const struct acpi_rsdp *r = table_ptr(given, 0x1000);
  118. if (r && rsdp_valid(r)) {
  119. serial_puts(SERIAL_COM1, "acpi: EFI RSDP at ");
  120. serial_puthex(SERIAL_COM1, given);
  121. serial_puts(SERIAL_COM1, "\n");
  122. return r;
  123. }
  124. serial_puts(SERIAL_COM1, "acpi: EFI RSDP invalid, scanning\n");
  125. }
  126. /* Opaque asm keeps -Warray-bounds quiet about the fixed BIOS pointer. */
  127. __asm__ volatile ("" : "+r"(ebda_seg));
  128. ebda = (unsigned int)(*ebda_seg) << 4;
  129. if (ebda) {
  130. for (i = 0; i < 1024; i += 16) {
  131. const struct acpi_rsdp *r =
  132. (const struct acpi_rsdp *)(ebda + i);
  133. if (rsdp_valid(r))
  134. return r;
  135. }
  136. }
  137. for (i = 0xE0000; i < 0x100000; i += 16) {
  138. const struct acpi_rsdp *r =
  139. (const struct acpi_rsdp *)(i);
  140. if (rsdp_valid(r))
  141. return r;
  142. }
  143. return 0;
  144. }
  145. /* True when a table signature is one of the four-character ACPI ids. */
  146. static int sig_is(const struct acpi_sdt *t, const char *sig)
  147. {
  148. return t->signature[0] == sig[0] && t->signature[1] == sig[1] &&
  149. t->signature[2] == sig[2] && t->signature[3] == sig[3];
  150. }
  151. /* Decode and skip an AML PkgLength field (one to four bytes). */
  152. static int aml_pkg_length(const unsigned char *p, unsigned int len,
  153. unsigned int *used)
  154. {
  155. unsigned int follow;
  156. if (!len)
  157. return -1;
  158. follow = (p[0] >> 6) & 3;
  159. if (len < follow + 1)
  160. return -1;
  161. *used = follow + 1;
  162. return 0;
  163. }
  164. /* Decode one integer package element (0, 1, Byte, Word or DWord prefix). */
  165. static int s5_elem(const unsigned char *p, unsigned int len, unsigned int *v)
  166. {
  167. if (!len)
  168. return -1;
  169. switch (p[0]) {
  170. case 0x00:
  171. *v = 0;
  172. return 1;
  173. case 0x01:
  174. *v = 1;
  175. return 1;
  176. case 0x0A:
  177. if (len < 2)
  178. return -1;
  179. *v = p[1];
  180. return 2;
  181. case 0x0B:
  182. if (len < 3)
  183. return -1;
  184. *v = p[1] | ((unsigned int)p[2] << 8);
  185. return 3;
  186. case 0x0C:
  187. if (len < 5)
  188. return -1;
  189. *v = p[1] | ((unsigned int)p[2] << 8) |
  190. ((unsigned int)p[3] << 16) | ((unsigned int)p[4] << 24);
  191. return 5;
  192. default:
  193. return -1;
  194. }
  195. }
  196. /* Find \_S5 in one DSDT/SSDT image and decode its PM1a/PM1b sleep types.
  197. * The AML Name string is 08 5F 53 35 5F (optionally with a root prefix
  198. * 5C); the package's first two integers are the PM1a/PM1b SLP_TYP values.
  199. * Anything that does not decode cleanly is ignored: guessing a type could
  200. * enter S3/S4 instead of powering the machine off. */
  201. static void scan_s5(const unsigned char *tbl, unsigned int len)
  202. {
  203. unsigned int i;
  204. if (s5_ok)
  205. return;
  206. for (i = 0; i + 5 < len; i++) {
  207. const unsigned char *p;
  208. unsigned int rem, used, nel, v1, v2;
  209. int n;
  210. if (tbl[i] != 0x08)
  211. continue;
  212. if (tbl[i + 1] == 0x5C && tbl[i + 2] == '_' &&
  213. tbl[i + 3] == 'S' && tbl[i + 4] == '5' &&
  214. tbl[i + 5] == '_')
  215. p = tbl + i + 6;
  216. else if (tbl[i + 1] == '_' && tbl[i + 2] == 'S' &&
  217. tbl[i + 3] == '5' && tbl[i + 4] == '_')
  218. p = tbl + i + 5;
  219. else
  220. continue;
  221. rem = len - (unsigned int)(p - tbl);
  222. if (!rem || *p != 0x12) /* PackageOp */
  223. continue;
  224. p++;
  225. rem--;
  226. if (aml_pkg_length(p, rem, &used) != 0)
  227. continue;
  228. p += used;
  229. rem -= used;
  230. if (!rem)
  231. continue;
  232. nel = *p++;
  233. rem--;
  234. if (nel < 1)
  235. continue;
  236. n = s5_elem(p, rem, &v1);
  237. if (n < 0 || (v1 & ~7u))
  238. continue;
  239. p += n;
  240. rem -= (unsigned int)n;
  241. v2 = v1;
  242. if (nel >= 2 && rem) {
  243. n = s5_elem(p, rem, &v2);
  244. if (n < 0 || (v2 & ~7u))
  245. continue;
  246. }
  247. s5a = v1;
  248. s5b = v2;
  249. s5_ok = 1;
  250. return;
  251. }
  252. }
  253. /* Map one DSDT/SSDT by address, validate it and scan for \_S5. The DSDT
  254. * pointed to by the FADT is scanned even with a bad checksum (some firmware
  255. * ships one and still expects the OS to use the table); SSDTs from the RSDT
  256. * were already checksum-validated by the table walk. */
  257. static void scan_s5_addr(unsigned int addr, int strict)
  258. {
  259. const struct acpi_sdt *t;
  260. unsigned int len;
  261. if (s5_ok || !addr || !range_ok(addr, sizeof(struct acpi_sdt)))
  262. return;
  263. t = table_ptr(addr, sizeof(struct acpi_sdt));
  264. if (!t)
  265. return;
  266. if (!sig_is(t, "DSDT") && !sig_is(t, "SSDT"))
  267. return;
  268. len = t->length;
  269. if (len < sizeof(struct acpi_sdt) || len > 0x100000u ||
  270. !range_ok(addr, len))
  271. return;
  272. t = table_ptr(addr, len);
  273. if (!t)
  274. return;
  275. if (!sum_ok((const unsigned char *)t, len) && strict)
  276. return;
  277. if (addr == dsdt_addr) {
  278. dsdt_ptr = (const unsigned char *)t;
  279. dsdt_len = len;
  280. }
  281. scan_s5((const unsigned char *)t, len);
  282. }
  283. /* Cache the FADT fields the S5 shutdown needs. */
  284. static void parse_fadt(const unsigned char *t, unsigned int len)
  285. {
  286. unsigned long long x;
  287. if (len < 109 || fadt_ok)
  288. return;
  289. fadt_ok = 1;
  290. fadt_pm1a_cnt = *(const unsigned int *)(t + 64);
  291. fadt_pm1b_cnt = *(const unsigned int *)(t + 68);
  292. fadt_pm1a_evt = *(const unsigned int *)(t + 56);
  293. fadt_pm1_cnt_len = t[89];
  294. fadt_smi_cmd = *(const unsigned int *)(t + 48);
  295. fadt_acpi_enable = t[52];
  296. if (len >= 116)
  297. fadt_flags = *(const unsigned int *)(t + 112);
  298. /* ACPI 2.0+ firmware may publish the DSDT only in X_DSDT. */
  299. if (len >= 148) {
  300. x = *(const unsigned long long *)(t + 140);
  301. if (x && (unsigned int)(x >> 32) == 0)
  302. dsdt_addr = (unsigned int)x;
  303. }
  304. if (!dsdt_addr)
  305. dsdt_addr = *(const unsigned int *)(t + 40);
  306. }
  307. /* Walk the RSDT/XSDT once: pick the MADT, cache the FADT and scan for \_S5. */
  308. static const struct acpi_sdt *scan_tables(const struct acpi_rsdp *rsdp)
  309. {
  310. unsigned int addr, len, i, step;
  311. const struct acpi_sdt *sdt;
  312. const struct acpi_sdt *madt = 0;
  313. /* Prefer the 64-bit XSDT when it is reachable from the 32-bit
  314. * identity map; otherwise fall back to the ACPI 1.0 RSDT. */
  315. if (rsdp->revision >= 2 && rsdp->xsdt &&
  316. (unsigned int)(rsdp->xsdt >> 32) == 0) {
  317. addr = (unsigned int)rsdp->xsdt;
  318. step = 8;
  319. } else {
  320. addr = rsdp->rsdt;
  321. step = 4;
  322. }
  323. if (!addr || !range_ok(addr, sizeof(struct acpi_sdt)))
  324. return 0;
  325. sdt = table_ptr(addr, sizeof(struct acpi_sdt));
  326. if (!sdt)
  327. return 0;
  328. len = sdt->length;
  329. if (len < sizeof(struct acpi_sdt) || len > 0x100000u ||
  330. !range_ok(addr, len))
  331. return 0;
  332. sdt = table_ptr(addr, len);
  333. if (!sdt || !sum_ok((const unsigned char *)sdt, len))
  334. return 0;
  335. for (i = sizeof(struct acpi_sdt); i + step <= len; i += step) {
  336. unsigned int taddr;
  337. const struct acpi_sdt *t;
  338. unsigned int tlen;
  339. if (step == 8) {
  340. unsigned long long a =
  341. *(const unsigned long long *)((const unsigned char *)sdt + i);
  342. if ((unsigned int)(a >> 32) != 0)
  343. continue;
  344. taddr = (unsigned int)a;
  345. } else {
  346. taddr = *(const unsigned int *)((const unsigned char *)sdt + i);
  347. }
  348. if (!taddr || !range_ok(taddr, sizeof(struct acpi_sdt)))
  349. continue;
  350. t = table_ptr(taddr, sizeof(struct acpi_sdt));
  351. if (!t)
  352. continue;
  353. tlen = t->length;
  354. if (tlen < sizeof(struct acpi_sdt) || tlen > 0x100000u ||
  355. !range_ok(taddr, tlen))
  356. continue;
  357. t = table_ptr(taddr, tlen);
  358. if (!t || !sum_ok((const unsigned char *)t, tlen))
  359. continue;
  360. if (sig_is(t, "FACP")) {
  361. /* FADT "century" is at offset 108 (ACPI 2.0+); 0 means
  362. * the firmware does not provide a century register. */
  363. if (tlen >= 109) {
  364. unsigned char c =
  365. ((const unsigned char *)t)[108];
  366. if (c >= 0x0E && c <= 0x7F)
  367. rtc_century_reg = c;
  368. }
  369. parse_fadt((const unsigned char *)t, tlen);
  370. continue;
  371. }
  372. if (sig_is(t, "DSDT")) {
  373. if (!dsdt_addr)
  374. dsdt_addr = taddr;
  375. continue;
  376. }
  377. if (sig_is(t, "SSDT")) {
  378. if (nssdt < 4)
  379. ssdt_addr[nssdt++] = taddr;
  380. continue;
  381. }
  382. if (sig_is(t, "APIC") && !madt)
  383. madt = t;
  384. }
  385. /* \_S5 normally lives in the DSDT; some firmware splits it into an
  386. * SSDT. Scan after the walk so the FADT's DSDT pointer is known. */
  387. scan_s5_addr(dsdt_addr, 0);
  388. for (i = 0; i < nssdt && !s5_ok; i++)
  389. scan_s5_addr(ssdt_addr[i], 1);
  390. return madt;
  391. }
  392. static unsigned int cpuid_apic_id(void)
  393. {
  394. unsigned int eax, ebx, ecx, edx;
  395. eax = 1;
  396. __asm__ volatile ("cpuid"
  397. : "+a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx));
  398. if (!(edx & (1u << 9))) /* CPUID.1:EDX.APIC */
  399. return 0xFFFFFFFFu;
  400. return ebx >> 24; /* initial local APIC ID */
  401. }
  402. static void parse_madt(const struct acpi_sdt *madt)
  403. {
  404. const unsigned char *p = (const unsigned char *)madt;
  405. unsigned int len = madt->length;
  406. unsigned int i;
  407. lapic_base = LAPIC_DEFAULT;
  408. if (len >= 44) {
  409. unsigned int base = *(const unsigned int *)(p + 36);
  410. if (base)
  411. lapic_base = base;
  412. }
  413. for (i = 44; i + 2 <= len; ) {
  414. unsigned char type = p[i];
  415. unsigned char elen = p[i + 1];
  416. if (elen < 2 || i + elen > len)
  417. break;
  418. if (type == 0 && elen >= 8) {
  419. /* Processor Local APIC: flags bit 0 = enabled. */
  420. if ((p[i + 4] & 1) && ncpu < ACPI_MAX_CPUS)
  421. ids[ncpu++] = p[i + 3];
  422. } else if (type == 1 && elen >= 12) {
  423. /* I/O APIC: id, 32-bit MMIO base, GSI base. */
  424. unsigned int base = *(const unsigned int *)(p + i + 4);
  425. if (base && nioapics < ACPI_MAX_IOAPICS)
  426. ioapic_bases[nioapics++] = base;
  427. } else if (type == 5 && elen >= 12) {
  428. /* Local APIC Address Override (64-bit). */
  429. unsigned int lo = *(const unsigned int *)(p + i + 4);
  430. unsigned int hi = *(const unsigned int *)(p + i + 8);
  431. if (!hi && lo)
  432. lapic_base = lo;
  433. }
  434. /* x2APIC (type 9), IOAPIC (1), NMI (4) and overrides are not
  435. * needed for the first SMP step. */
  436. i += elen;
  437. }
  438. }
  439. int acpi_init(void)
  440. {
  441. const struct acpi_rsdp *rsdp;
  442. const struct acpi_sdt *madt;
  443. unsigned int i;
  444. acpi_ok = 0;
  445. ncpu = 0;
  446. nioapics = 0;
  447. rtc_century_reg = 0;
  448. fadt_ok = 0;
  449. fadt_pm1a_cnt = 0;
  450. fadt_pm1b_cnt = 0;
  451. fadt_pm1a_evt = 0;
  452. fadt_pm1_cnt_len = 0;
  453. fadt_smi_cmd = 0;
  454. fadt_acpi_enable = 0;
  455. fadt_flags = 0;
  456. dsdt_addr = 0;
  457. nssdt = 0;
  458. dsdt_ptr = 0;
  459. dsdt_len = 0;
  460. s5a = 0;
  461. s5b = 0;
  462. s5_ok = 0;
  463. bsp_apic = cpuid_apic_id();
  464. rsdp = find_rsdp();
  465. if (!rsdp) {
  466. serial_puts(SERIAL_COM1, "acpi: no RSDP, staying UP\n");
  467. return 0;
  468. }
  469. /* Walk the tables even when the CPU has no APIC: the FADT also
  470. * carries the S5 shutdown registers. */
  471. madt = scan_tables(rsdp);
  472. if (bsp_apic == 0xFFFFFFFFu) {
  473. serial_puts(SERIAL_COM1, "acpi: no CPUID APIC, staying UP\n");
  474. return 0;
  475. }
  476. if (!madt) {
  477. serial_puts(SERIAL_COM1, "acpi: no MADT, staying UP\n");
  478. return 0;
  479. }
  480. parse_madt(madt);
  481. /* Put the BSP first. If firmware omitted it, keep the discovered list
  482. * but make sure entry 0 is a real CPU. */
  483. for (i = 0; i < ncpu; i++)
  484. if (ids[i] == bsp_apic)
  485. break;
  486. if (i < ncpu) {
  487. unsigned int t = ids[0];
  488. ids[0] = ids[i];
  489. ids[i] = t;
  490. } else if (ncpu) {
  491. ids[0] = bsp_apic;
  492. }
  493. if (!ncpu)
  494. return 0;
  495. acpi_ok = 1;
  496. return 1;
  497. }
  498. unsigned int acpi_cpu_count(void)
  499. {
  500. return acpi_ok ? ncpu : 0;
  501. }
  502. unsigned int acpi_cpu_apic_id(unsigned int index)
  503. {
  504. return (acpi_ok && index < ncpu) ? ids[index] : 0;
  505. }
  506. unsigned int acpi_bsp_apic_id(void)
  507. {
  508. return bsp_apic;
  509. }
  510. unsigned int acpi_lapic_base(void)
  511. {
  512. return lapic_base;
  513. }
  514. unsigned int acpi_ioapic_count(void)
  515. {
  516. return acpi_ok ? nioapics : 0;
  517. }
  518. unsigned int acpi_ioapic_base(unsigned int index)
  519. {
  520. return (acpi_ok && index < nioapics) ? ioapic_bases[index] : 0;
  521. }
  522. unsigned int acpi_rtc_century_reg(void)
  523. {
  524. return rtc_century_reg;
  525. }
  526. /* Put the machine into ACPI S5 (soft off) through the FADT PM1 control
  527. * blocks. Returns 0 when the SLP_EN sequence was issued, -1 when the
  528. * firmware does not provide a usable path. The caller must halt either
  529. * way: a machine that ignores the write will just keep running. */
  530. static unsigned int pm1_cnt_read(unsigned int port)
  531. {
  532. if (fadt_pm1_cnt_len >= 4)
  533. return port_inl((unsigned short)port);
  534. return port_inw((unsigned short)port);
  535. }
  536. static void pm1_cnt_put(unsigned int port, unsigned int v)
  537. {
  538. if (fadt_pm1_cnt_len >= 4)
  539. port_outl((unsigned short)port, v);
  540. else
  541. port_outw((unsigned short)port, (unsigned short)v);
  542. }
  543. /* Set the sleep type and optionally SLP_EN. SCI_EN and the other live bits
  544. * are preserved: clearing SCI_EN can make a real chipset ignore the request.
  545. * The type is written first and SLP_EN in a second write because some
  546. * chipsets sample SLP_TYP only when SLP_EN is asserted. */
  547. static void pm1_cnt_set(unsigned int port, unsigned int type, int enable)
  548. {
  549. unsigned int v = pm1_cnt_read(port);
  550. v &= ~(7u << 10);
  551. v |= type << 10;
  552. if (enable)
  553. v |= 1u << 13;
  554. pm1_cnt_put(port, v);
  555. }
  556. /* The ION/nForce DSDT's _PTS(5): record the sleep state in the BIOS NVS
  557. * (ASSB/AOTB/AAXB/OSTP at 0xCFF9E064, field offsets from the DSDT's BIOS
  558. * OperationRegion) and arm PS1S/PS1E in the SMI block at 0x4400. The SMM
  559. * reads that state when SLP_EN is written; arming PS1E without it hangs. */
  560. static void nvidia_s5_prepare(unsigned int smi_base)
  561. {
  562. unsigned char *nvs;
  563. unsigned char b;
  564. nvs = ioremap(0xCFF9E064u, 0x100, IOREMAP_RAM);
  565. if (!nvs)
  566. return;
  567. nvs[0x31] = 5; /* ASSB: S5 */
  568. nvs[0x32] = 3; /* AOTB: OSFL() for Linux */
  569. *(unsigned int *)(nvs + 0x33) = 0; /* AAXB */
  570. nvs[0x4D] = 1; /* OSTP: OSYS() for Linux */
  571. port_outb(0x80, 5); /* DBG8 */
  572. if (smi_base <= 0xFFC0u) {
  573. b = port_inb((unsigned short)(smi_base + 1));
  574. port_outb((unsigned short)(smi_base + 1),
  575. (unsigned char)(b | 0x80));
  576. b = port_inb((unsigned short)(smi_base + 5));
  577. port_outb((unsigned short)(smi_base + 5),
  578. (unsigned char)(b | 0x80));
  579. }
  580. }
  581. int acpi_poweroff(void)
  582. {
  583. unsigned int t0, sci, smi, cmd;
  584. if (!fadt_ok || !s5_ok)
  585. return -1;
  586. if (fadt_flags & (1u << 4)) /* HW_REDUCED_ACPI */
  587. return -1;
  588. if (!fadt_pm1a_cnt || fadt_pm1a_cnt > 0xFFFFu)
  589. return -1;
  590. /* Some firmware still has ACPI in legacy mode (SCI_EN clear), where
  591. * PM1_CNT writes are ignored. Do the SMI handshake first; the FADT
  592. * SMI_CMD is authoritative, but a few boards leave it zero and still
  593. * use the conventional 0xB2/0xF1 pair. */
  594. sci = pm1_cnt_read(fadt_pm1a_cnt) & 1;
  595. if (!sci) {
  596. smi = fadt_smi_cmd;
  597. cmd = fadt_acpi_enable;
  598. if (!smi || smi > 0xFFFFu)
  599. smi = 0xB2;
  600. if (!cmd)
  601. cmd = 0xF1;
  602. port_outb((unsigned short)smi, (unsigned char)cmd);
  603. t0 = get_ticks_ms();
  604. while (get_ticks_ms() - t0 < 3000) {
  605. if (pm1_cnt_read(fadt_pm1a_cnt) & 1)
  606. break;
  607. }
  608. sci = pm1_cnt_read(fadt_pm1a_cnt) & 1;
  609. }
  610. serial_puts(SERIAL_COM1, "poweroff: S5 ");
  611. serial_putdec(SERIAL_COM1, s5a);
  612. serial_puts(SERIAL_COM1, "/");
  613. serial_putdec(SERIAL_COM1, s5b);
  614. serial_puts(SERIAL_COM1, " pm1a ");
  615. serial_puthex(SERIAL_COM1, fadt_pm1a_cnt);
  616. if (fadt_pm1b_cnt) {
  617. serial_puts(SERIAL_COM1, " pm1b ");
  618. serial_puthex(SERIAL_COM1, fadt_pm1b_cnt);
  619. }
  620. serial_puts(SERIAL_COM1, " sci ");
  621. serial_putdec(SERIAL_COM1, sci);
  622. serial_puts(SERIAL_COM1, "\n");
  623. /* Set SLP_TYP first, then assert SLP_EN in a separate write. */
  624. pm1_cnt_set(fadt_pm1a_cnt, s5a, 0);
  625. if (fadt_pm1b_cnt && fadt_pm1b_cnt <= 0xFFFFu)
  626. pm1_cnt_set(fadt_pm1b_cnt, s5b, 0);
  627. /* Intel-style southbridges route SLP_EN through an SMI while
  628. * SMI_EN.SLP_SMI_EN (bit 4 at PMBASE+0x30) is set, so clear it.
  629. * NVIDIA nForce/MCP parts go through their firmware SMM as the
  630. * DSDT's _PTS(5) describes: nvidia_s5_prepare() records the sleep
  631. * state in the BIOS NVS and arms PS1S/PS1E before SLP_EN. */
  632. if (fadt_smi_cmd < 0x4000u) {
  633. if (fadt_pm1a_evt && fadt_pm1a_evt <= 0xFFC0u) {
  634. unsigned int se =
  635. port_inl((unsigned short)(fadt_pm1a_evt + 0x30));
  636. port_outl((unsigned short)(fadt_pm1a_evt + 0x30),
  637. se & ~(1u << 4));
  638. }
  639. } else {
  640. nvidia_s5_prepare(fadt_smi_cmd - 0x2Eu);
  641. }
  642. pm1_cnt_set(fadt_pm1a_cnt, s5a, 1);
  643. if (fadt_pm1b_cnt && fadt_pm1b_cnt <= 0xFFFFu)
  644. pm1_cnt_set(fadt_pm1b_cnt, s5b, 1);
  645. return 0;
  646. }
  647. int acpi_dump_dsdt(const char *path)
  648. {
  649. if (!dsdt_ptr || !dsdt_len)
  650. return -1;
  651. return sys_write_file(path, dsdt_ptr, dsdt_len);
  652. }
  653. void acpi_dump(void)
  654. {
  655. unsigned int i;
  656. if (fadt_ok) {
  657. serial_puts(SERIAL_COM1, "acpi: fadt pm1a ");
  658. serial_puthex(SERIAL_COM1, fadt_pm1a_cnt);
  659. serial_puts(SERIAL_COM1, " s5 ");
  660. if (s5_ok) {
  661. serial_putdec(SERIAL_COM1, s5a);
  662. serial_puts(SERIAL_COM1, "/");
  663. serial_putdec(SERIAL_COM1, s5b);
  664. } else {
  665. serial_puts(SERIAL_COM1, "?");
  666. }
  667. serial_puts(SERIAL_COM1, "\n");
  668. }
  669. if (!acpi_ok) {
  670. serial_puts(SERIAL_COM1, "acpi: unavailable, 1 cpu\n");
  671. return;
  672. }
  673. serial_puts(SERIAL_COM1, "acpi: lapic ");
  674. serial_puthex(SERIAL_COM1, lapic_base);
  675. serial_puts(SERIAL_COM1, ", ");
  676. serial_putdec(SERIAL_COM1, ncpu);
  677. serial_puts(SERIAL_COM1, " cpus:");
  678. for (i = 0; i < ncpu; i++) {
  679. serial_puts(SERIAL_COM1, " ");
  680. serial_putdec(SERIAL_COM1, ids[i]);
  681. }
  682. serial_puts(SERIAL_COM1, "\n");
  683. for (i = 0; i < nioapics; i++) {
  684. serial_puts(SERIAL_COM1, "acpi: ioapic ");
  685. serial_puthex(SERIAL_COM1, ioapic_bases[i]);
  686. serial_puts(SERIAL_COM1, "\n");
  687. }
  688. }