acpi_table.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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(acpi_header_t) + (sizeof(u32) * (i + 1));
  52. /* Re-calculate checksum */
  53. rsdt->header.checksum = 0;
  54. rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
  55. rsdt->header.length);
  56. /*
  57. * And now the same thing for the XSDT. We use the same index as for
  58. * now we want the XSDT and RSDT to always be in sync in U-Boot
  59. */
  60. if (xsdt) {
  61. /* Add table to the XSDT */
  62. xsdt->entry[i] = (u64)(u32)table;
  63. /* Fix XSDT length */
  64. xsdt->header.length = sizeof(acpi_header_t) +
  65. (sizeof(u64) * (i + 1));
  66. /* Re-calculate checksum */
  67. xsdt->header.checksum = 0;
  68. xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
  69. xsdt->header.length);
  70. }
  71. }
  72. static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
  73. u8 cpu, u8 apic)
  74. {
  75. lapic->type = LOCALAPIC; /* Local APIC structure */
  76. lapic->length = sizeof(struct acpi_madt_lapic);
  77. lapic->flags = LOCAL_APIC_FLAG_ENABLED; /* Processor/LAPIC enabled */
  78. lapic->processor_id = cpu;
  79. lapic->apic_id = apic;
  80. return lapic->length;
  81. }
  82. unsigned long acpi_create_madt_lapics(unsigned long current)
  83. {
  84. struct udevice *dev;
  85. for (uclass_find_first_device(UCLASS_CPU, &dev);
  86. dev;
  87. uclass_find_next_device(&dev)) {
  88. struct cpu_platdata *plat = dev_get_parent_platdata(dev);
  89. current += acpi_create_madt_lapic(
  90. (struct acpi_madt_lapic *)current,
  91. plat->cpu_id, plat->cpu_id);
  92. }
  93. return current;
  94. }
  95. int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id, u32 addr,
  96. u32 gsi_base)
  97. {
  98. ioapic->type = IOAPIC;
  99. ioapic->length = sizeof(struct acpi_madt_ioapic);
  100. ioapic->reserved = 0x00;
  101. ioapic->gsi_base = gsi_base;
  102. ioapic->ioapic_id = id;
  103. ioapic->ioapic_addr = addr;
  104. return ioapic->length;
  105. }
  106. int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
  107. u8 bus, u8 source, u32 gsirq, u16 flags)
  108. {
  109. irqoverride->type = IRQSOURCEOVERRIDE;
  110. irqoverride->length = sizeof(struct acpi_madt_irqoverride);
  111. irqoverride->bus = bus;
  112. irqoverride->source = source;
  113. irqoverride->gsirq = gsirq;
  114. irqoverride->flags = flags;
  115. return irqoverride->length;
  116. }
  117. int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
  118. u8 cpu, u16 flags, u8 lint)
  119. {
  120. lapic_nmi->type = LOCALNMITYPE;
  121. lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
  122. lapic_nmi->flags = flags;
  123. lapic_nmi->processor_id = cpu;
  124. lapic_nmi->lint = lint;
  125. return lapic_nmi->length;
  126. }
  127. static void fill_header(acpi_header_t *header, char *signature, int length)
  128. {
  129. memcpy(header->signature, signature, length);
  130. memcpy(header->oem_id, OEM_ID, 6);
  131. memcpy(header->oem_table_id, ACPI_TABLE_CREATOR, 8);
  132. memcpy(header->asl_compiler_id, ASLC, 4);
  133. }
  134. static void acpi_create_madt(struct acpi_madt *madt)
  135. {
  136. acpi_header_t *header = &(madt->header);
  137. unsigned long current = (unsigned long)madt + sizeof(struct acpi_madt);
  138. memset((void *)madt, 0, sizeof(struct acpi_madt));
  139. /* Fill out header fields */
  140. fill_header(header, "APIC", 4);
  141. header->length = sizeof(struct acpi_madt);
  142. /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
  143. header->revision = ACPI_REV_ACPI_2_0;
  144. madt->lapic_addr = LAPIC_DEFAULT_BASE;
  145. madt->flags = PCAT_COMPAT;
  146. current = acpi_fill_madt(current);
  147. /* (Re)calculate length and checksum */
  148. header->length = current - (unsigned long)madt;
  149. header->checksum = table_compute_checksum((void *)madt, header->length);
  150. }
  151. static int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig,
  152. u32 base, u16 seg_nr, u8 start, u8 end)
  153. {
  154. memset(mmconfig, 0, sizeof(*mmconfig));
  155. mmconfig->base_address = base;
  156. mmconfig->base_reserved = 0;
  157. mmconfig->pci_segment_group_number = seg_nr;
  158. mmconfig->start_bus_number = start;
  159. mmconfig->end_bus_number = end;
  160. return sizeof(struct acpi_mcfg_mmconfig);
  161. }
  162. static unsigned long acpi_fill_mcfg(unsigned long current)
  163. {
  164. current += acpi_create_mcfg_mmconfig
  165. ((struct acpi_mcfg_mmconfig *)current,
  166. CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
  167. return current;
  168. }
  169. /* MCFG is defined in the PCI Firmware Specification 3.0 */
  170. static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
  171. {
  172. acpi_header_t *header = &(mcfg->header);
  173. unsigned long current = (unsigned long)mcfg + sizeof(struct acpi_mcfg);
  174. memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
  175. /* Fill out header fields */
  176. fill_header(header, "MCFG", 4);
  177. header->length = sizeof(struct acpi_mcfg);
  178. /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
  179. header->revision = ACPI_REV_ACPI_2_0;
  180. current = acpi_fill_mcfg(current);
  181. /* (Re)calculate length and checksum */
  182. header->length = current - (unsigned long)mcfg;
  183. header->checksum = table_compute_checksum((void *)mcfg, header->length);
  184. }
  185. static void acpi_create_facs(struct acpi_facs *facs)
  186. {
  187. memset((void *)facs, 0, sizeof(struct acpi_facs));
  188. memcpy(facs->signature, "FACS", 4);
  189. facs->length = sizeof(struct acpi_facs);
  190. facs->hardware_signature = 0;
  191. facs->firmware_waking_vector = 0;
  192. facs->global_lock = 0;
  193. facs->flags = 0;
  194. facs->x_firmware_waking_vector_l = 0;
  195. facs->x_firmware_waking_vector_h = 0;
  196. facs->version = 1; /* ACPI 1.0: 0, ACPI 2.0/3.0: 1, ACPI 4.0: 2 */
  197. }
  198. static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
  199. {
  200. acpi_header_t *header = &(rsdt->header);
  201. /* Fill out header fields */
  202. fill_header(header, "RSDT", 4);
  203. header->length = sizeof(struct acpi_rsdt);
  204. /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
  205. header->revision = ACPI_REV_ACPI_2_0;
  206. /* Entries are filled in later, we come with an empty set */
  207. /* Fix checksum */
  208. header->checksum = table_compute_checksum((void *)rsdt,
  209. sizeof(struct acpi_rsdt));
  210. }
  211. static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
  212. {
  213. acpi_header_t *header = &(xsdt->header);
  214. /* Fill out header fields */
  215. fill_header(header, "XSDT", 4);
  216. header->length = sizeof(struct acpi_xsdt);
  217. /* ACPI 1.0/2.0: 1, ACPI 3.0: 2, ACPI 4.0: 3 */
  218. header->revision = ACPI_REV_ACPI_2_0;
  219. /* Entries are filled in later, we come with an empty set */
  220. /* Fix checksum */
  221. header->checksum = table_compute_checksum((void *)xsdt,
  222. sizeof(struct acpi_xsdt));
  223. }
  224. static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
  225. struct acpi_xsdt *xsdt)
  226. {
  227. memset(rsdp, 0, sizeof(struct acpi_rsdp));
  228. memcpy(rsdp->signature, RSDP_SIG, 8);
  229. memcpy(rsdp->oem_id, OEM_ID, 6);
  230. rsdp->length = sizeof(struct acpi_rsdp);
  231. rsdp->rsdt_address = (u32)rsdt;
  232. /*
  233. * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
  234. *
  235. * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
  236. * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
  237. * revision 0)
  238. */
  239. if (xsdt == NULL) {
  240. rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
  241. } else {
  242. rsdp->xsdt_address = (u64)(u32)xsdt;
  243. rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
  244. }
  245. /* Calculate checksums */
  246. rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
  247. rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
  248. sizeof(struct acpi_rsdp));
  249. }
  250. static void acpi_create_ssdt_generator(acpi_header_t *ssdt,
  251. const char *oem_table_id)
  252. {
  253. unsigned long current = (unsigned long)ssdt + sizeof(acpi_header_t);
  254. memset((void *)ssdt, 0, sizeof(acpi_header_t));
  255. memcpy(&ssdt->signature, "SSDT", 4);
  256. /* Access size in ACPI 2.0c/3.0/4.0/5.0 */
  257. ssdt->revision = ACPI_REV_ACPI_3_0;
  258. memcpy(&ssdt->oem_id, OEM_ID, 6);
  259. memcpy(&ssdt->oem_table_id, oem_table_id, 8);
  260. ssdt->oem_revision = OEM_REVISION;
  261. memcpy(&ssdt->asl_compiler_id, ASLC, 4);
  262. ssdt->asl_compiler_revision = ASL_COMPILER_REVISION;
  263. ssdt->length = sizeof(acpi_header_t);
  264. /* (Re)calculate length and checksum */
  265. ssdt->length = current - (unsigned long)ssdt;
  266. ssdt->checksum = table_compute_checksum((void *)ssdt, ssdt->length);
  267. }
  268. /*
  269. * QEMU's version of write_acpi_tables is defined in
  270. * arch/x86/cpu/qemu/fw_cfg.c
  271. */
  272. u32 write_acpi_tables(u32 start)
  273. {
  274. u32 current;
  275. struct acpi_rsdp *rsdp;
  276. struct acpi_rsdt *rsdt;
  277. struct acpi_xsdt *xsdt;
  278. struct acpi_facs *facs;
  279. acpi_header_t *dsdt;
  280. struct acpi_fadt *fadt;
  281. struct acpi_mcfg *mcfg;
  282. struct acpi_madt *madt;
  283. acpi_header_t *ssdt;
  284. current = start;
  285. /* Align ACPI tables to 16byte */
  286. current = ALIGN(current, 16);
  287. debug("ACPI: Writing ACPI tables at %x\n", start);
  288. /* We need at least an RSDP and an RSDT Table */
  289. rsdp = (struct acpi_rsdp *)current;
  290. current += sizeof(struct acpi_rsdp);
  291. current = ALIGN(current, 16);
  292. rsdt = (struct acpi_rsdt *)current;
  293. current += sizeof(struct acpi_rsdt);
  294. current = ALIGN(current, 16);
  295. xsdt = (struct acpi_xsdt *)current;
  296. current += sizeof(struct acpi_xsdt);
  297. current = ALIGN(current, 16);
  298. /* clear all table memory */
  299. memset((void *)start, 0, current - start);
  300. acpi_write_rsdp(rsdp, rsdt, xsdt);
  301. acpi_write_rsdt(rsdt);
  302. acpi_write_xsdt(xsdt);
  303. debug("ACPI: * FACS\n");
  304. facs = (struct acpi_facs *)current;
  305. current += sizeof(struct acpi_facs);
  306. current = ALIGN(current, 16);
  307. acpi_create_facs(facs);
  308. debug("ACPI: * DSDT\n");
  309. dsdt = (acpi_header_t *)current;
  310. memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
  311. if (dsdt->length >= sizeof(acpi_header_t)) {
  312. current += sizeof(acpi_header_t);
  313. memcpy((char *)current,
  314. (char *)&AmlCode + sizeof(acpi_header_t),
  315. dsdt->length - sizeof(acpi_header_t));
  316. current += dsdt->length - sizeof(acpi_header_t);
  317. /* (Re)calculate length and checksum */
  318. dsdt->length = current - (unsigned long)dsdt;
  319. dsdt->checksum = 0;
  320. dsdt->checksum = table_compute_checksum((void *)dsdt,
  321. dsdt->length);
  322. }
  323. current = ALIGN(current, 16);
  324. debug("ACPI: * FADT\n");
  325. fadt = (struct acpi_fadt *)current;
  326. current += sizeof(struct acpi_fadt);
  327. current = ALIGN(current, 16);
  328. acpi_create_fadt(fadt, facs, dsdt);
  329. acpi_add_table(rsdp, fadt);
  330. debug("ACPI: * MCFG\n");
  331. mcfg = (struct acpi_mcfg *)current;
  332. acpi_create_mcfg(mcfg);
  333. if (mcfg->header.length > sizeof(struct acpi_mcfg)) {
  334. current += mcfg->header.length;
  335. current = ALIGN(current, 16);
  336. acpi_add_table(rsdp, mcfg);
  337. }
  338. debug("ACPI: * MADT\n");
  339. madt = (struct acpi_madt *)current;
  340. acpi_create_madt(madt);
  341. if (madt->header.length > sizeof(struct acpi_madt)) {
  342. current += madt->header.length;
  343. acpi_add_table(rsdp, madt);
  344. }
  345. current = ALIGN(current, 16);
  346. debug("ACPI: * SSDT\n");
  347. ssdt = (acpi_header_t *)current;
  348. acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
  349. if (ssdt->length > sizeof(acpi_header_t)) {
  350. current += ssdt->length;
  351. acpi_add_table(rsdp, ssdt);
  352. current = ALIGN(current, 16);
  353. }
  354. debug("current = %x\n", current);
  355. debug("ACPI: done\n");
  356. return current;
  357. }