acpi.c 8.6 KB

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