acpi_table.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Based on acpi.c from coreboot
  4. *
  5. * Copyright (C) 2015, Saket Sinha <saket.sinha89@gmail.com>
  6. * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com>
  7. */
  8. #include <common.h>
  9. #include <cpu.h>
  10. #include <dm.h>
  11. #include <dm/uclass-internal.h>
  12. #include <serial.h>
  13. #include <version.h>
  14. #include <asm/acpi/global_nvs.h>
  15. #include <asm/acpi_table.h>
  16. #include <asm/ioapic.h>
  17. #include <asm/lapic.h>
  18. #include <asm/mpspec.h>
  19. #include <asm/tables.h>
  20. #include <asm/arch/global_nvs.h>
  21. /*
  22. * IASL compiles the dsdt entries and writes the hex values
  23. * to a C array AmlCode[] (see dsdt.c).
  24. */
  25. extern const unsigned char AmlCode[];
  26. /* ACPI RSDP address to be used in boot parameters */
  27. static ulong acpi_rsdp_addr;
  28. static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
  29. struct acpi_xsdt *xsdt)
  30. {
  31. memset(rsdp, 0, sizeof(struct acpi_rsdp));
  32. memcpy(rsdp->signature, RSDP_SIG, 8);
  33. memcpy(rsdp->oem_id, OEM_ID, 6);
  34. rsdp->length = sizeof(struct acpi_rsdp);
  35. rsdp->rsdt_address = (u32)rsdt;
  36. /*
  37. * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
  38. *
  39. * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
  40. * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
  41. * revision 0)
  42. */
  43. if (xsdt == NULL) {
  44. rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
  45. } else {
  46. rsdp->xsdt_address = (u64)(u32)xsdt;
  47. rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
  48. }
  49. /* Calculate checksums */
  50. rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
  51. rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
  52. sizeof(struct acpi_rsdp));
  53. }
  54. void acpi_fill_header(struct acpi_table_header *header, char *signature)
  55. {
  56. memcpy(header->signature, signature, 4);
  57. memcpy(header->oem_id, OEM_ID, 6);
  58. memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
  59. header->oem_revision = U_BOOT_BUILD_DATE;
  60. memcpy(header->aslc_id, ASLC_ID, 4);
  61. }
  62. static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
  63. {
  64. struct acpi_table_header *header = &(rsdt->header);
  65. /* Fill out header fields */
  66. acpi_fill_header(header, "RSDT");
  67. header->length = sizeof(struct acpi_rsdt);
  68. header->revision = 1;
  69. /* Entries are filled in later, we come with an empty set */
  70. /* Fix checksum */
  71. header->checksum = table_compute_checksum((void *)rsdt,
  72. sizeof(struct acpi_rsdt));
  73. }
  74. static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
  75. {
  76. struct acpi_table_header *header = &(xsdt->header);
  77. /* Fill out header fields */
  78. acpi_fill_header(header, "XSDT");
  79. header->length = sizeof(struct acpi_xsdt);
  80. header->revision = 1;
  81. /* Entries are filled in later, we come with an empty set */
  82. /* Fix checksum */
  83. header->checksum = table_compute_checksum((void *)xsdt,
  84. sizeof(struct acpi_xsdt));
  85. }
  86. /**
  87. * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
  88. * and checksum.
  89. */
  90. static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
  91. {
  92. int i, entries_num;
  93. struct acpi_rsdt *rsdt;
  94. struct acpi_xsdt *xsdt = NULL;
  95. /* The RSDT is mandatory while the XSDT is not */
  96. rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
  97. if (rsdp->xsdt_address)
  98. xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
  99. /* This should always be MAX_ACPI_TABLES */
  100. entries_num = ARRAY_SIZE(rsdt->entry);
  101. for (i = 0; i < entries_num; i++) {
  102. if (rsdt->entry[i] == 0)
  103. break;
  104. }
  105. if (i >= entries_num) {
  106. debug("ACPI: Error: too many tables\n");
  107. return;
  108. }
  109. /* Add table to the RSDT */
  110. rsdt->entry[i] = (u32)table;
  111. /* Fix RSDT length or the kernel will assume invalid entries */
  112. rsdt->header.length = sizeof(struct acpi_table_header) +
  113. (sizeof(u32) * (i + 1));
  114. /* Re-calculate checksum */
  115. rsdt->header.checksum = 0;
  116. rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
  117. rsdt->header.length);
  118. /*
  119. * And now the same thing for the XSDT. We use the same index as for
  120. * now we want the XSDT and RSDT to always be in sync in U-Boot
  121. */
  122. if (xsdt) {
  123. /* Add table to the XSDT */
  124. xsdt->entry[i] = (u64)(u32)table;
  125. /* Fix XSDT length */
  126. xsdt->header.length = sizeof(struct acpi_table_header) +
  127. (sizeof(u64) * (i + 1));
  128. /* Re-calculate checksum */
  129. xsdt->header.checksum = 0;
  130. xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
  131. xsdt->header.length);
  132. }
  133. }
  134. static void acpi_create_facs(struct acpi_facs *facs)
  135. {
  136. memset((void *)facs, 0, sizeof(struct acpi_facs));
  137. memcpy(facs->signature, "FACS", 4);
  138. facs->length = sizeof(struct acpi_facs);
  139. facs->hardware_signature = 0;
  140. facs->firmware_waking_vector = 0;
  141. facs->global_lock = 0;
  142. facs->flags = 0;
  143. facs->x_firmware_waking_vector_l = 0;
  144. facs->x_firmware_waking_vector_h = 0;
  145. facs->version = 1;
  146. }
  147. static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
  148. u8 cpu, u8 apic)
  149. {
  150. lapic->type = ACPI_APIC_LAPIC;
  151. lapic->length = sizeof(struct acpi_madt_lapic);
  152. lapic->flags = LOCAL_APIC_FLAG_ENABLED;
  153. lapic->processor_id = cpu;
  154. lapic->apic_id = apic;
  155. return lapic->length;
  156. }
  157. int acpi_create_madt_lapics(u32 current)
  158. {
  159. struct udevice *dev;
  160. int total_length = 0;
  161. for (uclass_find_first_device(UCLASS_CPU, &dev);
  162. dev;
  163. uclass_find_next_device(&dev)) {
  164. struct cpu_platdata *plat = dev_get_parent_platdata(dev);
  165. int length = acpi_create_madt_lapic(
  166. (struct acpi_madt_lapic *)current,
  167. plat->cpu_id, plat->cpu_id);
  168. current += length;
  169. total_length += length;
  170. }
  171. return total_length;
  172. }
  173. int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
  174. u32 addr, u32 gsi_base)
  175. {
  176. ioapic->type = ACPI_APIC_IOAPIC;
  177. ioapic->length = sizeof(struct acpi_madt_ioapic);
  178. ioapic->reserved = 0x00;
  179. ioapic->gsi_base = gsi_base;
  180. ioapic->ioapic_id = id;
  181. ioapic->ioapic_addr = addr;
  182. return ioapic->length;
  183. }
  184. int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
  185. u8 bus, u8 source, u32 gsirq, u16 flags)
  186. {
  187. irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
  188. irqoverride->length = sizeof(struct acpi_madt_irqoverride);
  189. irqoverride->bus = bus;
  190. irqoverride->source = source;
  191. irqoverride->gsirq = gsirq;
  192. irqoverride->flags = flags;
  193. return irqoverride->length;
  194. }
  195. int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
  196. u8 cpu, u16 flags, u8 lint)
  197. {
  198. lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
  199. lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
  200. lapic_nmi->flags = flags;
  201. lapic_nmi->processor_id = cpu;
  202. lapic_nmi->lint = lint;
  203. return lapic_nmi->length;
  204. }
  205. static int acpi_create_madt_irq_overrides(u32 current)
  206. {
  207. struct acpi_madt_irqoverride *irqovr;
  208. u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH;
  209. int length = 0;
  210. irqovr = (void *)current;
  211. length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0);
  212. irqovr = (void *)(current + length);
  213. length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags);
  214. return length;
  215. }
  216. __weak u32 acpi_fill_madt(u32 current)
  217. {
  218. current += acpi_create_madt_lapics(current);
  219. current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current,
  220. io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0);
  221. current += acpi_create_madt_irq_overrides(current);
  222. return current;
  223. }
  224. static void acpi_create_madt(struct acpi_madt *madt)
  225. {
  226. struct acpi_table_header *header = &(madt->header);
  227. u32 current = (u32)madt + sizeof(struct acpi_madt);
  228. memset((void *)madt, 0, sizeof(struct acpi_madt));
  229. /* Fill out header fields */
  230. acpi_fill_header(header, "APIC");
  231. header->length = sizeof(struct acpi_madt);
  232. header->revision = 4;
  233. madt->lapic_addr = LAPIC_DEFAULT_BASE;
  234. madt->flags = ACPI_MADT_PCAT_COMPAT;
  235. current = acpi_fill_madt(current);
  236. /* (Re)calculate length and checksum */
  237. header->length = current - (u32)madt;
  238. header->checksum = table_compute_checksum((void *)madt, header->length);
  239. }
  240. int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, u32 base,
  241. u16 seg_nr, u8 start, u8 end)
  242. {
  243. memset(mmconfig, 0, sizeof(*mmconfig));
  244. mmconfig->base_address_l = base;
  245. mmconfig->base_address_h = 0;
  246. mmconfig->pci_segment_group_number = seg_nr;
  247. mmconfig->start_bus_number = start;
  248. mmconfig->end_bus_number = end;
  249. return sizeof(struct acpi_mcfg_mmconfig);
  250. }
  251. __weak u32 acpi_fill_mcfg(u32 current)
  252. {
  253. current += acpi_create_mcfg_mmconfig
  254. ((struct acpi_mcfg_mmconfig *)current,
  255. CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
  256. return current;
  257. }
  258. /* MCFG is defined in the PCI Firmware Specification 3.0 */
  259. static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
  260. {
  261. struct acpi_table_header *header = &(mcfg->header);
  262. u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
  263. memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
  264. /* Fill out header fields */
  265. acpi_fill_header(header, "MCFG");
  266. header->length = sizeof(struct acpi_mcfg);
  267. header->revision = 1;
  268. current = acpi_fill_mcfg(current);
  269. /* (Re)calculate length and checksum */
  270. header->length = current - (u32)mcfg;
  271. header->checksum = table_compute_checksum((void *)mcfg, header->length);
  272. }
  273. __weak u32 acpi_fill_csrt(u32 current)
  274. {
  275. return current;
  276. }
  277. static void acpi_create_csrt(struct acpi_csrt *csrt)
  278. {
  279. struct acpi_table_header *header = &(csrt->header);
  280. u32 current = (u32)csrt + sizeof(struct acpi_csrt);
  281. memset((void *)csrt, 0, sizeof(struct acpi_csrt));
  282. /* Fill out header fields */
  283. acpi_fill_header(header, "CSRT");
  284. header->length = sizeof(struct acpi_csrt);
  285. header->revision = 0;
  286. current = acpi_fill_csrt(current);
  287. /* (Re)calculate length and checksum */
  288. header->length = current - (u32)csrt;
  289. header->checksum = table_compute_checksum((void *)csrt, header->length);
  290. }
  291. static void acpi_create_spcr(struct acpi_spcr *spcr)
  292. {
  293. struct acpi_table_header *header = &(spcr->header);
  294. struct serial_device_info serial_info = {0};
  295. ulong serial_address, serial_offset;
  296. struct udevice *dev;
  297. uint serial_config;
  298. uint serial_width;
  299. int access_size;
  300. int space_id;
  301. int ret = -ENODEV;
  302. /* Fill out header fields */
  303. acpi_fill_header(header, "SPCR");
  304. header->length = sizeof(struct acpi_spcr);
  305. header->revision = 2;
  306. /* Read the device once, here. It is reused below */
  307. dev = gd->cur_serial_dev;
  308. if (dev)
  309. ret = serial_getinfo(dev, &serial_info);
  310. if (ret)
  311. serial_info.type = SERIAL_CHIP_UNKNOWN;
  312. /* Encode chip type */
  313. switch (serial_info.type) {
  314. case SERIAL_CHIP_16550_COMPATIBLE:
  315. spcr->interface_type = ACPI_DBG2_16550_COMPATIBLE;
  316. break;
  317. case SERIAL_CHIP_UNKNOWN:
  318. default:
  319. spcr->interface_type = ACPI_DBG2_UNKNOWN;
  320. break;
  321. }
  322. /* Encode address space */
  323. switch (serial_info.addr_space) {
  324. case SERIAL_ADDRESS_SPACE_MEMORY:
  325. space_id = ACPI_ADDRESS_SPACE_MEMORY;
  326. break;
  327. case SERIAL_ADDRESS_SPACE_IO:
  328. default:
  329. space_id = ACPI_ADDRESS_SPACE_IO;
  330. break;
  331. }
  332. serial_width = serial_info.reg_width * 8;
  333. serial_offset = serial_info.reg_offset << serial_info.reg_shift;
  334. serial_address = serial_info.addr + serial_offset;
  335. /* Encode register access size */
  336. switch (serial_info.reg_shift) {
  337. case 0:
  338. access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
  339. break;
  340. case 1:
  341. access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
  342. break;
  343. case 2:
  344. access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
  345. break;
  346. case 3:
  347. access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
  348. break;
  349. default:
  350. access_size = ACPI_ACCESS_SIZE_UNDEFINED;
  351. break;
  352. }
  353. debug("UART type %u @ %lx\n", spcr->interface_type, serial_address);
  354. /* Fill GAS */
  355. spcr->serial_port.space_id = space_id;
  356. spcr->serial_port.bit_width = serial_width;
  357. spcr->serial_port.bit_offset = 0;
  358. spcr->serial_port.access_size = access_size;
  359. spcr->serial_port.addrl = lower_32_bits(serial_address);
  360. spcr->serial_port.addrh = upper_32_bits(serial_address);
  361. /* Encode baud rate */
  362. switch (serial_info.baudrate) {
  363. case 9600:
  364. spcr->baud_rate = 3;
  365. break;
  366. case 19200:
  367. spcr->baud_rate = 4;
  368. break;
  369. case 57600:
  370. spcr->baud_rate = 6;
  371. break;
  372. case 115200:
  373. spcr->baud_rate = 7;
  374. break;
  375. default:
  376. spcr->baud_rate = 0;
  377. break;
  378. }
  379. serial_config = SERIAL_DEFAULT_CONFIG;
  380. if (dev)
  381. ret = serial_getconfig(dev, &serial_config);
  382. spcr->parity = SERIAL_GET_PARITY(serial_config);
  383. spcr->stop_bits = SERIAL_GET_STOP(serial_config);
  384. /* No PCI devices for now */
  385. spcr->pci_device_id = 0xffff;
  386. spcr->pci_vendor_id = 0xffff;
  387. /* Fix checksum */
  388. header->checksum = table_compute_checksum((void *)spcr, header->length);
  389. }
  390. /*
  391. * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
  392. */
  393. ulong write_acpi_tables(ulong start)
  394. {
  395. u32 current;
  396. struct acpi_rsdp *rsdp;
  397. struct acpi_rsdt *rsdt;
  398. struct acpi_xsdt *xsdt;
  399. struct acpi_facs *facs;
  400. struct acpi_table_header *dsdt;
  401. struct acpi_fadt *fadt;
  402. struct acpi_mcfg *mcfg;
  403. struct acpi_madt *madt;
  404. struct acpi_csrt *csrt;
  405. struct acpi_spcr *spcr;
  406. int i;
  407. current = start;
  408. /* Align ACPI tables to 16 byte */
  409. current = ALIGN(current, 16);
  410. debug("ACPI: Writing ACPI tables at %lx\n", start);
  411. /* We need at least an RSDP and an RSDT Table */
  412. rsdp = (struct acpi_rsdp *)current;
  413. current += sizeof(struct acpi_rsdp);
  414. current = ALIGN(current, 16);
  415. rsdt = (struct acpi_rsdt *)current;
  416. current += sizeof(struct acpi_rsdt);
  417. current = ALIGN(current, 16);
  418. xsdt = (struct acpi_xsdt *)current;
  419. current += sizeof(struct acpi_xsdt);
  420. /*
  421. * Per ACPI spec, the FACS table address must be aligned to a 64 byte
  422. * boundary (Windows checks this, but Linux does not).
  423. */
  424. current = ALIGN(current, 64);
  425. /* clear all table memory */
  426. memset((void *)start, 0, current - start);
  427. acpi_write_rsdp(rsdp, rsdt, xsdt);
  428. acpi_write_rsdt(rsdt);
  429. acpi_write_xsdt(xsdt);
  430. debug("ACPI: * FACS\n");
  431. facs = (struct acpi_facs *)current;
  432. current += sizeof(struct acpi_facs);
  433. current = ALIGN(current, 16);
  434. acpi_create_facs(facs);
  435. debug("ACPI: * DSDT\n");
  436. dsdt = (struct acpi_table_header *)current;
  437. memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
  438. current += sizeof(struct acpi_table_header);
  439. memcpy((char *)current,
  440. (char *)&AmlCode + sizeof(struct acpi_table_header),
  441. dsdt->length - sizeof(struct acpi_table_header));
  442. current += dsdt->length - sizeof(struct acpi_table_header);
  443. current = ALIGN(current, 16);
  444. /* Pack GNVS into the ACPI table area */
  445. for (i = 0; i < dsdt->length; i++) {
  446. u32 *gnvs = (u32 *)((u32)dsdt + i);
  447. if (*gnvs == ACPI_GNVS_ADDR) {
  448. debug("Fix up global NVS in DSDT to 0x%08x\n", current);
  449. *gnvs = current;
  450. break;
  451. }
  452. }
  453. /* Update DSDT checksum since we patched the GNVS address */
  454. dsdt->checksum = 0;
  455. dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
  456. /* Fill in platform-specific global NVS variables */
  457. acpi_create_gnvs((struct acpi_global_nvs *)current);
  458. current += sizeof(struct acpi_global_nvs);
  459. current = ALIGN(current, 16);
  460. debug("ACPI: * FADT\n");
  461. fadt = (struct acpi_fadt *)current;
  462. current += sizeof(struct acpi_fadt);
  463. current = ALIGN(current, 16);
  464. acpi_create_fadt(fadt, facs, dsdt);
  465. acpi_add_table(rsdp, fadt);
  466. debug("ACPI: * MADT\n");
  467. madt = (struct acpi_madt *)current;
  468. acpi_create_madt(madt);
  469. current += madt->header.length;
  470. acpi_add_table(rsdp, madt);
  471. current = ALIGN(current, 16);
  472. debug("ACPI: * MCFG\n");
  473. mcfg = (struct acpi_mcfg *)current;
  474. acpi_create_mcfg(mcfg);
  475. current += mcfg->header.length;
  476. acpi_add_table(rsdp, mcfg);
  477. current = ALIGN(current, 16);
  478. debug("ACPI: * CSRT\n");
  479. csrt = (struct acpi_csrt *)current;
  480. acpi_create_csrt(csrt);
  481. current += csrt->header.length;
  482. acpi_add_table(rsdp, csrt);
  483. current = ALIGN(current, 16);
  484. debug("ACPI: * SPCR\n");
  485. spcr = (struct acpi_spcr *)current;
  486. acpi_create_spcr(spcr);
  487. current += spcr->header.length;
  488. acpi_add_table(rsdp, spcr);
  489. current = ALIGN(current, 16);
  490. debug("current = %x\n", current);
  491. acpi_rsdp_addr = (unsigned long)rsdp;
  492. debug("ACPI: done\n");
  493. return current;
  494. }
  495. ulong acpi_get_rsdp_addr(void)
  496. {
  497. return acpi_rsdp_addr;
  498. }