acpi.c 21 KB

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