acpi_table.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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.h>
  15. #include <asm/acpi_table.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. /*
  274. * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
  275. */
  276. ulong write_acpi_tables(ulong start)
  277. {
  278. u32 current;
  279. struct acpi_rsdp *rsdp;
  280. struct acpi_rsdt *rsdt;
  281. struct acpi_xsdt *xsdt;
  282. struct acpi_facs *facs;
  283. struct acpi_table_header *dsdt;
  284. struct acpi_fadt *fadt;
  285. struct acpi_mcfg *mcfg;
  286. struct acpi_madt *madt;
  287. int i;
  288. current = start;
  289. /* Align ACPI tables to 16 byte */
  290. current = ALIGN(current, 16);
  291. debug("ACPI: Writing ACPI tables at %lx\n", start);
  292. /* We need at least an RSDP and an RSDT Table */
  293. rsdp = (struct acpi_rsdp *)current;
  294. current += sizeof(struct acpi_rsdp);
  295. current = ALIGN(current, 16);
  296. rsdt = (struct acpi_rsdt *)current;
  297. current += sizeof(struct acpi_rsdt);
  298. current = ALIGN(current, 16);
  299. xsdt = (struct acpi_xsdt *)current;
  300. current += sizeof(struct acpi_xsdt);
  301. /*
  302. * Per ACPI spec, the FACS table address must be aligned to a 64 byte
  303. * boundary (Windows checks this, but Linux does not).
  304. */
  305. current = ALIGN(current, 64);
  306. /* clear all table memory */
  307. memset((void *)start, 0, current - start);
  308. acpi_write_rsdp(rsdp, rsdt, xsdt);
  309. acpi_write_rsdt(rsdt);
  310. acpi_write_xsdt(xsdt);
  311. debug("ACPI: * FACS\n");
  312. facs = (struct acpi_facs *)current;
  313. current += sizeof(struct acpi_facs);
  314. current = ALIGN(current, 16);
  315. acpi_create_facs(facs);
  316. debug("ACPI: * DSDT\n");
  317. dsdt = (struct acpi_table_header *)current;
  318. memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
  319. current += sizeof(struct acpi_table_header);
  320. memcpy((char *)current,
  321. (char *)&AmlCode + sizeof(struct acpi_table_header),
  322. dsdt->length - sizeof(struct acpi_table_header));
  323. current += dsdt->length - sizeof(struct acpi_table_header);
  324. current = ALIGN(current, 16);
  325. /* Pack GNVS into the ACPI table area */
  326. for (i = 0; i < dsdt->length; i++) {
  327. u32 *gnvs = (u32 *)((u32)dsdt + i);
  328. if (*gnvs == ACPI_GNVS_ADDR) {
  329. debug("Fix up global NVS in DSDT to 0x%08x\n", current);
  330. *gnvs = current;
  331. break;
  332. }
  333. }
  334. /* Update DSDT checksum since we patched the GNVS address */
  335. dsdt->checksum = 0;
  336. dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
  337. /* Fill in platform-specific global NVS variables */
  338. acpi_create_gnvs((struct acpi_global_nvs *)current);
  339. current += sizeof(struct acpi_global_nvs);
  340. current = ALIGN(current, 16);
  341. debug("ACPI: * FADT\n");
  342. fadt = (struct acpi_fadt *)current;
  343. current += sizeof(struct acpi_fadt);
  344. current = ALIGN(current, 16);
  345. acpi_create_fadt(fadt, facs, dsdt);
  346. acpi_add_table(rsdp, fadt);
  347. debug("ACPI: * MADT\n");
  348. madt = (struct acpi_madt *)current;
  349. acpi_create_madt(madt);
  350. current += madt->header.length;
  351. acpi_add_table(rsdp, madt);
  352. current = ALIGN(current, 16);
  353. debug("ACPI: * MCFG\n");
  354. mcfg = (struct acpi_mcfg *)current;
  355. acpi_create_mcfg(mcfg);
  356. current += mcfg->header.length;
  357. acpi_add_table(rsdp, mcfg);
  358. current = ALIGN(current, 16);
  359. debug("current = %x\n", current);
  360. acpi_rsdp_addr = (unsigned long)rsdp;
  361. debug("ACPI: done\n");
  362. /* Don't touch ACPI hardware on HW reduced platforms */
  363. if (fadt->flags & ACPI_FADT_HW_REDUCED_ACPI)
  364. return current;
  365. /*
  366. * Other than waiting for OSPM to request us to switch to ACPI mode,
  367. * do it by ourselves, since SMI will not be triggered.
  368. */
  369. enter_acpi_mode(fadt->pm1a_cnt_blk);
  370. return current;
  371. }
  372. ulong acpi_get_rsdp_addr(void)
  373. {
  374. return acpi_rsdp_addr;
  375. }