acpi_table.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 <version.h>
  14. #include <asm/acpi/global_nvs.h>
  15. #include <asm/acpi_table.h>
  16. #include <asm/io.h>
  17. #include <asm/ioapic.h>
  18. #include <asm/lapic.h>
  19. #include <asm/mpspec.h>
  20. #include <asm/tables.h>
  21. #include <asm/arch/global_nvs.h>
  22. /*
  23. * IASL compiles the dsdt entries and writes the hex values
  24. * to a C array AmlCode[] (see dsdt.c).
  25. */
  26. extern const unsigned char AmlCode[];
  27. /* ACPI RSDP address to be used in boot parameters */
  28. static ulong acpi_rsdp_addr;
  29. static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
  30. struct acpi_xsdt *xsdt)
  31. {
  32. memset(rsdp, 0, sizeof(struct acpi_rsdp));
  33. memcpy(rsdp->signature, RSDP_SIG, 8);
  34. memcpy(rsdp->oem_id, OEM_ID, 6);
  35. rsdp->length = sizeof(struct acpi_rsdp);
  36. rsdp->rsdt_address = (u32)rsdt;
  37. /*
  38. * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
  39. *
  40. * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
  41. * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
  42. * revision 0)
  43. */
  44. if (xsdt == NULL) {
  45. rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
  46. } else {
  47. rsdp->xsdt_address = (u64)(u32)xsdt;
  48. rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
  49. }
  50. /* Calculate checksums */
  51. rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
  52. rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
  53. sizeof(struct acpi_rsdp));
  54. }
  55. void acpi_fill_header(struct acpi_table_header *header, char *signature)
  56. {
  57. memcpy(header->signature, signature, 4);
  58. memcpy(header->oem_id, OEM_ID, 6);
  59. memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
  60. header->oem_revision = U_BOOT_BUILD_DATE;
  61. memcpy(header->aslc_id, ASLC_ID, 4);
  62. }
  63. static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
  64. {
  65. struct acpi_table_header *header = &(rsdt->header);
  66. /* Fill out header fields */
  67. acpi_fill_header(header, "RSDT");
  68. header->length = sizeof(struct acpi_rsdt);
  69. header->revision = 1;
  70. /* Entries are filled in later, we come with an empty set */
  71. /* Fix checksum */
  72. header->checksum = table_compute_checksum((void *)rsdt,
  73. sizeof(struct acpi_rsdt));
  74. }
  75. static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
  76. {
  77. struct acpi_table_header *header = &(xsdt->header);
  78. /* Fill out header fields */
  79. acpi_fill_header(header, "XSDT");
  80. header->length = sizeof(struct acpi_xsdt);
  81. header->revision = 1;
  82. /* Entries are filled in later, we come with an empty set */
  83. /* Fix checksum */
  84. header->checksum = table_compute_checksum((void *)xsdt,
  85. sizeof(struct acpi_xsdt));
  86. }
  87. /**
  88. * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
  89. * and checksum.
  90. */
  91. static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
  92. {
  93. int i, entries_num;
  94. struct acpi_rsdt *rsdt;
  95. struct acpi_xsdt *xsdt = NULL;
  96. /* The RSDT is mandatory while the XSDT is not */
  97. rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
  98. if (rsdp->xsdt_address)
  99. xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
  100. /* This should always be MAX_ACPI_TABLES */
  101. entries_num = ARRAY_SIZE(rsdt->entry);
  102. for (i = 0; i < entries_num; i++) {
  103. if (rsdt->entry[i] == 0)
  104. break;
  105. }
  106. if (i >= entries_num) {
  107. debug("ACPI: Error: too many tables\n");
  108. return;
  109. }
  110. /* Add table to the RSDT */
  111. rsdt->entry[i] = (u32)table;
  112. /* Fix RSDT length or the kernel will assume invalid entries */
  113. rsdt->header.length = sizeof(struct acpi_table_header) +
  114. (sizeof(u32) * (i + 1));
  115. /* Re-calculate checksum */
  116. rsdt->header.checksum = 0;
  117. rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
  118. rsdt->header.length);
  119. /*
  120. * And now the same thing for the XSDT. We use the same index as for
  121. * now we want the XSDT and RSDT to always be in sync in U-Boot
  122. */
  123. if (xsdt) {
  124. /* Add table to the XSDT */
  125. xsdt->entry[i] = (u64)(u32)table;
  126. /* Fix XSDT length */
  127. xsdt->header.length = sizeof(struct acpi_table_header) +
  128. (sizeof(u64) * (i + 1));
  129. /* Re-calculate checksum */
  130. xsdt->header.checksum = 0;
  131. xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
  132. xsdt->header.length);
  133. }
  134. }
  135. static void acpi_create_facs(struct acpi_facs *facs)
  136. {
  137. memset((void *)facs, 0, sizeof(struct acpi_facs));
  138. memcpy(facs->signature, "FACS", 4);
  139. facs->length = sizeof(struct acpi_facs);
  140. facs->hardware_signature = 0;
  141. facs->firmware_waking_vector = 0;
  142. facs->global_lock = 0;
  143. facs->flags = 0;
  144. facs->x_firmware_waking_vector_l = 0;
  145. facs->x_firmware_waking_vector_h = 0;
  146. facs->version = 1;
  147. }
  148. static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
  149. u8 cpu, u8 apic)
  150. {
  151. lapic->type = ACPI_APIC_LAPIC;
  152. lapic->length = sizeof(struct acpi_madt_lapic);
  153. lapic->flags = LOCAL_APIC_FLAG_ENABLED;
  154. lapic->processor_id = cpu;
  155. lapic->apic_id = apic;
  156. return lapic->length;
  157. }
  158. int acpi_create_madt_lapics(u32 current)
  159. {
  160. struct udevice *dev;
  161. int total_length = 0;
  162. for (uclass_find_first_device(UCLASS_CPU, &dev);
  163. dev;
  164. uclass_find_next_device(&dev)) {
  165. struct cpu_platdata *plat = dev_get_parent_platdata(dev);
  166. int length = acpi_create_madt_lapic(
  167. (struct acpi_madt_lapic *)current,
  168. plat->cpu_id, plat->cpu_id);
  169. current += length;
  170. total_length += length;
  171. }
  172. return total_length;
  173. }
  174. int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
  175. u32 addr, u32 gsi_base)
  176. {
  177. ioapic->type = ACPI_APIC_IOAPIC;
  178. ioapic->length = sizeof(struct acpi_madt_ioapic);
  179. ioapic->reserved = 0x00;
  180. ioapic->gsi_base = gsi_base;
  181. ioapic->ioapic_id = id;
  182. ioapic->ioapic_addr = addr;
  183. return ioapic->length;
  184. }
  185. int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
  186. u8 bus, u8 source, u32 gsirq, u16 flags)
  187. {
  188. irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
  189. irqoverride->length = sizeof(struct acpi_madt_irqoverride);
  190. irqoverride->bus = bus;
  191. irqoverride->source = source;
  192. irqoverride->gsirq = gsirq;
  193. irqoverride->flags = flags;
  194. return irqoverride->length;
  195. }
  196. int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
  197. u8 cpu, u16 flags, u8 lint)
  198. {
  199. lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
  200. lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
  201. lapic_nmi->flags = flags;
  202. lapic_nmi->processor_id = cpu;
  203. lapic_nmi->lint = lint;
  204. return lapic_nmi->length;
  205. }
  206. static int acpi_create_madt_irq_overrides(u32 current)
  207. {
  208. struct acpi_madt_irqoverride *irqovr;
  209. u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH;
  210. int length = 0;
  211. irqovr = (void *)current;
  212. length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0);
  213. irqovr = (void *)(current + length);
  214. length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags);
  215. return length;
  216. }
  217. __weak u32 acpi_fill_madt(u32 current)
  218. {
  219. current += acpi_create_madt_lapics(current);
  220. current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current,
  221. io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0);
  222. current += acpi_create_madt_irq_overrides(current);
  223. return current;
  224. }
  225. static void acpi_create_madt(struct acpi_madt *madt)
  226. {
  227. struct acpi_table_header *header = &(madt->header);
  228. u32 current = (u32)madt + sizeof(struct acpi_madt);
  229. memset((void *)madt, 0, sizeof(struct acpi_madt));
  230. /* Fill out header fields */
  231. acpi_fill_header(header, "APIC");
  232. header->length = sizeof(struct acpi_madt);
  233. header->revision = 4;
  234. madt->lapic_addr = LAPIC_DEFAULT_BASE;
  235. madt->flags = ACPI_MADT_PCAT_COMPAT;
  236. current = acpi_fill_madt(current);
  237. /* (Re)calculate length and checksum */
  238. header->length = current - (u32)madt;
  239. header->checksum = table_compute_checksum((void *)madt, header->length);
  240. }
  241. int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, u32 base,
  242. u16 seg_nr, u8 start, u8 end)
  243. {
  244. memset(mmconfig, 0, sizeof(*mmconfig));
  245. mmconfig->base_address_l = base;
  246. mmconfig->base_address_h = 0;
  247. mmconfig->pci_segment_group_number = seg_nr;
  248. mmconfig->start_bus_number = start;
  249. mmconfig->end_bus_number = end;
  250. return sizeof(struct acpi_mcfg_mmconfig);
  251. }
  252. __weak u32 acpi_fill_mcfg(u32 current)
  253. {
  254. current += acpi_create_mcfg_mmconfig
  255. ((struct acpi_mcfg_mmconfig *)current,
  256. CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
  257. return current;
  258. }
  259. /* MCFG is defined in the PCI Firmware Specification 3.0 */
  260. static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
  261. {
  262. struct acpi_table_header *header = &(mcfg->header);
  263. u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
  264. memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
  265. /* Fill out header fields */
  266. acpi_fill_header(header, "MCFG");
  267. header->length = sizeof(struct acpi_mcfg);
  268. header->revision = 1;
  269. current = acpi_fill_mcfg(current);
  270. /* (Re)calculate length and checksum */
  271. header->length = current - (u32)mcfg;
  272. header->checksum = table_compute_checksum((void *)mcfg, header->length);
  273. }
  274. void enter_acpi_mode(int pm1_cnt)
  275. {
  276. u16 val = inw(pm1_cnt);
  277. /*
  278. * PM1_CNT register bit0 selects the power management event to be
  279. * either an SCI or SMI interrupt. When this bit is set, then power
  280. * management events will generate an SCI interrupt. When this bit
  281. * is reset power management events will generate an SMI interrupt.
  282. *
  283. * Per ACPI spec, it is the responsibility of the hardware to set
  284. * or reset this bit. OSPM always preserves this bit position.
  285. *
  286. * U-Boot does not support SMI. And we don't have plan to support
  287. * anything running in SMM within U-Boot. To create a legacy-free
  288. * system, and expose ourselves to OSPM as working under ACPI mode
  289. * already, turn this bit on.
  290. */
  291. outw(val | PM1_CNT_SCI_EN, pm1_cnt);
  292. }
  293. /*
  294. * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
  295. */
  296. ulong write_acpi_tables(ulong start)
  297. {
  298. u32 current;
  299. struct acpi_rsdp *rsdp;
  300. struct acpi_rsdt *rsdt;
  301. struct acpi_xsdt *xsdt;
  302. struct acpi_facs *facs;
  303. struct acpi_table_header *dsdt;
  304. struct acpi_fadt *fadt;
  305. struct acpi_mcfg *mcfg;
  306. struct acpi_madt *madt;
  307. int i;
  308. current = start;
  309. /* Align ACPI tables to 16 byte */
  310. current = ALIGN(current, 16);
  311. debug("ACPI: Writing ACPI tables at %lx\n", start);
  312. /* We need at least an RSDP and an RSDT Table */
  313. rsdp = (struct acpi_rsdp *)current;
  314. current += sizeof(struct acpi_rsdp);
  315. current = ALIGN(current, 16);
  316. rsdt = (struct acpi_rsdt *)current;
  317. current += sizeof(struct acpi_rsdt);
  318. current = ALIGN(current, 16);
  319. xsdt = (struct acpi_xsdt *)current;
  320. current += sizeof(struct acpi_xsdt);
  321. /*
  322. * Per ACPI spec, the FACS table address must be aligned to a 64 byte
  323. * boundary (Windows checks this, but Linux does not).
  324. */
  325. current = ALIGN(current, 64);
  326. /* clear all table memory */
  327. memset((void *)start, 0, current - start);
  328. acpi_write_rsdp(rsdp, rsdt, xsdt);
  329. acpi_write_rsdt(rsdt);
  330. acpi_write_xsdt(xsdt);
  331. debug("ACPI: * FACS\n");
  332. facs = (struct acpi_facs *)current;
  333. current += sizeof(struct acpi_facs);
  334. current = ALIGN(current, 16);
  335. acpi_create_facs(facs);
  336. debug("ACPI: * DSDT\n");
  337. dsdt = (struct acpi_table_header *)current;
  338. memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
  339. current += sizeof(struct acpi_table_header);
  340. memcpy((char *)current,
  341. (char *)&AmlCode + sizeof(struct acpi_table_header),
  342. dsdt->length - sizeof(struct acpi_table_header));
  343. current += dsdt->length - sizeof(struct acpi_table_header);
  344. current = ALIGN(current, 16);
  345. /* Pack GNVS into the ACPI table area */
  346. for (i = 0; i < dsdt->length; i++) {
  347. u32 *gnvs = (u32 *)((u32)dsdt + i);
  348. if (*gnvs == ACPI_GNVS_ADDR) {
  349. debug("Fix up global NVS in DSDT to 0x%08x\n", current);
  350. *gnvs = current;
  351. break;
  352. }
  353. }
  354. /* Update DSDT checksum since we patched the GNVS address */
  355. dsdt->checksum = 0;
  356. dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
  357. /* Fill in platform-specific global NVS variables */
  358. acpi_create_gnvs((struct acpi_global_nvs *)current);
  359. current += sizeof(struct acpi_global_nvs);
  360. current = ALIGN(current, 16);
  361. debug("ACPI: * FADT\n");
  362. fadt = (struct acpi_fadt *)current;
  363. current += sizeof(struct acpi_fadt);
  364. current = ALIGN(current, 16);
  365. acpi_create_fadt(fadt, facs, dsdt);
  366. acpi_add_table(rsdp, fadt);
  367. debug("ACPI: * MADT\n");
  368. madt = (struct acpi_madt *)current;
  369. acpi_create_madt(madt);
  370. current += madt->header.length;
  371. acpi_add_table(rsdp, madt);
  372. current = ALIGN(current, 16);
  373. debug("ACPI: * MCFG\n");
  374. mcfg = (struct acpi_mcfg *)current;
  375. acpi_create_mcfg(mcfg);
  376. current += mcfg->header.length;
  377. acpi_add_table(rsdp, mcfg);
  378. current = ALIGN(current, 16);
  379. debug("current = %x\n", current);
  380. acpi_rsdp_addr = (unsigned long)rsdp;
  381. debug("ACPI: done\n");
  382. /* Don't touch ACPI hardware on HW reduced platforms */
  383. if (fadt->flags & ACPI_FADT_HW_REDUCED_ACPI)
  384. return current;
  385. /*
  386. * Other than waiting for OSPM to request us to switch to ACPI mode,
  387. * do it by ourselves, since SMI will not be triggered.
  388. */
  389. enter_acpi_mode(fadt->pm1a_cnt_blk);
  390. return current;
  391. }
  392. ulong acpi_get_rsdp_addr(void)
  393. {
  394. return acpi_rsdp_addr;
  395. }
  396. static struct acpi_rsdp *acpi_valid_rsdp(struct acpi_rsdp *rsdp)
  397. {
  398. if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
  399. return NULL;
  400. debug("Looking on %p for valid checksum\n", rsdp);
  401. if (table_compute_checksum((void *)rsdp, 20) != 0)
  402. return NULL;
  403. debug("acpi rsdp checksum 1 passed\n");
  404. if ((rsdp->revision > 1) &&
  405. (table_compute_checksum((void *)rsdp, rsdp->length) != 0))
  406. return NULL;
  407. debug("acpi rsdp checksum 2 passed\n");
  408. return rsdp;
  409. }
  410. struct acpi_fadt *acpi_find_fadt(void)
  411. {
  412. char *p, *end;
  413. struct acpi_rsdp *rsdp = NULL;
  414. struct acpi_rsdt *rsdt;
  415. struct acpi_fadt *fadt = NULL;
  416. int i;
  417. /* Find RSDP */
  418. for (p = (char *)ROM_TABLE_ADDR; p < (char *)ROM_TABLE_END; p += 16) {
  419. rsdp = acpi_valid_rsdp((struct acpi_rsdp *)p);
  420. if (rsdp)
  421. break;
  422. }
  423. if (rsdp == NULL)
  424. return NULL;
  425. debug("RSDP found at %p\n", rsdp);
  426. rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
  427. end = (char *)rsdt + rsdt->header.length;
  428. debug("RSDT found at %p ends at %p\n", rsdt, end);
  429. for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
  430. fadt = (struct acpi_fadt *)rsdt->entry[i];
  431. if (strncmp((char *)fadt, "FACP", 4) == 0)
  432. break;
  433. fadt = NULL;
  434. }
  435. if (fadt == NULL)
  436. return NULL;
  437. debug("FADT found at %p\n", fadt);
  438. return fadt;
  439. }
  440. void *acpi_find_wakeup_vector(struct acpi_fadt *fadt)
  441. {
  442. struct acpi_facs *facs;
  443. void *wake_vec;
  444. debug("Trying to find the wakeup vector...\n");
  445. facs = (struct acpi_facs *)fadt->firmware_ctrl;
  446. if (facs == NULL) {
  447. debug("No FACS found, wake up from S3 not possible.\n");
  448. return NULL;
  449. }
  450. debug("FACS found at %p\n", facs);
  451. wake_vec = (void *)facs->firmware_waking_vector;
  452. debug("OS waking vector is %p\n", wake_vec);
  453. return wake_vec;
  454. }