acpi.c 7.8 KB

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