Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Based on acpi.c from coreboot |
| 4 | * |
| 5 | * Copyright (C) 2015, Saket Sinha <saket.sinha89@gmail.com> |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 6 | * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com> |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 7 | */ |
| 8 | |
Simon Glass | 2326a8b | 2020-09-22 12:45:34 -0600 | [diff] [blame] | 9 | #define LOG_CATEGORY LOGC_ACPI |
| 10 | |
Simon Glass | 272a703 | 2020-09-22 12:45:32 -0600 | [diff] [blame] | 11 | #include <bloblist.h> |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 12 | #include <cpu.h> |
| 13 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 15 | #include <dm/uclass-internal.h> |
Simon Glass | 0e11384 | 2020-04-26 09:19:47 -0600 | [diff] [blame] | 16 | #include <mapmem.h> |
Andy Shevchenko | 4ca48c9 | 2018-11-20 23:52:38 +0200 | [diff] [blame] | 17 | #include <serial.h> |
Simon Glass | f0a8d68 | 2020-07-07 13:12:07 -0600 | [diff] [blame] | 18 | #include <acpi/acpigen.h> |
Simon Glass | 9597189 | 2020-09-22 12:45:10 -0600 | [diff] [blame] | 19 | #include <acpi/acpi_device.h> |
Simon Glass | 858fed1 | 2020-04-08 16:57:36 -0600 | [diff] [blame] | 20 | #include <acpi/acpi_table.h> |
Bin Meng | d9050c6 | 2016-06-17 02:13:16 -0700 | [diff] [blame] | 21 | #include <asm/acpi/global_nvs.h> |
Andy Shevchenko | 13a5d87 | 2017-07-21 22:32:04 +0300 | [diff] [blame] | 22 | #include <asm/ioapic.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 23 | #include <asm/global_data.h> |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 24 | #include <asm/lapic.h> |
Andy Shevchenko | 13a5d87 | 2017-07-21 22:32:04 +0300 | [diff] [blame] | 25 | #include <asm/mpspec.h> |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 26 | #include <asm/tables.h> |
Bin Meng | d9050c6 | 2016-06-17 02:13:16 -0700 | [diff] [blame] | 27 | #include <asm/arch/global_nvs.h> |
Simon Glass | 0e11384 | 2020-04-26 09:19:47 -0600 | [diff] [blame] | 28 | #include <dm/acpi.h> |
Simon Glass | 9ed41e7 | 2020-07-07 21:32:05 -0600 | [diff] [blame] | 29 | #include <linux/err.h> |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 30 | |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 31 | static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic, |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 32 | u8 cpu, u8 apic) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 33 | { |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 34 | lapic->type = ACPI_APIC_LAPIC; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 35 | lapic->length = sizeof(struct acpi_madt_lapic); |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 36 | lapic->flags = LOCAL_APIC_FLAG_ENABLED; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 37 | lapic->processor_id = cpu; |
| 38 | lapic->apic_id = apic; |
| 39 | |
| 40 | return lapic->length; |
| 41 | } |
| 42 | |
Bin Meng | 3c5234e | 2016-05-07 07:46:30 -0700 | [diff] [blame] | 43 | int acpi_create_madt_lapics(u32 current) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 44 | { |
| 45 | struct udevice *dev; |
George McCollister | 5a49f87 | 2016-06-07 13:40:18 -0500 | [diff] [blame] | 46 | int total_length = 0; |
Simon Glass | fcae547 | 2020-09-22 12:45:31 -0600 | [diff] [blame] | 47 | int cpu_num = 0; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 48 | |
| 49 | for (uclass_find_first_device(UCLASS_CPU, &dev); |
| 50 | dev; |
| 51 | uclass_find_next_device(&dev)) { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 52 | struct cpu_plat *plat = dev_get_parent_plat(dev); |
Simon Glass | fcae547 | 2020-09-22 12:45:31 -0600 | [diff] [blame] | 53 | int length; |
| 54 | |
| 55 | length = acpi_create_madt_lapic( |
| 56 | (struct acpi_madt_lapic *)current, cpu_num++, |
| 57 | plat->cpu_id); |
Bin Meng | 3c5234e | 2016-05-07 07:46:30 -0700 | [diff] [blame] | 58 | current += length; |
George McCollister | 5a49f87 | 2016-06-07 13:40:18 -0500 | [diff] [blame] | 59 | total_length += length; |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 60 | } |
| 61 | |
George McCollister | 5a49f87 | 2016-06-07 13:40:18 -0500 | [diff] [blame] | 62 | return total_length; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 63 | } |
| 64 | |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 65 | int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id, |
| 66 | u32 addr, u32 gsi_base) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 67 | { |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 68 | ioapic->type = ACPI_APIC_IOAPIC; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 69 | ioapic->length = sizeof(struct acpi_madt_ioapic); |
| 70 | ioapic->reserved = 0x00; |
| 71 | ioapic->gsi_base = gsi_base; |
| 72 | ioapic->ioapic_id = id; |
| 73 | ioapic->ioapic_addr = addr; |
| 74 | |
| 75 | return ioapic->length; |
| 76 | } |
| 77 | |
| 78 | int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride, |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 79 | u8 bus, u8 source, u32 gsirq, u16 flags) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 80 | { |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 81 | irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 82 | irqoverride->length = sizeof(struct acpi_madt_irqoverride); |
| 83 | irqoverride->bus = bus; |
| 84 | irqoverride->source = source; |
| 85 | irqoverride->gsirq = gsirq; |
| 86 | irqoverride->flags = flags; |
| 87 | |
| 88 | return irqoverride->length; |
| 89 | } |
| 90 | |
| 91 | int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi, |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 92 | u8 cpu, u16 flags, u8 lint) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 93 | { |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 94 | lapic_nmi->type = ACPI_APIC_LAPIC_NMI; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 95 | lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi); |
| 96 | lapic_nmi->flags = flags; |
| 97 | lapic_nmi->processor_id = cpu; |
| 98 | lapic_nmi->lint = lint; |
| 99 | |
| 100 | return lapic_nmi->length; |
| 101 | } |
| 102 | |
Andy Shevchenko | 13a5d87 | 2017-07-21 22:32:04 +0300 | [diff] [blame] | 103 | static int acpi_create_madt_irq_overrides(u32 current) |
| 104 | { |
| 105 | struct acpi_madt_irqoverride *irqovr; |
| 106 | u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH; |
| 107 | int length = 0; |
| 108 | |
| 109 | irqovr = (void *)current; |
| 110 | length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0); |
| 111 | |
| 112 | irqovr = (void *)(current + length); |
| 113 | length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags); |
| 114 | |
| 115 | return length; |
| 116 | } |
| 117 | |
| 118 | __weak u32 acpi_fill_madt(u32 current) |
| 119 | { |
| 120 | current += acpi_create_madt_lapics(current); |
| 121 | |
| 122 | current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current, |
| 123 | io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0); |
| 124 | |
| 125 | current += acpi_create_madt_irq_overrides(current); |
| 126 | |
| 127 | return current; |
| 128 | } |
| 129 | |
Simon Glass | b0d5fab | 2021-12-01 09:02:58 -0700 | [diff] [blame] | 130 | int acpi_write_madt(struct acpi_ctx *ctx, const struct acpi_writer *entry) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 131 | { |
Simon Glass | b0d5fab | 2021-12-01 09:02:58 -0700 | [diff] [blame] | 132 | struct acpi_table_header *header; |
| 133 | struct acpi_madt *madt; |
| 134 | u32 current; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 135 | |
Simon Glass | b0d5fab | 2021-12-01 09:02:58 -0700 | [diff] [blame] | 136 | madt = ctx->current; |
| 137 | |
| 138 | memset(madt, '\0', sizeof(struct acpi_madt)); |
| 139 | header = &madt->header; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 140 | |
| 141 | /* Fill out header fields */ |
Bin Meng | b063d5f | 2016-05-07 07:46:24 -0700 | [diff] [blame] | 142 | acpi_fill_header(header, "APIC"); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 143 | header->length = sizeof(struct acpi_madt); |
Simon Glass | f3694aa | 2020-07-16 21:22:37 -0600 | [diff] [blame] | 144 | header->revision = ACPI_MADT_REV_ACPI_3_0; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 145 | |
| 146 | madt->lapic_addr = LAPIC_DEFAULT_BASE; |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 147 | madt->flags = ACPI_MADT_PCAT_COMPAT; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 148 | |
Simon Glass | b0d5fab | 2021-12-01 09:02:58 -0700 | [diff] [blame] | 149 | current = (u32)madt + sizeof(struct acpi_madt); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 150 | current = acpi_fill_madt(current); |
| 151 | |
| 152 | /* (Re)calculate length and checksum */ |
Bin Meng | a1ec7db | 2016-05-07 07:46:26 -0700 | [diff] [blame] | 153 | header->length = current - (u32)madt; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 154 | |
| 155 | header->checksum = table_compute_checksum((void *)madt, header->length); |
Simon Glass | b0d5fab | 2021-12-01 09:02:58 -0700 | [diff] [blame] | 156 | acpi_add_table(ctx, madt); |
| 157 | acpi_inc(ctx, madt->header.length); |
| 158 | |
| 159 | return 0; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 160 | } |
Simon Glass | b0d5fab | 2021-12-01 09:02:58 -0700 | [diff] [blame] | 161 | ACPI_WRITER(5x86, NULL, acpi_write_madt, 0); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 162 | |
Simon Glass | 2802628 | 2020-09-22 12:45:33 -0600 | [diff] [blame] | 163 | /** |
| 164 | * acpi_create_tcpa() - Create a TCPA table |
| 165 | * |
Simon Glass | 2802628 | 2020-09-22 12:45:33 -0600 | [diff] [blame] | 166 | * Trusted Computing Platform Alliance Capabilities Table |
| 167 | * TCPA PC Specific Implementation SpecificationTCPA is defined in the PCI |
| 168 | * Firmware Specification 3.0 |
| 169 | */ |
Simon Glass | bb3b608 | 2021-12-01 09:02:59 -0700 | [diff] [blame] | 170 | int acpi_write_tcpa(struct acpi_ctx *ctx, const struct acpi_writer *entry) |
Simon Glass | 2802628 | 2020-09-22 12:45:33 -0600 | [diff] [blame] | 171 | { |
Simon Glass | bb3b608 | 2021-12-01 09:02:59 -0700 | [diff] [blame] | 172 | struct acpi_table_header *header; |
| 173 | struct acpi_tcpa *tcpa; |
| 174 | u32 current; |
Simon Glass | 2802628 | 2020-09-22 12:45:33 -0600 | [diff] [blame] | 175 | int size = 0x10000; /* Use this as the default size */ |
| 176 | void *log; |
| 177 | int ret; |
| 178 | |
Simon Glass | bb3b608 | 2021-12-01 09:02:59 -0700 | [diff] [blame] | 179 | if (!IS_ENABLED(CONFIG_TPM_V1)) |
| 180 | return -ENOENT; |
Simon Glass | 2802628 | 2020-09-22 12:45:33 -0600 | [diff] [blame] | 181 | if (!CONFIG_IS_ENABLED(BLOBLIST)) |
| 182 | return -ENXIO; |
Simon Glass | bb3b608 | 2021-12-01 09:02:59 -0700 | [diff] [blame] | 183 | |
| 184 | tcpa = ctx->current; |
| 185 | header = &tcpa->header; |
Simon Glass | 2802628 | 2020-09-22 12:45:33 -0600 | [diff] [blame] | 186 | memset(tcpa, '\0', sizeof(struct acpi_tcpa)); |
| 187 | |
| 188 | /* Fill out header fields */ |
| 189 | acpi_fill_header(header, "TCPA"); |
| 190 | header->length = sizeof(struct acpi_tcpa); |
| 191 | header->revision = 1; |
| 192 | |
| 193 | ret = bloblist_ensure_size_ret(BLOBLISTT_TCPA_LOG, &size, &log); |
| 194 | if (ret) |
| 195 | return log_msg_ret("blob", ret); |
| 196 | |
| 197 | tcpa->platform_class = 0; |
| 198 | tcpa->laml = size; |
Simon Glass | 919f835 | 2023-12-31 08:25:54 -0700 | [diff] [blame] | 199 | tcpa->lasa = nomap_to_sysmem(log); |
Simon Glass | 2802628 | 2020-09-22 12:45:33 -0600 | [diff] [blame] | 200 | |
| 201 | /* (Re)calculate length and checksum */ |
Simon Glass | bb3b608 | 2021-12-01 09:02:59 -0700 | [diff] [blame] | 202 | current = (u32)tcpa + sizeof(struct acpi_tcpa); |
Simon Glass | 2802628 | 2020-09-22 12:45:33 -0600 | [diff] [blame] | 203 | header->length = current - (u32)tcpa; |
Simon Glass | bb3b608 | 2021-12-01 09:02:59 -0700 | [diff] [blame] | 204 | header->checksum = table_compute_checksum(tcpa, header->length); |
| 205 | |
| 206 | acpi_inc(ctx, tcpa->header.length); |
| 207 | acpi_add_table(ctx, tcpa); |
Simon Glass | 2802628 | 2020-09-22 12:45:33 -0600 | [diff] [blame] | 208 | |
| 209 | return 0; |
| 210 | } |
Simon Glass | bb3b608 | 2021-12-01 09:02:59 -0700 | [diff] [blame] | 211 | ACPI_WRITER(5tcpa, "TCPA", acpi_write_tcpa, 0); |
Simon Glass | 2802628 | 2020-09-22 12:45:33 -0600 | [diff] [blame] | 212 | |
Simon Glass | 272a703 | 2020-09-22 12:45:32 -0600 | [diff] [blame] | 213 | static int get_tpm2_log(void **ptrp, int *sizep) |
| 214 | { |
| 215 | const int tpm2_default_log_len = 0x10000; |
| 216 | int size; |
| 217 | int ret; |
| 218 | |
| 219 | *sizep = 0; |
| 220 | size = tpm2_default_log_len; |
| 221 | ret = bloblist_ensure_size_ret(BLOBLISTT_TPM2_TCG_LOG, &size, ptrp); |
| 222 | if (ret) |
| 223 | return log_msg_ret("blob", ret); |
| 224 | *sizep = size; |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
Simon Glass | c753694 | 2021-12-01 09:02:57 -0700 | [diff] [blame] | 229 | static int acpi_write_tpm2(struct acpi_ctx *ctx, |
| 230 | const struct acpi_writer *entry) |
Simon Glass | 272a703 | 2020-09-22 12:45:32 -0600 | [diff] [blame] | 231 | { |
Simon Glass | c753694 | 2021-12-01 09:02:57 -0700 | [diff] [blame] | 232 | struct acpi_table_header *header; |
| 233 | struct acpi_tpm2 *tpm2; |
Simon Glass | 272a703 | 2020-09-22 12:45:32 -0600 | [diff] [blame] | 234 | int tpm2_log_len; |
| 235 | void *lasa; |
| 236 | int ret; |
| 237 | |
Simon Glass | c753694 | 2021-12-01 09:02:57 -0700 | [diff] [blame] | 238 | if (!IS_ENABLED(CONFIG_TPM_V2)) |
| 239 | return log_msg_ret("none", -ENOENT); |
| 240 | |
| 241 | tpm2 = ctx->current; |
| 242 | header = &tpm2->header; |
| 243 | memset(tpm2, '\0', sizeof(struct acpi_tpm2)); |
Simon Glass | 272a703 | 2020-09-22 12:45:32 -0600 | [diff] [blame] | 244 | |
| 245 | /* |
| 246 | * Some payloads like SeaBIOS depend on log area to use TPM2. |
| 247 | * Get the memory size and address of TPM2 log area or initialize it. |
| 248 | */ |
| 249 | ret = get_tpm2_log(&lasa, &tpm2_log_len); |
| 250 | if (ret) |
Simon Glass | c753694 | 2021-12-01 09:02:57 -0700 | [diff] [blame] | 251 | return log_msg_ret("log", ret); |
Simon Glass | 272a703 | 2020-09-22 12:45:32 -0600 | [diff] [blame] | 252 | |
| 253 | /* Fill out header fields. */ |
| 254 | acpi_fill_header(header, "TPM2"); |
Heinrich Schuchardt | 10de8a8 | 2024-01-21 12:52:48 +0100 | [diff] [blame] | 255 | memcpy(header->creator_id, ASLC_ID, 4); |
Simon Glass | 272a703 | 2020-09-22 12:45:32 -0600 | [diff] [blame] | 256 | |
| 257 | header->length = sizeof(struct acpi_tpm2); |
| 258 | header->revision = acpi_get_table_revision(ACPITAB_TPM2); |
| 259 | |
Simon Glass | c753694 | 2021-12-01 09:02:57 -0700 | [diff] [blame] | 260 | /* Hard to detect for U-Boot. Just set it to 0 */ |
Simon Glass | 272a703 | 2020-09-22 12:45:32 -0600 | [diff] [blame] | 261 | tpm2->platform_class = 0; |
| 262 | |
| 263 | /* Must be set to 0 for FIFO-interface support */ |
| 264 | tpm2->control_area = 0; |
| 265 | tpm2->start_method = 6; |
| 266 | memset(tpm2->msp, 0, sizeof(tpm2->msp)); |
| 267 | |
| 268 | /* Fill the log area size and start address fields. */ |
| 269 | tpm2->laml = tpm2_log_len; |
Simon Glass | 919f835 | 2023-12-31 08:25:54 -0700 | [diff] [blame] | 270 | tpm2->lasa = nomap_to_sysmem(lasa); |
Simon Glass | 272a703 | 2020-09-22 12:45:32 -0600 | [diff] [blame] | 271 | |
| 272 | /* Calculate checksum. */ |
Simon Glass | c753694 | 2021-12-01 09:02:57 -0700 | [diff] [blame] | 273 | header->checksum = table_compute_checksum(tpm2, header->length); |
| 274 | |
| 275 | acpi_inc(ctx, tpm2->header.length); |
| 276 | acpi_add_table(ctx, tpm2); |
Simon Glass | 272a703 | 2020-09-22 12:45:32 -0600 | [diff] [blame] | 277 | |
| 278 | return 0; |
| 279 | } |
Simon Glass | c753694 | 2021-12-01 09:02:57 -0700 | [diff] [blame] | 280 | ACPI_WRITER(5tpm2, "TPM2", acpi_write_tpm2, 0); |
Simon Glass | 272a703 | 2020-09-22 12:45:32 -0600 | [diff] [blame] | 281 | |
Simon Glass | 6e6bc9b | 2021-12-01 09:03:00 -0700 | [diff] [blame] | 282 | int acpi_write_spcr(struct acpi_ctx *ctx, const struct acpi_writer *entry) |
Andy Shevchenko | 607dbd1 | 2019-07-14 19:23:57 +0300 | [diff] [blame] | 283 | { |
Andy Shevchenko | 4ca48c9 | 2018-11-20 23:52:38 +0200 | [diff] [blame] | 284 | struct serial_device_info serial_info = {0}; |
| 285 | ulong serial_address, serial_offset; |
Simon Glass | 6e6bc9b | 2021-12-01 09:03:00 -0700 | [diff] [blame] | 286 | struct acpi_table_header *header; |
| 287 | struct acpi_spcr *spcr; |
Simon Glass | daaff93 | 2018-12-28 14:23:08 -0700 | [diff] [blame] | 288 | struct udevice *dev; |
Andy Shevchenko | 4ca48c9 | 2018-11-20 23:52:38 +0200 | [diff] [blame] | 289 | uint serial_config; |
| 290 | uint serial_width; |
| 291 | int access_size; |
| 292 | int space_id; |
Andy Shevchenko | bf9c8e3 | 2019-02-28 17:19:54 +0200 | [diff] [blame] | 293 | int ret = -ENODEV; |
Andy Shevchenko | 4ca48c9 | 2018-11-20 23:52:38 +0200 | [diff] [blame] | 294 | |
Simon Glass | 6e6bc9b | 2021-12-01 09:03:00 -0700 | [diff] [blame] | 295 | spcr = ctx->current; |
| 296 | header = &spcr->header; |
| 297 | |
| 298 | memset(spcr, '\0', sizeof(struct acpi_spcr)); |
Wolfgang Wallner | 13c23e9 | 2020-09-16 16:57:53 +0200 | [diff] [blame] | 299 | |
Andy Shevchenko | 4ca48c9 | 2018-11-20 23:52:38 +0200 | [diff] [blame] | 300 | /* Fill out header fields */ |
| 301 | acpi_fill_header(header, "SPCR"); |
| 302 | header->length = sizeof(struct acpi_spcr); |
| 303 | header->revision = 2; |
| 304 | |
Simon Glass | 896c164 | 2018-12-28 14:23:10 -0700 | [diff] [blame] | 305 | /* Read the device once, here. It is reused below */ |
Andy Shevchenko | bf9c8e3 | 2019-02-28 17:19:54 +0200 | [diff] [blame] | 306 | dev = gd->cur_serial_dev; |
| 307 | if (dev) |
Simon Glass | 896c164 | 2018-12-28 14:23:10 -0700 | [diff] [blame] | 308 | ret = serial_getinfo(dev, &serial_info); |
Andy Shevchenko | 4ca48c9 | 2018-11-20 23:52:38 +0200 | [diff] [blame] | 309 | if (ret) |
| 310 | serial_info.type = SERIAL_CHIP_UNKNOWN; |
| 311 | |
| 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 | |
| 323 | /* Encode address space */ |
| 324 | switch (serial_info.addr_space) { |
| 325 | case SERIAL_ADDRESS_SPACE_MEMORY: |
| 326 | space_id = ACPI_ADDRESS_SPACE_MEMORY; |
| 327 | break; |
| 328 | case SERIAL_ADDRESS_SPACE_IO: |
| 329 | default: |
| 330 | space_id = ACPI_ADDRESS_SPACE_IO; |
| 331 | break; |
| 332 | } |
| 333 | |
| 334 | serial_width = serial_info.reg_width * 8; |
| 335 | serial_offset = serial_info.reg_offset << serial_info.reg_shift; |
| 336 | serial_address = serial_info.addr + serial_offset; |
| 337 | |
| 338 | /* Encode register access size */ |
| 339 | switch (serial_info.reg_shift) { |
| 340 | case 0: |
| 341 | access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS; |
| 342 | break; |
| 343 | case 1: |
| 344 | access_size = ACPI_ACCESS_SIZE_WORD_ACCESS; |
| 345 | break; |
| 346 | case 2: |
| 347 | access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS; |
| 348 | break; |
| 349 | case 3: |
| 350 | access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS; |
| 351 | break; |
| 352 | default: |
| 353 | access_size = ACPI_ACCESS_SIZE_UNDEFINED; |
| 354 | break; |
| 355 | } |
| 356 | |
| 357 | debug("UART type %u @ %lx\n", spcr->interface_type, serial_address); |
| 358 | |
| 359 | /* Fill GAS */ |
| 360 | spcr->serial_port.space_id = space_id; |
| 361 | spcr->serial_port.bit_width = serial_width; |
| 362 | spcr->serial_port.bit_offset = 0; |
| 363 | spcr->serial_port.access_size = access_size; |
| 364 | spcr->serial_port.addrl = lower_32_bits(serial_address); |
| 365 | spcr->serial_port.addrh = upper_32_bits(serial_address); |
| 366 | |
| 367 | /* Encode baud rate */ |
| 368 | switch (serial_info.baudrate) { |
| 369 | case 9600: |
| 370 | spcr->baud_rate = 3; |
| 371 | break; |
| 372 | case 19200: |
| 373 | spcr->baud_rate = 4; |
| 374 | break; |
| 375 | case 57600: |
| 376 | spcr->baud_rate = 6; |
| 377 | break; |
| 378 | case 115200: |
| 379 | spcr->baud_rate = 7; |
| 380 | break; |
| 381 | default: |
| 382 | spcr->baud_rate = 0; |
| 383 | break; |
| 384 | } |
| 385 | |
Simon Glass | 896c164 | 2018-12-28 14:23:10 -0700 | [diff] [blame] | 386 | serial_config = SERIAL_DEFAULT_CONFIG; |
| 387 | if (dev) |
Simon Glass | daaff93 | 2018-12-28 14:23:08 -0700 | [diff] [blame] | 388 | ret = serial_getconfig(dev, &serial_config); |
Andy Shevchenko | 4ca48c9 | 2018-11-20 23:52:38 +0200 | [diff] [blame] | 389 | |
| 390 | spcr->parity = SERIAL_GET_PARITY(serial_config); |
| 391 | spcr->stop_bits = SERIAL_GET_STOP(serial_config); |
| 392 | |
| 393 | /* No PCI devices for now */ |
| 394 | spcr->pci_device_id = 0xffff; |
| 395 | spcr->pci_vendor_id = 0xffff; |
| 396 | |
Andy Shevchenko | 225cc8a | 2020-02-27 17:21:56 +0200 | [diff] [blame] | 397 | /* |
| 398 | * SPCR has no clue if the UART base clock speed is different |
| 399 | * to the default one. However, the SPCR 1.04 defines baud rate |
| 400 | * 0 as a preconfigured state of UART and OS is supposed not |
| 401 | * to touch the configuration of the serial device. |
| 402 | */ |
| 403 | if (serial_info.clock != SERIAL_DEFAULT_CLOCK) |
| 404 | spcr->baud_rate = 0; |
| 405 | |
Andy Shevchenko | 4ca48c9 | 2018-11-20 23:52:38 +0200 | [diff] [blame] | 406 | /* Fix checksum */ |
| 407 | header->checksum = table_compute_checksum((void *)spcr, header->length); |
Simon Glass | 6e6bc9b | 2021-12-01 09:03:00 -0700 | [diff] [blame] | 408 | |
| 409 | acpi_add_table(ctx, spcr); |
| 410 | acpi_inc(ctx, spcr->header.length); |
| 411 | |
| 412 | return 0; |
Andy Shevchenko | 4ca48c9 | 2018-11-20 23:52:38 +0200 | [diff] [blame] | 413 | } |
Simon Glass | 6e6bc9b | 2021-12-01 09:03:00 -0700 | [diff] [blame] | 414 | ACPI_WRITER(5spcr, "SPCR", acpi_write_spcr, 0); |
Andy Shevchenko | 4ca48c9 | 2018-11-20 23:52:38 +0200 | [diff] [blame] | 415 | |
Simon Glass | d2a98eb | 2021-12-01 09:02:53 -0700 | [diff] [blame] | 416 | int acpi_write_gnvs(struct acpi_ctx *ctx, const struct acpi_writer *entry) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 417 | { |
Simon Glass | 0e11384 | 2020-04-26 09:19:47 -0600 | [diff] [blame] | 418 | ulong addr; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 419 | |
Simon Glass | 6fe570a | 2020-09-22 12:44:53 -0600 | [diff] [blame] | 420 | if (!IS_ENABLED(CONFIG_ACPI_GNVS_EXTERNAL)) { |
Simon Glass | d2a98eb | 2021-12-01 09:02:53 -0700 | [diff] [blame] | 421 | int i; |
| 422 | |
| 423 | /* We need the DSDT to be done */ |
| 424 | if (!ctx->dsdt) |
| 425 | return log_msg_ret("dsdt", -EAGAIN); |
| 426 | |
Simon Glass | 6fe570a | 2020-09-22 12:44:53 -0600 | [diff] [blame] | 427 | /* Pack GNVS into the ACPI table area */ |
Simon Glass | 83c3cb5 | 2021-12-01 09:02:52 -0700 | [diff] [blame] | 428 | for (i = 0; i < ctx->dsdt->length; i++) { |
| 429 | u32 *gnvs = (u32 *)((u32)ctx->dsdt + i); |
Simon Glass | 0e11384 | 2020-04-26 09:19:47 -0600 | [diff] [blame] | 430 | |
Simon Glass | 6fe570a | 2020-09-22 12:44:53 -0600 | [diff] [blame] | 431 | if (*gnvs == ACPI_GNVS_ADDR) { |
Simon Glass | 919f835 | 2023-12-31 08:25:54 -0700 | [diff] [blame] | 432 | *gnvs = nomap_to_sysmem(ctx->current); |
Simon Glass | d2a98eb | 2021-12-01 09:02:53 -0700 | [diff] [blame] | 433 | log_debug("Fix up global NVS in DSDT to %#08x\n", |
| 434 | *gnvs); |
Simon Glass | 6fe570a | 2020-09-22 12:44:53 -0600 | [diff] [blame] | 435 | break; |
| 436 | } |
Bin Meng | d9050c6 | 2016-06-17 02:13:16 -0700 | [diff] [blame] | 437 | } |
Simon Glass | 6fe570a | 2020-09-22 12:44:53 -0600 | [diff] [blame] | 438 | |
| 439 | /* |
Simon Glass | d2a98eb | 2021-12-01 09:02:53 -0700 | [diff] [blame] | 440 | * Recalculate the length and update the DSDT checksum since we |
| 441 | * patched the GNVS address. Set the checksum to zero since it |
| 442 | * is part of the region being checksummed. |
Simon Glass | 6fe570a | 2020-09-22 12:44:53 -0600 | [diff] [blame] | 443 | */ |
Simon Glass | d2a98eb | 2021-12-01 09:02:53 -0700 | [diff] [blame] | 444 | ctx->dsdt->checksum = 0; |
| 445 | ctx->dsdt->checksum = table_compute_checksum((void *)ctx->dsdt, |
| 446 | ctx->dsdt->length); |
Bin Meng | d9050c6 | 2016-06-17 02:13:16 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Simon Glass | d2a98eb | 2021-12-01 09:02:53 -0700 | [diff] [blame] | 449 | /* Fill in platform-specific global NVS variables */ |
Simon Glass | 9ed41e7 | 2020-07-07 21:32:05 -0600 | [diff] [blame] | 450 | addr = acpi_create_gnvs(ctx->current); |
| 451 | if (IS_ERR_VALUE(addr)) |
Simon Glass | d2a98eb | 2021-12-01 09:02:53 -0700 | [diff] [blame] | 452 | return log_msg_ret("gnvs", (int)addr); |
Simon Glass | 9ed41e7 | 2020-07-07 21:32:05 -0600 | [diff] [blame] | 453 | |
Simon Glass | 0e11384 | 2020-04-26 09:19:47 -0600 | [diff] [blame] | 454 | acpi_inc_align(ctx, sizeof(struct acpi_global_nvs)); |
Bin Meng | d9050c6 | 2016-06-17 02:13:16 -0700 | [diff] [blame] | 455 | |
Simon Glass | d2a98eb | 2021-12-01 09:02:53 -0700 | [diff] [blame] | 456 | return 0; |
| 457 | } |
| 458 | ACPI_WRITER(4gnvs, "GNVS", acpi_write_gnvs, 0); |
| 459 | |
Simon Glass | 4ffe8b0 | 2020-09-22 12:45:09 -0600 | [diff] [blame] | 460 | /** |
| 461 | * acpi_write_hpet() - Write out a HPET table |
| 462 | * |
| 463 | * Write out the table for High-Precision Event Timers |
| 464 | * |
| 465 | * @hpet: Place to put HPET table |
| 466 | */ |
| 467 | static int acpi_create_hpet(struct acpi_hpet *hpet) |
| 468 | { |
| 469 | struct acpi_table_header *header = &hpet->header; |
| 470 | struct acpi_gen_regaddr *addr = &hpet->addr; |
| 471 | |
| 472 | /* |
| 473 | * See IA-PC HPET (High Precision Event Timers) Specification v1.0a |
| 474 | * https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/software-developers-hpet-spec-1-0a.pdf |
| 475 | */ |
| 476 | memset((void *)hpet, '\0', sizeof(struct acpi_hpet)); |
| 477 | |
| 478 | /* Fill out header fields. */ |
| 479 | acpi_fill_header(header, "HPET"); |
| 480 | |
Simon Glass | 4ffe8b0 | 2020-09-22 12:45:09 -0600 | [diff] [blame] | 481 | header->length = sizeof(struct acpi_hpet); |
| 482 | header->revision = acpi_get_table_revision(ACPITAB_HPET); |
| 483 | |
| 484 | /* Fill out HPET address */ |
| 485 | addr->space_id = 0; /* Memory */ |
| 486 | addr->bit_width = 64; |
| 487 | addr->bit_offset = 0; |
| 488 | addr->addrl = CONFIG_HPET_ADDRESS & 0xffffffff; |
| 489 | addr->addrh = ((unsigned long long)CONFIG_HPET_ADDRESS) >> 32; |
| 490 | |
| 491 | hpet->id = *(u32 *)CONFIG_HPET_ADDRESS; |
| 492 | hpet->number = 0; |
| 493 | hpet->min_tick = 0; /* HPET_MIN_TICKS */ |
| 494 | |
| 495 | header->checksum = table_compute_checksum(hpet, |
| 496 | sizeof(struct acpi_hpet)); |
| 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | int acpi_write_hpet(struct acpi_ctx *ctx) |
| 502 | { |
| 503 | struct acpi_hpet *hpet; |
| 504 | int ret; |
| 505 | |
| 506 | log_debug("ACPI: * HPET\n"); |
| 507 | |
| 508 | hpet = ctx->current; |
| 509 | acpi_inc_align(ctx, sizeof(struct acpi_hpet)); |
| 510 | acpi_create_hpet(hpet); |
| 511 | ret = acpi_add_table(ctx, hpet); |
| 512 | if (ret) |
| 513 | return log_msg_ret("add", ret); |
| 514 | |
| 515 | return 0; |
| 516 | } |
Simon Glass | 9597189 | 2020-09-22 12:45:10 -0600 | [diff] [blame] | 517 | |
| 518 | int acpi_write_dbg2_pci_uart(struct acpi_ctx *ctx, struct udevice *dev, |
| 519 | uint access_size) |
| 520 | { |
| 521 | struct acpi_dbg2_header *dbg2 = ctx->current; |
| 522 | char path[ACPI_PATH_MAX]; |
| 523 | struct acpi_gen_regaddr address; |
| 524 | phys_addr_t addr; |
| 525 | int ret; |
| 526 | |
| 527 | if (!device_active(dev)) { |
| 528 | log_info("Device not enabled\n"); |
| 529 | return -EACCES; |
| 530 | } |
| 531 | /* |
| 532 | * PCI devices don't remember their resource allocation information in |
| 533 | * U-Boot at present. We assume that MMIO is used for the UART and that |
| 534 | * the address space is 32 bytes: ns16550 uses 8 registers of up to |
| 535 | * 32-bits each. This is only for debugging so it is not a big deal. |
| 536 | */ |
| 537 | addr = dm_pci_read_bar32(dev, 0); |
Simon Glass | 7508120 | 2020-11-04 09:57:41 -0700 | [diff] [blame] | 538 | log_debug("UART addr %lx\n", (ulong)addr); |
Simon Glass | 9597189 | 2020-09-22 12:45:10 -0600 | [diff] [blame] | 539 | |
| 540 | memset(&address, '\0', sizeof(address)); |
| 541 | address.space_id = ACPI_ADDRESS_SPACE_MEMORY; |
| 542 | address.addrl = (uint32_t)addr; |
| 543 | address.addrh = (uint32_t)((addr >> 32) & 0xffffffff); |
| 544 | address.access_size = access_size; |
| 545 | |
| 546 | ret = acpi_device_path(dev, path, sizeof(path)); |
| 547 | if (ret) |
| 548 | return log_msg_ret("path", ret); |
| 549 | acpi_create_dbg2(dbg2, ACPI_DBG2_SERIAL_PORT, |
| 550 | ACPI_DBG2_16550_COMPATIBLE, &address, 0x1000, path); |
| 551 | |
| 552 | acpi_inc_align(ctx, dbg2->header.length); |
| 553 | acpi_add_table(ctx, dbg2); |
| 554 | |
| 555 | return 0; |
| 556 | } |
Simon Glass | 87cf8d2 | 2020-09-22 12:45:16 -0600 | [diff] [blame] | 557 | |
| 558 | void acpi_fadt_common(struct acpi_fadt *fadt, struct acpi_facs *facs, |
| 559 | void *dsdt) |
| 560 | { |
| 561 | struct acpi_table_header *header = &fadt->header; |
| 562 | |
| 563 | memset((void *)fadt, '\0', sizeof(struct acpi_fadt)); |
| 564 | |
| 565 | acpi_fill_header(header, "FACP"); |
| 566 | header->length = sizeof(struct acpi_fadt); |
| 567 | header->revision = 4; |
| 568 | memcpy(header->oem_id, OEM_ID, 6); |
| 569 | memcpy(header->oem_table_id, OEM_TABLE_ID, 8); |
Heinrich Schuchardt | 10de8a8 | 2024-01-21 12:52:48 +0100 | [diff] [blame] | 570 | memcpy(header->creator_id, ASLC_ID, 4); |
Simon Glass | 87cf8d2 | 2020-09-22 12:45:16 -0600 | [diff] [blame] | 571 | |
Heinrich Schuchardt | ec95806 | 2023-12-16 09:11:57 +0100 | [diff] [blame] | 572 | fadt->x_firmware_ctrl = map_to_sysmem(facs); |
| 573 | fadt->x_dsdt = map_to_sysmem(dsdt); |
Simon Glass | 87cf8d2 | 2020-09-22 12:45:16 -0600 | [diff] [blame] | 574 | |
| 575 | fadt->preferred_pm_profile = ACPI_PM_MOBILE; |
| 576 | |
| 577 | /* Use ACPI 3.0 revision */ |
| 578 | fadt->header.revision = 4; |
| 579 | } |
| 580 | |
| 581 | void acpi_create_dmar_drhd(struct acpi_ctx *ctx, uint flags, uint segment, |
| 582 | u64 bar) |
| 583 | { |
| 584 | struct dmar_entry *drhd = ctx->current; |
| 585 | |
| 586 | memset(drhd, '\0', sizeof(*drhd)); |
| 587 | drhd->type = DMAR_DRHD; |
| 588 | drhd->length = sizeof(*drhd); /* will be fixed up later */ |
| 589 | drhd->flags = flags; |
| 590 | drhd->segment = segment; |
| 591 | drhd->bar = bar; |
| 592 | acpi_inc(ctx, drhd->length); |
| 593 | } |
| 594 | |
| 595 | void acpi_create_dmar_rmrr(struct acpi_ctx *ctx, uint segment, u64 bar, |
| 596 | u64 limit) |
| 597 | { |
| 598 | struct dmar_rmrr_entry *rmrr = ctx->current; |
| 599 | |
| 600 | memset(rmrr, '\0', sizeof(*rmrr)); |
| 601 | rmrr->type = DMAR_RMRR; |
| 602 | rmrr->length = sizeof(*rmrr); /* will be fixed up later */ |
| 603 | rmrr->segment = segment; |
| 604 | rmrr->bar = bar; |
| 605 | rmrr->limit = limit; |
| 606 | acpi_inc(ctx, rmrr->length); |
| 607 | } |
| 608 | |
| 609 | void acpi_dmar_drhd_fixup(struct acpi_ctx *ctx, void *base) |
| 610 | { |
| 611 | struct dmar_entry *drhd = base; |
| 612 | |
| 613 | drhd->length = ctx->current - base; |
| 614 | } |
| 615 | |
| 616 | void acpi_dmar_rmrr_fixup(struct acpi_ctx *ctx, void *base) |
| 617 | { |
| 618 | struct dmar_rmrr_entry *rmrr = base; |
| 619 | |
| 620 | rmrr->length = ctx->current - base; |
| 621 | } |
| 622 | |
| 623 | static int acpi_create_dmar_ds(struct acpi_ctx *ctx, enum dev_scope_type type, |
| 624 | uint enumeration_id, pci_dev_t bdf) |
| 625 | { |
| 626 | /* we don't support longer paths yet */ |
| 627 | const size_t dev_scope_length = sizeof(struct dev_scope) + 2; |
| 628 | struct dev_scope *ds = ctx->current; |
| 629 | |
| 630 | memset(ds, '\0', dev_scope_length); |
| 631 | ds->type = type; |
| 632 | ds->length = dev_scope_length; |
| 633 | ds->enumeration = enumeration_id; |
| 634 | ds->start_bus = PCI_BUS(bdf); |
| 635 | ds->path[0].dev = PCI_DEV(bdf); |
| 636 | ds->path[0].fn = PCI_FUNC(bdf); |
| 637 | |
| 638 | return ds->length; |
| 639 | } |
| 640 | |
| 641 | int acpi_create_dmar_ds_pci_br(struct acpi_ctx *ctx, pci_dev_t bdf) |
| 642 | { |
| 643 | return acpi_create_dmar_ds(ctx, SCOPE_PCI_SUB, 0, bdf); |
| 644 | } |
| 645 | |
| 646 | int acpi_create_dmar_ds_pci(struct acpi_ctx *ctx, pci_dev_t bdf) |
| 647 | { |
| 648 | return acpi_create_dmar_ds(ctx, SCOPE_PCI_ENDPOINT, 0, bdf); |
| 649 | } |
| 650 | |
| 651 | int acpi_create_dmar_ds_ioapic(struct acpi_ctx *ctx, uint enumeration_id, |
| 652 | pci_dev_t bdf) |
| 653 | { |
| 654 | return acpi_create_dmar_ds(ctx, SCOPE_IOAPIC, enumeration_id, bdf); |
| 655 | } |
| 656 | |
| 657 | int acpi_create_dmar_ds_msi_hpet(struct acpi_ctx *ctx, uint enumeration_id, |
| 658 | pci_dev_t bdf) |
| 659 | { |
| 660 | return acpi_create_dmar_ds(ctx, SCOPE_MSI_HPET, enumeration_id, bdf); |
| 661 | } |