acpi_table.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Based on acpi.c from coreboot
  4. *
  5. * Copyright (C) 2015, Saket Sinha <saket.sinha89@gmail.com>
  6. * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com>
  7. */
  8. #include <common.h>
  9. #include <cpu.h>
  10. #include <dm.h>
  11. #include <dm/uclass-internal.h>
  12. #include <version.h>
  13. #include <asm/acpi/global_nvs.h>
  14. #include <asm/acpi_table.h>
  15. #include <asm/io.h>
  16. #include <asm/ioapic.h>
  17. #include <asm/lapic.h>
  18. #include <asm/mpspec.h>
  19. #include <asm/tables.h>
  20. #include <asm/arch/global_nvs.h>
  21. /*
  22. * IASL compiles the dsdt entries and writes the hex values
  23. * to a C array AmlCode[] (see dsdt.c).
  24. */
  25. extern const unsigned char AmlCode[];
  26. /* ACPI RSDP address to be used in boot parameters */
  27. static ulong acpi_rsdp_addr;
  28. static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
  29. struct acpi_xsdt *xsdt)
  30. {
  31. memset(rsdp, 0, sizeof(struct acpi_rsdp));
  32. memcpy(rsdp->signature, RSDP_SIG, 8);
  33. memcpy(rsdp->oem_id, OEM_ID, 6);
  34. rsdp->length = sizeof(struct acpi_rsdp);
  35. rsdp->rsdt_address = (u32)rsdt;
  36. /*
  37. * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
  38. *
  39. * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
  40. * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
  41. * revision 0)
  42. */
  43. if (xsdt == NULL) {
  44. rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
  45. } else {
  46. rsdp->xsdt_address = (u64)(u32)xsdt;
  47. rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
  48. }
  49. /* Calculate checksums */
  50. rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
  51. rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
  52. sizeof(struct acpi_rsdp));
  53. }
  54. void acpi_fill_header(struct acpi_table_header *header, char *signature)
  55. {
  56. memcpy(header->signature, signature, 4);
  57. memcpy(header->oem_id, OEM_ID, 6);
  58. memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
  59. header->oem_revision = U_BOOT_BUILD_DATE;
  60. memcpy(header->aslc_id, ASLC_ID, 4);
  61. }
  62. static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
  63. {
  64. struct acpi_table_header *header = &(rsdt->header);
  65. /* Fill out header fields */
  66. acpi_fill_header(header, "RSDT");
  67. header->length = sizeof(struct acpi_rsdt);
  68. header->revision = 1;
  69. /* Entries are filled in later, we come with an empty set */
  70. /* Fix checksum */
  71. header->checksum = table_compute_checksum((void *)rsdt,
  72. sizeof(struct acpi_rsdt));
  73. }
  74. static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
  75. {
  76. struct acpi_table_header *header = &(xsdt->header);
  77. /* Fill out header fields */
  78. acpi_fill_header(header, "XSDT");
  79. header->length = sizeof(struct acpi_xsdt);
  80. header->revision = 1;
  81. /* Entries are filled in later, we come with an empty set */
  82. /* Fix checksum */
  83. header->checksum = table_compute_checksum((void *)xsdt,
  84. sizeof(struct acpi_xsdt));
  85. }
  86. /**
  87. * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
  88. * and checksum.
  89. */
  90. static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
  91. {
  92. int i, entries_num;
  93. struct acpi_rsdt *rsdt;
  94. struct acpi_xsdt *xsdt = NULL;
  95. /* The RSDT is mandatory while the XSDT is not */
  96. rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
  97. if (rsdp->xsdt_address)
  98. xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
  99. /* This should always be MAX_ACPI_TABLES */
  100. entries_num = ARRAY_SIZE(rsdt->entry);
  101. for (i = 0; i < entries_num; i++) {
  102. if (rsdt->entry[i] == 0)
  103. break;
  104. }
  105. if (i >= entries_num) {
  106. debug("ACPI: Error: too many tables\n");
  107. return;
  108. }
  109. /* Add table to the RSDT */
  110. rsdt->entry[i] = (u32)table;
  111. /* Fix RSDT length or the kernel will assume invalid entries */
  112. rsdt->header.length = sizeof(struct acpi_table_header) +
  113. (sizeof(u32) * (i + 1));
  114. /* Re-calculate checksum */
  115. rsdt->header.checksum = 0;
  116. rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
  117. rsdt->header.length);
  118. /*
  119. * And now the same thing for the XSDT. We use the same index as for
  120. * now we want the XSDT and RSDT to always be in sync in U-Boot
  121. */
  122. if (xsdt) {
  123. /* Add table to the XSDT */
  124. xsdt->entry[i] = (u64)(u32)table;
  125. /* Fix XSDT length */
  126. xsdt->header.length = sizeof(struct acpi_table_header) +
  127. (sizeof(u64) * (i + 1));
  128. /* Re-calculate checksum */
  129. xsdt->header.checksum = 0;
  130. xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
  131. xsdt->header.length);
  132. }
  133. }
  134. static void acpi_create_facs(struct acpi_facs *facs)
  135. {
  136. memset((void *)facs, 0, sizeof(struct acpi_facs));
  137. memcpy(facs->signature, "FACS", 4);
  138. facs->length = sizeof(struct acpi_facs);
  139. facs->hardware_signature = 0;
  140. facs->firmware_waking_vector = 0;
  141. facs->global_lock = 0;
  142. facs->flags = 0;
  143. facs->x_firmware_waking_vector_l = 0;
  144. facs->x_firmware_waking_vector_h = 0;
  145. facs->version = 1;
  146. }
  147. static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
  148. u8 cpu, u8 apic)
  149. {
  150. lapic->type = ACPI_APIC_LAPIC;
  151. lapic->length = sizeof(struct acpi_madt_lapic);
  152. lapic->flags = LOCAL_APIC_FLAG_ENABLED;
  153. lapic->processor_id = cpu;
  154. lapic->apic_id = apic;
  155. return lapic->length;
  156. }
  157. int acpi_create_madt_lapics(u32 current)
  158. {
  159. struct udevice *dev;
  160. int total_length = 0;
  161. for (uclass_find_first_device(UCLASS_CPU, &dev);
  162. dev;
  163. uclass_find_next_device(&dev)) {
  164. struct cpu_platdata *plat = dev_get_parent_platdata(dev);
  165. int length = acpi_create_madt_lapic(
  166. (struct acpi_madt_lapic *)current,
  167. plat->cpu_id, plat->cpu_id);
  168. current += length;
  169. total_length += length;
  170. }
  171. return total_length;
  172. }
  173. int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
  174. u32 addr, u32 gsi_base)
  175. {
  176. ioapic->type = ACPI_APIC_IOAPIC;
  177. ioapic->length = sizeof(struct acpi_madt_ioapic);
  178. ioapic->reserved = 0x00;
  179. ioapic->gsi_base = gsi_base;
  180. ioapic->ioapic_id = id;
  181. ioapic->ioapic_addr = addr;
  182. return ioapic->length;
  183. }
  184. int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
  185. u8 bus, u8 source, u32 gsirq, u16 flags)
  186. {
  187. irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
  188. irqoverride->length = sizeof(struct acpi_madt_irqoverride);
  189. irqoverride->bus = bus;
  190. irqoverride->source = source;
  191. irqoverride->gsirq = gsirq;
  192. irqoverride->flags = flags;
  193. return irqoverride->length;
  194. }
  195. int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
  196. u8 cpu, u16 flags, u8 lint)
  197. {
  198. lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
  199. lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
  200. lapic_nmi->flags = flags;
  201. lapic_nmi->processor_id = cpu;
  202. lapic_nmi->lint = lint;
  203. return lapic_nmi->length;
  204. }
  205. static int acpi_create_madt_irq_overrides(u32 current)
  206. {
  207. struct acpi_madt_irqoverride *irqovr;
  208. u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH;
  209. int length = 0;
  210. irqovr = (void *)current;
  211. length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0);
  212. irqovr = (void *)(current + length);
  213. length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags);
  214. return length;
  215. }
  216. __weak u32 acpi_fill_madt(u32 current)
  217. {
  218. current += acpi_create_madt_lapics(current);
  219. current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current,
  220. io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0);
  221. current += acpi_create_madt_irq_overrides(current);
  222. return current;
  223. }
  224. static void acpi_create_madt(struct acpi_madt *madt)
  225. {
  226. struct acpi_table_header *header = &(madt->header);
  227. u32 current = (u32)madt + sizeof(struct acpi_madt);
  228. memset((void *)madt, 0, sizeof(struct acpi_madt));
  229. /* Fill out header fields */
  230. acpi_fill_header(header, "APIC");
  231. header->length = sizeof(struct acpi_madt);
  232. header->revision = 4;
  233. madt->lapic_addr = LAPIC_DEFAULT_BASE;
  234. madt->flags = ACPI_MADT_PCAT_COMPAT;
  235. current = acpi_fill_madt(current);
  236. /* (Re)calculate length and checksum */
  237. header->length = current - (u32)madt;
  238. header->checksum = table_compute_checksum((void *)madt, header->length);
  239. }
  240. int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, u32 base,
  241. u16 seg_nr, u8 start, u8 end)
  242. {
  243. memset(mmconfig, 0, sizeof(*mmconfig));
  244. mmconfig->base_address_l = base;
  245. mmconfig->base_address_h = 0;
  246. mmconfig->pci_segment_group_number = seg_nr;
  247. mmconfig->start_bus_number = start;
  248. mmconfig->end_bus_number = end;
  249. return sizeof(struct acpi_mcfg_mmconfig);
  250. }
  251. __weak u32 acpi_fill_mcfg(u32 current)
  252. {
  253. current += acpi_create_mcfg_mmconfig
  254. ((struct acpi_mcfg_mmconfig *)current,
  255. CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
  256. return current;
  257. }
  258. /* MCFG is defined in the PCI Firmware Specification 3.0 */
  259. static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
  260. {
  261. struct acpi_table_header *header = &(mcfg->header);
  262. u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
  263. memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
  264. /* Fill out header fields */
  265. acpi_fill_header(header, "MCFG");
  266. header->length = sizeof(struct acpi_mcfg);
  267. header->revision = 1;
  268. current = acpi_fill_mcfg(current);
  269. /* (Re)calculate length and checksum */
  270. header->length = current - (u32)mcfg;
  271. header->checksum = table_compute_checksum((void *)mcfg, header->length);
  272. }
  273. void enter_acpi_mode(int pm1_cnt)
  274. {
  275. u16 val = inw(pm1_cnt);
  276. /*
  277. * PM1_CNT register bit0 selects the power management event to be
  278. * either an SCI or SMI interrupt. When this bit is set, then power
  279. * management events will generate an SCI interrupt. When this bit
  280. * is reset power management events will generate an SMI interrupt.
  281. *
  282. * Per ACPI spec, it is the responsibility of the hardware to set
  283. * or reset this bit. OSPM always preserves this bit position.
  284. *
  285. * U-Boot does not support SMI. And we don't have plan to support
  286. * anything running in SMM within U-Boot. To create a legacy-free
  287. * system, and expose ourselves to OSPM as working under ACPI mode
  288. * already, turn this bit on.
  289. */
  290. outw(val | PM1_CNT_SCI_EN, pm1_cnt);
  291. }
  292. /*
  293. * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
  294. */
  295. ulong write_acpi_tables(ulong start)
  296. {
  297. u32 current;
  298. struct acpi_rsdp *rsdp;
  299. struct acpi_rsdt *rsdt;
  300. struct acpi_xsdt *xsdt;
  301. struct acpi_facs *facs;
  302. struct acpi_table_header *dsdt;
  303. struct acpi_fadt *fadt;
  304. struct acpi_mcfg *mcfg;
  305. struct acpi_madt *madt;
  306. int i;
  307. current = start;
  308. /* Align ACPI tables to 16 byte */
  309. current = ALIGN(current, 16);
  310. debug("ACPI: Writing ACPI tables at %lx\n", start);
  311. /* We need at least an RSDP and an RSDT Table */
  312. rsdp = (struct acpi_rsdp *)current;
  313. current += sizeof(struct acpi_rsdp);
  314. current = ALIGN(current, 16);
  315. rsdt = (struct acpi_rsdt *)current;
  316. current += sizeof(struct acpi_rsdt);
  317. current = ALIGN(current, 16);
  318. xsdt = (struct acpi_xsdt *)current;
  319. current += sizeof(struct acpi_xsdt);
  320. /*
  321. * Per ACPI spec, the FACS table address must be aligned to a 64 byte
  322. * boundary (Windows checks this, but Linux does not).
  323. */
  324. current = ALIGN(current, 64);
  325. /* clear all table memory */
  326. memset((void *)start, 0, current - start);
  327. acpi_write_rsdp(rsdp, rsdt, xsdt);
  328. acpi_write_rsdt(rsdt);
  329. acpi_write_xsdt(xsdt);
  330. debug("ACPI: * FACS\n");
  331. facs = (struct acpi_facs *)current;
  332. current += sizeof(struct acpi_facs);
  333. current = ALIGN(current, 16);
  334. acpi_create_facs(facs);
  335. debug("ACPI: * DSDT\n");
  336. dsdt = (struct acpi_table_header *)current;
  337. memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
  338. current += sizeof(struct acpi_table_header);
  339. memcpy((char *)current,
  340. (char *)&AmlCode + sizeof(struct acpi_table_header),
  341. dsdt->length - sizeof(struct acpi_table_header));
  342. current += dsdt->length - sizeof(struct acpi_table_header);
  343. current = ALIGN(current, 16);
  344. /* Pack GNVS into the ACPI table area */
  345. for (i = 0; i < dsdt->length; i++) {
  346. u32 *gnvs = (u32 *)((u32)dsdt + i);
  347. if (*gnvs == ACPI_GNVS_ADDR) {
  348. debug("Fix up global NVS in DSDT to 0x%08x\n", current);
  349. *gnvs = current;
  350. break;
  351. }
  352. }
  353. /* Update DSDT checksum since we patched the GNVS address */
  354. dsdt->checksum = 0;
  355. dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
  356. /* Fill in platform-specific global NVS variables */
  357. acpi_create_gnvs((struct acpi_global_nvs *)current);
  358. current += sizeof(struct acpi_global_nvs);
  359. current = ALIGN(current, 16);
  360. debug("ACPI: * FADT\n");
  361. fadt = (struct acpi_fadt *)current;
  362. current += sizeof(struct acpi_fadt);
  363. current = ALIGN(current, 16);
  364. acpi_create_fadt(fadt, facs, dsdt);
  365. acpi_add_table(rsdp, fadt);
  366. debug("ACPI: * MADT\n");
  367. madt = (struct acpi_madt *)current;
  368. acpi_create_madt(madt);
  369. current += madt->header.length;
  370. acpi_add_table(rsdp, madt);
  371. current = ALIGN(current, 16);
  372. debug("ACPI: * MCFG\n");
  373. mcfg = (struct acpi_mcfg *)current;
  374. acpi_create_mcfg(mcfg);
  375. current += mcfg->header.length;
  376. acpi_add_table(rsdp, mcfg);
  377. current = ALIGN(current, 16);
  378. debug("current = %x\n", current);
  379. acpi_rsdp_addr = (unsigned long)rsdp;
  380. debug("ACPI: done\n");
  381. /* Don't touch ACPI hardware on HW reduced platforms */
  382. if (fadt->flags & ACPI_FADT_HW_REDUCED_ACPI)
  383. return current;
  384. /*
  385. * Other than waiting for OSPM to request us to switch to ACPI mode,
  386. * do it by ourselves, since SMI will not be triggered.
  387. */
  388. enter_acpi_mode(fadt->pm1a_cnt_blk);
  389. return current;
  390. }
  391. ulong acpi_get_rsdp_addr(void)
  392. {
  393. return acpi_rsdp_addr;
  394. }
  395. static struct acpi_rsdp *acpi_valid_rsdp(struct acpi_rsdp *rsdp)
  396. {
  397. if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
  398. return NULL;
  399. debug("Looking on %p for valid checksum\n", rsdp);
  400. if (table_compute_checksum((void *)rsdp, 20) != 0)
  401. return NULL;
  402. debug("acpi rsdp checksum 1 passed\n");
  403. if ((rsdp->revision > 1) &&
  404. (table_compute_checksum((void *)rsdp, rsdp->length) != 0))
  405. return NULL;
  406. debug("acpi rsdp checksum 2 passed\n");
  407. return rsdp;
  408. }
  409. struct acpi_fadt *acpi_find_fadt(void)
  410. {
  411. char *p, *end;
  412. struct acpi_rsdp *rsdp = NULL;
  413. struct acpi_rsdt *rsdt;
  414. struct acpi_fadt *fadt = NULL;
  415. int i;
  416. /* Find RSDP */
  417. for (p = (char *)ROM_TABLE_ADDR; p < (char *)ROM_TABLE_END; p += 16) {
  418. rsdp = acpi_valid_rsdp((struct acpi_rsdp *)p);
  419. if (rsdp)
  420. break;
  421. }
  422. if (rsdp == NULL)
  423. return NULL;
  424. debug("RSDP found at %p\n", rsdp);
  425. rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
  426. end = (char *)rsdt + rsdt->header.length;
  427. debug("RSDT found at %p ends at %p\n", rsdt, end);
  428. for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
  429. fadt = (struct acpi_fadt *)rsdt->entry[i];
  430. if (strncmp((char *)fadt, "FACP", 4) == 0)
  431. break;
  432. fadt = NULL;
  433. }
  434. if (fadt == NULL)
  435. return NULL;
  436. debug("FADT found at %p\n", fadt);
  437. return fadt;
  438. }
  439. void *acpi_find_wakeup_vector(struct acpi_fadt *fadt)
  440. {
  441. struct acpi_facs *facs;
  442. void *wake_vec;
  443. debug("Trying to find the wakeup vector...\n");
  444. facs = (struct acpi_facs *)fadt->firmware_ctrl;
  445. if (facs == NULL) {
  446. debug("No FACS found, wake up from S3 not possible.\n");
  447. return NULL;
  448. }
  449. debug("FACS found at %p\n", facs);
  450. wake_vec = (void *)facs->firmware_waking_vector;
  451. debug("OS waking vector is %p\n", wake_vec);
  452. return wake_vec;
  453. }