acpi_table.c 10 KB

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