Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Based on acpi.c from coreboot |
| 3 | * |
| 4 | * Copyright (C) 2015, Saket Sinha <saket.sinha89@gmail.com> |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 5 | * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com> |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 6 | * |
| 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <cpu.h> |
| 12 | #include <dm.h> |
| 13 | #include <dm/uclass-internal.h> |
Andy Shevchenko | 87e9537 | 2017-07-21 22:32:02 +0300 | [diff] [blame] | 14 | #include <version.h> |
Bin Meng | d9050c6 | 2016-06-17 02:13:16 -0700 | [diff] [blame] | 15 | #include <asm/acpi/global_nvs.h> |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 16 | #include <asm/acpi_table.h> |
Bin Meng | e259eb2 | 2016-05-11 07:45:03 -0700 | [diff] [blame] | 17 | #include <asm/io.h> |
Andy Shevchenko | 13a5d87 | 2017-07-21 22:32:04 +0300 | [diff] [blame] | 18 | #include <asm/ioapic.h> |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 19 | #include <asm/lapic.h> |
Andy Shevchenko | 13a5d87 | 2017-07-21 22:32:04 +0300 | [diff] [blame] | 20 | #include <asm/mpspec.h> |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 21 | #include <asm/tables.h> |
Bin Meng | d9050c6 | 2016-06-17 02:13:16 -0700 | [diff] [blame] | 22 | #include <asm/arch/global_nvs.h> |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 23 | |
| 24 | /* |
Bin Meng | b063d5f | 2016-05-07 07:46:24 -0700 | [diff] [blame] | 25 | * IASL compiles the dsdt entries and writes the hex values |
| 26 | * to a C array AmlCode[] (see dsdt.c). |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 27 | */ |
| 28 | extern const unsigned char AmlCode[]; |
| 29 | |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 30 | static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt, |
| 31 | struct acpi_xsdt *xsdt) |
| 32 | { |
| 33 | memset(rsdp, 0, sizeof(struct acpi_rsdp)); |
| 34 | |
| 35 | memcpy(rsdp->signature, RSDP_SIG, 8); |
| 36 | memcpy(rsdp->oem_id, OEM_ID, 6); |
| 37 | |
| 38 | rsdp->length = sizeof(struct acpi_rsdp); |
| 39 | rsdp->rsdt_address = (u32)rsdt; |
| 40 | |
| 41 | /* |
| 42 | * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2 |
| 43 | * |
| 44 | * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2. |
| 45 | * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR |
| 46 | * revision 0) |
| 47 | */ |
| 48 | if (xsdt == NULL) { |
| 49 | rsdp->revision = ACPI_RSDP_REV_ACPI_1_0; |
| 50 | } else { |
| 51 | rsdp->xsdt_address = (u64)(u32)xsdt; |
| 52 | rsdp->revision = ACPI_RSDP_REV_ACPI_2_0; |
| 53 | } |
| 54 | |
| 55 | /* Calculate checksums */ |
| 56 | rsdp->checksum = table_compute_checksum((void *)rsdp, 20); |
| 57 | rsdp->ext_checksum = table_compute_checksum((void *)rsdp, |
| 58 | sizeof(struct acpi_rsdp)); |
| 59 | } |
| 60 | |
| 61 | void acpi_fill_header(struct acpi_table_header *header, char *signature) |
| 62 | { |
| 63 | memcpy(header->signature, signature, 4); |
| 64 | memcpy(header->oem_id, OEM_ID, 6); |
| 65 | memcpy(header->oem_table_id, OEM_TABLE_ID, 8); |
Andy Shevchenko | 87e9537 | 2017-07-21 22:32:02 +0300 | [diff] [blame] | 66 | header->oem_revision = U_BOOT_BUILD_DATE; |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 67 | memcpy(header->aslc_id, ASLC_ID, 4); |
| 68 | } |
| 69 | |
| 70 | static void acpi_write_rsdt(struct acpi_rsdt *rsdt) |
| 71 | { |
| 72 | struct acpi_table_header *header = &(rsdt->header); |
| 73 | |
| 74 | /* Fill out header fields */ |
| 75 | acpi_fill_header(header, "RSDT"); |
| 76 | header->length = sizeof(struct acpi_rsdt); |
Bin Meng | f662fe4 | 2016-05-07 07:46:28 -0700 | [diff] [blame] | 77 | header->revision = 1; |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 78 | |
| 79 | /* Entries are filled in later, we come with an empty set */ |
| 80 | |
| 81 | /* Fix checksum */ |
| 82 | header->checksum = table_compute_checksum((void *)rsdt, |
| 83 | sizeof(struct acpi_rsdt)); |
| 84 | } |
| 85 | |
| 86 | static void acpi_write_xsdt(struct acpi_xsdt *xsdt) |
| 87 | { |
| 88 | struct acpi_table_header *header = &(xsdt->header); |
| 89 | |
| 90 | /* Fill out header fields */ |
| 91 | acpi_fill_header(header, "XSDT"); |
| 92 | header->length = sizeof(struct acpi_xsdt); |
Bin Meng | f662fe4 | 2016-05-07 07:46:28 -0700 | [diff] [blame] | 93 | header->revision = 1; |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 94 | |
| 95 | /* Entries are filled in later, we come with an empty set */ |
| 96 | |
| 97 | /* Fix checksum */ |
| 98 | header->checksum = table_compute_checksum((void *)xsdt, |
| 99 | sizeof(struct acpi_xsdt)); |
| 100 | } |
| 101 | |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 102 | /** |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 103 | * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length |
| 104 | * and checksum. |
| 105 | */ |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 106 | static void acpi_add_table(struct acpi_rsdp *rsdp, void *table) |
| 107 | { |
| 108 | int i, entries_num; |
| 109 | struct acpi_rsdt *rsdt; |
| 110 | struct acpi_xsdt *xsdt = NULL; |
| 111 | |
| 112 | /* The RSDT is mandatory while the XSDT is not */ |
| 113 | rsdt = (struct acpi_rsdt *)rsdp->rsdt_address; |
| 114 | |
| 115 | if (rsdp->xsdt_address) |
| 116 | xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address); |
| 117 | |
| 118 | /* This should always be MAX_ACPI_TABLES */ |
| 119 | entries_num = ARRAY_SIZE(rsdt->entry); |
| 120 | |
| 121 | for (i = 0; i < entries_num; i++) { |
| 122 | if (rsdt->entry[i] == 0) |
| 123 | break; |
| 124 | } |
| 125 | |
| 126 | if (i >= entries_num) { |
Bin Meng | d2d2218 | 2016-05-07 07:46:12 -0700 | [diff] [blame] | 127 | debug("ACPI: Error: too many tables\n"); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 128 | return; |
| 129 | } |
| 130 | |
| 131 | /* Add table to the RSDT */ |
| 132 | rsdt->entry[i] = (u32)table; |
| 133 | |
| 134 | /* Fix RSDT length or the kernel will assume invalid entries */ |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 135 | rsdt->header.length = sizeof(struct acpi_table_header) + |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 136 | (sizeof(u32) * (i + 1)); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 137 | |
| 138 | /* Re-calculate checksum */ |
| 139 | rsdt->header.checksum = 0; |
| 140 | rsdt->header.checksum = table_compute_checksum((u8 *)rsdt, |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 141 | rsdt->header.length); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 142 | |
| 143 | /* |
| 144 | * And now the same thing for the XSDT. We use the same index as for |
| 145 | * now we want the XSDT and RSDT to always be in sync in U-Boot |
| 146 | */ |
| 147 | if (xsdt) { |
| 148 | /* Add table to the XSDT */ |
| 149 | xsdt->entry[i] = (u64)(u32)table; |
| 150 | |
| 151 | /* Fix XSDT length */ |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 152 | xsdt->header.length = sizeof(struct acpi_table_header) + |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 153 | (sizeof(u64) * (i + 1)); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 154 | |
| 155 | /* Re-calculate checksum */ |
| 156 | xsdt->header.checksum = 0; |
| 157 | xsdt->header.checksum = table_compute_checksum((u8 *)xsdt, |
| 158 | xsdt->header.length); |
| 159 | } |
| 160 | } |
| 161 | |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 162 | static void acpi_create_facs(struct acpi_facs *facs) |
| 163 | { |
| 164 | memset((void *)facs, 0, sizeof(struct acpi_facs)); |
| 165 | |
| 166 | memcpy(facs->signature, "FACS", 4); |
| 167 | facs->length = sizeof(struct acpi_facs); |
| 168 | facs->hardware_signature = 0; |
| 169 | facs->firmware_waking_vector = 0; |
| 170 | facs->global_lock = 0; |
| 171 | facs->flags = 0; |
| 172 | facs->x_firmware_waking_vector_l = 0; |
| 173 | facs->x_firmware_waking_vector_h = 0; |
| 174 | facs->version = 1; |
| 175 | } |
| 176 | |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 177 | static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic, |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 178 | u8 cpu, u8 apic) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 179 | { |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 180 | lapic->type = ACPI_APIC_LAPIC; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 181 | lapic->length = sizeof(struct acpi_madt_lapic); |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 182 | lapic->flags = LOCAL_APIC_FLAG_ENABLED; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 183 | lapic->processor_id = cpu; |
| 184 | lapic->apic_id = apic; |
| 185 | |
| 186 | return lapic->length; |
| 187 | } |
| 188 | |
Bin Meng | 3c5234e | 2016-05-07 07:46:30 -0700 | [diff] [blame] | 189 | int acpi_create_madt_lapics(u32 current) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 190 | { |
| 191 | struct udevice *dev; |
George McCollister | 5a49f87 | 2016-06-07 13:40:18 -0500 | [diff] [blame] | 192 | int total_length = 0; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 193 | |
| 194 | for (uclass_find_first_device(UCLASS_CPU, &dev); |
| 195 | dev; |
| 196 | uclass_find_next_device(&dev)) { |
| 197 | struct cpu_platdata *plat = dev_get_parent_platdata(dev); |
George McCollister | 5a49f87 | 2016-06-07 13:40:18 -0500 | [diff] [blame] | 198 | int length = acpi_create_madt_lapic( |
| 199 | (struct acpi_madt_lapic *)current, |
| 200 | plat->cpu_id, plat->cpu_id); |
Bin Meng | 3c5234e | 2016-05-07 07:46:30 -0700 | [diff] [blame] | 201 | current += length; |
George McCollister | 5a49f87 | 2016-06-07 13:40:18 -0500 | [diff] [blame] | 202 | total_length += length; |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 203 | } |
| 204 | |
George McCollister | 5a49f87 | 2016-06-07 13:40:18 -0500 | [diff] [blame] | 205 | return total_length; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 206 | } |
| 207 | |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 208 | int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id, |
| 209 | u32 addr, u32 gsi_base) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 210 | { |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 211 | ioapic->type = ACPI_APIC_IOAPIC; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 212 | ioapic->length = sizeof(struct acpi_madt_ioapic); |
| 213 | ioapic->reserved = 0x00; |
| 214 | ioapic->gsi_base = gsi_base; |
| 215 | ioapic->ioapic_id = id; |
| 216 | ioapic->ioapic_addr = addr; |
| 217 | |
| 218 | return ioapic->length; |
| 219 | } |
| 220 | |
| 221 | int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride, |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 222 | u8 bus, u8 source, u32 gsirq, u16 flags) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 223 | { |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 224 | irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 225 | irqoverride->length = sizeof(struct acpi_madt_irqoverride); |
| 226 | irqoverride->bus = bus; |
| 227 | irqoverride->source = source; |
| 228 | irqoverride->gsirq = gsirq; |
| 229 | irqoverride->flags = flags; |
| 230 | |
| 231 | return irqoverride->length; |
| 232 | } |
| 233 | |
| 234 | 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] | 235 | u8 cpu, u16 flags, u8 lint) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 236 | { |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 237 | lapic_nmi->type = ACPI_APIC_LAPIC_NMI; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 238 | lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi); |
| 239 | lapic_nmi->flags = flags; |
| 240 | lapic_nmi->processor_id = cpu; |
| 241 | lapic_nmi->lint = lint; |
| 242 | |
| 243 | return lapic_nmi->length; |
| 244 | } |
| 245 | |
Andy Shevchenko | 13a5d87 | 2017-07-21 22:32:04 +0300 | [diff] [blame] | 246 | static int acpi_create_madt_irq_overrides(u32 current) |
| 247 | { |
| 248 | struct acpi_madt_irqoverride *irqovr; |
| 249 | u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH; |
| 250 | int length = 0; |
| 251 | |
| 252 | irqovr = (void *)current; |
| 253 | length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0); |
| 254 | |
| 255 | irqovr = (void *)(current + length); |
| 256 | length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags); |
| 257 | |
| 258 | return length; |
| 259 | } |
| 260 | |
| 261 | __weak u32 acpi_fill_madt(u32 current) |
| 262 | { |
| 263 | current += acpi_create_madt_lapics(current); |
| 264 | |
| 265 | current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current, |
| 266 | io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0); |
| 267 | |
| 268 | current += acpi_create_madt_irq_overrides(current); |
| 269 | |
| 270 | return current; |
| 271 | } |
| 272 | |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 273 | static void acpi_create_madt(struct acpi_madt *madt) |
| 274 | { |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 275 | struct acpi_table_header *header = &(madt->header); |
Bin Meng | a1ec7db | 2016-05-07 07:46:26 -0700 | [diff] [blame] | 276 | u32 current = (u32)madt + sizeof(struct acpi_madt); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 277 | |
| 278 | memset((void *)madt, 0, sizeof(struct acpi_madt)); |
| 279 | |
| 280 | /* Fill out header fields */ |
Bin Meng | b063d5f | 2016-05-07 07:46:24 -0700 | [diff] [blame] | 281 | acpi_fill_header(header, "APIC"); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 282 | header->length = sizeof(struct acpi_madt); |
Bin Meng | f662fe4 | 2016-05-07 07:46:28 -0700 | [diff] [blame] | 283 | header->revision = 4; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 284 | |
| 285 | madt->lapic_addr = LAPIC_DEFAULT_BASE; |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 286 | madt->flags = ACPI_MADT_PCAT_COMPAT; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 287 | |
| 288 | current = acpi_fill_madt(current); |
| 289 | |
| 290 | /* (Re)calculate length and checksum */ |
Bin Meng | a1ec7db | 2016-05-07 07:46:26 -0700 | [diff] [blame] | 291 | header->length = current - (u32)madt; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 292 | |
| 293 | header->checksum = table_compute_checksum((void *)madt, header->length); |
| 294 | } |
| 295 | |
Andy Shevchenko | c1ae980 | 2017-07-21 22:32:05 +0300 | [diff] [blame] | 296 | int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, u32 base, |
| 297 | u16 seg_nr, u8 start, u8 end) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 298 | { |
| 299 | memset(mmconfig, 0, sizeof(*mmconfig)); |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 300 | mmconfig->base_address_l = base; |
| 301 | mmconfig->base_address_h = 0; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 302 | mmconfig->pci_segment_group_number = seg_nr; |
| 303 | mmconfig->start_bus_number = start; |
| 304 | mmconfig->end_bus_number = end; |
| 305 | |
| 306 | return sizeof(struct acpi_mcfg_mmconfig); |
| 307 | } |
| 308 | |
Andy Shevchenko | c1ae980 | 2017-07-21 22:32:05 +0300 | [diff] [blame] | 309 | __weak u32 acpi_fill_mcfg(u32 current) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 310 | { |
| 311 | current += acpi_create_mcfg_mmconfig |
| 312 | ((struct acpi_mcfg_mmconfig *)current, |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 313 | CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 314 | |
| 315 | return current; |
| 316 | } |
| 317 | |
| 318 | /* MCFG is defined in the PCI Firmware Specification 3.0 */ |
| 319 | static void acpi_create_mcfg(struct acpi_mcfg *mcfg) |
| 320 | { |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 321 | struct acpi_table_header *header = &(mcfg->header); |
Bin Meng | a1ec7db | 2016-05-07 07:46:26 -0700 | [diff] [blame] | 322 | u32 current = (u32)mcfg + sizeof(struct acpi_mcfg); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 323 | |
| 324 | memset((void *)mcfg, 0, sizeof(struct acpi_mcfg)); |
| 325 | |
| 326 | /* Fill out header fields */ |
Bin Meng | b063d5f | 2016-05-07 07:46:24 -0700 | [diff] [blame] | 327 | acpi_fill_header(header, "MCFG"); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 328 | header->length = sizeof(struct acpi_mcfg); |
Bin Meng | f662fe4 | 2016-05-07 07:46:28 -0700 | [diff] [blame] | 329 | header->revision = 1; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 330 | |
| 331 | current = acpi_fill_mcfg(current); |
| 332 | |
| 333 | /* (Re)calculate length and checksum */ |
Bin Meng | a1ec7db | 2016-05-07 07:46:26 -0700 | [diff] [blame] | 334 | header->length = current - (u32)mcfg; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 335 | header->checksum = table_compute_checksum((void *)mcfg, header->length); |
| 336 | } |
| 337 | |
Bin Meng | daa5b83 | 2017-04-21 07:24:43 -0700 | [diff] [blame] | 338 | void enter_acpi_mode(int pm1_cnt) |
Bin Meng | e259eb2 | 2016-05-11 07:45:03 -0700 | [diff] [blame] | 339 | { |
Bin Meng | 313ddd6 | 2017-04-21 07:24:42 -0700 | [diff] [blame] | 340 | u16 val = inw(pm1_cnt); |
| 341 | |
Bin Meng | e259eb2 | 2016-05-11 07:45:03 -0700 | [diff] [blame] | 342 | /* |
| 343 | * PM1_CNT register bit0 selects the power management event to be |
| 344 | * either an SCI or SMI interrupt. When this bit is set, then power |
| 345 | * management events will generate an SCI interrupt. When this bit |
| 346 | * is reset power management events will generate an SMI interrupt. |
| 347 | * |
| 348 | * Per ACPI spec, it is the responsibility of the hardware to set |
| 349 | * or reset this bit. OSPM always preserves this bit position. |
| 350 | * |
| 351 | * U-Boot does not support SMI. And we don't have plan to support |
| 352 | * anything running in SMM within U-Boot. To create a legacy-free |
| 353 | * system, and expose ourselves to OSPM as working under ACPI mode |
| 354 | * already, turn this bit on. |
| 355 | */ |
Bin Meng | 313ddd6 | 2017-04-21 07:24:42 -0700 | [diff] [blame] | 356 | outw(val | PM1_CNT_SCI_EN, pm1_cnt); |
Bin Meng | e259eb2 | 2016-05-11 07:45:03 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Miao Yan | 3b68c52 | 2016-01-20 01:57:06 -0800 | [diff] [blame] | 359 | /* |
| 360 | * QEMU's version of write_acpi_tables is defined in |
Tom Rini | bcb3c8d | 2016-05-06 10:40:22 -0400 | [diff] [blame] | 361 | * arch/x86/cpu/qemu/acpi_table.c |
Miao Yan | 3b68c52 | 2016-01-20 01:57:06 -0800 | [diff] [blame] | 362 | */ |
Simon Glass | ca37a39 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 363 | ulong write_acpi_tables(ulong start) |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 364 | { |
Bin Meng | 5c54910 | 2016-02-27 22:58:00 -0800 | [diff] [blame] | 365 | u32 current; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 366 | struct acpi_rsdp *rsdp; |
| 367 | struct acpi_rsdt *rsdt; |
| 368 | struct acpi_xsdt *xsdt; |
| 369 | struct acpi_facs *facs; |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 370 | struct acpi_table_header *dsdt; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 371 | struct acpi_fadt *fadt; |
| 372 | struct acpi_mcfg *mcfg; |
| 373 | struct acpi_madt *madt; |
Bin Meng | d9050c6 | 2016-06-17 02:13:16 -0700 | [diff] [blame] | 374 | int i; |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 375 | |
| 376 | current = start; |
| 377 | |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 378 | /* Align ACPI tables to 16 byte */ |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 379 | current = ALIGN(current, 16); |
| 380 | |
Simon Glass | ca37a39 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 381 | debug("ACPI: Writing ACPI tables at %lx\n", start); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 382 | |
| 383 | /* We need at least an RSDP and an RSDT Table */ |
| 384 | rsdp = (struct acpi_rsdp *)current; |
| 385 | current += sizeof(struct acpi_rsdp); |
| 386 | current = ALIGN(current, 16); |
| 387 | rsdt = (struct acpi_rsdt *)current; |
| 388 | current += sizeof(struct acpi_rsdt); |
| 389 | current = ALIGN(current, 16); |
| 390 | xsdt = (struct acpi_xsdt *)current; |
| 391 | current += sizeof(struct acpi_xsdt); |
Bin Meng | 3d938d1 | 2016-05-07 07:46:27 -0700 | [diff] [blame] | 392 | /* |
| 393 | * Per ACPI spec, the FACS table address must be aligned to a 64 byte |
| 394 | * boundary (Windows checks this, but Linux does not). |
| 395 | */ |
| 396 | current = ALIGN(current, 64); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 397 | |
| 398 | /* clear all table memory */ |
| 399 | memset((void *)start, 0, current - start); |
| 400 | |
| 401 | acpi_write_rsdp(rsdp, rsdt, xsdt); |
| 402 | acpi_write_rsdt(rsdt); |
| 403 | acpi_write_xsdt(xsdt); |
| 404 | |
| 405 | debug("ACPI: * FACS\n"); |
| 406 | facs = (struct acpi_facs *)current; |
| 407 | current += sizeof(struct acpi_facs); |
| 408 | current = ALIGN(current, 16); |
| 409 | |
| 410 | acpi_create_facs(facs); |
| 411 | |
| 412 | debug("ACPI: * DSDT\n"); |
Bin Meng | 6a42158 | 2016-05-07 07:46:21 -0700 | [diff] [blame] | 413 | dsdt = (struct acpi_table_header *)current; |
| 414 | memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header)); |
Bin Meng | d90434b | 2016-05-11 07:45:05 -0700 | [diff] [blame] | 415 | current += sizeof(struct acpi_table_header); |
| 416 | memcpy((char *)current, |
| 417 | (char *)&AmlCode + sizeof(struct acpi_table_header), |
| 418 | dsdt->length - sizeof(struct acpi_table_header)); |
| 419 | current += dsdt->length - sizeof(struct acpi_table_header); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 420 | current = ALIGN(current, 16); |
| 421 | |
Bin Meng | d9050c6 | 2016-06-17 02:13:16 -0700 | [diff] [blame] | 422 | /* Pack GNVS into the ACPI table area */ |
| 423 | for (i = 0; i < dsdt->length; i++) { |
| 424 | u32 *gnvs = (u32 *)((u32)dsdt + i); |
| 425 | if (*gnvs == ACPI_GNVS_ADDR) { |
| 426 | debug("Fix up global NVS in DSDT to 0x%08x\n", current); |
| 427 | *gnvs = current; |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /* Update DSDT checksum since we patched the GNVS address */ |
| 433 | dsdt->checksum = 0; |
| 434 | dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length); |
| 435 | |
| 436 | /* Fill in platform-specific global NVS variables */ |
| 437 | acpi_create_gnvs((struct acpi_global_nvs *)current); |
| 438 | current += sizeof(struct acpi_global_nvs); |
| 439 | current = ALIGN(current, 16); |
| 440 | |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 441 | debug("ACPI: * FADT\n"); |
| 442 | fadt = (struct acpi_fadt *)current; |
| 443 | current += sizeof(struct acpi_fadt); |
| 444 | current = ALIGN(current, 16); |
| 445 | acpi_create_fadt(fadt, facs, dsdt); |
| 446 | acpi_add_table(rsdp, fadt); |
| 447 | |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 448 | debug("ACPI: * MADT\n"); |
| 449 | madt = (struct acpi_madt *)current; |
| 450 | acpi_create_madt(madt); |
Bin Meng | d90434b | 2016-05-11 07:45:05 -0700 | [diff] [blame] | 451 | current += madt->header.length; |
| 452 | acpi_add_table(rsdp, madt); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 453 | current = ALIGN(current, 16); |
| 454 | |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 455 | debug("ACPI: * MCFG\n"); |
| 456 | mcfg = (struct acpi_mcfg *)current; |
| 457 | acpi_create_mcfg(mcfg); |
Bin Meng | d90434b | 2016-05-11 07:45:05 -0700 | [diff] [blame] | 458 | current += mcfg->header.length; |
| 459 | acpi_add_table(rsdp, mcfg); |
| 460 | current = ALIGN(current, 16); |
Bin Meng | 44256b0 | 2016-05-07 07:46:25 -0700 | [diff] [blame] | 461 | |
Bin Meng | d2d2218 | 2016-05-07 07:46:12 -0700 | [diff] [blame] | 462 | debug("current = %x\n", current); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 463 | |
Bin Meng | d2d2218 | 2016-05-07 07:46:12 -0700 | [diff] [blame] | 464 | debug("ACPI: done\n"); |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 465 | |
Andy Shevchenko | 683b696 | 2017-07-21 22:32:06 +0300 | [diff] [blame] | 466 | /* Don't touch ACPI hardware on HW reduced platforms */ |
| 467 | if (fadt->flags & ACPI_FADT_HW_REDUCED_ACPI) |
| 468 | return current; |
| 469 | |
Bin Meng | e259eb2 | 2016-05-11 07:45:03 -0700 | [diff] [blame] | 470 | /* |
| 471 | * Other than waiting for OSPM to request us to switch to ACPI mode, |
| 472 | * do it by ourselves, since SMI will not be triggered. |
| 473 | */ |
| 474 | enter_acpi_mode(fadt->pm1a_cnt_blk); |
| 475 | |
Saket Sinha | 331141a | 2015-08-22 12:20:55 +0530 | [diff] [blame] | 476 | return current; |
| 477 | } |
Bin Meng | 34bc74a | 2017-04-21 07:24:36 -0700 | [diff] [blame] | 478 | |
| 479 | static struct acpi_rsdp *acpi_valid_rsdp(struct acpi_rsdp *rsdp) |
| 480 | { |
| 481 | if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0) |
| 482 | return NULL; |
| 483 | |
| 484 | debug("Looking on %p for valid checksum\n", rsdp); |
| 485 | |
| 486 | if (table_compute_checksum((void *)rsdp, 20) != 0) |
| 487 | return NULL; |
| 488 | debug("acpi rsdp checksum 1 passed\n"); |
| 489 | |
| 490 | if ((rsdp->revision > 1) && |
| 491 | (table_compute_checksum((void *)rsdp, rsdp->length) != 0)) |
| 492 | return NULL; |
| 493 | debug("acpi rsdp checksum 2 passed\n"); |
| 494 | |
| 495 | return rsdp; |
| 496 | } |
| 497 | |
Bin Meng | 280aebe | 2017-04-21 07:24:44 -0700 | [diff] [blame] | 498 | struct acpi_fadt *acpi_find_fadt(void) |
Bin Meng | 34bc74a | 2017-04-21 07:24:36 -0700 | [diff] [blame] | 499 | { |
| 500 | char *p, *end; |
| 501 | struct acpi_rsdp *rsdp = NULL; |
| 502 | struct acpi_rsdt *rsdt; |
| 503 | struct acpi_fadt *fadt = NULL; |
Bin Meng | 34bc74a | 2017-04-21 07:24:36 -0700 | [diff] [blame] | 504 | int i; |
| 505 | |
Bin Meng | 34bc74a | 2017-04-21 07:24:36 -0700 | [diff] [blame] | 506 | /* Find RSDP */ |
| 507 | for (p = (char *)ROM_TABLE_ADDR; p < (char *)ROM_TABLE_END; p += 16) { |
| 508 | rsdp = acpi_valid_rsdp((struct acpi_rsdp *)p); |
| 509 | if (rsdp) |
| 510 | break; |
| 511 | } |
| 512 | |
| 513 | if (rsdp == NULL) |
| 514 | return NULL; |
| 515 | |
| 516 | debug("RSDP found at %p\n", rsdp); |
| 517 | rsdt = (struct acpi_rsdt *)rsdp->rsdt_address; |
| 518 | |
| 519 | end = (char *)rsdt + rsdt->header.length; |
| 520 | debug("RSDT found at %p ends at %p\n", rsdt, end); |
| 521 | |
| 522 | for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) { |
| 523 | fadt = (struct acpi_fadt *)rsdt->entry[i]; |
| 524 | if (strncmp((char *)fadt, "FACP", 4) == 0) |
| 525 | break; |
| 526 | fadt = NULL; |
| 527 | } |
| 528 | |
| 529 | if (fadt == NULL) |
| 530 | return NULL; |
| 531 | |
| 532 | debug("FADT found at %p\n", fadt); |
Bin Meng | 280aebe | 2017-04-21 07:24:44 -0700 | [diff] [blame] | 533 | return fadt; |
| 534 | } |
| 535 | |
| 536 | void *acpi_find_wakeup_vector(struct acpi_fadt *fadt) |
| 537 | { |
| 538 | struct acpi_facs *facs; |
| 539 | void *wake_vec; |
| 540 | |
| 541 | debug("Trying to find the wakeup vector...\n"); |
| 542 | |
Bin Meng | 34bc74a | 2017-04-21 07:24:36 -0700 | [diff] [blame] | 543 | facs = (struct acpi_facs *)fadt->firmware_ctrl; |
| 544 | |
| 545 | if (facs == NULL) { |
| 546 | debug("No FACS found, wake up from S3 not possible.\n"); |
| 547 | return NULL; |
| 548 | } |
| 549 | |
| 550 | debug("FACS found at %p\n", facs); |
| 551 | wake_vec = (void *)facs->firmware_waking_vector; |
| 552 | debug("OS waking vector is %p\n", wake_vec); |
| 553 | |
| 554 | return wake_vec; |
| 555 | } |