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