acpi_table.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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. #define LOG_CATEGORY LOGC_ACPI
  9. #include <common.h>
  10. #include <bloblist.h>
  11. #include <cpu.h>
  12. #include <dm.h>
  13. #include <log.h>
  14. #include <dm/uclass-internal.h>
  15. #include <mapmem.h>
  16. #include <serial.h>
  17. #include <version.h>
  18. #include <acpi/acpigen.h>
  19. #include <acpi/acpi_device.h>
  20. #include <acpi/acpi_table.h>
  21. #include <asm/acpi/global_nvs.h>
  22. #include <asm/ioapic.h>
  23. #include <asm/lapic.h>
  24. #include <asm/mpspec.h>
  25. #include <asm/tables.h>
  26. #include <asm/arch/global_nvs.h>
  27. #include <dm/acpi.h>
  28. #include <linux/err.h>
  29. /*
  30. * IASL compiles the dsdt entries and writes the hex values
  31. * to a C array AmlCode[] (see dsdt.c).
  32. */
  33. extern const unsigned char AmlCode[];
  34. /* ACPI RSDP address to be used in boot parameters */
  35. static ulong acpi_rsdp_addr;
  36. static void acpi_create_facs(struct acpi_facs *facs)
  37. {
  38. memset((void *)facs, 0, sizeof(struct acpi_facs));
  39. memcpy(facs->signature, "FACS", 4);
  40. facs->length = sizeof(struct acpi_facs);
  41. facs->hardware_signature = 0;
  42. facs->firmware_waking_vector = 0;
  43. facs->global_lock = 0;
  44. facs->flags = 0;
  45. facs->x_firmware_waking_vector_l = 0;
  46. facs->x_firmware_waking_vector_h = 0;
  47. facs->version = 1;
  48. }
  49. static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
  50. u8 cpu, u8 apic)
  51. {
  52. lapic->type = ACPI_APIC_LAPIC;
  53. lapic->length = sizeof(struct acpi_madt_lapic);
  54. lapic->flags = LOCAL_APIC_FLAG_ENABLED;
  55. lapic->processor_id = cpu;
  56. lapic->apic_id = apic;
  57. return lapic->length;
  58. }
  59. int acpi_create_madt_lapics(u32 current)
  60. {
  61. struct udevice *dev;
  62. int total_length = 0;
  63. int cpu_num = 0;
  64. for (uclass_find_first_device(UCLASS_CPU, &dev);
  65. dev;
  66. uclass_find_next_device(&dev)) {
  67. struct cpu_plat *plat = dev_get_parent_plat(dev);
  68. int length;
  69. length = acpi_create_madt_lapic(
  70. (struct acpi_madt_lapic *)current, cpu_num++,
  71. plat->cpu_id);
  72. current += length;
  73. total_length += length;
  74. }
  75. return total_length;
  76. }
  77. int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
  78. u32 addr, u32 gsi_base)
  79. {
  80. ioapic->type = ACPI_APIC_IOAPIC;
  81. ioapic->length = sizeof(struct acpi_madt_ioapic);
  82. ioapic->reserved = 0x00;
  83. ioapic->gsi_base = gsi_base;
  84. ioapic->ioapic_id = id;
  85. ioapic->ioapic_addr = addr;
  86. return ioapic->length;
  87. }
  88. int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
  89. u8 bus, u8 source, u32 gsirq, u16 flags)
  90. {
  91. irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
  92. irqoverride->length = sizeof(struct acpi_madt_irqoverride);
  93. irqoverride->bus = bus;
  94. irqoverride->source = source;
  95. irqoverride->gsirq = gsirq;
  96. irqoverride->flags = flags;
  97. return irqoverride->length;
  98. }
  99. int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
  100. u8 cpu, u16 flags, u8 lint)
  101. {
  102. lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
  103. lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
  104. lapic_nmi->flags = flags;
  105. lapic_nmi->processor_id = cpu;
  106. lapic_nmi->lint = lint;
  107. return lapic_nmi->length;
  108. }
  109. static int acpi_create_madt_irq_overrides(u32 current)
  110. {
  111. struct acpi_madt_irqoverride *irqovr;
  112. u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH;
  113. int length = 0;
  114. irqovr = (void *)current;
  115. length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0);
  116. irqovr = (void *)(current + length);
  117. length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags);
  118. return length;
  119. }
  120. __weak u32 acpi_fill_madt(u32 current)
  121. {
  122. current += acpi_create_madt_lapics(current);
  123. current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current,
  124. io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0);
  125. current += acpi_create_madt_irq_overrides(current);
  126. return current;
  127. }
  128. static void acpi_create_madt(struct acpi_madt *madt)
  129. {
  130. struct acpi_table_header *header = &(madt->header);
  131. u32 current = (u32)madt + sizeof(struct acpi_madt);
  132. memset((void *)madt, 0, sizeof(struct acpi_madt));
  133. /* Fill out header fields */
  134. acpi_fill_header(header, "APIC");
  135. header->length = sizeof(struct acpi_madt);
  136. header->revision = ACPI_MADT_REV_ACPI_3_0;
  137. madt->lapic_addr = LAPIC_DEFAULT_BASE;
  138. madt->flags = ACPI_MADT_PCAT_COMPAT;
  139. current = acpi_fill_madt(current);
  140. /* (Re)calculate length and checksum */
  141. header->length = current - (u32)madt;
  142. header->checksum = table_compute_checksum((void *)madt, header->length);
  143. }
  144. int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, u32 base,
  145. u16 seg_nr, u8 start, u8 end)
  146. {
  147. memset(mmconfig, 0, sizeof(*mmconfig));
  148. mmconfig->base_address_l = base;
  149. mmconfig->base_address_h = 0;
  150. mmconfig->pci_segment_group_number = seg_nr;
  151. mmconfig->start_bus_number = start;
  152. mmconfig->end_bus_number = end;
  153. return sizeof(struct acpi_mcfg_mmconfig);
  154. }
  155. __weak u32 acpi_fill_mcfg(u32 current)
  156. {
  157. current += acpi_create_mcfg_mmconfig
  158. ((struct acpi_mcfg_mmconfig *)current,
  159. CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
  160. return current;
  161. }
  162. /* MCFG is defined in the PCI Firmware Specification 3.0 */
  163. static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
  164. {
  165. struct acpi_table_header *header = &(mcfg->header);
  166. u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
  167. memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
  168. /* Fill out header fields */
  169. acpi_fill_header(header, "MCFG");
  170. header->length = sizeof(struct acpi_mcfg);
  171. header->revision = 1;
  172. current = acpi_fill_mcfg(current);
  173. /* (Re)calculate length and checksum */
  174. header->length = current - (u32)mcfg;
  175. header->checksum = table_compute_checksum((void *)mcfg, header->length);
  176. }
  177. /**
  178. * acpi_create_tcpa() - Create a TCPA table
  179. *
  180. * @tcpa: Pointer to place to put table
  181. *
  182. * Trusted Computing Platform Alliance Capabilities Table
  183. * TCPA PC Specific Implementation SpecificationTCPA is defined in the PCI
  184. * Firmware Specification 3.0
  185. */
  186. static int acpi_create_tcpa(struct acpi_tcpa *tcpa)
  187. {
  188. struct acpi_table_header *header = &tcpa->header;
  189. u32 current = (u32)tcpa + sizeof(struct acpi_tcpa);
  190. int size = 0x10000; /* Use this as the default size */
  191. void *log;
  192. int ret;
  193. if (!CONFIG_IS_ENABLED(BLOBLIST))
  194. return -ENXIO;
  195. memset(tcpa, '\0', sizeof(struct acpi_tcpa));
  196. /* Fill out header fields */
  197. acpi_fill_header(header, "TCPA");
  198. header->length = sizeof(struct acpi_tcpa);
  199. header->revision = 1;
  200. ret = bloblist_ensure_size_ret(BLOBLISTT_TCPA_LOG, &size, &log);
  201. if (ret)
  202. return log_msg_ret("blob", ret);
  203. tcpa->platform_class = 0;
  204. tcpa->laml = size;
  205. tcpa->lasa = (ulong)log;
  206. /* (Re)calculate length and checksum */
  207. header->length = current - (u32)tcpa;
  208. header->checksum = table_compute_checksum((void *)tcpa, header->length);
  209. return 0;
  210. }
  211. static int get_tpm2_log(void **ptrp, int *sizep)
  212. {
  213. const int tpm2_default_log_len = 0x10000;
  214. int size;
  215. int ret;
  216. *sizep = 0;
  217. size = tpm2_default_log_len;
  218. ret = bloblist_ensure_size_ret(BLOBLISTT_TPM2_TCG_LOG, &size, ptrp);
  219. if (ret)
  220. return log_msg_ret("blob", ret);
  221. *sizep = size;
  222. return 0;
  223. }
  224. static int acpi_create_tpm2(struct acpi_tpm2 *tpm2)
  225. {
  226. struct acpi_table_header *header = &tpm2->header;
  227. int tpm2_log_len;
  228. void *lasa;
  229. int ret;
  230. memset((void *)tpm2, 0, sizeof(struct acpi_tpm2));
  231. /*
  232. * Some payloads like SeaBIOS depend on log area to use TPM2.
  233. * Get the memory size and address of TPM2 log area or initialize it.
  234. */
  235. ret = get_tpm2_log(&lasa, &tpm2_log_len);
  236. if (ret)
  237. return ret;
  238. /* Fill out header fields. */
  239. acpi_fill_header(header, "TPM2");
  240. memcpy(header->aslc_id, ASLC_ID, 4);
  241. header->length = sizeof(struct acpi_tpm2);
  242. header->revision = acpi_get_table_revision(ACPITAB_TPM2);
  243. /* Hard to detect for coreboot. Just set it to 0 */
  244. tpm2->platform_class = 0;
  245. /* Must be set to 0 for FIFO-interface support */
  246. tpm2->control_area = 0;
  247. tpm2->start_method = 6;
  248. memset(tpm2->msp, 0, sizeof(tpm2->msp));
  249. /* Fill the log area size and start address fields. */
  250. tpm2->laml = tpm2_log_len;
  251. tpm2->lasa = (uintptr_t)lasa;
  252. /* Calculate checksum. */
  253. header->checksum = table_compute_checksum((void *)tpm2, header->length);
  254. return 0;
  255. }
  256. __weak u32 acpi_fill_csrt(u32 current)
  257. {
  258. return 0;
  259. }
  260. static int acpi_create_csrt(struct acpi_csrt *csrt)
  261. {
  262. struct acpi_table_header *header = &(csrt->header);
  263. u32 current = (u32)csrt + sizeof(struct acpi_csrt);
  264. uint ptr;
  265. memset((void *)csrt, 0, sizeof(struct acpi_csrt));
  266. /* Fill out header fields */
  267. acpi_fill_header(header, "CSRT");
  268. header->length = sizeof(struct acpi_csrt);
  269. header->revision = 0;
  270. ptr = acpi_fill_csrt(current);
  271. if (!ptr)
  272. return -ENOENT;
  273. current = ptr;
  274. /* (Re)calculate length and checksum */
  275. header->length = current - (u32)csrt;
  276. header->checksum = table_compute_checksum((void *)csrt, header->length);
  277. return 0;
  278. }
  279. static void acpi_create_spcr(struct acpi_spcr *spcr)
  280. {
  281. struct acpi_table_header *header = &(spcr->header);
  282. struct serial_device_info serial_info = {0};
  283. ulong serial_address, serial_offset;
  284. struct udevice *dev;
  285. uint serial_config;
  286. uint serial_width;
  287. int access_size;
  288. int space_id;
  289. int ret = -ENODEV;
  290. memset((void *)spcr, 0, sizeof(struct acpi_spcr));
  291. /* Fill out header fields */
  292. acpi_fill_header(header, "SPCR");
  293. header->length = sizeof(struct acpi_spcr);
  294. header->revision = 2;
  295. /* Read the device once, here. It is reused below */
  296. dev = gd->cur_serial_dev;
  297. if (dev)
  298. ret = serial_getinfo(dev, &serial_info);
  299. if (ret)
  300. serial_info.type = SERIAL_CHIP_UNKNOWN;
  301. /* Encode chip type */
  302. switch (serial_info.type) {
  303. case SERIAL_CHIP_16550_COMPATIBLE:
  304. spcr->interface_type = ACPI_DBG2_16550_COMPATIBLE;
  305. break;
  306. case SERIAL_CHIP_UNKNOWN:
  307. default:
  308. spcr->interface_type = ACPI_DBG2_UNKNOWN;
  309. break;
  310. }
  311. /* Encode address space */
  312. switch (serial_info.addr_space) {
  313. case SERIAL_ADDRESS_SPACE_MEMORY:
  314. space_id = ACPI_ADDRESS_SPACE_MEMORY;
  315. break;
  316. case SERIAL_ADDRESS_SPACE_IO:
  317. default:
  318. space_id = ACPI_ADDRESS_SPACE_IO;
  319. break;
  320. }
  321. serial_width = serial_info.reg_width * 8;
  322. serial_offset = serial_info.reg_offset << serial_info.reg_shift;
  323. serial_address = serial_info.addr + serial_offset;
  324. /* Encode register access size */
  325. switch (serial_info.reg_shift) {
  326. case 0:
  327. access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
  328. break;
  329. case 1:
  330. access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
  331. break;
  332. case 2:
  333. access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
  334. break;
  335. case 3:
  336. access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
  337. break;
  338. default:
  339. access_size = ACPI_ACCESS_SIZE_UNDEFINED;
  340. break;
  341. }
  342. debug("UART type %u @ %lx\n", spcr->interface_type, serial_address);
  343. /* Fill GAS */
  344. spcr->serial_port.space_id = space_id;
  345. spcr->serial_port.bit_width = serial_width;
  346. spcr->serial_port.bit_offset = 0;
  347. spcr->serial_port.access_size = access_size;
  348. spcr->serial_port.addrl = lower_32_bits(serial_address);
  349. spcr->serial_port.addrh = upper_32_bits(serial_address);
  350. /* Encode baud rate */
  351. switch (serial_info.baudrate) {
  352. case 9600:
  353. spcr->baud_rate = 3;
  354. break;
  355. case 19200:
  356. spcr->baud_rate = 4;
  357. break;
  358. case 57600:
  359. spcr->baud_rate = 6;
  360. break;
  361. case 115200:
  362. spcr->baud_rate = 7;
  363. break;
  364. default:
  365. spcr->baud_rate = 0;
  366. break;
  367. }
  368. serial_config = SERIAL_DEFAULT_CONFIG;
  369. if (dev)
  370. ret = serial_getconfig(dev, &serial_config);
  371. spcr->parity = SERIAL_GET_PARITY(serial_config);
  372. spcr->stop_bits = SERIAL_GET_STOP(serial_config);
  373. /* No PCI devices for now */
  374. spcr->pci_device_id = 0xffff;
  375. spcr->pci_vendor_id = 0xffff;
  376. /*
  377. * SPCR has no clue if the UART base clock speed is different
  378. * to the default one. However, the SPCR 1.04 defines baud rate
  379. * 0 as a preconfigured state of UART and OS is supposed not
  380. * to touch the configuration of the serial device.
  381. */
  382. if (serial_info.clock != SERIAL_DEFAULT_CLOCK)
  383. spcr->baud_rate = 0;
  384. /* Fix checksum */
  385. header->checksum = table_compute_checksum((void *)spcr, header->length);
  386. }
  387. static int acpi_create_ssdt(struct acpi_ctx *ctx,
  388. struct acpi_table_header *ssdt,
  389. const char *oem_table_id)
  390. {
  391. memset((void *)ssdt, '\0', sizeof(struct acpi_table_header));
  392. acpi_fill_header(ssdt, "SSDT");
  393. ssdt->revision = acpi_get_table_revision(ACPITAB_SSDT);
  394. ssdt->aslc_revision = 1;
  395. ssdt->length = sizeof(struct acpi_table_header);
  396. acpi_inc(ctx, sizeof(struct acpi_table_header));
  397. acpi_fill_ssdt(ctx);
  398. /* (Re)calculate length and checksum */
  399. ssdt->length = ctx->current - (void *)ssdt;
  400. ssdt->checksum = table_compute_checksum((void *)ssdt, ssdt->length);
  401. log_debug("SSDT at %p, length %x\n", ssdt, ssdt->length);
  402. /* Drop the table if it is empty */
  403. if (ssdt->length == sizeof(struct acpi_table_header)) {
  404. ctx->current = ssdt;
  405. return -ENOENT;
  406. }
  407. acpi_align(ctx);
  408. return 0;
  409. }
  410. /*
  411. * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
  412. */
  413. ulong write_acpi_tables(ulong start_addr)
  414. {
  415. const int thl = sizeof(struct acpi_table_header);
  416. struct acpi_ctx *ctx;
  417. struct acpi_facs *facs;
  418. struct acpi_table_header *dsdt;
  419. struct acpi_fadt *fadt;
  420. struct acpi_table_header *ssdt;
  421. struct acpi_mcfg *mcfg;
  422. struct acpi_tcpa *tcpa;
  423. struct acpi_madt *madt;
  424. struct acpi_csrt *csrt;
  425. struct acpi_spcr *spcr;
  426. void *start;
  427. int aml_len;
  428. ulong addr;
  429. int ret;
  430. int i;
  431. ctx = calloc(1, sizeof(*ctx));
  432. if (!ctx)
  433. return log_msg_ret("mem", -ENOMEM);
  434. gd->acpi_ctx = ctx;
  435. start = map_sysmem(start_addr, 0);
  436. debug("ACPI: Writing ACPI tables at %lx\n", start_addr);
  437. acpi_reset_items();
  438. acpi_setup_base_tables(ctx, start);
  439. debug("ACPI: * FACS\n");
  440. facs = ctx->current;
  441. acpi_inc_align(ctx, sizeof(struct acpi_facs));
  442. acpi_create_facs(facs);
  443. debug("ACPI: * DSDT\n");
  444. dsdt = ctx->current;
  445. /* Put the table header first */
  446. memcpy(dsdt, &AmlCode, thl);
  447. acpi_inc(ctx, thl);
  448. log_debug("DSDT starts at %p, hdr ends at %p\n", dsdt, ctx->current);
  449. /* If the table is not empty, allow devices to inject things */
  450. aml_len = dsdt->length - thl;
  451. if (aml_len) {
  452. void *base = ctx->current;
  453. acpi_inject_dsdt(ctx);
  454. log_debug("Added %x bytes from inject_dsdt, now at %p\n",
  455. ctx->current - base, ctx->current);
  456. log_debug("Copy AML code size %x to %p\n", aml_len,
  457. ctx->current);
  458. memcpy(ctx->current, AmlCode + thl, aml_len);
  459. acpi_inc(ctx, aml_len);
  460. }
  461. dsdt->length = ctx->current - (void *)dsdt;
  462. acpi_align(ctx);
  463. log_debug("Updated DSDT length to %x, total %x\n", dsdt->length,
  464. ctx->current - (void *)dsdt);
  465. if (!IS_ENABLED(CONFIG_ACPI_GNVS_EXTERNAL)) {
  466. /* Pack GNVS into the ACPI table area */
  467. for (i = 0; i < dsdt->length; i++) {
  468. u32 *gnvs = (u32 *)((u32)dsdt + i);
  469. if (*gnvs == ACPI_GNVS_ADDR) {
  470. *gnvs = map_to_sysmem(ctx->current);
  471. debug("Fix up global NVS in DSDT to %#08x\n",
  472. *gnvs);
  473. break;
  474. }
  475. }
  476. /*
  477. * Fill in platform-specific global NVS variables. If this fails
  478. * we cannot return the error but this should only happen while
  479. * debugging.
  480. */
  481. addr = acpi_create_gnvs(ctx->current);
  482. if (IS_ERR_VALUE(addr))
  483. printf("Error: Gailed to create GNVS\n");
  484. acpi_inc_align(ctx, sizeof(struct acpi_global_nvs));
  485. }
  486. /*
  487. * Recalculate the length and update the DSDT checksum since we patched
  488. * the GNVS address. Set the checksum to zero since it is part of the
  489. * region being checksummed.
  490. */
  491. dsdt->checksum = 0;
  492. dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
  493. /*
  494. * Fill in platform-specific global NVS variables. If this fails we
  495. * cannot return the error but this should only happen while debugging.
  496. */
  497. addr = acpi_create_gnvs(ctx->current);
  498. if (IS_ERR_VALUE(addr))
  499. printf("Error: Failed to create GNVS\n");
  500. acpi_inc_align(ctx, sizeof(struct acpi_global_nvs));
  501. debug("ACPI: * FADT\n");
  502. fadt = ctx->current;
  503. acpi_inc_align(ctx, sizeof(struct acpi_fadt));
  504. acpi_create_fadt(fadt, facs, dsdt);
  505. acpi_add_table(ctx, fadt);
  506. debug("ACPI: * SSDT\n");
  507. ssdt = (struct acpi_table_header *)ctx->current;
  508. if (!acpi_create_ssdt(ctx, ssdt, OEM_TABLE_ID))
  509. acpi_add_table(ctx, ssdt);
  510. debug("ACPI: * MCFG\n");
  511. mcfg = ctx->current;
  512. acpi_create_mcfg(mcfg);
  513. acpi_inc_align(ctx, mcfg->header.length);
  514. acpi_add_table(ctx, mcfg);
  515. if (IS_ENABLED(CONFIG_TPM_V2)) {
  516. struct acpi_tpm2 *tpm2;
  517. debug("ACPI: * TPM2\n");
  518. tpm2 = (struct acpi_tpm2 *)ctx->current;
  519. ret = acpi_create_tpm2(tpm2);
  520. if (!ret) {
  521. acpi_inc_align(ctx, tpm2->header.length);
  522. acpi_add_table(ctx, tpm2);
  523. } else {
  524. log_warning("TPM2 table creation failed\n");
  525. }
  526. }
  527. debug("ACPI: * MADT\n");
  528. madt = ctx->current;
  529. acpi_create_madt(madt);
  530. acpi_inc_align(ctx, madt->header.length);
  531. acpi_add_table(ctx, madt);
  532. if (IS_ENABLED(CONFIG_TPM_V1)) {
  533. debug("ACPI: * TCPA\n");
  534. tcpa = (struct acpi_tcpa *)ctx->current;
  535. ret = acpi_create_tcpa(tcpa);
  536. if (ret) {
  537. log_warning("Failed to create TCPA table (err=%d)\n",
  538. ret);
  539. } else {
  540. acpi_inc_align(ctx, tcpa->header.length);
  541. acpi_add_table(ctx, tcpa);
  542. }
  543. }
  544. debug("ACPI: * CSRT\n");
  545. csrt = ctx->current;
  546. if (!acpi_create_csrt(csrt)) {
  547. acpi_inc_align(ctx, csrt->header.length);
  548. acpi_add_table(ctx, csrt);
  549. }
  550. debug("ACPI: * SPCR\n");
  551. spcr = ctx->current;
  552. acpi_create_spcr(spcr);
  553. acpi_inc_align(ctx, spcr->header.length);
  554. acpi_add_table(ctx, spcr);
  555. acpi_write_dev_tables(ctx);
  556. addr = map_to_sysmem(ctx->current);
  557. debug("current = %lx\n", addr);
  558. acpi_rsdp_addr = (unsigned long)ctx->rsdp;
  559. debug("ACPI: done\n");
  560. return addr;
  561. }
  562. ulong acpi_get_rsdp_addr(void)
  563. {
  564. return acpi_rsdp_addr;
  565. }
  566. /**
  567. * acpi_write_hpet() - Write out a HPET table
  568. *
  569. * Write out the table for High-Precision Event Timers
  570. *
  571. * @hpet: Place to put HPET table
  572. */
  573. static int acpi_create_hpet(struct acpi_hpet *hpet)
  574. {
  575. struct acpi_table_header *header = &hpet->header;
  576. struct acpi_gen_regaddr *addr = &hpet->addr;
  577. /*
  578. * See IA-PC HPET (High Precision Event Timers) Specification v1.0a
  579. * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/software-developers-hpet-spec-1-0a.pdf
  580. */
  581. memset((void *)hpet, '\0', sizeof(struct acpi_hpet));
  582. /* Fill out header fields. */
  583. acpi_fill_header(header, "HPET");
  584. header->aslc_revision = ASL_REVISION;
  585. header->length = sizeof(struct acpi_hpet);
  586. header->revision = acpi_get_table_revision(ACPITAB_HPET);
  587. /* Fill out HPET address */
  588. addr->space_id = 0; /* Memory */
  589. addr->bit_width = 64;
  590. addr->bit_offset = 0;
  591. addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff;
  592. addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32;
  593. hpet->id = *(u32 *)CONFIG_HPET_ADDRESS;
  594. hpet->number = 0;
  595. hpet->min_tick = 0; /* HPET_MIN_TICKS */
  596. header->checksum = table_compute_checksum(hpet,
  597. sizeof(struct acpi_hpet));
  598. return 0;
  599. }
  600. int acpi_write_hpet(struct acpi_ctx *ctx)
  601. {
  602. struct acpi_hpet *hpet;
  603. int ret;
  604. log_debug("ACPI: * HPET\n");
  605. hpet = ctx->current;
  606. acpi_inc_align(ctx, sizeof(struct acpi_hpet));
  607. acpi_create_hpet(hpet);
  608. ret = acpi_add_table(ctx, hpet);
  609. if (ret)
  610. return log_msg_ret("add", ret);
  611. return 0;
  612. }
  613. int acpi_write_dbg2_pci_uart(struct acpi_ctx *ctx, struct udevice *dev,
  614. uint access_size)
  615. {
  616. struct acpi_dbg2_header *dbg2 = ctx->current;
  617. char path[ACPI_PATH_MAX];
  618. struct acpi_gen_regaddr address;
  619. phys_addr_t addr;
  620. int ret;
  621. if (!device_active(dev)) {
  622. log_info("Device not enabled\n");
  623. return -EACCES;
  624. }
  625. /*
  626. * PCI devices don't remember their resource allocation information in
  627. * U-Boot at present. We assume that MMIO is used for the UART and that
  628. * the address space is 32 bytes: ns16550 uses 8 registers of up to
  629. * 32-bits each. This is only for debugging so it is not a big deal.
  630. */
  631. addr = dm_pci_read_bar32(dev, 0);
  632. log_debug("UART addr %lx\n", (ulong)addr);
  633. memset(&address, '\0', sizeof(address));
  634. address.space_id = ACPI_ADDRESS_SPACE_MEMORY;
  635. address.addrl = (uint32_t)addr;
  636. address.addrh = (uint32_t)((addr >> 32) & 0xffffffff);
  637. address.access_size = access_size;
  638. ret = acpi_device_path(dev, path, sizeof(path));
  639. if (ret)
  640. return log_msg_ret("path", ret);
  641. acpi_create_dbg2(dbg2, ACPI_DBG2_SERIAL_PORT,
  642. ACPI_DBG2_16550_COMPATIBLE, &address, 0x1000, path);
  643. acpi_inc_align(ctx, dbg2->header.length);
  644. acpi_add_table(ctx, dbg2);
  645. return 0;
  646. }
  647. void acpi_fadt_common(struct acpi_fadt *fadt, struct acpi_facs *facs,
  648. void *dsdt)
  649. {
  650. struct acpi_table_header *header = &fadt->header;
  651. memset((void *)fadt, '\0', sizeof(struct acpi_fadt));
  652. acpi_fill_header(header, "FACP");
  653. header->length = sizeof(struct acpi_fadt);
  654. header->revision = 4;
  655. memcpy(header->oem_id, OEM_ID, 6);
  656. memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
  657. memcpy(header->aslc_id, ASLC_ID, 4);
  658. header->aslc_revision = 1;
  659. fadt->firmware_ctrl = (unsigned long)facs;
  660. fadt->dsdt = (unsigned long)dsdt;
  661. fadt->x_firmware_ctl_l = (unsigned long)facs;
  662. fadt->x_firmware_ctl_h = 0;
  663. fadt->x_dsdt_l = (unsigned long)dsdt;
  664. fadt->x_dsdt_h = 0;
  665. fadt->preferred_pm_profile = ACPI_PM_MOBILE;
  666. /* Use ACPI 3.0 revision */
  667. fadt->header.revision = 4;
  668. }
  669. void acpi_create_dmar_drhd(struct acpi_ctx *ctx, uint flags, uint segment,
  670. u64 bar)
  671. {
  672. struct dmar_entry *drhd = ctx->current;
  673. memset(drhd, '\0', sizeof(*drhd));
  674. drhd->type = DMAR_DRHD;
  675. drhd->length = sizeof(*drhd); /* will be fixed up later */
  676. drhd->flags = flags;
  677. drhd->segment = segment;
  678. drhd->bar = bar;
  679. acpi_inc(ctx, drhd->length);
  680. }
  681. void acpi_create_dmar_rmrr(struct acpi_ctx *ctx, uint segment, u64 bar,
  682. u64 limit)
  683. {
  684. struct dmar_rmrr_entry *rmrr = ctx->current;
  685. memset(rmrr, '\0', sizeof(*rmrr));
  686. rmrr->type = DMAR_RMRR;
  687. rmrr->length = sizeof(*rmrr); /* will be fixed up later */
  688. rmrr->segment = segment;
  689. rmrr->bar = bar;
  690. rmrr->limit = limit;
  691. acpi_inc(ctx, rmrr->length);
  692. }
  693. void acpi_dmar_drhd_fixup(struct acpi_ctx *ctx, void *base)
  694. {
  695. struct dmar_entry *drhd = base;
  696. drhd->length = ctx->current - base;
  697. }
  698. void acpi_dmar_rmrr_fixup(struct acpi_ctx *ctx, void *base)
  699. {
  700. struct dmar_rmrr_entry *rmrr = base;
  701. rmrr->length = ctx->current - base;
  702. }
  703. static int acpi_create_dmar_ds(struct acpi_ctx *ctx, enum dev_scope_type type,
  704. uint enumeration_id, pci_dev_t bdf)
  705. {
  706. /* we don't support longer paths yet */
  707. const size_t dev_scope_length = sizeof(struct dev_scope) + 2;
  708. struct dev_scope *ds = ctx->current;
  709. memset(ds, '\0', dev_scope_length);
  710. ds->type = type;
  711. ds->length = dev_scope_length;
  712. ds->enumeration = enumeration_id;
  713. ds->start_bus = PCI_BUS(bdf);
  714. ds->path[0].dev = PCI_DEV(bdf);
  715. ds->path[0].fn = PCI_FUNC(bdf);
  716. return ds->length;
  717. }
  718. int acpi_create_dmar_ds_pci_br(struct acpi_ctx *ctx, pci_dev_t bdf)
  719. {
  720. return acpi_create_dmar_ds(ctx, SCOPE_PCI_SUB, 0, bdf);
  721. }
  722. int acpi_create_dmar_ds_pci(struct acpi_ctx *ctx, pci_dev_t bdf)
  723. {
  724. return acpi_create_dmar_ds(ctx, SCOPE_PCI_ENDPOINT, 0, bdf);
  725. }
  726. int acpi_create_dmar_ds_ioapic(struct acpi_ctx *ctx, uint enumeration_id,
  727. pci_dev_t bdf)
  728. {
  729. return acpi_create_dmar_ds(ctx, SCOPE_IOAPIC, enumeration_id, bdf);
  730. }
  731. int acpi_create_dmar_ds_msi_hpet(struct acpi_ctx *ctx, uint enumeration_id,
  732. pci_dev_t bdf)
  733. {
  734. return acpi_create_dmar_ds(ctx, SCOPE_MSI_HPET, enumeration_id, bdf);
  735. }