acpi.c 9.1 KB

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