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