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