acpi_table.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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/ioapic.h>
  16. #include <asm/lapic.h>
  17. #include <asm/mpspec.h>
  18. #include <asm/tables.h>
  19. #include <asm/arch/global_nvs.h>
  20. /*
  21. * IASL compiles the dsdt entries and writes the hex values
  22. * to a C array AmlCode[] (see dsdt.c).
  23. */
  24. extern const unsigned char AmlCode[];
  25. /* ACPI RSDP address to be used in boot parameters */
  26. static ulong acpi_rsdp_addr;
  27. static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
  28. struct acpi_xsdt *xsdt)
  29. {
  30. memset(rsdp, 0, sizeof(struct acpi_rsdp));
  31. memcpy(rsdp->signature, RSDP_SIG, 8);
  32. memcpy(rsdp->oem_id, OEM_ID, 6);
  33. rsdp->length = sizeof(struct acpi_rsdp);
  34. rsdp->rsdt_address = (u32)rsdt;
  35. /*
  36. * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
  37. *
  38. * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
  39. * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
  40. * revision 0)
  41. */
  42. if (xsdt == NULL) {
  43. rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
  44. } else {
  45. rsdp->xsdt_address = (u64)(u32)xsdt;
  46. rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
  47. }
  48. /* Calculate checksums */
  49. rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
  50. rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
  51. sizeof(struct acpi_rsdp));
  52. }
  53. void acpi_fill_header(struct acpi_table_header *header, char *signature)
  54. {
  55. memcpy(header->signature, signature, 4);
  56. memcpy(header->oem_id, OEM_ID, 6);
  57. memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
  58. header->oem_revision = U_BOOT_BUILD_DATE;
  59. memcpy(header->aslc_id, ASLC_ID, 4);
  60. }
  61. static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
  62. {
  63. struct acpi_table_header *header = &(rsdt->header);
  64. /* Fill out header fields */
  65. acpi_fill_header(header, "RSDT");
  66. header->length = sizeof(struct acpi_rsdt);
  67. header->revision = 1;
  68. /* Entries are filled in later, we come with an empty set */
  69. /* Fix checksum */
  70. header->checksum = table_compute_checksum((void *)rsdt,
  71. sizeof(struct acpi_rsdt));
  72. }
  73. static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
  74. {
  75. struct acpi_table_header *header = &(xsdt->header);
  76. /* Fill out header fields */
  77. acpi_fill_header(header, "XSDT");
  78. header->length = sizeof(struct acpi_xsdt);
  79. header->revision = 1;
  80. /* Entries are filled in later, we come with an empty set */
  81. /* Fix checksum */
  82. header->checksum = table_compute_checksum((void *)xsdt,
  83. sizeof(struct acpi_xsdt));
  84. }
  85. /**
  86. * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
  87. * and checksum.
  88. */
  89. static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
  90. {
  91. int i, entries_num;
  92. struct acpi_rsdt *rsdt;
  93. struct acpi_xsdt *xsdt = NULL;
  94. /* The RSDT is mandatory while the XSDT is not */
  95. rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
  96. if (rsdp->xsdt_address)
  97. xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
  98. /* This should always be MAX_ACPI_TABLES */
  99. entries_num = ARRAY_SIZE(rsdt->entry);
  100. for (i = 0; i < entries_num; i++) {
  101. if (rsdt->entry[i] == 0)
  102. break;
  103. }
  104. if (i >= entries_num) {
  105. debug("ACPI: Error: too many tables\n");
  106. return;
  107. }
  108. /* Add table to the RSDT */
  109. rsdt->entry[i] = (u32)table;
  110. /* Fix RSDT length or the kernel will assume invalid entries */
  111. rsdt->header.length = sizeof(struct acpi_table_header) +
  112. (sizeof(u32) * (i + 1));
  113. /* Re-calculate checksum */
  114. rsdt->header.checksum = 0;
  115. rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
  116. rsdt->header.length);
  117. /*
  118. * And now the same thing for the XSDT. We use the same index as for
  119. * now we want the XSDT and RSDT to always be in sync in U-Boot
  120. */
  121. if (xsdt) {
  122. /* Add table to the XSDT */
  123. xsdt->entry[i] = (u64)(u32)table;
  124. /* Fix XSDT length */
  125. xsdt->header.length = sizeof(struct acpi_table_header) +
  126. (sizeof(u64) * (i + 1));
  127. /* Re-calculate checksum */
  128. xsdt->header.checksum = 0;
  129. xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
  130. xsdt->header.length);
  131. }
  132. }
  133. static void acpi_create_facs(struct acpi_facs *facs)
  134. {
  135. memset((void *)facs, 0, sizeof(struct acpi_facs));
  136. memcpy(facs->signature, "FACS", 4);
  137. facs->length = sizeof(struct acpi_facs);
  138. facs->hardware_signature = 0;
  139. facs->firmware_waking_vector = 0;
  140. facs->global_lock = 0;
  141. facs->flags = 0;
  142. facs->x_firmware_waking_vector_l = 0;
  143. facs->x_firmware_waking_vector_h = 0;
  144. facs->version = 1;
  145. }
  146. static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
  147. u8 cpu, u8 apic)
  148. {
  149. lapic->type = ACPI_APIC_LAPIC;
  150. lapic->length = sizeof(struct acpi_madt_lapic);
  151. lapic->flags = LOCAL_APIC_FLAG_ENABLED;
  152. lapic->processor_id = cpu;
  153. lapic->apic_id = apic;
  154. return lapic->length;
  155. }
  156. int acpi_create_madt_lapics(u32 current)
  157. {
  158. struct udevice *dev;
  159. int total_length = 0;
  160. for (uclass_find_first_device(UCLASS_CPU, &dev);
  161. dev;
  162. uclass_find_next_device(&dev)) {
  163. struct cpu_platdata *plat = dev_get_parent_platdata(dev);
  164. int length = acpi_create_madt_lapic(
  165. (struct acpi_madt_lapic *)current,
  166. plat->cpu_id, plat->cpu_id);
  167. current += length;
  168. total_length += length;
  169. }
  170. return total_length;
  171. }
  172. int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
  173. u32 addr, u32 gsi_base)
  174. {
  175. ioapic->type = ACPI_APIC_IOAPIC;
  176. ioapic->length = sizeof(struct acpi_madt_ioapic);
  177. ioapic->reserved = 0x00;
  178. ioapic->gsi_base = gsi_base;
  179. ioapic->ioapic_id = id;
  180. ioapic->ioapic_addr = addr;
  181. return ioapic->length;
  182. }
  183. int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
  184. u8 bus, u8 source, u32 gsirq, u16 flags)
  185. {
  186. irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
  187. irqoverride->length = sizeof(struct acpi_madt_irqoverride);
  188. irqoverride->bus = bus;
  189. irqoverride->source = source;
  190. irqoverride->gsirq = gsirq;
  191. irqoverride->flags = flags;
  192. return irqoverride->length;
  193. }
  194. int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
  195. u8 cpu, u16 flags, u8 lint)
  196. {
  197. lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
  198. lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
  199. lapic_nmi->flags = flags;
  200. lapic_nmi->processor_id = cpu;
  201. lapic_nmi->lint = lint;
  202. return lapic_nmi->length;
  203. }
  204. static int acpi_create_madt_irq_overrides(u32 current)
  205. {
  206. struct acpi_madt_irqoverride *irqovr;
  207. u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH;
  208. int length = 0;
  209. irqovr = (void *)current;
  210. length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0);
  211. irqovr = (void *)(current + length);
  212. length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags);
  213. return length;
  214. }
  215. __weak u32 acpi_fill_madt(u32 current)
  216. {
  217. current += acpi_create_madt_lapics(current);
  218. current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current,
  219. io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0);
  220. current += acpi_create_madt_irq_overrides(current);
  221. return current;
  222. }
  223. static void acpi_create_madt(struct acpi_madt *madt)
  224. {
  225. struct acpi_table_header *header = &(madt->header);
  226. u32 current = (u32)madt + sizeof(struct acpi_madt);
  227. memset((void *)madt, 0, sizeof(struct acpi_madt));
  228. /* Fill out header fields */
  229. acpi_fill_header(header, "APIC");
  230. header->length = sizeof(struct acpi_madt);
  231. header->revision = 4;
  232. madt->lapic_addr = LAPIC_DEFAULT_BASE;
  233. madt->flags = ACPI_MADT_PCAT_COMPAT;
  234. current = acpi_fill_madt(current);
  235. /* (Re)calculate length and checksum */
  236. header->length = current - (u32)madt;
  237. header->checksum = table_compute_checksum((void *)madt, header->length);
  238. }
  239. int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, u32 base,
  240. u16 seg_nr, u8 start, u8 end)
  241. {
  242. memset(mmconfig, 0, sizeof(*mmconfig));
  243. mmconfig->base_address_l = base;
  244. mmconfig->base_address_h = 0;
  245. mmconfig->pci_segment_group_number = seg_nr;
  246. mmconfig->start_bus_number = start;
  247. mmconfig->end_bus_number = end;
  248. return sizeof(struct acpi_mcfg_mmconfig);
  249. }
  250. __weak u32 acpi_fill_mcfg(u32 current)
  251. {
  252. current += acpi_create_mcfg_mmconfig
  253. ((struct acpi_mcfg_mmconfig *)current,
  254. CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
  255. return current;
  256. }
  257. /* MCFG is defined in the PCI Firmware Specification 3.0 */
  258. static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
  259. {
  260. struct acpi_table_header *header = &(mcfg->header);
  261. u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
  262. memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
  263. /* Fill out header fields */
  264. acpi_fill_header(header, "MCFG");
  265. header->length = sizeof(struct acpi_mcfg);
  266. header->revision = 1;
  267. current = acpi_fill_mcfg(current);
  268. /* (Re)calculate length and checksum */
  269. header->length = current - (u32)mcfg;
  270. header->checksum = table_compute_checksum((void *)mcfg, header->length);
  271. }
  272. /*
  273. * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
  274. */
  275. ulong write_acpi_tables(ulong start)
  276. {
  277. u32 current;
  278. struct acpi_rsdp *rsdp;
  279. struct acpi_rsdt *rsdt;
  280. struct acpi_xsdt *xsdt;
  281. struct acpi_facs *facs;
  282. struct acpi_table_header *dsdt;
  283. struct acpi_fadt *fadt;
  284. struct acpi_mcfg *mcfg;
  285. struct acpi_madt *madt;
  286. int i;
  287. current = start;
  288. /* Align ACPI tables to 16 byte */
  289. current = ALIGN(current, 16);
  290. debug("ACPI: Writing ACPI tables at %lx\n", start);
  291. /* We need at least an RSDP and an RSDT Table */
  292. rsdp = (struct acpi_rsdp *)current;
  293. current += sizeof(struct acpi_rsdp);
  294. current = ALIGN(current, 16);
  295. rsdt = (struct acpi_rsdt *)current;
  296. current += sizeof(struct acpi_rsdt);
  297. current = ALIGN(current, 16);
  298. xsdt = (struct acpi_xsdt *)current;
  299. current += sizeof(struct acpi_xsdt);
  300. /*
  301. * Per ACPI spec, the FACS table address must be aligned to a 64 byte
  302. * boundary (Windows checks this, but Linux does not).
  303. */
  304. current = ALIGN(current, 64);
  305. /* clear all table memory */
  306. memset((void *)start, 0, current - start);
  307. acpi_write_rsdp(rsdp, rsdt, xsdt);
  308. acpi_write_rsdt(rsdt);
  309. acpi_write_xsdt(xsdt);
  310. debug("ACPI: * FACS\n");
  311. facs = (struct acpi_facs *)current;
  312. current += sizeof(struct acpi_facs);
  313. current = ALIGN(current, 16);
  314. acpi_create_facs(facs);
  315. debug("ACPI: * DSDT\n");
  316. dsdt = (struct acpi_table_header *)current;
  317. memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
  318. current += sizeof(struct acpi_table_header);
  319. memcpy((char *)current,
  320. (char *)&AmlCode + sizeof(struct acpi_table_header),
  321. dsdt->length - sizeof(struct acpi_table_header));
  322. current += dsdt->length - sizeof(struct acpi_table_header);
  323. current = ALIGN(current, 16);
  324. /* Pack GNVS into the ACPI table area */
  325. for (i = 0; i < dsdt->length; i++) {
  326. u32 *gnvs = (u32 *)((u32)dsdt + i);
  327. if (*gnvs == ACPI_GNVS_ADDR) {
  328. debug("Fix up global NVS in DSDT to 0x%08x\n", current);
  329. *gnvs = current;
  330. break;
  331. }
  332. }
  333. /* Update DSDT checksum since we patched the GNVS address */
  334. dsdt->checksum = 0;
  335. dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
  336. /* Fill in platform-specific global NVS variables */
  337. acpi_create_gnvs((struct acpi_global_nvs *)current);
  338. current += sizeof(struct acpi_global_nvs);
  339. current = ALIGN(current, 16);
  340. debug("ACPI: * FADT\n");
  341. fadt = (struct acpi_fadt *)current;
  342. current += sizeof(struct acpi_fadt);
  343. current = ALIGN(current, 16);
  344. acpi_create_fadt(fadt, facs, dsdt);
  345. acpi_add_table(rsdp, fadt);
  346. debug("ACPI: * MADT\n");
  347. madt = (struct acpi_madt *)current;
  348. acpi_create_madt(madt);
  349. current += madt->header.length;
  350. acpi_add_table(rsdp, madt);
  351. current = ALIGN(current, 16);
  352. debug("ACPI: * MCFG\n");
  353. mcfg = (struct acpi_mcfg *)current;
  354. acpi_create_mcfg(mcfg);
  355. current += mcfg->header.length;
  356. acpi_add_table(rsdp, mcfg);
  357. current = ALIGN(current, 16);
  358. debug("current = %x\n", current);
  359. acpi_rsdp_addr = (unsigned long)rsdp;
  360. debug("ACPI: done\n");
  361. return current;
  362. }
  363. ulong acpi_get_rsdp_addr(void)
  364. {
  365. return acpi_rsdp_addr;
  366. }