acpi_table.c 12 KB

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