acpi_table.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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/io.h>
  15. #include <asm/lapic.h>
  16. #include <asm/tables.h>
  17. /*
  18. * IASL compiles the dsdt entries and writes the hex values
  19. * to a C array AmlCode[] (see dsdt.c).
  20. */
  21. extern const unsigned char AmlCode[];
  22. static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
  23. struct acpi_xsdt *xsdt)
  24. {
  25. memset(rsdp, 0, sizeof(struct acpi_rsdp));
  26. memcpy(rsdp->signature, RSDP_SIG, 8);
  27. memcpy(rsdp->oem_id, OEM_ID, 6);
  28. rsdp->length = sizeof(struct acpi_rsdp);
  29. rsdp->rsdt_address = (u32)rsdt;
  30. /*
  31. * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
  32. *
  33. * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
  34. * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
  35. * revision 0)
  36. */
  37. if (xsdt == NULL) {
  38. rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
  39. } else {
  40. rsdp->xsdt_address = (u64)(u32)xsdt;
  41. rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
  42. }
  43. /* Calculate checksums */
  44. rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
  45. rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
  46. sizeof(struct acpi_rsdp));
  47. }
  48. void acpi_fill_header(struct acpi_table_header *header, char *signature)
  49. {
  50. memcpy(header->signature, signature, 4);
  51. memcpy(header->oem_id, OEM_ID, 6);
  52. memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
  53. memcpy(header->aslc_id, ASLC_ID, 4);
  54. }
  55. static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
  56. {
  57. struct acpi_table_header *header = &(rsdt->header);
  58. /* Fill out header fields */
  59. acpi_fill_header(header, "RSDT");
  60. header->length = sizeof(struct acpi_rsdt);
  61. header->revision = 1;
  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. header->revision = 1;
  74. /* Entries are filled in later, we come with an empty set */
  75. /* Fix checksum */
  76. header->checksum = table_compute_checksum((void *)xsdt,
  77. sizeof(struct acpi_xsdt));
  78. }
  79. /**
  80. * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
  81. * and checksum.
  82. */
  83. static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
  84. {
  85. int i, entries_num;
  86. struct acpi_rsdt *rsdt;
  87. struct acpi_xsdt *xsdt = NULL;
  88. /* The RSDT is mandatory while the XSDT is not */
  89. rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
  90. if (rsdp->xsdt_address)
  91. xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
  92. /* This should always be MAX_ACPI_TABLES */
  93. entries_num = ARRAY_SIZE(rsdt->entry);
  94. for (i = 0; i < entries_num; i++) {
  95. if (rsdt->entry[i] == 0)
  96. break;
  97. }
  98. if (i >= entries_num) {
  99. debug("ACPI: Error: too many tables\n");
  100. return;
  101. }
  102. /* Add table to the RSDT */
  103. rsdt->entry[i] = (u32)table;
  104. /* Fix RSDT length or the kernel will assume invalid entries */
  105. rsdt->header.length = sizeof(struct acpi_table_header) +
  106. (sizeof(u32) * (i + 1));
  107. /* Re-calculate checksum */
  108. rsdt->header.checksum = 0;
  109. rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
  110. rsdt->header.length);
  111. /*
  112. * And now the same thing for the XSDT. We use the same index as for
  113. * now we want the XSDT and RSDT to always be in sync in U-Boot
  114. */
  115. if (xsdt) {
  116. /* Add table to the XSDT */
  117. xsdt->entry[i] = (u64)(u32)table;
  118. /* Fix XSDT length */
  119. xsdt->header.length = sizeof(struct acpi_table_header) +
  120. (sizeof(u64) * (i + 1));
  121. /* Re-calculate checksum */
  122. xsdt->header.checksum = 0;
  123. xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
  124. xsdt->header.length);
  125. }
  126. }
  127. static void acpi_create_facs(struct acpi_facs *facs)
  128. {
  129. memset((void *)facs, 0, sizeof(struct acpi_facs));
  130. memcpy(facs->signature, "FACS", 4);
  131. facs->length = sizeof(struct acpi_facs);
  132. facs->hardware_signature = 0;
  133. facs->firmware_waking_vector = 0;
  134. facs->global_lock = 0;
  135. facs->flags = 0;
  136. facs->x_firmware_waking_vector_l = 0;
  137. facs->x_firmware_waking_vector_h = 0;
  138. facs->version = 1;
  139. }
  140. static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
  141. u8 cpu, u8 apic)
  142. {
  143. lapic->type = ACPI_APIC_LAPIC;
  144. lapic->length = sizeof(struct acpi_madt_lapic);
  145. lapic->flags = LOCAL_APIC_FLAG_ENABLED;
  146. lapic->processor_id = cpu;
  147. lapic->apic_id = apic;
  148. return lapic->length;
  149. }
  150. int acpi_create_madt_lapics(u32 current)
  151. {
  152. struct udevice *dev;
  153. int total_length = 0;
  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. int length = acpi_create_madt_lapic(
  159. (struct acpi_madt_lapic *)current,
  160. plat->cpu_id, plat->cpu_id);
  161. current += length;
  162. total_length += length;
  163. }
  164. return total_length;
  165. }
  166. int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
  167. u32 addr, u32 gsi_base)
  168. {
  169. ioapic->type = ACPI_APIC_IOAPIC;
  170. ioapic->length = sizeof(struct acpi_madt_ioapic);
  171. ioapic->reserved = 0x00;
  172. ioapic->gsi_base = gsi_base;
  173. ioapic->ioapic_id = id;
  174. ioapic->ioapic_addr = addr;
  175. return ioapic->length;
  176. }
  177. int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
  178. u8 bus, u8 source, u32 gsirq, u16 flags)
  179. {
  180. irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
  181. irqoverride->length = sizeof(struct acpi_madt_irqoverride);
  182. irqoverride->bus = bus;
  183. irqoverride->source = source;
  184. irqoverride->gsirq = gsirq;
  185. irqoverride->flags = flags;
  186. return irqoverride->length;
  187. }
  188. int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
  189. u8 cpu, u16 flags, u8 lint)
  190. {
  191. lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
  192. lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
  193. lapic_nmi->flags = flags;
  194. lapic_nmi->processor_id = cpu;
  195. lapic_nmi->lint = lint;
  196. return lapic_nmi->length;
  197. }
  198. static void acpi_create_madt(struct acpi_madt *madt)
  199. {
  200. struct acpi_table_header *header = &(madt->header);
  201. u32 current = (u32)madt + sizeof(struct acpi_madt);
  202. memset((void *)madt, 0, sizeof(struct acpi_madt));
  203. /* Fill out header fields */
  204. acpi_fill_header(header, "APIC");
  205. header->length = sizeof(struct acpi_madt);
  206. header->revision = 4;
  207. madt->lapic_addr = LAPIC_DEFAULT_BASE;
  208. madt->flags = ACPI_MADT_PCAT_COMPAT;
  209. current = acpi_fill_madt(current);
  210. /* (Re)calculate length and checksum */
  211. header->length = current - (u32)madt;
  212. header->checksum = table_compute_checksum((void *)madt, header->length);
  213. }
  214. static int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig,
  215. u32 base, u16 seg_nr, u8 start, u8 end)
  216. {
  217. memset(mmconfig, 0, sizeof(*mmconfig));
  218. mmconfig->base_address_l = base;
  219. mmconfig->base_address_h = 0;
  220. mmconfig->pci_segment_group_number = seg_nr;
  221. mmconfig->start_bus_number = start;
  222. mmconfig->end_bus_number = end;
  223. return sizeof(struct acpi_mcfg_mmconfig);
  224. }
  225. static u32 acpi_fill_mcfg(u32 current)
  226. {
  227. current += acpi_create_mcfg_mmconfig
  228. ((struct acpi_mcfg_mmconfig *)current,
  229. CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
  230. return current;
  231. }
  232. /* MCFG is defined in the PCI Firmware Specification 3.0 */
  233. static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
  234. {
  235. struct acpi_table_header *header = &(mcfg->header);
  236. u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
  237. memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
  238. /* Fill out header fields */
  239. acpi_fill_header(header, "MCFG");
  240. header->length = sizeof(struct acpi_mcfg);
  241. header->revision = 1;
  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. static void enter_acpi_mode(int pm1_cnt)
  248. {
  249. /*
  250. * PM1_CNT register bit0 selects the power management event to be
  251. * either an SCI or SMI interrupt. When this bit is set, then power
  252. * management events will generate an SCI interrupt. When this bit
  253. * is reset power management events will generate an SMI interrupt.
  254. *
  255. * Per ACPI spec, it is the responsibility of the hardware to set
  256. * or reset this bit. OSPM always preserves this bit position.
  257. *
  258. * U-Boot does not support SMI. And we don't have plan to support
  259. * anything running in SMM within U-Boot. To create a legacy-free
  260. * system, and expose ourselves to OSPM as working under ACPI mode
  261. * already, turn this bit on.
  262. */
  263. outw(PM1_CNT_SCI_EN, pm1_cnt);
  264. }
  265. /*
  266. * QEMU's version of write_acpi_tables is defined in
  267. * arch/x86/cpu/qemu/acpi_table.c
  268. */
  269. u32 write_acpi_tables(u32 start)
  270. {
  271. u32 current;
  272. struct acpi_rsdp *rsdp;
  273. struct acpi_rsdt *rsdt;
  274. struct acpi_xsdt *xsdt;
  275. struct acpi_facs *facs;
  276. struct acpi_table_header *dsdt;
  277. struct acpi_fadt *fadt;
  278. struct acpi_mcfg *mcfg;
  279. struct acpi_madt *madt;
  280. current = start;
  281. /* Align ACPI tables to 16 byte */
  282. current = ALIGN(current, 16);
  283. debug("ACPI: Writing ACPI tables at %x\n", start);
  284. /* We need at least an RSDP and an RSDT Table */
  285. rsdp = (struct acpi_rsdp *)current;
  286. current += sizeof(struct acpi_rsdp);
  287. current = ALIGN(current, 16);
  288. rsdt = (struct acpi_rsdt *)current;
  289. current += sizeof(struct acpi_rsdt);
  290. current = ALIGN(current, 16);
  291. xsdt = (struct acpi_xsdt *)current;
  292. current += sizeof(struct acpi_xsdt);
  293. /*
  294. * Per ACPI spec, the FACS table address must be aligned to a 64 byte
  295. * boundary (Windows checks this, but Linux does not).
  296. */
  297. current = ALIGN(current, 64);
  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 = (struct acpi_table_header *)current;
  310. memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
  311. current += sizeof(struct acpi_table_header);
  312. memcpy((char *)current,
  313. (char *)&AmlCode + sizeof(struct acpi_table_header),
  314. dsdt->length - sizeof(struct acpi_table_header));
  315. current += dsdt->length - sizeof(struct acpi_table_header);
  316. current = ALIGN(current, 16);
  317. debug("ACPI: * FADT\n");
  318. fadt = (struct acpi_fadt *)current;
  319. current += sizeof(struct acpi_fadt);
  320. current = ALIGN(current, 16);
  321. acpi_create_fadt(fadt, facs, dsdt);
  322. acpi_add_table(rsdp, fadt);
  323. debug("ACPI: * MADT\n");
  324. madt = (struct acpi_madt *)current;
  325. acpi_create_madt(madt);
  326. current += madt->header.length;
  327. acpi_add_table(rsdp, madt);
  328. current = ALIGN(current, 16);
  329. debug("ACPI: * MCFG\n");
  330. mcfg = (struct acpi_mcfg *)current;
  331. acpi_create_mcfg(mcfg);
  332. current += mcfg->header.length;
  333. acpi_add_table(rsdp, mcfg);
  334. current = ALIGN(current, 16);
  335. debug("current = %x\n", current);
  336. debug("ACPI: done\n");
  337. /*
  338. * Other than waiting for OSPM to request us to switch to ACPI mode,
  339. * do it by ourselves, since SMI will not be triggered.
  340. */
  341. enter_acpi_mode(fadt->pm1a_cnt_blk);
  342. return current;
  343. }