acpi.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* Minimal ACPI RSDP/RSDT/XSDT/MADT parser. See acpi.h.
  2. *
  3. * ACPI tables are usually below the 128 MB identity map, but on larger
  4. * machines they can sit above it; every table page is mapped through
  5. * paging_map_ram() before it is read. 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 "serial.h"
  12. struct acpi_rsdp {
  13. char signature[8];
  14. unsigned char checksum;
  15. char oem_id[6];
  16. unsigned char revision;
  17. unsigned int rsdt;
  18. unsigned int length;
  19. unsigned long long xsdt;
  20. unsigned char ext_checksum;
  21. unsigned char reserved[3];
  22. } __attribute__((packed));
  23. struct acpi_sdt {
  24. char signature[4];
  25. unsigned int length;
  26. unsigned char revision;
  27. unsigned char checksum;
  28. char oem_id[6];
  29. char oem_table_id[8];
  30. unsigned int oem_revision;
  31. unsigned int creator_id;
  32. unsigned int creator_revision;
  33. } __attribute__((packed));
  34. #define RSDP_SIG "RSD PTR "
  35. #define MADT_SIG "APIC"
  36. #define LAPIC_DEFAULT 0xFEE00000u
  37. static unsigned int ids[ACPI_MAX_CPUS];
  38. static unsigned int ncpu;
  39. static unsigned int bsp_apic;
  40. static unsigned int lapic_base;
  41. static unsigned int ioapic_bases[ACPI_MAX_IOAPICS];
  42. static unsigned int nioapics;
  43. static int acpi_ok;
  44. /* FADT "century" register (offset 108), 0 when absent. The RTC uses it to
  45. * keep the two-digit CMOS year in the right century; rtc.c only writes it
  46. * when the firmware points at a real CMOS register. */
  47. static unsigned int rtc_century_reg;
  48. static void *phys_ptr(unsigned int addr)
  49. {
  50. return (void *)addr;
  51. }
  52. static int range_ok(unsigned int addr, unsigned int len)
  53. {
  54. if (addr >= 0xFFFFF000u || len > 0x100000u)
  55. return 0;
  56. return addr + len >= addr;
  57. }
  58. /* Map every 4 MB page touched by [addr, addr+len). */
  59. static void map_table(unsigned int addr, unsigned int len)
  60. {
  61. unsigned int p = addr & 0xFFC00000u;
  62. unsigned int last = (addr + len - 1u) & 0xFFC00000u;
  63. for (;;) {
  64. paging_map_ram(p, 0x400000u);
  65. if (p >= last)
  66. break;
  67. if (p > 0xFFC00000u - 0x400000u)
  68. break;
  69. p += 0x400000u;
  70. }
  71. }
  72. static int sum_ok(const unsigned char *p, unsigned int len)
  73. {
  74. unsigned char sum = 0;
  75. unsigned int i;
  76. for (i = 0; i < len; i++)
  77. sum = (unsigned char)(sum + p[i]);
  78. return sum == 0;
  79. }
  80. static int rsdp_valid(const struct acpi_rsdp *r)
  81. {
  82. unsigned int i;
  83. for (i = 0; i < 8; i++)
  84. if (r->signature[i] != RSDP_SIG[i])
  85. return 0;
  86. if (!sum_ok((const unsigned char *)r, 20))
  87. return 0;
  88. if (r->revision >= 2) {
  89. if (r->length < 36 || r->length > 4096)
  90. return 0;
  91. if (!sum_ok((const unsigned char *)r, r->length))
  92. return 0;
  93. }
  94. return 1;
  95. }
  96. static const struct acpi_rsdp *find_rsdp(void)
  97. {
  98. volatile unsigned short *ebda_seg = (volatile unsigned short *)0x40E;
  99. unsigned int ebda, i;
  100. unsigned int given = bootinfo_rsdp();
  101. /* The UEFI loader passes the RSDP address from the system table. UEFI
  102. * places ACPI tables outside the 128 MB identity map often enough that
  103. * the scan below would never see them, so map the page first. */
  104. if (given && given < 0xFFF00000u) {
  105. const struct acpi_rsdp *r;
  106. paging_map_ram(given & 0xFFC00000u, 0x800000u);
  107. r = (const struct acpi_rsdp *)given;
  108. if (rsdp_valid(r)) {
  109. serial_puts(SERIAL_COM1, "acpi: EFI RSDP at ");
  110. serial_puthex(SERIAL_COM1, given);
  111. serial_puts(SERIAL_COM1, "\n");
  112. return r;
  113. }
  114. serial_puts(SERIAL_COM1, "acpi: EFI RSDP invalid, scanning\n");
  115. }
  116. /* Opaque asm keeps -Warray-bounds quiet about the fixed BIOS pointer. */
  117. __asm__ volatile ("" : "+r"(ebda_seg));
  118. ebda = (unsigned int)(*ebda_seg) << 4;
  119. if (ebda) {
  120. for (i = 0; i < 1024; i += 16) {
  121. const struct acpi_rsdp *r =
  122. (const struct acpi_rsdp *)(ebda + i);
  123. if (rsdp_valid(r))
  124. return r;
  125. }
  126. }
  127. for (i = 0xE0000; i < 0x100000; i += 16) {
  128. const struct acpi_rsdp *r =
  129. (const struct acpi_rsdp *)(i);
  130. if (rsdp_valid(r))
  131. return r;
  132. }
  133. return 0;
  134. }
  135. static const struct acpi_sdt *find_madt(const struct acpi_rsdp *rsdp)
  136. {
  137. unsigned int addr, len, i, step;
  138. const struct acpi_sdt *sdt;
  139. const struct acpi_sdt *madt = 0;
  140. /* Prefer the 64-bit XSDT when it is reachable from the 32-bit
  141. * identity map; otherwise fall back to the ACPI 1.0 RSDT. */
  142. if (rsdp->revision >= 2 && rsdp->xsdt &&
  143. (unsigned int)(rsdp->xsdt >> 32) == 0) {
  144. addr = (unsigned int)rsdp->xsdt;
  145. step = 8;
  146. } else {
  147. addr = rsdp->rsdt;
  148. step = 4;
  149. }
  150. if (!addr || !range_ok(addr, sizeof(struct acpi_sdt)))
  151. return 0;
  152. map_table(addr, sizeof(struct acpi_sdt));
  153. sdt = (const struct acpi_sdt *)phys_ptr(addr);
  154. len = sdt->length;
  155. if (len < sizeof(struct acpi_sdt) || len > 0x100000u)
  156. return 0;
  157. map_table(addr, len);
  158. if (!sum_ok((const unsigned char *)sdt, len))
  159. return 0;
  160. for (i = sizeof(struct acpi_sdt); i + step <= len; i += step) {
  161. unsigned int taddr;
  162. const struct acpi_sdt *t;
  163. unsigned int tlen;
  164. if (step == 8) {
  165. unsigned long long a =
  166. *(const unsigned long long *)((const unsigned char *)sdt + i);
  167. if ((unsigned int)(a >> 32) != 0)
  168. continue;
  169. taddr = (unsigned int)a;
  170. } else {
  171. taddr = *(const unsigned int *)((const unsigned char *)sdt + i);
  172. }
  173. if (!taddr || !range_ok(taddr, sizeof(struct acpi_sdt)))
  174. continue;
  175. map_table(taddr, sizeof(struct acpi_sdt));
  176. t = (const struct acpi_sdt *)phys_ptr(taddr);
  177. tlen = t->length;
  178. if (tlen < sizeof(struct acpi_sdt) || tlen > 0x100000u)
  179. continue;
  180. map_table(taddr, tlen);
  181. if (!sum_ok((const unsigned char *)t, tlen))
  182. continue;
  183. if (t->signature[0] == 'F' && t->signature[1] == 'A' &&
  184. t->signature[2] == 'C' && t->signature[3] == 'P') {
  185. /* FADT "century" is at offset 108 (ACPI 2.0+); 0 means
  186. * the firmware does not provide a century register. */
  187. if (tlen >= 109) {
  188. unsigned char c =
  189. ((const unsigned char *)t)[108];
  190. if (c >= 0x0E && c <= 0x7F)
  191. rtc_century_reg = c;
  192. }
  193. continue;
  194. }
  195. if (t->signature[0] == 'A' && t->signature[1] == 'P' &&
  196. t->signature[2] == 'I' && t->signature[3] == 'C' &&
  197. !madt)
  198. madt = t;
  199. }
  200. return madt;
  201. }
  202. static unsigned int cpuid_apic_id(void)
  203. {
  204. unsigned int eax, ebx, ecx, edx;
  205. eax = 1;
  206. __asm__ volatile ("cpuid"
  207. : "+a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx));
  208. if (!(edx & (1u << 9))) /* CPUID.1:EDX.APIC */
  209. return 0xFFFFFFFFu;
  210. return ebx >> 24; /* initial local APIC ID */
  211. }
  212. static void parse_madt(const struct acpi_sdt *madt)
  213. {
  214. const unsigned char *p = (const unsigned char *)madt;
  215. unsigned int len = madt->length;
  216. unsigned int i;
  217. lapic_base = LAPIC_DEFAULT;
  218. if (len >= 44) {
  219. unsigned int base = *(const unsigned int *)(p + 36);
  220. if (base)
  221. lapic_base = base;
  222. }
  223. for (i = 44; i + 2 <= len; ) {
  224. unsigned char type = p[i];
  225. unsigned char elen = p[i + 1];
  226. if (elen < 2 || i + elen > len)
  227. break;
  228. if (type == 0 && elen >= 8) {
  229. /* Processor Local APIC: flags bit 0 = enabled. */
  230. if ((p[i + 4] & 1) && ncpu < ACPI_MAX_CPUS)
  231. ids[ncpu++] = p[i + 3];
  232. } else if (type == 1 && elen >= 12) {
  233. /* I/O APIC: id, 32-bit MMIO base, GSI base. */
  234. unsigned int base = *(const unsigned int *)(p + i + 4);
  235. if (base && nioapics < ACPI_MAX_IOAPICS)
  236. ioapic_bases[nioapics++] = base;
  237. } else if (type == 5 && elen >= 12) {
  238. /* Local APIC Address Override (64-bit). */
  239. unsigned int lo = *(const unsigned int *)(p + i + 4);
  240. unsigned int hi = *(const unsigned int *)(p + i + 8);
  241. if (!hi && lo)
  242. lapic_base = lo;
  243. }
  244. /* x2APIC (type 9), IOAPIC (1), NMI (4) and overrides are not
  245. * needed for the first SMP step. */
  246. i += elen;
  247. }
  248. }
  249. int acpi_init(void)
  250. {
  251. const struct acpi_rsdp *rsdp;
  252. const struct acpi_sdt *madt;
  253. unsigned int i;
  254. acpi_ok = 0;
  255. ncpu = 0;
  256. nioapics = 0;
  257. rtc_century_reg = 0;
  258. bsp_apic = cpuid_apic_id();
  259. if (bsp_apic == 0xFFFFFFFFu)
  260. return 0;
  261. rsdp = find_rsdp();
  262. if (!rsdp) {
  263. serial_puts(SERIAL_COM1, "acpi: no RSDP, staying UP\n");
  264. return 0;
  265. }
  266. madt = find_madt(rsdp);
  267. if (!madt) {
  268. serial_puts(SERIAL_COM1, "acpi: no MADT, staying UP\n");
  269. return 0;
  270. }
  271. parse_madt(madt);
  272. /* Put the BSP first. If firmware omitted it, keep the discovered list
  273. * but make sure entry 0 is a real CPU. */
  274. for (i = 0; i < ncpu; i++)
  275. if (ids[i] == bsp_apic)
  276. break;
  277. if (i < ncpu) {
  278. unsigned int t = ids[0];
  279. ids[0] = ids[i];
  280. ids[i] = t;
  281. } else if (ncpu) {
  282. ids[0] = bsp_apic;
  283. }
  284. if (!ncpu)
  285. return 0;
  286. acpi_ok = 1;
  287. return 1;
  288. }
  289. unsigned int acpi_cpu_count(void)
  290. {
  291. return acpi_ok ? ncpu : 0;
  292. }
  293. unsigned int acpi_cpu_apic_id(unsigned int index)
  294. {
  295. return (acpi_ok && index < ncpu) ? ids[index] : 0;
  296. }
  297. unsigned int acpi_bsp_apic_id(void)
  298. {
  299. return bsp_apic;
  300. }
  301. unsigned int acpi_lapic_base(void)
  302. {
  303. return lapic_base;
  304. }
  305. unsigned int acpi_ioapic_count(void)
  306. {
  307. return acpi_ok ? nioapics : 0;
  308. }
  309. unsigned int acpi_ioapic_base(unsigned int index)
  310. {
  311. return (acpi_ok && index < nioapics) ? ioapic_bases[index] : 0;
  312. }
  313. unsigned int acpi_rtc_century_reg(void)
  314. {
  315. return rtc_century_reg;
  316. }
  317. void acpi_dump(void)
  318. {
  319. unsigned int i;
  320. if (!acpi_ok) {
  321. serial_puts(SERIAL_COM1, "acpi: unavailable, 1 cpu\n");
  322. return;
  323. }
  324. serial_puts(SERIAL_COM1, "acpi: lapic ");
  325. serial_puthex(SERIAL_COM1, lapic_base);
  326. serial_puts(SERIAL_COM1, ", ");
  327. serial_putdec(SERIAL_COM1, ncpu);
  328. serial_puts(SERIAL_COM1, " cpus:");
  329. for (i = 0; i < ncpu; i++) {
  330. serial_puts(SERIAL_COM1, " ");
  331. serial_putdec(SERIAL_COM1, ids[i]);
  332. }
  333. serial_puts(SERIAL_COM1, "\n");
  334. for (i = 0; i < nioapics; i++) {
  335. serial_puts(SERIAL_COM1, "acpi: ioapic ");
  336. serial_puthex(SERIAL_COM1, ioapic_bases[i]);
  337. serial_puts(SERIAL_COM1, "\n");
  338. }
  339. }