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 | |
| 9 | #include <common.h> |
| 10 | #include <cpu.h> |
| 11 | #include <dm.h> |
| 12 | #include <dm/uclass-internal.h> |
Andy Shevchenko | 87e9537 | 2017-07-21 22:32:02 +0300 | [diff] [blame] | 13 | #include <version.h> |
Bin Meng | d9050c6 | 2016-06-17 02:13:16 -0700 | [diff] [blame] | 14 | #include <asm/acpi/global_nvs.h> |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 15 | #include <asm/acpi_table.h> |
Andy Shevchenko | 13a5d87 | 2017-07-21 22:32:04 +0300 | [diff] [blame] | 16 | #include <asm/ioapic.h> |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 17 | #include <asm/lapic.h> |
Andy Shevchenko | 13a5d87 | 2017-07-21 22:32:04 +0300 | [diff] [blame] | 18 | #include <asm/mpspec.h> |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 19 | #include <asm/tables.h> |
Bin Meng | d9050c6 | 2016-06-17 02:13:16 -0700 | [diff] [blame] | 20 | #include <asm/arch/global_nvs.h> |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 21 | |
| 22 | /* |
Bin Meng | b063d5f | 2016-05-07 07:46:24 -0700 | [diff] [blame] | 23 | * IASL compiles the dsdt entries and writes the hex values |
| 24 | * to a C array AmlCode[] (see dsdt.c). |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 25 | */ |
| 26 | extern const unsigned char AmlCode[]; |
| 27 | |
Andy Shevchenko | 66d3e63 | 2018-01-10 19:40:15 +0200 | [diff] [blame] | 28 | /* ACPI RSDP address to be used in boot parameters */ |
Bin Meng | e102925 | 2018-01-30 05:01:16 -0800 | [diff] [blame] | 29 | static ulong acpi_rsdp_addr; |
Andy Shevchenko | 66d3e63 | 2018-01-10 19:40:15 +0200 | [diff] [blame] | 30 | |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 31 | static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt, |
| 32 | struct acpi_xsdt *xsdt) |
| 33 | { |
| 34 | memset(rsdp, 0, sizeof(struct acpi_rsdp)); |
| 35 | |
| 36 | memcpy(rsdp->signature, RSDP_SIG, 8); |
| 37 | memcpy(rsdp->oem_id, OEM_ID, 6); |
| 38 | |
| 39 | rsdp->length = sizeof(struct acpi_rsdp); |
| 40 | rsdp->rsdt_address = (u32)rsdt; |
| 41 | |
| 42 | /* |
| 43 | * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2 |
| 44 | * |
| 45 | * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2. |
| 46 | * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR |
| 47 | * revision 0) |
| 48 | */ |
| 49 | if (xsdt == NULL) { |
| 50 | rsdp->revision = ACPI_RSDP_REV_ACPI_1_0; |
| 51 | } else { |
| 52 | rsdp->xsdt_address = (u64)(u32)xsdt; |
| 53 | rsdp->revision = ACPI_RSDP_REV_ACPI_2_0; |
| 54 | } |
| 55 | |
| 56 | /* Calculate checksums */ |
| 57 | rsdp->checksum = table_compute_checksum((void *)rsdp, 20); |
| 58 | rsdp->ext_checksum = table_compute_checksum((void *)rsdp, |
| 59 | sizeof(struct acpi_rsdp)); |
| 60 | } |
| 61 | |
| 62 | void acpi_fill_header(struct acpi_table_header *header, char *signature) |
| 63 | { |
| 64 | memcpy(header->signature, signature, 4); |
| 65 | memcpy(header->oem_id, OEM_ID, 6); |
| 66 | memcpy(header->oem_table_id, OEM_TABLE_ID, 8); |
Andy Shevchenko | 87e9537 | 2017-07-21 22:32:02 +0300 | [diff] [blame] | 67 | header->oem_revision = U_BOOT_BUILD_DATE; |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 68 | memcpy(header->aslc_id, ASLC_ID, 4); |
| 69 | } |
| 70 | |
| 71 | static void acpi_write_rsdt(struct acpi_rsdt *rsdt) |
| 72 | { |
| 73 | struct acpi_table_header *header = &(rsdt->header); |
| 74 | |
| 75 | /* Fill out header fields */ |
| 76 | acpi_fill_header(header, "RSDT"); |
| 77 | header->length = sizeof(struct acpi_rsdt); |
Bin Meng | f662fe4 | 2016-05-07 07:46:28 -0700 | [diff] [blame] | 78 | header->revision = 1; |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 79 | |
| 80 | /* Entries are filled in later, we come with an empty set */ |
| 81 | |
| 82 | /* Fix checksum */ |
| 83 | header->checksum = table_compute_checksum((void *)rsdt, |
| 84 | sizeof(struct acpi_rsdt)); |
| 85 | } |
| 86 | |
| 87 | static void acpi_write_xsdt(struct acpi_xsdt *xsdt) |
| 88 | { |
| 89 | struct acpi_table_header *header = &(xsdt->header); |
| 90 | |
| 91 | /* Fill out header fields */ |
| 92 | acpi_fill_header(header, "XSDT"); |
| 93 | header->length = sizeof(struct acpi_xsdt); |
Bin Meng | f662fe4 | 2016-05-07 07:46:28 -0700 | [diff] [blame] | 94 | header->revision = 1; |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 95 | |
| 96 | /* Entries are filled in later, we come with an empty set */ |
| 97 | |
| 98 | /* Fix checksum */ |
| 99 | header->checksum = table_compute_checksum((void *)xsdt, |
| 100 | sizeof(struct acpi_xsdt)); |
| 101 | } |
| 102 | |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 103 | /** |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 104 | * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length |
| 105 | * and checksum. |
| 106 | */ |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 107 | static void acpi_add_table(struct acpi_rsdp *rsdp, void *table) |
| 108 | { |
| 109 | int i, entries_num; |
| 110 | struct acpi_rsdt *rsdt; |
| 111 | struct acpi_xsdt *xsdt = NULL; |
| 112 | |
| 113 | /* The RSDT is mandatory while the XSDT is not */ |
| 114 | rsdt = (struct acpi_rsdt *)rsdp->rsdt_address; |
| 115 | |
| 116 | if (rsdp->xsdt_address) |
| 117 | xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address); |
| 118 | |
| 119 | /* This should always be MAX_ACPI_TABLES */ |
| 120 | entries_num = ARRAY_SIZE(rsdt->entry); |
| 121 | |
| 122 | for (i = 0; i < entries_num; i++) { |
| 123 | if (rsdt->entry[i] == 0) |
| 124 | break; |
| 125 | } |
| 126 | |
| 127 | if (i >= entries_num) { |
Bin Meng | d2d2218 | 2016-05-07 07:46:12 -0700 | [diff] [blame] | 128 | debug("ACPI: Error: too many tables\n"); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | |
| 132 | /* Add table to the RSDT */ |
| 133 | rsdt->entry[i] = (u32)table; |
| 134 | |
| 135 | /* Fix RSDT length or the kernel will assume invalid entries */ |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 136 | rsdt->header.length = sizeof(struct acpi_table_header) + |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 137 | (sizeof(u32) * (i + 1)); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 138 | |
| 139 | /* Re-calculate checksum */ |
| 140 | rsdt->header.checksum = 0; |
| 141 | rsdt->header.checksum = table_compute_checksum((u8 *)rsdt, |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 142 | rsdt->header.length); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * And now the same thing for the XSDT. We use the same index as for |
| 146 | * now we want the XSDT and RSDT to always be in sync in U-Boot |
| 147 | */ |
| 148 | if (xsdt) { |
| 149 | /* Add table to the XSDT */ |
| 150 | xsdt->entry[i] = (u64)(u32)table; |
| 151 | |
| 152 | /* Fix XSDT length */ |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 153 | xsdt->header.length = sizeof(struct acpi_table_header) + |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 154 | (sizeof(u64) * (i + 1)); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 155 | |
| 156 | /* Re-calculate checksum */ |
| 157 | xsdt->header.checksum = 0; |
| 158 | xsdt->header.checksum = table_compute_checksum((u8 *)xsdt, |
| 159 | xsdt->header.length); |
| 160 | } |
| 161 | } |
| 162 | |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 163 | static void acpi_create_facs(struct acpi_facs *facs) |
| 164 | { |
| 165 | memset((void *)facs, 0, sizeof(struct acpi_facs)); |
| 166 | |
| 167 | memcpy(facs->signature, "FACS", 4); |
| 168 | facs->length = sizeof(struct acpi_facs); |
| 169 | facs->hardware_signature = 0; |
| 170 | facs->firmware_waking_vector = 0; |
| 171 | facs->global_lock = 0; |
| 172 | facs->flags = 0; |
| 173 | facs->x_firmware_waking_vector_l = 0; |
| 174 | facs->x_firmware_waking_vector_h = 0; |
| 175 | facs->version = 1; |
| 176 | } |
| 177 | |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 178 | static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic, |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 179 | u8 cpu, u8 apic) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 180 | { |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 181 | lapic->type = ACPI_APIC_LAPIC; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 182 | lapic->length = sizeof(struct acpi_madt_lapic); |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 183 | lapic->flags = LOCAL_APIC_FLAG_ENABLED; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 184 | lapic->processor_id = cpu; |
| 185 | lapic->apic_id = apic; |
| 186 | |
| 187 | return lapic->length; |
| 188 | } |
| 189 | |
Bin Meng | 3c5234e | 2016-05-07 07:46:30 -0700 | [diff] [blame] | 190 | int acpi_create_madt_lapics(u32 current) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 191 | { |
| 192 | struct udevice *dev; |
George McCollister | 5a49f87 | 2016-06-07 13:40:18 -0500 | [diff] [blame] | 193 | int total_length = 0; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 194 | |
| 195 | for (uclass_find_first_device(UCLASS_CPU, &dev); |
| 196 | dev; |
| 197 | uclass_find_next_device(&dev)) { |
| 198 | struct cpu_platdata *plat = dev_get_parent_platdata(dev); |
George McCollister | 5a49f87 | 2016-06-07 13:40:18 -0500 | [diff] [blame] | 199 | int length = acpi_create_madt_lapic( |
| 200 | (struct acpi_madt_lapic *)current, |
| 201 | plat->cpu_id, plat->cpu_id); |
Bin Meng | 3c5234e | 2016-05-07 07:46:30 -0700 | [diff] [blame] | 202 | current += length; |
George McCollister | 5a49f87 | 2016-06-07 13:40:18 -0500 | [diff] [blame] | 203 | total_length += length; |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 204 | } |
| 205 | |
George McCollister | 5a49f87 | 2016-06-07 13:40:18 -0500 | [diff] [blame] | 206 | return total_length; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 207 | } |
| 208 | |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 209 | int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id, |
| 210 | u32 addr, u32 gsi_base) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 211 | { |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 212 | ioapic->type = ACPI_APIC_IOAPIC; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 213 | ioapic->length = sizeof(struct acpi_madt_ioapic); |
| 214 | ioapic->reserved = 0x00; |
| 215 | ioapic->gsi_base = gsi_base; |
| 216 | ioapic->ioapic_id = id; |
| 217 | ioapic->ioapic_addr = addr; |
| 218 | |
| 219 | return ioapic->length; |
| 220 | } |
| 221 | |
| 222 | int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride, |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 223 | u8 bus, u8 source, u32 gsirq, u16 flags) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 224 | { |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 225 | irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 226 | irqoverride->length = sizeof(struct acpi_madt_irqoverride); |
| 227 | irqoverride->bus = bus; |
| 228 | irqoverride->source = source; |
| 229 | irqoverride->gsirq = gsirq; |
| 230 | irqoverride->flags = flags; |
| 231 | |
| 232 | return irqoverride->length; |
| 233 | } |
| 234 | |
| 235 | 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] | 236 | u8 cpu, u16 flags, u8 lint) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 237 | { |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 238 | lapic_nmi->type = ACPI_APIC_LAPIC_NMI; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 239 | lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi); |
| 240 | lapic_nmi->flags = flags; |
| 241 | lapic_nmi->processor_id = cpu; |
| 242 | lapic_nmi->lint = lint; |
| 243 | |
| 244 | return lapic_nmi->length; |
| 245 | } |
| 246 | |
Andy Shevchenko | 13a5d87 | 2017-07-21 22:32:04 +0300 | [diff] [blame] | 247 | static int acpi_create_madt_irq_overrides(u32 current) |
| 248 | { |
| 249 | struct acpi_madt_irqoverride *irqovr; |
| 250 | u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH; |
| 251 | int length = 0; |
| 252 | |
| 253 | irqovr = (void *)current; |
| 254 | length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0); |
| 255 | |
| 256 | irqovr = (void *)(current + length); |
| 257 | length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags); |
| 258 | |
| 259 | return length; |
| 260 | } |
| 261 | |
| 262 | __weak u32 acpi_fill_madt(u32 current) |
| 263 | { |
| 264 | current += acpi_create_madt_lapics(current); |
| 265 | |
| 266 | current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current, |
| 267 | io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0); |
| 268 | |
| 269 | current += acpi_create_madt_irq_overrides(current); |
| 270 | |
| 271 | return current; |
| 272 | } |
| 273 | |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 274 | static void acpi_create_madt(struct acpi_madt *madt) |
| 275 | { |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 276 | struct acpi_table_header *header = &(madt->header); |
Bin Meng | a1ec7db | 2016-05-07 07:46:26 -0700 | [diff] [blame] | 277 | u32 current = (u32)madt + sizeof(struct acpi_madt); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 278 | |
| 279 | memset((void *)madt, 0, sizeof(struct acpi_madt)); |
| 280 | |
| 281 | /* Fill out header fields */ |
Bin Meng | b063d5f | 2016-05-07 07:46:24 -0700 | [diff] [blame] | 282 | acpi_fill_header(header, "APIC"); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 283 | header->length = sizeof(struct acpi_madt); |
Bin Meng | f662fe4 | 2016-05-07 07:46:28 -0700 | [diff] [blame] | 284 | header->revision = 4; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 285 | |
| 286 | madt->lapic_addr = LAPIC_DEFAULT_BASE; |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 287 | madt->flags = ACPI_MADT_PCAT_COMPAT; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 288 | |
| 289 | current = acpi_fill_madt(current); |
| 290 | |
| 291 | /* (Re)calculate length and checksum */ |
Bin Meng | a1ec7db | 2016-05-07 07:46:26 -0700 | [diff] [blame] | 292 | header->length = current - (u32)madt; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 293 | |
| 294 | header->checksum = table_compute_checksum((void *)madt, header->length); |
| 295 | } |
| 296 | |
Andy Shevchenko | c1ae980 | 2017-07-21 22:32:05 +0300 | [diff] [blame] | 297 | int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, u32 base, |
| 298 | u16 seg_nr, u8 start, u8 end) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 299 | { |
| 300 | memset(mmconfig, 0, sizeof(*mmconfig)); |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 301 | mmconfig->base_address_l = base; |
| 302 | mmconfig->base_address_h = 0; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 303 | mmconfig->pci_segment_group_number = seg_nr; |
| 304 | mmconfig->start_bus_number = start; |
| 305 | mmconfig->end_bus_number = end; |
| 306 | |
| 307 | return sizeof(struct acpi_mcfg_mmconfig); |
| 308 | } |
| 309 | |
Andy Shevchenko | c1ae980 | 2017-07-21 22:32:05 +0300 | [diff] [blame] | 310 | __weak u32 acpi_fill_mcfg(u32 current) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 311 | { |
| 312 | current += acpi_create_mcfg_mmconfig |
| 313 | ((struct acpi_mcfg_mmconfig *)current, |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 314 | CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 315 | |
| 316 | return current; |
| 317 | } |
| 318 | |
| 319 | /* MCFG is defined in the PCI Firmware Specification 3.0 */ |
| 320 | static void acpi_create_mcfg(struct acpi_mcfg *mcfg) |
| 321 | { |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 322 | struct acpi_table_header *header = &(mcfg->header); |
Bin Meng | a1ec7db | 2016-05-07 07:46:26 -0700 | [diff] [blame] | 323 | u32 current = (u32)mcfg + sizeof(struct acpi_mcfg); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 324 | |
| 325 | memset((void *)mcfg, 0, sizeof(struct acpi_mcfg)); |
| 326 | |
| 327 | /* Fill out header fields */ |
Bin Meng | b063d5f | 2016-05-07 07:46:24 -0700 | [diff] [blame] | 328 | acpi_fill_header(header, "MCFG"); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 329 | header->length = sizeof(struct acpi_mcfg); |
Bin Meng | f662fe4 | 2016-05-07 07:46:28 -0700 | [diff] [blame] | 330 | header->revision = 1; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 331 | |
| 332 | current = acpi_fill_mcfg(current); |
| 333 | |
| 334 | /* (Re)calculate length and checksum */ |
Bin Meng | a1ec7db | 2016-05-07 07:46:26 -0700 | [diff] [blame] | 335 | header->length = current - (u32)mcfg; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 336 | header->checksum = table_compute_checksum((void *)mcfg, header->length); |
| 337 | } |
| 338 | |
Miao Yan | 3b68c52 | 2016-01-20 01:57:06 -0800 | [diff] [blame] | 339 | /* |
Andy Shevchenko | 4b05bac | 2018-01-10 19:33:10 +0200 | [diff] [blame] | 340 | * 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] | 341 | */ |
Simon Glass | ca37a39 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 342 | ulong write_acpi_tables(ulong start) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 343 | { |
Bin Meng | 5c54910 | 2016-02-27 22:58:00 -0800 | [diff] [blame] | 344 | u32 current; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 345 | struct acpi_rsdp *rsdp; |
| 346 | struct acpi_rsdt *rsdt; |
| 347 | struct acpi_xsdt *xsdt; |
| 348 | struct acpi_facs *facs; |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 349 | struct acpi_table_header *dsdt; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 350 | struct acpi_fadt *fadt; |
| 351 | struct acpi_mcfg *mcfg; |
| 352 | struct acpi_madt *madt; |
Bin Meng | d9050c6 | 2016-06-17 02:13:16 -0700 | [diff] [blame] | 353 | int i; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 354 | |
| 355 | current = start; |
| 356 | |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 357 | /* Align ACPI tables to 16 byte */ |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 358 | current = ALIGN(current, 16); |
| 359 | |
Simon Glass | ca37a39 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 360 | debug("ACPI: Writing ACPI tables at %lx\n", start); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 361 | |
| 362 | /* We need at least an RSDP and an RSDT Table */ |
| 363 | rsdp = (struct acpi_rsdp *)current; |
| 364 | current += sizeof(struct acpi_rsdp); |
| 365 | current = ALIGN(current, 16); |
| 366 | rsdt = (struct acpi_rsdt *)current; |
| 367 | current += sizeof(struct acpi_rsdt); |
| 368 | current = ALIGN(current, 16); |
| 369 | xsdt = (struct acpi_xsdt *)current; |
| 370 | current += sizeof(struct acpi_xsdt); |
Bin Meng | 3d938d1 | 2016-05-07 07:46:27 -0700 | [diff] [blame] | 371 | /* |
| 372 | * Per ACPI spec, the FACS table address must be aligned to a 64 byte |
| 373 | * boundary (Windows checks this, but Linux does not). |
| 374 | */ |
| 375 | current = ALIGN(current, 64); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 376 | |
| 377 | /* clear all table memory */ |
| 378 | memset((void *)start, 0, current - start); |
| 379 | |
| 380 | acpi_write_rsdp(rsdp, rsdt, xsdt); |
| 381 | acpi_write_rsdt(rsdt); |
| 382 | acpi_write_xsdt(xsdt); |
| 383 | |
| 384 | debug("ACPI: * FACS\n"); |
| 385 | facs = (struct acpi_facs *)current; |
| 386 | current += sizeof(struct acpi_facs); |
| 387 | current = ALIGN(current, 16); |
| 388 | |
| 389 | acpi_create_facs(facs); |
| 390 | |
| 391 | debug("ACPI: * DSDT\n"); |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 392 | dsdt = (struct acpi_table_header *)current; |
| 393 | memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header)); |
Bin Meng | d90434b | 2016-05-11 07:45:05 -0700 | [diff] [blame] | 394 | current += sizeof(struct acpi_table_header); |
| 395 | memcpy((char *)current, |
| 396 | (char *)&AmlCode + sizeof(struct acpi_table_header), |
| 397 | dsdt->length - sizeof(struct acpi_table_header)); |
| 398 | current += dsdt->length - sizeof(struct acpi_table_header); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 399 | current = ALIGN(current, 16); |
| 400 | |
Bin Meng | d9050c6 | 2016-06-17 02:13:16 -0700 | [diff] [blame] | 401 | /* Pack GNVS into the ACPI table area */ |
| 402 | for (i = 0; i < dsdt->length; i++) { |
| 403 | u32 *gnvs = (u32 *)((u32)dsdt + i); |
| 404 | if (*gnvs == ACPI_GNVS_ADDR) { |
| 405 | debug("Fix up global NVS in DSDT to 0x%08x\n", current); |
| 406 | *gnvs = current; |
| 407 | break; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | /* Update DSDT checksum since we patched the GNVS address */ |
| 412 | dsdt->checksum = 0; |
| 413 | dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length); |
| 414 | |
| 415 | /* Fill in platform-specific global NVS variables */ |
| 416 | acpi_create_gnvs((struct acpi_global_nvs *)current); |
| 417 | current += sizeof(struct acpi_global_nvs); |
| 418 | current = ALIGN(current, 16); |
| 419 | |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 420 | debug("ACPI: * FADT\n"); |
| 421 | fadt = (struct acpi_fadt *)current; |
| 422 | current += sizeof(struct acpi_fadt); |
| 423 | current = ALIGN(current, 16); |
| 424 | acpi_create_fadt(fadt, facs, dsdt); |
| 425 | acpi_add_table(rsdp, fadt); |
| 426 | |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 427 | debug("ACPI: * MADT\n"); |
| 428 | madt = (struct acpi_madt *)current; |
| 429 | acpi_create_madt(madt); |
Bin Meng | d90434b | 2016-05-11 07:45:05 -0700 | [diff] [blame] | 430 | current += madt->header.length; |
| 431 | acpi_add_table(rsdp, madt); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 432 | current = ALIGN(current, 16); |
| 433 | |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 434 | debug("ACPI: * MCFG\n"); |
| 435 | mcfg = (struct acpi_mcfg *)current; |
| 436 | acpi_create_mcfg(mcfg); |
Bin Meng | d90434b | 2016-05-11 07:45:05 -0700 | [diff] [blame] | 437 | current += mcfg->header.length; |
| 438 | acpi_add_table(rsdp, mcfg); |
| 439 | current = ALIGN(current, 16); |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 440 | |
Bin Meng | d2d2218 | 2016-05-07 07:46:12 -0700 | [diff] [blame] | 441 | debug("current = %x\n", current); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 442 | |
Andy Shevchenko | 66d3e63 | 2018-01-10 19:40:15 +0200 | [diff] [blame] | 443 | acpi_rsdp_addr = (unsigned long)rsdp; |
Bin Meng | d2d2218 | 2016-05-07 07:46:12 -0700 | [diff] [blame] | 444 | debug("ACPI: done\n"); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 445 | |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 446 | return current; |
| 447 | } |
Bin Meng | 34bc74a | 2017-04-21 07:24:36 -0700 | [diff] [blame] | 448 | |
Bin Meng | e102925 | 2018-01-30 05:01:16 -0800 | [diff] [blame] | 449 | ulong acpi_get_rsdp_addr(void) |
| 450 | { |
| 451 | return acpi_rsdp_addr; |
| 452 | } |