acpi_table.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. header->revision = 1;
  61. /* Entries are filled in later, we come with an empty set */
  62. /* Fix checksum */
  63. header->checksum = table_compute_checksum((void *)rsdt,
  64. sizeof(struct acpi_rsdt));
  65. }
  66. static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
  67. {
  68. struct acpi_table_header *header = &(xsdt->header);
  69. /* Fill out header fields */
  70. acpi_fill_header(header, "XSDT");
  71. header->length = sizeof(struct acpi_xsdt);
  72. header->revision = 1;
  73. /* Entries are filled in later, we come with an empty set */
  74. /* Fix checksum */
  75. header->checksum = table_compute_checksum((void *)xsdt,
  76. sizeof(struct acpi_xsdt));
  77. }
  78. /**
  79. * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
  80. * and checksum.
  81. */
  82. static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
  83. {
  84. int i, entries_num;
  85. struct acpi_rsdt *rsdt;
  86. struct acpi_xsdt *xsdt = NULL;
  87. /* The RSDT is mandatory while the XSDT is not */
  88. rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
  89. if (rsdp->xsdt_address)
  90. xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
  91. /* This should always be MAX_ACPI_TABLES */
  92. entries_num = ARRAY_SIZE(rsdt->entry);
  93. for (i = 0; i < entries_num; i++) {
  94. if (rsdt->entry[i] == 0)
  95. break;
  96. }
  97. if (i >= entries_num) {
  98. debug("ACPI: Error: too many tables\n");
  99. return;
  100. }
  101. /* Add table to the RSDT */
  102. rsdt->entry[i] = (u32)table;
  103. /* Fix RSDT length or the kernel will assume invalid entries */
  104. rsdt->header.length = sizeof(struct acpi_table_header) +
  105. (sizeof(u32) * (i + 1));
  106. /* Re-calculate checksum */
  107. rsdt->header.checksum = 0;
  108. rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
  109. rsdt->header.length);
  110. /*
  111. * And now the same thing for the XSDT. We use the same index as for
  112. * now we want the XSDT and RSDT to always be in sync in U-Boot
  113. */
  114. if (xsdt) {
  115. /* Add table to the XSDT */
  116. xsdt->entry[i] = (u64)(u32)table;
  117. /* Fix XSDT length */
  118. xsdt->header.length = sizeof(struct acpi_table_header) +
  119. (sizeof(u64) * (i + 1));
  120. /* Re-calculate checksum */
  121. xsdt->header.checksum = 0;
  122. xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
  123. xsdt->header.length);
  124. }
  125. }
  126. static void acpi_create_facs(struct acpi_facs *facs)
  127. {
  128. memset((void *)facs, 0, sizeof(struct acpi_facs));
  129. memcpy(facs->signature, "FACS", 4);
  130. facs->length = sizeof(struct acpi_facs);
  131. facs->hardware_signature = 0;
  132. facs->firmware_waking_vector = 0;
  133. facs->global_lock = 0;
  134. facs->flags = 0;
  135. facs->x_firmware_waking_vector_l = 0;
  136. facs->x_firmware_waking_vector_h = 0;
  137. facs->version = 1;
  138. }
  139. static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
  140. u8 cpu, u8 apic)
  141. {
  142. lapic->type = ACPI_APIC_LAPIC;
  143. lapic->length = sizeof(struct acpi_madt_lapic);
  144. lapic->flags = LOCAL_APIC_FLAG_ENABLED;
  145. lapic->processor_id = cpu;
  146. lapic->apic_id = apic;
  147. return lapic->length;
  148. }
  149. int acpi_create_madt_lapics(u32 current)
  150. {
  151. struct udevice *dev;
  152. int length = 0;
  153. for (uclass_find_first_device(UCLASS_CPU, &dev);
  154. dev;
  155. uclass_find_next_device(&dev)) {
  156. struct cpu_platdata *plat = dev_get_parent_platdata(dev);
  157. length += acpi_create_madt_lapic(
  158. (struct acpi_madt_lapic *)current,
  159. plat->cpu_id, plat->cpu_id);
  160. current += length;
  161. }
  162. return length;
  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. header->revision = 4;
  205. madt->lapic_addr = LAPIC_DEFAULT_BASE;
  206. madt->flags = ACPI_MADT_PCAT_COMPAT;
  207. current = acpi_fill_madt(current);
  208. /* (Re)calculate length and checksum */
  209. header->length = current - (u32)madt;
  210. header->checksum = table_compute_checksum((void *)madt, header->length);
  211. }
  212. static int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig,
  213. u32 base, u16 seg_nr, u8 start, u8 end)
  214. {
  215. memset(mmconfig, 0, sizeof(*mmconfig));
  216. mmconfig->base_address_l = base;
  217. mmconfig->base_address_h = 0;
  218. mmconfig->pci_segment_group_number = seg_nr;
  219. mmconfig->start_bus_number = start;
  220. mmconfig->end_bus_number = end;
  221. return sizeof(struct acpi_mcfg_mmconfig);
  222. }
  223. static u32 acpi_fill_mcfg(u32 current)
  224. {
  225. current += acpi_create_mcfg_mmconfig
  226. ((struct acpi_mcfg_mmconfig *)current,
  227. CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
  228. return current;
  229. }
  230. /* MCFG is defined in the PCI Firmware Specification 3.0 */
  231. static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
  232. {
  233. struct acpi_table_header *header = &(mcfg->header);
  234. u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
  235. memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
  236. /* Fill out header fields */
  237. acpi_fill_header(header, "MCFG");
  238. header->length = sizeof(struct acpi_mcfg);
  239. header->revision = 1;
  240. current = acpi_fill_mcfg(current);
  241. /* (Re)calculate length and checksum */
  242. header->length = current - (u32)mcfg;
  243. header->checksum = table_compute_checksum((void *)mcfg, header->length);
  244. }
  245. /*
  246. * QEMU's version of write_acpi_tables is defined in
  247. * arch/x86/cpu/qemu/acpi_table.c
  248. */
  249. u32 write_acpi_tables(u32 start)
  250. {
  251. u32 current;
  252. struct acpi_rsdp *rsdp;
  253. struct acpi_rsdt *rsdt;
  254. struct acpi_xsdt *xsdt;
  255. struct acpi_facs *facs;
  256. struct acpi_table_header *dsdt;
  257. struct acpi_fadt *fadt;
  258. struct acpi_mcfg *mcfg;
  259. struct acpi_madt *madt;
  260. current = start;
  261. /* Align ACPI tables to 16 byte */
  262. current = ALIGN(current, 16);
  263. debug("ACPI: Writing ACPI tables at %x\n", start);
  264. /* We need at least an RSDP and an RSDT Table */
  265. rsdp = (struct acpi_rsdp *)current;
  266. current += sizeof(struct acpi_rsdp);
  267. current = ALIGN(current, 16);
  268. rsdt = (struct acpi_rsdt *)current;
  269. current += sizeof(struct acpi_rsdt);
  270. current = ALIGN(current, 16);
  271. xsdt = (struct acpi_xsdt *)current;
  272. current += sizeof(struct acpi_xsdt);
  273. /*
  274. * Per ACPI spec, the FACS table address must be aligned to a 64 byte
  275. * boundary (Windows checks this, but Linux does not).
  276. */
  277. current = ALIGN(current, 64);
  278. /* clear all table memory */
  279. memset((void *)start, 0, current - start);
  280. acpi_write_rsdp(rsdp, rsdt, xsdt);
  281. acpi_write_rsdt(rsdt);
  282. acpi_write_xsdt(xsdt);
  283. debug("ACPI: * FACS\n");
  284. facs = (struct acpi_facs *)current;
  285. current += sizeof(struct acpi_facs);
  286. current = ALIGN(current, 16);
  287. acpi_create_facs(facs);
  288. debug("ACPI: * DSDT\n");
  289. dsdt = (struct acpi_table_header *)current;
  290. memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
  291. if (dsdt->length >= sizeof(struct acpi_table_header)) {
  292. current += sizeof(struct acpi_table_header);
  293. memcpy((char *)current,
  294. (char *)&AmlCode + sizeof(struct acpi_table_header),
  295. dsdt->length - sizeof(struct acpi_table_header));
  296. current += dsdt->length - sizeof(struct acpi_table_header);
  297. /* (Re)calculate length and checksum */
  298. dsdt->length = current - (u32)dsdt;
  299. dsdt->checksum = 0;
  300. dsdt->checksum = table_compute_checksum((void *)dsdt,
  301. dsdt->length);
  302. }
  303. current = ALIGN(current, 16);
  304. debug("ACPI: * FADT\n");
  305. fadt = (struct acpi_fadt *)current;
  306. current += sizeof(struct acpi_fadt);
  307. current = ALIGN(current, 16);
  308. acpi_create_fadt(fadt, facs, dsdt);
  309. acpi_add_table(rsdp, fadt);
  310. debug("ACPI: * MADT\n");
  311. madt = (struct acpi_madt *)current;
  312. acpi_create_madt(madt);
  313. if (madt->header.length > sizeof(struct acpi_madt)) {
  314. current += madt->header.length;
  315. acpi_add_table(rsdp, madt);
  316. }
  317. current = ALIGN(current, 16);
  318. debug("ACPI: * MCFG\n");
  319. mcfg = (struct acpi_mcfg *)current;
  320. acpi_create_mcfg(mcfg);
  321. if (mcfg->header.length > sizeof(struct acpi_mcfg)) {
  322. current += mcfg->header.length;
  323. current = ALIGN(current, 16);
  324. acpi_add_table(rsdp, mcfg);
  325. }
  326. debug("current = %x\n", current);
  327. debug("ACPI: done\n");
  328. return current;
  329. }